1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-12 17:57:29 +00:00

Fix saved collections not refreshing properly on delete. Fixes austinhuang0131/barinsta#966

This commit is contained in:
Ammar Githam 2021-07-04 20:32:46 +09:00
parent 363b5c2d74
commit 506e0c5685
2 changed files with 15 additions and 5 deletions

View File

@ -8,6 +8,8 @@ import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import java.util.Objects;
import awais.instagrabber.adapters.viewholder.TopicClusterViewHolder;
import awais.instagrabber.databinding.ItemDiscoverTopicBinding;
import awais.instagrabber.repositories.responses.saved.SavedCollection;
@ -23,10 +25,10 @@ public class SavedCollectionsAdapter extends ListAdapter<SavedCollection, TopicC
public boolean areContentsTheSame(@NonNull final SavedCollection oldItem, @NonNull final SavedCollection newItem) {
if (oldItem.getCoverMediaList() != null && newItem.getCoverMediaList() != null
&& oldItem.getCoverMediaList().size() == newItem.getCoverMediaList().size()) {
return oldItem.getCoverMediaList().get(0).getId().equals(newItem.getCoverMediaList().get(0).getId());
return Objects.equals(oldItem.getCoverMediaList().get(0).getId(), newItem.getCoverMediaList().get(0).getId());
}
else if (oldItem.getCoverMedia() != null && newItem.getCoverMedia() != null) {
return oldItem.getCoverMedia().getId().equals(newItem.getCoverMedia().getId());
return Objects.equals(oldItem.getCoverMedia().getId(), newItem.getCoverMedia().getId());
}
return false;
}

View File

@ -51,12 +51,14 @@ public class SavedCollectionsFragment extends Fragment implements SwipeRefreshLa
private boolean shouldRefresh = true;
private boolean isSaving;
private ProfileService profileService;
private SavedCollectionsAdapter adapter;
@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragmentActivity = (MainActivity) requireActivity();
profileService = ProfileService.getInstance();
savedCollectionsViewModel = new ViewModelProvider(fragmentActivity).get(SavedCollectionsViewModel.class);
setHasOptionsMenu(true);
}
@ -75,6 +77,7 @@ public class SavedCollectionsFragment extends Fragment implements SwipeRefreshLa
@Override
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
setupObservers();
if (!shouldRefresh) return;
binding.swipeRefreshLayout.setOnRefreshListener(this);
init();
@ -140,9 +143,8 @@ public class SavedCollectionsFragment extends Fragment implements SwipeRefreshLa
}
public void setupTopics() {
savedCollectionsViewModel = new ViewModelProvider(fragmentActivity).get(SavedCollectionsViewModel.class);
binding.topicsRecyclerView.addItemDecoration(new GridSpacingItemDecoration(Utils.convertDpToPx(2)));
final SavedCollectionsAdapter adapter = new SavedCollectionsAdapter((topicCluster, root, cover, title, titleColor, backgroundColor) -> {
adapter = new SavedCollectionsAdapter((topicCluster, root, cover, title, titleColor, backgroundColor) -> {
final NavController navController = NavHostFragment.findNavController(this);
if (isSaving) {
setNavControllerResult(navController, topicCluster.getCollectionId());
@ -160,7 +162,13 @@ public class SavedCollectionsFragment extends Fragment implements SwipeRefreshLa
}
});
binding.topicsRecyclerView.setAdapter(adapter);
savedCollectionsViewModel.getList().observe(getViewLifecycleOwner(), adapter::submitList);
}
private void setupObservers() {
savedCollectionsViewModel.getList().observe(getViewLifecycleOwner(), list -> {
if (adapter == null) return;
adapter.submitList(list);
});
}
private void fetchTopics(final String maxId) {