From 9ca59d69fabb75ef2d608b1c973cdf372f44511d Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Sat, 27 Apr 2019 18:20:42 +0200 Subject: [PATCH] fix Glide crash in MainActivity (#1224) --- .../com/keylesspalace/tusky/MainActivity.java | 26 ++++--------- .../tusky/network/MastodonApi.java | 2 +- .../tusky/viewmodel/EditProfileViewModel.kt | 37 ++++++++----------- 3 files changed, 23 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java index 80d7abb8..c515a219 100644 --- a/app/src/main/java/com/keylesspalace/tusky/MainActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/MainActivity.java @@ -23,7 +23,6 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; -import androidx.annotation.NonNull; import androidx.annotation.Nullable; import com.bumptech.glide.Glide; @@ -518,25 +517,14 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut } private void fetchUserInfo() { - - mastodonApi.accountVerifyCredentials().enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - onFetchUserInfoSuccess(response.body()); - } else { - onFetchUserInfoFailure(new Exception(response.message())); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - onFetchUserInfoFailure((Exception) t); - } - }); + mastodonApi.accountVerifyCredentials() + .observeOn(AndroidSchedulers.mainThread()) + .as(autoDisposable(from(this, Lifecycle.Event.ON_DESTROY))) + .subscribe(this::onFetchUserInfoSuccess, MainActivity::onFetchUserInfoFailure); } private void onFetchUserInfoSuccess(Account me) { + // Add the header image and avatar from the account, into the navigation drawer header. ImageView background = headerResult.getHeaderBackgroundView(); @@ -598,8 +586,8 @@ public final class MainActivity extends BottomSheetActivity implements ActionBut headerResult.setActiveProfile(accountManager.getActiveAccount().getId()); } - private static void onFetchUserInfoFailure(Exception exception) { - Log.e(TAG, "Failed to fetch user info. " + exception.getMessage()); + private static void onFetchUserInfoFailure(Throwable throwable) { + Log.e(TAG, "Failed to fetch user info. " + throwable.getMessage()); } @Nullable 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 309f2e68..8ef89a95 100644 --- a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.java +++ b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.java @@ -176,7 +176,7 @@ public interface MastodonApi { Single unpinStatus(@Path("id") String statusId); @GET("api/v1/accounts/verify_credentials") - Call accountVerifyCredentials(); + Single accountVerifyCredentials(); @FormUrlEncoded @PATCH("api/v1/accounts/update_credentials") diff --git a/app/src/main/java/com/keylesspalace/tusky/viewmodel/EditProfileViewModel.kt b/app/src/main/java/com/keylesspalace/tusky/viewmodel/EditProfileViewModel.kt index 6fec56f8..40f2b242 100644 --- a/app/src/main/java/com/keylesspalace/tusky/viewmodel/EditProfileViewModel.kt +++ b/app/src/main/java/com/keylesspalace/tusky/viewmodel/EditProfileViewModel.kt @@ -31,6 +31,8 @@ import com.keylesspalace.tusky.entity.StringField import com.keylesspalace.tusky.network.MastodonApi import com.keylesspalace.tusky.util.* import io.reactivex.Single +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.rxkotlin.addTo import io.reactivex.schedulers.Schedulers import okhttp3.MediaType import okhttp3.MultipartBody @@ -63,32 +65,24 @@ class EditProfileViewModel @Inject constructor( private var oldProfileData: Account? = null - private val callList: MutableList> = mutableListOf() + private val disposeables = CompositeDisposable() fun obtainProfile() { if(profileData.value == null || profileData.value is Error) { profileData.postValue(Loading()) - val call = mastodonApi.accountVerifyCredentials() - call.enqueue(object : Callback { - override fun onResponse(call: Call, - response: Response) { - if (response.isSuccessful) { - val profile = response.body() - oldProfileData = profile - profileData.postValue(Success(profile)) - } else { - profileData.postValue(Error()) - } - } + mastodonApi.accountVerifyCredentials() + .subscribe( + {profile -> + oldProfileData = profile + profileData.postValue(Success(profile)) + }, + { + profileData.postValue(Error()) + }) + .addTo(disposeables) - override fun onFailure(call: Call, t: Throwable) { - profileData.postValue(Error()) - } - }) - - callList.add(call) } } @@ -138,6 +132,7 @@ class EditProfileViewModel @Inject constructor( }, { imageLiveData.postValue(Error()) }) + .addTo(disposeables) } fun save(newDisplayName: String, newNote: String, newLocked: Boolean, newFields: List, context: Context) { @@ -267,9 +262,7 @@ class EditProfileViewModel @Inject constructor( } override fun onCleared() { - callList.forEach { - it.cancel() - } + disposeables.dispose() }