mirror of
				https://github.com/KokaKiwi/BarInsta
				synced 2025-10-31 03:25:34 +00:00 
			
		
		
		
	fix comments duplicate fetch
logged-in users should not fetch anonymous endpoint
This commit is contained in:
		
							parent
							
								
									77bca1b8ec
								
							
						
					
					
						commit
						66acf16d53
					
				| @ -106,8 +106,21 @@ public final class CommentsViewerFragment extends BottomSheetDialogFragment { | |||||||
|         binding.swipeRefreshLayout.setNestedScrollingEnabled(false); |         binding.swipeRefreshLayout.setNestedScrollingEnabled(false); | ||||||
|         root = binding.getRoot(); |         root = binding.getRoot(); | ||||||
|         appStateViewModel.getCurrentUserLiveData().observe(getViewLifecycleOwner(), userResource -> { |         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); |             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; |         if (getArguments() == null) return root; | ||||||
|         final CommentsViewerFragmentArgs args = CommentsViewerFragmentArgs.fromBundle(getArguments()); |         final CommentsViewerFragmentArgs args = CommentsViewerFragmentArgs.fromBundle(getArguments()); | ||||||
|  | |||||||
| @ -58,7 +58,7 @@ public class AppStateViewModel extends AndroidViewModel { | |||||||
|     public void fetchProfileDetails() { |     public void fetchProfileDetails() { | ||||||
|         currentUser.postValue(Resource.loading(null)); |         currentUser.postValue(Resource.loading(null)); | ||||||
|         final long uid = CookieUtils.getUserIdFromCookie(cookie); |         final long uid = CookieUtils.getUserIdFromCookie(cookie); | ||||||
|         if (userRepository == null) { |         if (uid == 0L) { | ||||||
|             currentUser.postValue(Resource.success(null)); |             currentUser.postValue(Resource.success(null)); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -43,7 +43,6 @@ import static awais.instagrabber.utils.Utils.settingsHelper; | |||||||
| public class CommentsViewerViewModel extends ViewModel { | public class CommentsViewerViewModel extends ViewModel { | ||||||
|     private static final String TAG = CommentsViewerViewModel.class.getSimpleName(); |     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<Long> currentUserId = new MutableLiveData<>(0L); | ||||||
|     private final MutableLiveData<Resource<List<Comment>>> rootList = new MutableLiveData<>(); |     private final MutableLiveData<Resource<List<Comment>>> rootList = new MutableLiveData<>(); | ||||||
|     private final MutableLiveData<Integer> rootCount = new MutableLiveData<>(0); |     private final MutableLiveData<Integer> rootCount = new MutableLiveData<>(0); | ||||||
| @ -130,18 +129,12 @@ public class CommentsViewerViewModel extends ViewModel { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setCurrentUser(final User currentUser) { |     public void setCurrentUser(final User currentUser) { | ||||||
|         isLoggedIn.postValue(currentUser != null); |  | ||||||
|         currentUserId.postValue(currentUser == null ? 0 : currentUser.getPk()); |         currentUserId.postValue(currentUser == null ? 0 : currentUser.getPk()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public void setPostDetails(final String shortCode, final String postId, final long postUserId) { |     public void setPostDetails(final String shortCode, final String postId, final long postUserId) { | ||||||
|         this.shortCode = shortCode; |         this.shortCode = shortCode; | ||||||
|         this.postId = postId; |         this.postId = postId; | ||||||
|         fetchComments(); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     public LiveData<Boolean> isLoggedIn() { |  | ||||||
|         return isLoggedIn; |  | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     public LiveData<Long> getCurrentUserId() { |     public LiveData<Long> getCurrentUserId() { | ||||||
| @ -174,7 +167,7 @@ public class CommentsViewerViewModel extends ViewModel { | |||||||
|         if (shortCode == null || postId == null) return; |         if (shortCode == null || postId == null) return; | ||||||
|         if (!rootHasNext) return; |         if (!rootHasNext) return; | ||||||
|         rootList.postValue(Resource.loading(getPrevList(rootList))); |         rootList.postValue(Resource.loading(getPrevList(rootList))); | ||||||
|         if (isLoggedIn.getValue()) { |         if (currentUserId.getValue() != 0L) { | ||||||
|             commentService.fetchComments(postId, rootCursor, ccb); |             commentService.fetchComments(postId, rootCursor, ccb); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| @ -202,8 +195,7 @@ public class CommentsViewerViewModel extends ViewModel { | |||||||
|             list = getPrevList(replyList); |             list = getPrevList(replyList); | ||||||
|         } |         } | ||||||
|         replyList.postValue(Resource.loading(list)); |         replyList.postValue(Resource.loading(list)); | ||||||
|         final Boolean isLoggedInValue = isLoggedIn.getValue(); |         if (currentUserId.getValue() != 0L) { | ||||||
|         if (isLoggedInValue != null && isLoggedInValue) { |  | ||||||
|             commentService.fetchChildComments(postId, commentId, repliesCursor, rcb); |             commentService.fetchChildComments(postId, commentId, repliesCursor, rcb); | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
|  | |||||||
| @ -79,9 +79,7 @@ public class CommentService { | |||||||
|         request.enqueue(new Callback<CommentsFetchResponse>() { |         request.enqueue(new Callback<CommentsFetchResponse>() { | ||||||
|             @Override |             @Override | ||||||
|             public void onResponse(@NonNull final Call<CommentsFetchResponse> call, @NonNull final Response<CommentsFetchResponse> response) { |             public void onResponse(@NonNull final Call<CommentsFetchResponse> call, @NonNull final Response<CommentsFetchResponse> response) { | ||||||
|                 final CommentsFetchResponse cfr = response.body(); |                 callback.onSuccess(response.body()); | ||||||
|                 if (cfr == null) callback.onFailure(new Exception("response is empty")); |  | ||||||
|                 callback.onSuccess(cfr); |  | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
|             @Override |             @Override | ||||||
|  | |||||||
| @ -6,6 +6,7 @@ import awais.instagrabber.repositories.serializers.CaptionDeserializer | |||||||
| import awais.instagrabber.utils.Utils | import awais.instagrabber.utils.Utils | ||||||
| import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor | import awais.instagrabber.webservices.interceptors.AddCookiesInterceptor | ||||||
| import awais.instagrabber.webservices.interceptors.IgErrorsInterceptor | import awais.instagrabber.webservices.interceptors.IgErrorsInterceptor | ||||||
|  | //import awais.instagrabber.webservices.interceptors.LoggingInterceptor | ||||||
| import com.google.gson.FieldNamingPolicy | import com.google.gson.FieldNamingPolicy | ||||||
| import com.google.gson.GsonBuilder | import com.google.gson.GsonBuilder | ||||||
| import okhttp3.Cache | import okhttp3.Cache | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user