mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
fix: persist album sort preference.
This commit is contained in:
parent
16d25a1f1d
commit
2854ac6354
3 changed files with 102 additions and 29 deletions
|
|
@ -32,17 +32,19 @@ import com.cappielloantonio.tempo.interfaces.ClickCallback;
|
|||
import com.cappielloantonio.tempo.ui.activity.MainActivity;
|
||||
import com.cappielloantonio.tempo.ui.adapter.AlbumCatalogueAdapter;
|
||||
import com.cappielloantonio.tempo.util.Constants;
|
||||
import com.cappielloantonio.tempo.util.Preferences;
|
||||
import com.cappielloantonio.tempo.viewmodel.AlbumCatalogueViewModel;
|
||||
|
||||
@OptIn(markerClass = UnstableApi.class)
|
||||
public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
||||
private static final String TAG = "ArtistCatalogueFragment";
|
||||
private static final String TAG = "AlbumCatalogueFragment";
|
||||
|
||||
private FragmentAlbumCatalogueBinding bind;
|
||||
private MainActivity activity;
|
||||
private AlbumCatalogueViewModel albumCatalogueViewModel;
|
||||
|
||||
private AlbumCatalogueAdapter albumAdapter;
|
||||
private String currentSortOrder;
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
|
|
@ -115,7 +117,10 @@ public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
|||
albumAdapter = new AlbumCatalogueAdapter(this, true);
|
||||
albumAdapter.setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY);
|
||||
bind.albumCatalogueRecyclerView.setAdapter(albumAdapter);
|
||||
albumCatalogueViewModel.getAlbumList().observe(getViewLifecycleOwner(), albums -> albumAdapter.setItems(albums));
|
||||
albumCatalogueViewModel.getAlbumList().observe(getViewLifecycleOwner(), albums -> {
|
||||
albumAdapter.setItems(albums);
|
||||
applySavedSortOrder();
|
||||
});
|
||||
|
||||
bind.albumCatalogueRecyclerView.setOnTouchListener((v, event) -> {
|
||||
hideKeyboard(v);
|
||||
|
|
@ -137,6 +142,44 @@ public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
|||
});
|
||||
}
|
||||
|
||||
private void applySavedSortOrder() {
|
||||
String savedSortOrder = Preferences.getAlbumSortOrder();
|
||||
currentSortOrder = savedSortOrder;
|
||||
albumAdapter.sort(savedSortOrder);
|
||||
updateSortIndicator();
|
||||
}
|
||||
|
||||
private void updateSortIndicator() {
|
||||
if (bind == null) return;
|
||||
|
||||
String sortText = getSortDisplayText(currentSortOrder);
|
||||
bind.albumListSortTextView.setText(sortText);
|
||||
bind.albumListSortTextView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
private String getSortDisplayText(String sortOrder) {
|
||||
if (sortOrder == null) return "";
|
||||
|
||||
switch (sortOrder) {
|
||||
case Constants.ALBUM_ORDER_BY_NAME:
|
||||
return getString(R.string.menu_sort_name);
|
||||
case Constants.ALBUM_ORDER_BY_ARTIST:
|
||||
return getString(R.string.menu_group_by_artist);
|
||||
case Constants.ALBUM_ORDER_BY_YEAR:
|
||||
return getString(R.string.menu_sort_year);
|
||||
case Constants.ALBUM_ORDER_BY_RANDOM:
|
||||
return getString(R.string.menu_sort_random);
|
||||
case Constants.ALBUM_ORDER_BY_RECENTLY_ADDED:
|
||||
return getString(R.string.menu_sort_recently_added);
|
||||
case Constants.ALBUM_ORDER_BY_RECENTLY_PLAYED:
|
||||
return getString(R.string.menu_sort_recently_played);
|
||||
case Constants.ALBUM_ORDER_BY_MOST_PLAYED:
|
||||
return getString(R.string.menu_sort_most_played);
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
|
||||
inflater.inflate(R.menu.toolbar_menu, menu);
|
||||
|
|
@ -172,26 +215,29 @@ public class AlbumCatalogueFragment extends Fragment implements ClickCallback {
|
|||
popup.getMenuInflater().inflate(menuResource, popup.getMenu());
|
||||
|
||||
popup.setOnMenuItemClickListener(menuItem -> {
|
||||
String newSortOrder = null;
|
||||
|
||||
if (menuItem.getItemId() == R.id.menu_album_sort_name) {
|
||||
albumAdapter.sort(Constants.ALBUM_ORDER_BY_NAME);
|
||||
return true;
|
||||
newSortOrder = Constants.ALBUM_ORDER_BY_NAME;
|
||||
} else if (menuItem.getItemId() == R.id.menu_album_sort_artist) {
|
||||
albumAdapter.sort(Constants.ALBUM_ORDER_BY_ARTIST);
|
||||
return true;
|
||||
newSortOrder = Constants.ALBUM_ORDER_BY_ARTIST;
|
||||
} else if (menuItem.getItemId() == R.id.menu_album_sort_year) {
|
||||
albumAdapter.sort(Constants.ALBUM_ORDER_BY_YEAR);
|
||||
return true;
|
||||
newSortOrder = Constants.ALBUM_ORDER_BY_YEAR;
|
||||
} else if (menuItem.getItemId() == R.id.menu_album_sort_random) {
|
||||
albumAdapter.sort(Constants.ALBUM_ORDER_BY_RANDOM);
|
||||
return true;
|
||||
newSortOrder = Constants.ALBUM_ORDER_BY_RANDOM;
|
||||
} else if (menuItem.getItemId() == R.id.menu_album_sort_recently_added) {
|
||||
albumAdapter.sort(Constants.ALBUM_ORDER_BY_RECENTLY_ADDED);
|
||||
return true;
|
||||
newSortOrder = Constants.ALBUM_ORDER_BY_RECENTLY_ADDED;
|
||||
} else if (menuItem.getItemId() == R.id.menu_album_sort_recently_played) {
|
||||
albumAdapter.sort(Constants.ALBUM_ORDER_BY_RECENTLY_PLAYED);
|
||||
return true;
|
||||
newSortOrder = Constants.ALBUM_ORDER_BY_RECENTLY_PLAYED;
|
||||
} else if (menuItem.getItemId() == R.id.menu_album_sort_most_played) {
|
||||
albumAdapter.sort(Constants.ALBUM_ORDER_BY_MOST_PLAYED);
|
||||
newSortOrder = Constants.ALBUM_ORDER_BY_MOST_PLAYED;
|
||||
}
|
||||
|
||||
if (newSortOrder != null) {
|
||||
currentSortOrder = newSortOrder;
|
||||
albumAdapter.sort(newSortOrder);
|
||||
Preferences.setAlbumSortOrder(newSortOrder);
|
||||
updateSortIndicator();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -76,6 +76,8 @@ object Preferences {
|
|||
private const val EQUALIZER_ENABLED = "equalizer_enabled"
|
||||
private const val EQUALIZER_BAND_LEVELS = "equalizer_band_levels"
|
||||
private const val MINI_SHUFFLE_BUTTON_VISIBILITY = "mini_shuffle_button_visibility"
|
||||
private const val ALBUM_SORT_ORDER = "album_sort_order"
|
||||
private const val DEFAULT_ALBUM_SORT_ORDER = Constants.ALBUM_ORDER_BY_NAME
|
||||
|
||||
@JvmStatic
|
||||
fun getServer(): String? {
|
||||
|
|
@ -638,4 +640,14 @@ object Preferences {
|
|||
if (parts.size < bandCount) return ShortArray(bandCount.toInt())
|
||||
return ShortArray(bandCount.toInt()) { i -> parts[i].toShortOrNull() ?: 0 }
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getAlbumSortOrder(): String {
|
||||
return App.getInstance().preferences.getString(ALBUM_SORT_ORDER, DEFAULT_ALBUM_SORT_ORDER) ?: DEFAULT_ALBUM_SORT_ORDER
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun setAlbumSortOrder(sortOrder: String) {
|
||||
App.getInstance().preferences.edit().putString(ALBUM_SORT_ORDER, sortOrder).apply()
|
||||
}
|
||||
}
|
||||
|
|
@ -41,23 +41,40 @@
|
|||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/sort_container"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/albumListSortTextView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:textAppearance="@style/TextAppearance.Material3.BodySmall"
|
||||
android:paddingEnd="8dp"
|
||||
android:visibility="gone" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/album_list_sort_image_view"
|
||||
style="@style/Widget.Material3.Button.TonalButton.Icon"
|
||||
android:layout_width="52dp"
|
||||
android:layout_height="52dp"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:insetLeft="0dp"
|
||||
android:insetTop="0dp"
|
||||
android:insetRight="0dp"
|
||||
android:insetBottom="0dp"
|
||||
app:cornerRadius="30dp"
|
||||
app:icon="@drawable/ic_sort_list"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
app:icon="@drawable/ic_sort_list" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/album_list_progress_loader"
|
||||
|
|
@ -71,7 +88,6 @@
|
|||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"/>
|
||||
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</com.google.android.material.appbar.AppBarLayout>
|
||||
|
||||
|
|
@ -87,4 +103,3 @@
|
|||
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
|
||||
</androidx.coordinatorlayout.widget.CoordinatorLayout>
|
||||
</LinearLayout>
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue