From 1d9eb43442b8f87c7076273d1d4a8535200b660d Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Sun, 13 Jun 2021 20:01:09 +0900 Subject: [PATCH] Make currentUser in AppStateViewModel a Resource --- .../comments/CommentsViewerFragment.java | 5 ++++- .../DirectMessageSettingsFragment.kt | 2 +- .../DirectMessageThreadFragment.java | 8 +++++-- .../fragments/main/ProfileFragment.java | 4 +++- .../viewmodels/AppStateViewModel.java | 21 +++++++++++++------ 5 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/fragments/comments/CommentsViewerFragment.java b/app/src/main/java/awais/instagrabber/fragments/comments/CommentsViewerFragment.java index aa70ad25..6e3ba016 100644 --- a/app/src/main/java/awais/instagrabber/fragments/comments/CommentsViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/comments/CommentsViewerFragment.java @@ -102,7 +102,10 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment { binding.swipeRefreshLayout.setEnabled(false); binding.swipeRefreshLayout.setNestedScrollingEnabled(false); root = binding.getRoot(); - appStateViewModel.getCurrentUserLiveData().observe(getViewLifecycleOwner(), user -> viewModel.setCurrentUser(user)); + appStateViewModel.getCurrentUserLiveData().observe(getViewLifecycleOwner(), userResource -> { + if (userResource == null || userResource.data == null) return; + viewModel.setCurrentUser(userResource.data); + }); if (getArguments() == null) return root; final CommentsViewerFragmentArgs args = CommentsViewerFragmentArgs.fromBundle(getArguments()); viewModel.setPostDetails(args.getShortCode(), args.getPostId(), args.getPostUserId()); diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.kt b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.kt index 74b56d7a..0cb6a474 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.kt +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.kt @@ -55,7 +55,7 @@ class DirectMessageSettingsFragment : Fragment(), ConfirmDialogFragmentCallback val args = DirectMessageSettingsFragmentArgs.fromBundle(arguments) val fragmentActivity = requireActivity() as MainActivity val appStateViewModel: AppStateViewModel by activityViewModels() - val currentUser = appStateViewModel.currentUser ?: return + val currentUser = appStateViewModel.currentUser?.data ?: return val viewModelFactory = DirectSettingsViewModelFactory( fragmentActivity.application, args.threadId, diff --git a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java index d46e9399..450b21e7 100644 --- a/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java @@ -360,7 +360,9 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact final Bundle arguments = getArguments(); if (arguments == null) return; final DirectMessageThreadFragmentArgs fragmentArgs = DirectMessageThreadFragmentArgs.fromBundle(arguments); - final User currentUser = appStateViewModel.getCurrentUser(); + final Resource currentUserResource = appStateViewModel.getCurrentUser(); + if (currentUserResource == null) return; + final User currentUser = currentUserResource.data; if (currentUser == null) return; final DirectThreadViewModelFactory viewModelFactory = new DirectThreadViewModelFactory( fragmentActivity.getApplication(), @@ -987,7 +989,9 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact itemsAdapter.setThread(thread); return; } - final User currentUser = appStateViewModel.getCurrentUser(); + final Resource currentUserResource = appStateViewModel.getCurrentUser(); + if (currentUserResource == null) return; + final User currentUser = currentUserResource.data; if (currentUser == null) return; itemsAdapter = new DirectItemsAdapter(currentUser, thread, directItemCallback, directItemLongClickListener); itemsAdapter.setHasStableIds(true); 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 e8d199b6..6df0400e 100644 --- a/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java @@ -628,7 +628,9 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe usernameTemp = usernameTemp.substring(1); } if (TextUtils.isEmpty(usernameTemp)) { - appStateViewModel.getCurrentUserLiveData().observe(getViewLifecycleOwner(), user -> { + appStateViewModel.getCurrentUserLiveData().observe(getViewLifecycleOwner(), userResource -> { + if (userResource == null) return; + final User user = userResource.data; if (user == null) return; profileModel = user; username = 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 856063d0..22367861 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java @@ -9,6 +9,7 @@ import androidx.lifecycle.AndroidViewModel; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; +import awais.instagrabber.models.Resource; import awais.instagrabber.repositories.responses.User; import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.CookieUtils; @@ -23,7 +24,7 @@ public class AppStateViewModel extends AndroidViewModel { private static final String TAG = AppStateViewModel.class.getSimpleName(); private final String cookie; - private final MutableLiveData currentUser = new MutableLiveData<>(); + private final MutableLiveData> currentUser = new MutableLiveData<>(Resource.loading(null)); private UserRepository userRepository; @@ -32,30 +33,38 @@ public class AppStateViewModel extends AndroidViewModel { // Log.d(TAG, "AppStateViewModel: constructor"); cookie = settingsHelper.getString(Constants.COOKIE); final boolean isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) != 0; - if (!isLoggedIn) return; + if (!isLoggedIn) { + currentUser.postValue(Resource.success(null)); + return; + } userRepository = UserRepository.Companion.getInstance(); // final AccountRepository accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(application)); fetchProfileDetails(); } @Nullable - public User getCurrentUser() { + public Resource getCurrentUser() { return currentUser.getValue(); } - public LiveData getCurrentUserLiveData() { + public LiveData> getCurrentUserLiveData() { return currentUser; } private void fetchProfileDetails() { + currentUser.postValue(Resource.loading(null)); final long uid = CookieUtils.getUserIdFromCookie(cookie); - if (userRepository == null) return; + if (userRepository == null) { + currentUser.postValue(Resource.success(null)); + return; + } userRepository.getUserInfo(uid, CoroutineUtilsKt.getContinuation((user, throwable) -> { if (throwable != null) { Log.e(TAG, "onFailure: ", throwable); + currentUser.postValue(Resource.error(throwable.getMessage(), null)); return; } - currentUser.postValue(user); + currentUser.postValue(Resource.success(user)); }, Dispatchers.getIO())); } }