Browse Source

Make currentUser in AppStateViewModel a Resource

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
1d9eb43442
  1. 5
      app/src/main/java/awais/instagrabber/fragments/comments/CommentsViewerFragment.java
  2. 2
      app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageSettingsFragment.kt
  3. 8
      app/src/main/java/awais/instagrabber/fragments/directmessages/DirectMessageThreadFragment.java
  4. 4
      app/src/main/java/awais/instagrabber/fragments/main/ProfileFragment.java
  5. 21
      app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java

5
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());

2
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,

8
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<User> 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<User> 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);

4
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();

21
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<User> currentUser = new MutableLiveData<>();
private final MutableLiveData<Resource<User>> 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<User> getCurrentUser() {
return currentUser.getValue();
}
public LiveData<User> getCurrentUserLiveData() {
public LiveData<Resource<User>> 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()));
}
}
Loading…
Cancel
Save