From e20fda322e39d79c7b92c95d10946528d74b9769 Mon Sep 17 00:00:00 2001
From: Nik Clayton <nik@ngo.org.uk>
Date: Wed, 7 Dec 2022 19:34:31 +0100
Subject: [PATCH] Remove rxjava from API calls used by
 AccountListFragment::fetchAccounts() (#3005)

* Remove rxjava from API calls used by AccountListFragment

* Use "Throwable" type and name
---
 .../tusky/fragment/AccountListFragment.kt     | 43 ++++++++++---------
 .../tusky/network/MastodonApi.kt              | 28 ++++++------
 2 files changed, 37 insertions(+), 34 deletions(-)

diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/AccountListFragment.kt b/app/src/main/java/com/keylesspalace/tusky/fragment/AccountListFragment.kt
index 49723965..9e57d2dc 100644
--- a/app/src/main/java/com/keylesspalace/tusky/fragment/AccountListFragment.kt
+++ b/app/src/main/java/com/keylesspalace/tusky/fragment/AccountListFragment.kt
@@ -20,6 +20,7 @@ import android.util.Log
 import android.view.View
 import androidx.fragment.app.Fragment
 import androidx.lifecycle.Lifecycle
+import androidx.lifecycle.lifecycleScope
 import androidx.preference.PreferenceManager
 import androidx.recyclerview.widget.ConcatAdapter
 import androidx.recyclerview.widget.DividerItemDecoration
@@ -53,10 +54,9 @@ import com.keylesspalace.tusky.util.show
 import com.keylesspalace.tusky.util.viewBinding
 import com.keylesspalace.tusky.view.EndlessOnScrollListener
 import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
-import io.reactivex.rxjava3.core.Single
+import kotlinx.coroutines.launch
 import retrofit2.Response
 import java.io.IOException
-import java.util.HashMap
 import javax.inject.Inject
 
 class AccountListFragment : Fragment(R.layout.fragment_account_list), AccountActionListener, Injectable {
@@ -255,7 +255,7 @@ class AccountListFragment : Fragment(R.layout.fragment_account_list), AccountAct
         followRequestsAdapter.removeItem(position)
     }
 
-    private fun getFetchCallByListType(fromId: String?): Single<Response<List<TimelineAccount>>> {
+    private suspend fun getFetchCallByListType(fromId: String?): Response<List<TimelineAccount>> {
         return when (type) {
             Type.FOLLOWS -> {
                 val accountId = requireId(type, id)
@@ -293,24 +293,27 @@ class AccountListFragment : Fragment(R.layout.fragment_account_list), AccountAct
             binding.recyclerView.post { adapter.setBottomLoading(true) }
         }
 
-        getFetchCallByListType(fromId)
-            .observeOn(AndroidSchedulers.mainThread())
-            .autoDispose(from(this, Lifecycle.Event.ON_DESTROY))
-            .subscribe(
-                { response ->
-                    val accountList = response.body()
-
-                    if (response.isSuccessful && accountList != null) {
-                        val linkHeader = response.headers()["Link"]
-                        onFetchAccountsSuccess(accountList, linkHeader)
-                    } else {
-                        onFetchAccountsFailure(Exception(response.message()))
-                    }
-                },
-                { throwable ->
-                    onFetchAccountsFailure(throwable)
+        lifecycleScope.launch {
+            try {
+                val response = getFetchCallByListType(fromId)
+                if (!response.isSuccessful) {
+                    onFetchAccountsFailure(Exception(response.message()))
+                    return@launch
                 }
-            )
+
+                val accountList = response.body()
+
+                if (accountList == null) {
+                    onFetchAccountsFailure(Exception(response.message()))
+                    return@launch
+                }
+
+                val linkHeader = response.headers()["Link"]
+                onFetchAccountsSuccess(accountList, linkHeader)
+            } catch (throwable: Throwable) {
+                onFetchAccountsFailure(throwable)
+            }
+        }
     }
 
     private fun onFetchAccountsSuccess(accounts: List<TimelineAccount>, linkHeader: String?) {
diff --git a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt
index e344650f..1c7a8f6b 100644
--- a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt
+++ b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt
@@ -180,16 +180,16 @@ interface MastodonApi {
     ): NetworkResult<StatusContext>
 
     @GET("api/v1/statuses/{id}/reblogged_by")
-    fun statusRebloggedBy(
+    suspend fun statusRebloggedBy(
         @Path("id") statusId: String,
         @Query("max_id") maxId: String?
-    ): Single<Response<List<TimelineAccount>>>
+    ): Response<List<TimelineAccount>>
 
     @GET("api/v1/statuses/{id}/favourited_by")
-    fun statusFavouritedBy(
+    suspend fun statusFavouritedBy(
         @Path("id") statusId: String,
         @Query("max_id") maxId: String?
-    ): Single<Response<List<TimelineAccount>>>
+    ): Response<List<TimelineAccount>>
 
     @DELETE("api/v1/statuses/{id}")
     fun deleteStatus(
@@ -331,16 +331,16 @@ interface MastodonApi {
     ): Response<List<Status>>
 
     @GET("api/v1/accounts/{id}/followers")
-    fun accountFollowers(
+    suspend fun accountFollowers(
         @Path("id") accountId: String,
         @Query("max_id") maxId: String?
-    ): Single<Response<List<TimelineAccount>>>
+    ): Response<List<TimelineAccount>>
 
     @GET("api/v1/accounts/{id}/following")
-    fun accountFollowing(
+    suspend fun accountFollowing(
         @Path("id") accountId: String,
         @Query("max_id") maxId: String?
-    ): Single<Response<List<TimelineAccount>>>
+    ): Response<List<TimelineAccount>>
 
     @FormUrlEncoded
     @POST("api/v1/accounts/{id}/follow")
@@ -394,14 +394,14 @@ interface MastodonApi {
     ): Single<Relationship>
 
     @GET("api/v1/blocks")
-    fun blocks(
+    suspend fun blocks(
         @Query("max_id") maxId: String?
-    ): Single<Response<List<TimelineAccount>>>
+    ): Response<List<TimelineAccount>>
 
     @GET("api/v1/mutes")
-    fun mutes(
+    suspend fun mutes(
         @Query("max_id") maxId: String?
-    ): Single<Response<List<TimelineAccount>>>
+    ): Response<List<TimelineAccount>>
 
     @GET("api/v1/domain_blocks")
     fun domainBlocks(
@@ -436,9 +436,9 @@ interface MastodonApi {
     ): Response<List<Status>>
 
     @GET("api/v1/follow_requests")
-    fun followRequests(
+    suspend fun followRequests(
         @Query("max_id") maxId: String?
-    ): Single<Response<List<TimelineAccount>>>
+    ): Response<List<TimelineAccount>>
 
     @POST("api/v1/follow_requests/{id}/authorize")
     fun authorizeFollowRequest(