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 6bc8643c..79827995 100644 --- a/app/src/main/java/awais/instagrabber/fragments/comments/CommentsViewerFragment.java +++ b/app/src/main/java/awais/instagrabber/fragments/comments/CommentsViewerFragment.java @@ -106,8 +106,21 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment { binding.swipeRefreshLayout.setNestedScrollingEnabled(false); root = binding.getRoot(); appStateViewModel.getCurrentUserLiveData().observe(getViewLifecycleOwner(), userResource -> { - if (userResource == null || userResource.data == null) return; + if (userResource == null || userResource.status == Resource.Status.LOADING) return; viewModel.setCurrentUser(userResource.data); + if (userResource.data == null) { + viewModel.fetchComments(); + return; + } + viewModel.getCurrentUserId().observe(getViewLifecycleOwner(), new Observer() { + @Override + public void onChanged(final Long i) { + if (i != 0L) { + viewModel.fetchComments(); + viewModel.getCurrentUserId().removeObserver(this); + } + } + }); }); if (getArguments() == null) return root; final CommentsViewerFragmentArgs args = CommentsViewerFragmentArgs.fromBundle(getArguments()); diff --git a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java index b471c978..155c43d6 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java @@ -58,7 +58,7 @@ public class AppStateViewModel extends AndroidViewModel { public void fetchProfileDetails() { currentUser.postValue(Resource.loading(null)); final long uid = CookieUtils.getUserIdFromCookie(cookie); - if (userRepository == null) { + if (uid == 0L) { currentUser.postValue(Resource.success(null)); return; } diff --git a/app/src/main/java/awais/instagrabber/viewmodels/CommentsViewerViewModel.java b/app/src/main/java/awais/instagrabber/viewmodels/CommentsViewerViewModel.java index 16a7b0d2..21eb2b19 100644 --- a/app/src/main/java/awais/instagrabber/viewmodels/CommentsViewerViewModel.java +++ b/app/src/main/java/awais/instagrabber/viewmodels/CommentsViewerViewModel.java @@ -43,7 +43,6 @@ import static awais.instagrabber.utils.Utils.settingsHelper; public class CommentsViewerViewModel extends ViewModel { private static final String TAG = CommentsViewerViewModel.class.getSimpleName(); - private final MutableLiveData isLoggedIn = new MutableLiveData<>(false); private final MutableLiveData currentUserId = new MutableLiveData<>(0L); private final MutableLiveData>> rootList = new MutableLiveData<>(); private final MutableLiveData rootCount = new MutableLiveData<>(0); @@ -130,18 +129,12 @@ public class CommentsViewerViewModel extends ViewModel { } public void setCurrentUser(final User currentUser) { - isLoggedIn.postValue(currentUser != null); currentUserId.postValue(currentUser == null ? 0 : currentUser.getPk()); } public void setPostDetails(final String shortCode, final String postId, final long postUserId) { this.shortCode = shortCode; this.postId = postId; - fetchComments(); - } - - public LiveData isLoggedIn() { - return isLoggedIn; } public LiveData getCurrentUserId() { @@ -174,7 +167,7 @@ public class CommentsViewerViewModel extends ViewModel { if (shortCode == null || postId == null) return; if (!rootHasNext) return; rootList.postValue(Resource.loading(getPrevList(rootList))); - if (isLoggedIn.getValue()) { + if (currentUserId.getValue() != 0L) { commentService.fetchComments(postId, rootCursor, ccb); return; } @@ -202,8 +195,7 @@ public class CommentsViewerViewModel extends ViewModel { list = getPrevList(replyList); } replyList.postValue(Resource.loading(list)); - final Boolean isLoggedInValue = isLoggedIn.getValue(); - if (isLoggedInValue != null && isLoggedInValue) { + if (currentUserId.getValue() != 0L) { commentService.fetchChildComments(postId, commentId, repliesCursor, rcb); return; } diff --git a/app/src/main/java/awais/instagrabber/webservices/CommentService.java b/app/src/main/java/awais/instagrabber/webservices/CommentService.java index 5a8945aa..f67931e3 100644 --- a/app/src/main/java/awais/instagrabber/webservices/CommentService.java +++ b/app/src/main/java/awais/instagrabber/webservices/CommentService.java @@ -79,9 +79,7 @@ public class CommentService { request.enqueue(new Callback() { @Override public void onResponse(@NonNull final Call call, @NonNull final Response response) { - final CommentsFetchResponse cfr = response.body(); - if (cfr == null) callback.onFailure(new Exception("response is empty")); - callback.onSuccess(cfr); + callback.onSuccess(response.body()); } @Override diff --git a/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.kt b/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.kt index a081644b..022bb592 100644 --- a/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.kt +++ b/app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.kt @@ -6,6 +6,7 @@ import awais.instagrabber.repositories.serializers.CaptionDeserializer import awais.instagrabber.utils.Utils import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor import awais.instagrabber.webservices.interceptors.IgErrorsInterceptor +//import awais.instagrabber.webservices.interceptors.LoggingInterceptor import com.google.gson.FieldNamingPolicy import com.google.gson.GsonBuilder import okhttp3.Cache