From 1621c1e68969ad19913754acbe72bdd2fe63b53d Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Wed, 20 Feb 2019 18:42:18 +0100 Subject: [PATCH] fix crash in AccountListFragment (#1065) * fix crash in AccountListFragment * remove unnecessary subscribeOn --- .../tusky/fragment/AccountListFragment.kt | 46 ++++++++++--------- .../tusky/network/MastodonApi.java | 15 +++--- 2 files changed, 33 insertions(+), 28 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 8e801577f..36d5b4aa4 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.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.lifecycle.Lifecycle import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -39,6 +40,10 @@ import com.keylesspalace.tusky.util.ThemeUtils import com.keylesspalace.tusky.util.hide import com.keylesspalace.tusky.util.show import com.keylesspalace.tusky.view.EndlessOnScrollListener +import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider.from +import com.uber.autodispose.autoDisposable +import io.reactivex.Single +import io.reactivex.android.schedulers.AndroidSchedulers import kotlinx.android.synthetic.main.fragment_account_list.* import retrofit2.Call import retrofit2.Callback @@ -46,6 +51,7 @@ import retrofit2.Response import java.io.IOException import javax.inject.Inject + class AccountListFragment : BaseFragment(), AccountActionListener, Injectable { @Inject @@ -248,7 +254,7 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable { Log.e(TAG, "Failed to $verb account id $accountId.") } - private fun getFetchCallByListType(type: Type, fromId: String?): Call> { + private fun getFetchCallByListType(type: Type, fromId: String?): Single>> { return when (type) { Type.FOLLOWS -> api.accountFollowing(id, fromId) Type.FOLLOWERS -> api.accountFollowers(id, fromId) @@ -270,24 +276,22 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable { recyclerView.post { adapter.setBottomLoading(true) } } - val cb = object : Callback> { - override fun onResponse(call: Call>, response: Response>) { - val accountList = response.body() - if (response.isSuccessful && accountList != null) { - val linkHeader = response.headers().get("Link") - onFetchAccountsSuccess(accountList, linkHeader) - } else { - onFetchAccountsFailure(Exception(response.message())) - } - } + getFetchCallByListType(type, id) + .observeOn(AndroidSchedulers.mainThread()) + .autoDisposable(from(this, Lifecycle.Event.ON_DESTROY)) + .subscribe({ response -> + val accountList = response.body() + + if (response.isSuccessful && accountList != null) { + val linkHeader = response.headers().get("Link") + onFetchAccountsSuccess(accountList, linkHeader) + } else { + onFetchAccountsFailure(Exception(response.message())) + } + }, {throwable -> + onFetchAccountsFailure(throwable) + }) - override fun onFailure(call: Call>, t: Throwable) { - onFetchAccountsFailure(t as Exception) - } - } - val listCall = getFetchCallByListType(type, id) - callList.add(listCall) - listCall.enqueue(cb) } private fun onFetchAccountsSuccess(accounts: List, linkHeader: String?) { @@ -319,13 +323,13 @@ class AccountListFragment : BaseFragment(), AccountActionListener, Injectable { } } - private fun onFetchAccountsFailure(exception: Exception) { + private fun onFetchAccountsFailure(throwable: Throwable) { fetching = false - Log.e(TAG, "Fetch failure", exception) + Log.e(TAG, "Fetch failure", throwable) if (adapter.itemCount == 0) { messageView.show() - if (exception is IOException) { + if (throwable is IOException) { messageView.setup(R.drawable.elephant_offline, R.string.error_network) { messageView.hide() this.fetchAccounts(null) diff --git a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.java b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.java index bd54d2223..7a7712e78 100644 --- a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.java +++ b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.java @@ -38,6 +38,7 @@ import okhttp3.MultipartBody; import okhttp3.RequestBody; import okhttp3.ResponseBody; import retrofit2.Call; +import retrofit2.Response; import retrofit2.http.DELETE; import retrofit2.http.Field; import retrofit2.http.FormUrlEncoded; @@ -139,12 +140,12 @@ public interface MastodonApi { Call statusContext(@Path("id") String statusId); @GET("api/v1/statuses/{id}/reblogged_by") - Call> statusRebloggedBy( + Single>> statusRebloggedBy( @Path("id") String statusId, @Query("max_id") String maxId); @GET("api/v1/statuses/{id}/favourited_by") - Call> statusFavouritedBy( + Single>> statusFavouritedBy( @Path("id") String statusId, @Query("max_id") String maxId); @@ -223,12 +224,12 @@ public interface MastodonApi { @Nullable @Query("pinned") Boolean pinned); @GET("api/v1/accounts/{id}/followers") - Call> accountFollowers( + Single>> accountFollowers( @Path("id") String accountId, @Query("max_id") String maxId); @GET("api/v1/accounts/{id}/following") - Call> accountFollowing( + Single>> accountFollowing( @Path("id") String accountId, @Query("max_id") String maxId); @@ -255,10 +256,10 @@ public interface MastodonApi { Call> relationships(@Query("id[]") List accountIds); @GET("api/v1/blocks") - Call> blocks(@Query("max_id") String maxId); + Single>> blocks(@Query("max_id") String maxId); @GET("api/v1/mutes") - Call> mutes(@Query("max_id") String maxId); + Single>> mutes(@Query("max_id") String maxId); @GET("api/v1/favourites") Call> favourites( @@ -267,7 +268,7 @@ public interface MastodonApi { @Query("limit") Integer limit); @GET("api/v1/follow_requests") - Call> followRequests(@Query("max_id") String maxId); + Single>> followRequests(@Query("max_id") String maxId); @POST("api/v1/follow_requests/{id}/authorize") Call authorizeFollowRequest(@Path("id") String accountId);