mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 09:33:33 +00:00
fix: persist album sorting on resume
This commit is contained in:
parent
118f742cb6
commit
c3cce18600
2 changed files with 44 additions and 10 deletions
|
|
@ -152,6 +152,12 @@ public class AlbumCatalogueAdapter extends RecyclerView.Adapter<AlbumCatalogueAd
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setItemsWithoutFilter(List<AlbumID3> albums) {
|
||||||
|
this.albumsFull = new ArrayList<>(albums);
|
||||||
|
this.albums = new ArrayList<>(albums);
|
||||||
|
notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
|
||||||
public void sort(String order) {
|
public void sort(String order) {
|
||||||
if (albums == null) return;
|
if (albums == null) return;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ package com.cappielloantonio.tempo.ui.fragment;
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
|
|
@ -35,6 +36,10 @@ import com.cappielloantonio.tempo.util.Constants;
|
||||||
import com.cappielloantonio.tempo.util.Preferences;
|
import com.cappielloantonio.tempo.util.Preferences;
|
||||||
import com.cappielloantonio.tempo.viewmodel.AlbumCatalogueViewModel;
|
import com.cappielloantonio.tempo.viewmodel.AlbumCatalogueViewModel;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@OptIn(markerClass = UnstableApi.class)
|
@OptIn(markerClass = UnstableApi.class)
|
||||||
public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
||||||
private static final String TAG = "AlbumCatalogueFragment";
|
private static final String TAG = "AlbumCatalogueFragment";
|
||||||
|
|
@ -45,15 +50,33 @@ public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
||||||
|
|
||||||
private AlbumCatalogueAdapter albumAdapter;
|
private AlbumCatalogueAdapter albumAdapter;
|
||||||
private String currentSortOrder;
|
private String currentSortOrder;
|
||||||
|
private List<com.cappielloantonio.tempo.subsonic.models.AlbumID3> originalAlbums;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setHasOptionsMenu(true);
|
setHasOptionsMenu(true);
|
||||||
|
currentSortOrder = Preferences.getAlbumSortOrder();
|
||||||
|
|
||||||
initData();
|
initData();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
String latestSort = Preferences.getAlbumSortOrder();
|
||||||
|
|
||||||
|
if (!latestSort.equals(currentSortOrder)) {
|
||||||
|
currentSortOrder = latestSort;
|
||||||
|
}
|
||||||
|
// Re-apply sort when returning to fragment
|
||||||
|
if (originalAlbums != null && currentSortOrder != null) {
|
||||||
|
applySortToAlbums(currentSortOrder);
|
||||||
|
} else {
|
||||||
|
Log.d(TAG, "onResume - Cannot re-sort, missing data");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy() {
|
public void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|
@ -118,8 +141,10 @@ public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
||||||
albumAdapter.setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY);
|
albumAdapter.setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY);
|
||||||
bind.albumCatalogueRecyclerView.setAdapter(albumAdapter);
|
bind.albumCatalogueRecyclerView.setAdapter(albumAdapter);
|
||||||
albumCatalogueViewModel.getAlbumList().observe(getViewLifecycleOwner(), albums -> {
|
albumCatalogueViewModel.getAlbumList().observe(getViewLifecycleOwner(), albums -> {
|
||||||
albumAdapter.setItems(albums);
|
originalAlbums = albums;
|
||||||
applySavedSortOrder();
|
currentSortOrder = Preferences.getAlbumSortOrder();
|
||||||
|
applySortToAlbums(currentSortOrder);
|
||||||
|
updateSortIndicator();
|
||||||
});
|
});
|
||||||
|
|
||||||
bind.albumCatalogueRecyclerView.setOnTouchListener((v, event) -> {
|
bind.albumCatalogueRecyclerView.setOnTouchListener((v, event) -> {
|
||||||
|
|
@ -130,6 +155,16 @@ public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
||||||
bind.albumListSortImageView.setOnClickListener(view -> showPopupMenu(view, R.menu.sort_album_popup_menu));
|
bind.albumListSortImageView.setOnClickListener(view -> showPopupMenu(view, R.menu.sort_album_popup_menu));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void applySortToAlbums(String sortOrder) {
|
||||||
|
if (originalAlbums == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
albumAdapter.setItemsWithoutFilter(originalAlbums);
|
||||||
|
if (sortOrder != null) {
|
||||||
|
albumAdapter.sort(sortOrder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void initProgressLoader() {
|
private void initProgressLoader() {
|
||||||
albumCatalogueViewModel.getLoadingStatus().observe(getViewLifecycleOwner(), isLoading -> {
|
albumCatalogueViewModel.getLoadingStatus().observe(getViewLifecycleOwner(), isLoading -> {
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
|
|
@ -142,13 +177,6 @@ public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void applySavedSortOrder() {
|
|
||||||
String savedSortOrder = Preferences.getAlbumSortOrder();
|
|
||||||
currentSortOrder = savedSortOrder;
|
|
||||||
albumAdapter.sort(savedSortOrder);
|
|
||||||
updateSortIndicator();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateSortIndicator() {
|
private void updateSortIndicator() {
|
||||||
if (bind == null) return;
|
if (bind == null) return;
|
||||||
|
|
||||||
|
|
@ -235,8 +263,8 @@ public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
||||||
|
|
||||||
if (newSortOrder != null) {
|
if (newSortOrder != null) {
|
||||||
currentSortOrder = newSortOrder;
|
currentSortOrder = newSortOrder;
|
||||||
albumAdapter.sort(newSortOrder);
|
|
||||||
Preferences.setAlbumSortOrder(newSortOrder);
|
Preferences.setAlbumSortOrder(newSortOrder);
|
||||||
|
applySortToAlbums(newSortOrder);
|
||||||
updateSortIndicator();
|
updateSortIndicator();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue