1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 06:37:30 +00:00

Make currentUser in AppStateViewModel a Resource

This commit is contained in:
Ammar Githam 2021-06-13 20:01:09 +09:00
parent 39ddfac03b
commit 1d9eb43442
5 changed files with 29 additions and 11 deletions

View File

@ -102,7 +102,10 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment {
binding.swipeRefreshLayout.setEnabled(false); binding.swipeRefreshLayout.setEnabled(false);
binding.swipeRefreshLayout.setNestedScrollingEnabled(false); binding.swipeRefreshLayout.setNestedScrollingEnabled(false);
root = binding.getRoot(); 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; if (getArguments() == null) return root;
final CommentsViewerFragmentArgs args = CommentsViewerFragmentArgs.fromBundle(getArguments()); final CommentsViewerFragmentArgs args = CommentsViewerFragmentArgs.fromBundle(getArguments());
viewModel.setPostDetails(args.getShortCode(), args.getPostId(), args.getPostUserId()); viewModel.setPostDetails(args.getShortCode(), args.getPostId(), args.getPostUserId());

View File

@ -55,7 +55,7 @@ class DirectMessageSettingsFragment : Fragment(), ConfirmDialogFragmentCallback
val args = DirectMessageSettingsFragmentArgs.fromBundle(arguments) val args = DirectMessageSettingsFragmentArgs.fromBundle(arguments)
val fragmentActivity = requireActivity() as MainActivity val fragmentActivity = requireActivity() as MainActivity
val appStateViewModel: AppStateViewModel by activityViewModels() val appStateViewModel: AppStateViewModel by activityViewModels()
val currentUser = appStateViewModel.currentUser ?: return val currentUser = appStateViewModel.currentUser?.data ?: return
val viewModelFactory = DirectSettingsViewModelFactory( val viewModelFactory = DirectSettingsViewModelFactory(
fragmentActivity.application, fragmentActivity.application,
args.threadId, args.threadId,

View File

@ -360,7 +360,9 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
final Bundle arguments = getArguments(); final Bundle arguments = getArguments();
if (arguments == null) return; if (arguments == null) return;
final DirectMessageThreadFragmentArgs fragmentArgs = DirectMessageThreadFragmentArgs.fromBundle(arguments); final DirectMessageThreadFragmentArgs fragmentArgs = DirectMessageThreadFragmentArgs.fromBundle(arguments);
final User currentUser = appStateViewModel.getCurrentUser(); final Resource<User> currentUserResource = appStateViewModel.getCurrentUser();
if (currentUserResource == null) return;
final User currentUser = currentUserResource.data;
if (currentUser == null) return; if (currentUser == null) return;
final DirectThreadViewModelFactory viewModelFactory = new DirectThreadViewModelFactory( final DirectThreadViewModelFactory viewModelFactory = new DirectThreadViewModelFactory(
fragmentActivity.getApplication(), fragmentActivity.getApplication(),
@ -987,7 +989,9 @@ public class DirectMessageThreadFragment extends Fragment implements DirectReact
itemsAdapter.setThread(thread); itemsAdapter.setThread(thread);
return; return;
} }
final User currentUser = appStateViewModel.getCurrentUser(); final Resource<User> currentUserResource = appStateViewModel.getCurrentUser();
if (currentUserResource == null) return;
final User currentUser = currentUserResource.data;
if (currentUser == null) return; if (currentUser == null) return;
itemsAdapter = new DirectItemsAdapter(currentUser, thread, directItemCallback, directItemLongClickListener); itemsAdapter = new DirectItemsAdapter(currentUser, thread, directItemCallback, directItemLongClickListener);
itemsAdapter.setHasStableIds(true); itemsAdapter.setHasStableIds(true);

View File

@ -628,7 +628,9 @@ public class ProfileFragment extends Fragment implements SwipeRefreshLayout.OnRe
usernameTemp = usernameTemp.substring(1); usernameTemp = usernameTemp.substring(1);
} }
if (TextUtils.isEmpty(usernameTemp)) { 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; if (user == null) return;
profileModel = user; profileModel = user;
username = profileModel.getUsername(); username = profileModel.getUsername();

View File

@ -9,6 +9,7 @@ import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData; import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;
import awais.instagrabber.models.Resource;
import awais.instagrabber.repositories.responses.User; import awais.instagrabber.repositories.responses.User;
import awais.instagrabber.utils.Constants; import awais.instagrabber.utils.Constants;
import awais.instagrabber.utils.CookieUtils; import awais.instagrabber.utils.CookieUtils;
@ -23,7 +24,7 @@ public class AppStateViewModel extends AndroidViewModel {
private static final String TAG = AppStateViewModel.class.getSimpleName(); private static final String TAG = AppStateViewModel.class.getSimpleName();
private final String cookie; private final String cookie;
private final MutableLiveData<User> currentUser = new MutableLiveData<>(); private final MutableLiveData<Resource<User>> currentUser = new MutableLiveData<>(Resource.loading(null));
private UserRepository userRepository; private UserRepository userRepository;
@ -32,30 +33,38 @@ public class AppStateViewModel extends AndroidViewModel {
// Log.d(TAG, "AppStateViewModel: constructor"); // Log.d(TAG, "AppStateViewModel: constructor");
cookie = settingsHelper.getString(Constants.COOKIE); cookie = settingsHelper.getString(Constants.COOKIE);
final boolean isLoggedIn = !TextUtils.isEmpty(cookie) && CookieUtils.getUserIdFromCookie(cookie) != 0; 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(); userRepository = UserRepository.Companion.getInstance();
// final AccountRepository accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(application)); // final AccountRepository accountRepository = AccountRepository.getInstance(AccountDataSource.getInstance(application));
fetchProfileDetails(); fetchProfileDetails();
} }
@Nullable @Nullable
public User getCurrentUser() { public Resource<User> getCurrentUser() {
return currentUser.getValue(); return currentUser.getValue();
} }
public LiveData<User> getCurrentUserLiveData() { public LiveData<Resource<User>> getCurrentUserLiveData() {
return currentUser; return currentUser;
} }
private void fetchProfileDetails() { private void fetchProfileDetails() {
currentUser.postValue(Resource.loading(null));
final long uid = CookieUtils.getUserIdFromCookie(cookie); 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) -> { userRepository.getUserInfo(uid, CoroutineUtilsKt.getContinuation((user, throwable) -> {
if (throwable != null) { if (throwable != null) {
Log.e(TAG, "onFailure: ", throwable); Log.e(TAG, "onFailure: ", throwable);
currentUser.postValue(Resource.error(throwable.getMessage(), null));
return; return;
} }
currentUser.postValue(user); currentUser.postValue(Resource.success(user));
}, Dispatchers.getIO())); }, Dispatchers.getIO()));
} }
} }