mirror of
https://github.com/antebudimir/tempus.git
synced 2026-01-01 01:53:31 +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();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue