Added sort options to albumlistfragment (#309)

List is sorted by default on album title. User can change this to artist or artist/year.
This commit is contained in:
Martijn Brekhof 2016-11-09 09:46:42 +01:00 committed by Synced Synapse
parent 11b8d12b31
commit 49a253af34
4 changed files with 126 additions and 3 deletions

View File

@ -45,7 +45,10 @@ public class Settings {
SORT_BY_DATE_ADDED = 1,
SORT_BY_RATING = 2,
SORT_BY_YEAR = 3,
SORT_BY_LENGTH = 4;
SORT_BY_LENGTH = 4,
SORT_BY_ALBUM = 5,
SORT_BY_ARTIST = 6,
SORT_BY_ARTIST_YEAR = 7 ;
/**
* Preferences keys.
@ -88,6 +91,10 @@ public class Settings {
public static final String KEY_PREF_MOVIES_SORT_ORDER = "movies_sort_order";
public static final int DEFAULT_PREF_MOVIES_SORT_ORDER = SORT_BY_NAME;
// Sort order on albums
public static final String KEY_PREF_ALBUMS_SORT_ORDER = "albums_sort_order";
public static final int DEFAULT_PREF_ALBUMS_SORT_ORDER = SORT_BY_ALBUM;
// Ignore articles on movie sorting
public static final String KEY_PREF_MOVIES_IGNORE_PREFIXES = "movies_ignore_prefixes";
public static final boolean DEFAULT_PREF_MOVIES_IGNORE_PREFIXES = false;

View File

@ -18,14 +18,18 @@ package org.xbmc.kore.ui;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.support.v4.content.CursorLoader;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
@ -35,6 +39,7 @@ import android.widget.PopupMenu;
import android.widget.TextView;
import org.xbmc.kore.R;
import org.xbmc.kore.Settings;
import org.xbmc.kore.host.HostInfo;
import org.xbmc.kore.host.HostManager;
import org.xbmc.kore.jsonrpc.type.PlaylistType;
@ -92,6 +97,64 @@ public class AlbumListFragment extends AbstractCursorListFragment {
return fragment;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.album_list, menu);
MenuItem sortByAlbum = menu.findItem(R.id.action_sort_by_album),
sortByArtist = menu.findItem(R.id.action_sort_by_artist),
sortByArtistYear = menu.findItem(R.id.action_sort_by_artist_year);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
int sortOrder = preferences.getInt(Settings.KEY_PREF_ALBUMS_SORT_ORDER, Settings.DEFAULT_PREF_ALBUMS_SORT_ORDER);
switch (sortOrder) {
case Settings.SORT_BY_ALBUM:
sortByAlbum.setChecked(true);
break;
case Settings.SORT_BY_ARTIST:
sortByArtist.setChecked(true);
break;
case Settings.SORT_BY_ARTIST_YEAR:
sortByArtistYear.setChecked(true);
break;
}
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
switch (item.getItemId()) {
case R.id.action_sort_by_album:
item.setChecked(!item.isChecked());
preferences.edit()
.putInt(Settings.KEY_PREF_ALBUMS_SORT_ORDER, Settings.SORT_BY_ALBUM)
.apply();
refreshList();
break;
case R.id.action_sort_by_artist:
item.setChecked(!item.isChecked());
preferences.edit()
.putInt(Settings.KEY_PREF_ALBUMS_SORT_ORDER, Settings.SORT_BY_ARTIST)
.apply();
refreshList();
break;
case R.id.action_sort_by_artist_year:
item.setChecked(!item.isChecked());
preferences.edit()
.putInt(Settings.KEY_PREF_ALBUMS_SORT_ORDER, Settings.SORT_BY_ARTIST_YEAR)
.apply();
refreshList();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onListItemClicked(View view) {
// Get the movie id from the tag
@ -127,8 +190,20 @@ public class AlbumListFragment extends AbstractCursorListFragment {
selectionArgs = new String[] {"%" + searchFilter + "%"};
}
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
String sortOrderStr;
int sortOrder = preferences.getInt(Settings.KEY_PREF_ALBUMS_SORT_ORDER, Settings.DEFAULT_PREF_ALBUMS_SORT_ORDER);
if (sortOrder == Settings.SORT_BY_ARTIST) {
sortOrderStr = AlbumListQuery.SORT_BY_ARTIST;
} else if (sortOrder == Settings.SORT_BY_ARTIST_YEAR) {
sortOrderStr = AlbumListQuery.SORT_BY_ARTIST_YEAR;
} else {
sortOrderStr = AlbumListQuery.SORT_BY_ALBUM;
}
return new CursorLoader(getActivity(), uri,
AlbumListQuery.PROJECTION, selection, selectionArgs, AlbumListQuery.SORT);
AlbumListQuery.PROJECTION, selection, selectionArgs, sortOrderStr);
}
@Override
@ -175,7 +250,10 @@ public class AlbumListFragment extends AbstractCursorListFragment {
MediaContract.Albums.RATING,
};
String SORT = MediaDatabase.sortCommonTokens(MediaContract.Albums.TITLE) + " ASC";
String SORT_BY_ALBUM = MediaDatabase.sortCommonTokens(MediaContract.Albums.TITLE) + " ASC";
String SORT_BY_ARTIST = MediaDatabase.sortCommonTokens(MediaContract.Albums.DISPLAYARTIST) + " ASC";
String SORT_BY_ARTIST_YEAR = MediaDatabase.sortCommonTokens(MediaContract.Albums.DISPLAYARTIST)
+ " ASC, " + MediaContract.Albums.YEAR + " ASC";
final int ID = 0;
final int ALBUMID = 1;

View File

@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2015 Synced Synapse. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_sort_criteria"
android:title="@string/sort_order"
app:showAsAction="never">
<menu>
<group android:checkableBehavior="single"
android:menuCategory="alternative">
<item android:id="@+id/action_sort_by_album"
android:title="@string/by_album"/>
<item android:id="@+id/action_sort_by_artist"
android:title="@string/by_artist"/>
<item android:id="@+id/action_sort_by_artist_year"
android:title="@string/by_artist_and_year"/>
</group>
</menu>
</item>
</menu>

View File

@ -388,5 +388,8 @@
<string name="write_storage_permission_denied">Permission denied. Won\'t be able to download files.</string>
<string name="no_songs_to_download">No songs to download</string>
<string name="by_album">By album</string>
<string name="by_artist">By artist</string>
<string name="by_artist_and_year">By artist and year</string>
</resources>