From f657f61aa7a0c1fd1bc1555813ebc421e0714acb Mon Sep 17 00:00:00 2001 From: Austin Huang Date: Fri, 18 Jun 2021 20:00:42 -0400 Subject: [PATCH] proper self-profile handling when offline; close #1328 --- .../awais/instagrabber/fragments/main/ProfileFragment.java | 7 ++++++- .../awais/instagrabber/viewmodels/AppStateViewModel.java | 7 +++++-- 2 files changed, 11 insertions(+), 3 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 a2d22793..e0ed90b4 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -692,6 +692,11 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe if (userResource == null) return; final User user = userResource.data; if (user == null) return; + username = user.getUsername(); + if (TextUtils.isEmpty(username)) { + appStateViewModel.fetchProfileDetails(); + return; + } profileModel = user; username = profileModel.getUsername(); setUsernameDelayed(); @@ -1099,7 +1104,7 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe } private void updateAccountInfo() { - if (profileModel == null) return; + if (profileModel == null || TextUtils.isEmpty(profileModel.getUsername())) 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 22367861..727f6e5a 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java @@ -51,7 +51,7 @@ public class AppStateViewModel extends AndroidViewModel { return currentUser; } - private void fetchProfileDetails() { + public void fetchProfileDetails() { currentUser.postValue(Resource.loading(null)); final long uid = CookieUtils.getUserIdFromCookie(cookie); if (userRepository == null) { @@ -61,7 +61,10 @@ public class AppStateViewModel extends AndroidViewModel { userRepository.getUserInfo(uid, CoroutineUtilsKt.getContinuation((user, throwable) -> { if (throwable != null) { Log.e(TAG, "onFailure: ", throwable); - currentUser.postValue(Resource.error(throwable.getMessage(), null)); + final User backup = currentUser.getValue().data != null ? + currentUser.getValue().data : + new User(uid); + currentUser.postValue(Resource.error(throwable.getMessage(), backup)); return; } currentUser.postValue(Resource.success(user));