mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Added the ability to sort items in the genre catalogue
This commit is contained in:
parent
e6b997bc35
commit
3eee78ad4e
5 changed files with 73 additions and 4 deletions
|
|
@ -13,11 +13,14 @@ import androidx.annotation.NonNull;
|
|||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.cappielloantonio.play.R;
|
||||
import com.cappielloantonio.play.model.Artist;
|
||||
import com.cappielloantonio.play.model.Genre;
|
||||
import com.cappielloantonio.play.ui.activity.MainActivity;
|
||||
import com.cappielloantonio.play.util.MusicUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class GenreCatalogueAdapter extends RecyclerView.Adapter<GenreCatalogueAdapter.ViewHolder> implements Filterable {
|
||||
|
|
@ -129,4 +132,17 @@ public class GenreCatalogueAdapter extends RecyclerView.Adapter<GenreCatalogueAd
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void sort(String order) {
|
||||
switch (order) {
|
||||
case Genre.ORDER_BY_NAME:
|
||||
genres.sort(Comparator.comparing(Genre::getName));
|
||||
break;
|
||||
case Genre.ORDER_BY_RANDOM:
|
||||
Collections.shuffle(genres);
|
||||
break;
|
||||
}
|
||||
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,9 @@ import android.os.Parcelable;
|
|||
import androidx.annotation.NonNull;
|
||||
|
||||
public class Genre implements Parcelable {
|
||||
public static final String ORDER_BY_NAME = "ORDER_BY_NAME";
|
||||
public static final String ORDER_BY_RANDOM = "ORDER_BY_RANDOM";
|
||||
|
||||
private String id;
|
||||
private String name;
|
||||
private int songCount;
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.view.inputmethod.InputMethodManager;
|
||||
import android.widget.PopupMenu;
|
||||
import android.widget.SearchView;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
|
@ -25,6 +26,8 @@ import com.cappielloantonio.play.R;
|
|||
import com.cappielloantonio.play.adapter.GenreCatalogueAdapter;
|
||||
import com.cappielloantonio.play.databinding.FragmentGenreCatalogueBinding;
|
||||
import com.cappielloantonio.play.helper.recyclerview.GridItemDecoration;
|
||||
import com.cappielloantonio.play.model.Artist;
|
||||
import com.cappielloantonio.play.model.Genre;
|
||||
import com.cappielloantonio.play.model.Media;
|
||||
import com.cappielloantonio.play.ui.activity.MainActivity;
|
||||
import com.cappielloantonio.play.viewmodel.GenreCatalogueViewModel;
|
||||
|
|
@ -54,7 +57,7 @@ public class GenreCatalogueFragment extends Fragment {
|
|||
|
||||
init();
|
||||
initAppBar();
|
||||
initArtistCatalogueView();
|
||||
initGenreCatalogueView();
|
||||
|
||||
return view;
|
||||
}
|
||||
|
|
@ -88,7 +91,6 @@ public class GenreCatalogueFragment extends Fragment {
|
|||
activity.navController.navigateUp();
|
||||
});
|
||||
|
||||
|
||||
bind.appBarLayout.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
|
||||
if ((bind.genreInfoSector.getHeight() + verticalOffset) < (2 * ViewCompat.getMinimumHeight(bind.toolbar))) {
|
||||
bind.toolbar.setTitle(R.string.genre_catalogue_title);
|
||||
|
|
@ -99,7 +101,7 @@ public class GenreCatalogueFragment extends Fragment {
|
|||
}
|
||||
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
private void initArtistCatalogueView() {
|
||||
private void initGenreCatalogueView() {
|
||||
bind.genreCatalogueRecyclerView.setLayoutManager(new GridLayoutManager(requireContext(), 2));
|
||||
bind.genreCatalogueRecyclerView.addItemDecoration(new GridItemDecoration(2, 16, false));
|
||||
bind.genreCatalogueRecyclerView.setHasFixedSize(true);
|
||||
|
|
@ -120,6 +122,8 @@ public class GenreCatalogueFragment extends Fragment {
|
|||
hideKeyboard(v);
|
||||
return false;
|
||||
});
|
||||
|
||||
bind.genreListSortImageView.setOnClickListener(view -> showPopupMenu(view, R.menu.sort_genre_popup_menu));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -151,4 +155,23 @@ public class GenreCatalogueFragment extends Fragment {
|
|||
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
|
||||
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
|
||||
}
|
||||
|
||||
private void showPopupMenu(View view, int menuResource) {
|
||||
PopupMenu popup = new PopupMenu(requireContext(), view);
|
||||
popup.getMenuInflater().inflate(menuResource, popup.getMenu());
|
||||
|
||||
popup.setOnMenuItemClickListener(menuItem -> {
|
||||
if (menuItem.getItemId() == R.id.menu_genre_sort_name) {
|
||||
genreCatalogueAdapter.sort(Genre.ORDER_BY_NAME);
|
||||
return true;
|
||||
} else if (menuItem.getItemId() == R.id.menu_genre_sort_random) {
|
||||
genreCatalogueAdapter.sort(Genre.ORDER_BY_RANDOM);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
popup.show();
|
||||
}
|
||||
}
|
||||
|
|
@ -49,7 +49,7 @@
|
|||
android:layout_height="52dp"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:insetLeft="0dp"
|
||||
android:insetTop="0dp"
|
||||
|
|
@ -58,6 +58,24 @@
|
|||
app:cornerRadius="30dp"
|
||||
app:icon="@drawable/ic_filter_list"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toStartOf="@+id/genre_list_sort_image_view" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/genre_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" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</com.google.android.material.appbar.AppBarLayout>
|
||||
|
|
|
|||
9
app/src/main/res/menu/sort_genre_popup_menu.xml
Normal file
9
app/src/main/res/menu/sort_genre_popup_menu.xml
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<item
|
||||
android:id="@+id/menu_genre_sort_name"
|
||||
android:title="@string/menu_sort_name" />
|
||||
<item
|
||||
android:id="@+id/menu_genre_sort_random"
|
||||
android:title="@string/menu_sort_random" />
|
||||
</menu>
|
||||
Loading…
Add table
Add a link
Reference in a new issue