Browse Source

fix comments duplicate fetch

logged-in users should not fetch anonymous endpoint
master
Austin Huang 3 years ago
parent
commit
66acf16d53
No known key found for this signature in database GPG Key ID: 84C23AA04587A91F
  1. 15
      app/src/main/java/awais/instagrabber/fragments/comments/CommentsViewerFragment.java
  2. 2
      app/src/main/java/awais/instagrabber/viewmodels/AppStateViewModel.java
  3. 12
      app/src/main/java/awais/instagrabber/viewmodels/CommentsViewerViewModel.java
  4. 4
      app/src/main/java/awais/instagrabber/webservices/CommentService.java
  5. 1
      app/src/main/java/awais/instagrabber/webservices/RetrofitFactory.kt

15
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<Long>() {
@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());

2
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;
}

12
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<Boolean> isLoggedIn = new MutableLiveData<>(false);
private final MutableLiveData<Long> currentUserId = new MutableLiveData<>(0L);
private final MutableLiveData<Resource<List<Comment>>> rootList = new MutableLiveData<>();
private final MutableLiveData<Integer> 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<Boolean> isLoggedIn() {
return isLoggedIn;
}
public LiveData<Long> 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;
}

4
app/src/main/java/awais/instagrabber/webservices/CommentService.java

@ -79,9 +79,7 @@ public class CommentService {
request.enqueue(new Callback<CommentsFetchResponse>() {
@Override
public void onResponse(@NonNull final Call<CommentsFetchResponse> call, @NonNull final Response<CommentsFetchResponse> response) {
final CommentsFetchResponse cfr = response.body();
if (cfr == null) callback.onFailure(new Exception("response is empty"));
callback.onSuccess(cfr);
callback.onSuccess(response.body());
}
@Override

1
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

Loading…
Cancel
Save