Browse Source

Merge branch 'master' into feature/multistack-navigation

renovate/androidx.fragment-fragment-ktx-1.x
Ammar Githam 3 years ago
parent
commit
0d35f37bbc
  1. 2
      app/build.gradle
  2. 6
      app/src/main/java/awais/instagrabber/adapters/SavedCollectionsAdapter.java
  3. 14
      app/src/main/java/awais/instagrabber/fragments/SavedCollectionsFragment.java

2
app/build.gradle

@ -246,7 +246,7 @@ dependencies {
testImplementation "androidx.test.ext:junit-ktx:1.1.3" testImplementation "androidx.test.ext:junit-ktx:1.1.3"
testImplementation "androidx.test:core-ktx:1.4.0" testImplementation "androidx.test:core-ktx:1.4.0"
testImplementation "androidx.arch.core:core-testing:2.1.0" testImplementation "androidx.arch.core:core-testing:2.1.0"
testImplementation "org.robolectric:robolectric:4.6"
testImplementation "org.robolectric:robolectric:4.5.1"
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.0' testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.0'
androidTestImplementation 'org.junit.jupiter:junit-jupiter:5.7.2' androidTestImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'

6
app/src/main/java/awais/instagrabber/adapters/SavedCollectionsAdapter.java

@ -8,6 +8,8 @@ import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter; import androidx.recyclerview.widget.ListAdapter;
import java.util.Objects;
import awais.instagrabber.adapters.viewholder.TopicClusterViewHolder; import awais.instagrabber.adapters.viewholder.TopicClusterViewHolder;
import awais.instagrabber.databinding.ItemDiscoverTopicBinding; import awais.instagrabber.databinding.ItemDiscoverTopicBinding;
import awais.instagrabber.repositories.responses.saved.SavedCollection; 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) { public boolean areContentsTheSame(@NonNull final SavedCollection oldItem, @NonNull final SavedCollection newItem) {
if (oldItem.getCoverMediaList() != null && newItem.getCoverMediaList() != null if (oldItem.getCoverMediaList() != null && newItem.getCoverMediaList() != null
&& oldItem.getCoverMediaList().size() == newItem.getCoverMediaList().size()) { && 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) { 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; return false;
} }

14
app/src/main/java/awais/instagrabber/fragments/SavedCollectionsFragment.java

@ -52,12 +52,14 @@ public class SavedCollectionsFragment extends Fragment implements SwipeRefreshLa
private boolean shouldRefresh = true; private boolean shouldRefresh = true;
private boolean isSaving; private boolean isSaving;
private ProfileService profileService; private ProfileService profileService;
private SavedCollectionsAdapter adapter;
@Override @Override
public void onCreate(@Nullable final Bundle savedInstanceState) { public void onCreate(@Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
fragmentActivity = (MainActivity) requireActivity(); fragmentActivity = (MainActivity) requireActivity();
profileService = ProfileService.getInstance(); profileService = ProfileService.getInstance();
savedCollectionsViewModel = new ViewModelProvider(fragmentActivity).get(SavedCollectionsViewModel.class);
setHasOptionsMenu(true); setHasOptionsMenu(true);
} }
@ -76,6 +78,7 @@ public class SavedCollectionsFragment extends Fragment implements SwipeRefreshLa
@Override @Override
public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) { public void onViewCreated(@NonNull final View view, @Nullable final Bundle savedInstanceState) {
setupObservers();
if (!shouldRefresh) return; if (!shouldRefresh) return;
binding.swipeRefreshLayout.setOnRefreshListener(this); binding.swipeRefreshLayout.setOnRefreshListener(this);
init(); init();
@ -141,9 +144,8 @@ public class SavedCollectionsFragment extends Fragment implements SwipeRefreshLa
} }
public void setupTopics() { public void setupTopics() {
savedCollectionsViewModel = new ViewModelProvider(fragmentActivity).get(SavedCollectionsViewModel.class);
binding.topicsRecyclerView.addItemDecoration(new GridSpacingItemDecoration(Utils.convertDpToPx(2))); 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); final NavController navController = NavHostFragment.findNavController(this);
if (isSaving) { if (isSaving) {
setNavControllerResult(navController, topicCluster.getCollectionId()); setNavControllerResult(navController, topicCluster.getCollectionId());
@ -161,7 +163,13 @@ public class SavedCollectionsFragment extends Fragment implements SwipeRefreshLa
} }
}); });
binding.topicsRecyclerView.setAdapter(adapter); 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) { private void fetchTopics(final String maxId) {

Loading…
Cancel
Save