Implement getFilters() without rxjava (#2990)

This commit is contained in:
Nik Clayton 2022-12-07 19:30:53 +01:00 committed by GitHub
parent 25443217c2
commit f796f77f9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 14 deletions

View file

@ -7,6 +7,7 @@ import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import at.connyduck.calladapter.networkresult.fold
import at.connyduck.calladapter.networkresult.getOrElse
import com.keylesspalace.tusky.appstore.EventHub
import com.keylesspalace.tusky.appstore.PreferenceChangedEvent
import com.keylesspalace.tusky.databinding.ActivityFiltersBinding
@ -19,7 +20,6 @@ import com.keylesspalace.tusky.view.getSecondsForDurationIndex
import com.keylesspalace.tusky.view.setupEditDialogForFilter
import com.keylesspalace.tusky.view.showAddFilterDialog
import kotlinx.coroutines.launch
import kotlinx.coroutines.rx3.await
import java.io.IOException
import javax.inject.Inject
@ -150,12 +150,10 @@ class FiltersActivity : BaseActivity() {
binding.filterProgressBar.show()
lifecycleScope.launch {
val newFilters = try {
api.getFilters().await()
} catch (t: Exception) {
val newFilters = api.getFilters().getOrElse {
binding.filterProgressBar.hide()
binding.filterMessageView.show()
if (t is IOException) {
if (it is IOException) {
binding.filterMessageView.setup(
R.drawable.elephant_offline,
R.string.error_network

View file

@ -20,6 +20,7 @@ import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import at.connyduck.calladapter.networkresult.getOrElse
import com.keylesspalace.tusky.appstore.BlockEvent
import com.keylesspalace.tusky.appstore.BookmarkEvent
import com.keylesspalace.tusky.appstore.DomainMuteEvent
@ -280,10 +281,8 @@ abstract class TimelineViewModel(
private fun reloadFilters() {
viewModelScope.launch {
val filters = try {
api.getFilters().await()
} catch (t: Exception) {
Log.e(TAG, "Failed to fetch filters", t)
val filters = api.getFilters().getOrElse {
Log.e(TAG, "Failed to fetch filters", it)
return@launch
}
filterModel.initWithFilters(

View file

@ -337,12 +337,11 @@ class ViewThreadViewModel @Inject constructor(
private fun loadFilters() {
viewModelScope.launch {
val filters = try {
api.getFilters().await()
} catch (t: Exception) {
Log.w(TAG, "Failed to fetch filters", t)
val filters = api.getFilters().getOrElse {
Log.w(TAG, "Failed to fetch filters", it)
return@launch
}
filterModel.initWithFilters(
filters.filter { filter ->
filter.context.contains(Filter.THREAD)

View file

@ -82,7 +82,7 @@ interface MastodonApi {
suspend fun getInstance(@Header(DOMAIN_HEADER) domain: String? = null): NetworkResult<Instance>
@GET("api/v1/filters")
fun getFilters(): Single<List<Filter>>
suspend fun getFilters(): NetworkResult<List<Filter>>
@GET("api/v1/timelines/home")
@Throws(Exception::class)