From 666a36ff2070b6ee0dae4da56a50560583a4eefc Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Wed, 31 Mar 2021 21:43:20 +0900 Subject: [PATCH] Fix crash when fetching self profile info is slow --- .../fragments/main/ProfileFragment.java | 51 ++++++++++--------- .../viewmodels/AppStateViewModel.java | 15 ++++-- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java index 4b3bfcfd..c0998f13 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -12,7 +12,6 @@ import android.text.style.RelativeSizeSpan; import android.text.style.StyleSpan; import android.util.Log; import android.view.ActionMode; -import android.view.Gravity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; @@ -604,12 +603,16 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe usernameTemp = usernameTemp.substring(1); } if (TextUtils.isEmpty(usernameTemp)) { - profileModel = appStateViewModel.getCurrentUser(); - username = profileModel.getUsername(); - setUsernameDelayed(); - setProfileDetails(); + appStateViewModel.getCurrentUserLiveData().observe(getViewLifecycleOwner(), user -> { + if (user == null) return; + profileModel = user; + username = profileModel.getUsername(); + setUsernameDelayed(); + setProfileDetails(); + }); + return; } - else if (isLoggedIn) { + if (isLoggedIn) { userService.getUsernameInfo(usernameTemp, new ServiceCallback() { @Override public void onSuccess(final User user) { @@ -643,26 +646,25 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } catch (final Throwable ignored) {} } }); + return; } - else { - graphQLService.fetchUser(usernameTemp, new ServiceCallback() { - @Override - public void onSuccess(final User user) { - profileModel = user; - setProfileDetails(); - } + graphQLService.fetchUser(usernameTemp, new ServiceCallback() { + @Override + public void onSuccess(final User user) { + profileModel = user; + setProfileDetails(); + } - @Override - public void onFailure(final Throwable t) { - Log.e(TAG, "Error fetching profile", t); - final Context context = getContext(); - try { - if (t == null) Toast.makeText(context, R.string.error_loading_profile, Toast.LENGTH_LONG).show(); - else Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); - } catch (final Throwable ignored) {} - } - }); - } + @Override + public void onFailure(final Throwable t) { + Log.e(TAG, "Error fetching profile", t); + final Context context = getContext(); + try { + if (t == null) Toast.makeText(context, R.string.error_loading_profile, Toast.LENGTH_LONG).show(); + else Toast.makeText(context, t.getMessage(), Toast.LENGTH_SHORT).show(); + } catch (final Throwable ignored) {} + } + }); } private void setProfileDetails() { @@ -987,6 +989,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } private void updateAccountInfo() { + if (profileModel == null) return; accountRepository.insertOrUpdateAccount( profileModel.getPk(), profileModel.getUsername(), diff --git a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java index dcea12ed..ab1cbabb 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java @@ -1,9 +1,12 @@ package awais.instagrabber.viewmodels; import android.app.Application; +import android.util.Log; import androidx.annotation.NonNull; import androidx.lifecycle.AndroidViewModel; +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; import awais.instagrabber.db.datasources.AccountDataSource; import awais.instagrabber.db.repositories.AccountRepository; @@ -21,8 +24,8 @@ public class AppStateViewModel extends AndroidViewModel { private final String cookie; private final boolean isLoggedIn; + private final MutableLiveData currentUser = new MutableLiveData<>(); - private User currentUser; private AccountRepository accountRepository; private UserService userService; @@ -38,6 +41,10 @@ public class AppStateViewModel extends AndroidViewModel { } public User getCurrentUser() { + return currentUser.getValue(); + } + + public LiveData getCurrentUserLiveData() { return currentUser; } @@ -46,11 +53,13 @@ public class AppStateViewModel extends AndroidViewModel { userService.getUserInfo(uid, new ServiceCallback() { @Override public void onSuccess(final User user) { - currentUser = user; + currentUser.postValue(user); } @Override - public void onFailure(final Throwable t) {} + public void onFailure(final Throwable t) { + Log.e(TAG, "onFailure: ", t); + } }); } }