allow sorting the songs by artist

This commit is contained in:
tibbi 2016-07-08 15:34:14 +02:00
parent feba6fcdd0
commit 26e6065203
8 changed files with 87 additions and 3 deletions

View file

@ -5,6 +5,7 @@ import android.content.SharedPreferences;
public class Config {
private SharedPreferences mPrefs;
public static final int SORT_BY_TITLE = 0;
public static Config newInstance(Context context) {
return new Config(context);
@ -37,4 +38,12 @@ public class Config {
public void setIsNumericProgressEnabled(boolean enabled) {
mPrefs.edit().putBoolean(Constants.NUMERIC_PROGRESS, enabled).apply();
}
public int getSorting() {
return mPrefs.getInt(Constants.SORTING, SORT_BY_TITLE);
}
public void setSorting(int sorting) {
mPrefs.edit().putInt(Constants.SORTING, sorting).apply();
}
}

View file

@ -30,4 +30,5 @@ public class Constants {
public static final String IS_FIRST_RUN = "is_first_run";
public static final String SHUFFLE = "shuffle";
public static final String NUMERIC_PROGRESS = "numeric_progress";
public static final String SORTING = "sorting";
}

View file

@ -90,6 +90,7 @@ public class MusicService extends Service
}
private void initService() {
mConfig = Config.newInstance(getApplicationContext());
mSongs = new ArrayList<>();
mPlayedSongIndexes = new ArrayList<>();
mIgnoredPaths = new ArrayList<>();
@ -98,7 +99,6 @@ public class MusicService extends Service
mHeadsetPlugReceiver = new HeadsetPlugReceiver();
mIncomingCallReceiver = new IncomingCallReceiver(this);
mWasPlayingAtCall = false;
mConfig = Config.newInstance(getApplicationContext());
initMediaPlayerIfNeeded();
createNotificationButtons();
setupNotification();
@ -159,7 +159,7 @@ public class MusicService extends Service
mIgnoredPaths = Arrays.asList(intent.getStringArrayExtra(Constants.DELETED_SONGS));
}
fillPlaylist();
getSortedSongs();
if (intent.getExtras() != null && intent.getExtras().containsKey(Constants.UPDATE_ACTIVITY)) {
mBus.post(new Events.PlaylistUpdated(mSongs));
@ -232,7 +232,11 @@ public class MusicService extends Service
fillPlaylist();
Collections.sort(mSongs, new Comparator<Song>() {
public int compare(Song a, Song b) {
return a.getTitle().compareTo(b.getTitle());
if (mConfig.getSorting() == Config.SORT_BY_TITLE) {
return a.getTitle().compareTo(b.getTitle());
} else {
return a.getArtist().compareTo(b.getArtist());
}
}
});
}

View file

@ -1,19 +1,25 @@
package com.simplemobiletools.musicplayer.activities;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.SwitchCompat;
import com.simplemobiletools.musicplayer.Config;
import com.simplemobiletools.musicplayer.Constants;
import com.simplemobiletools.musicplayer.MusicService;
import com.simplemobiletools.musicplayer.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnItemSelected;
public class SettingsActivity extends AppCompatActivity {
@BindView(R.id.settings_shuffle) SwitchCompat mShuffleSwitch;
@BindView(R.id.settings_numeric_progress) SwitchCompat mNumericProgressSwitch;
@BindView(R.id.settings_sorting) AppCompatSpinner mSortingSpinner;
private static Config mConfig;
@ -26,6 +32,7 @@ public class SettingsActivity extends AppCompatActivity {
setupShuffle();
setupNumericProgress();
setupSorting();
}
private void setupShuffle() {
@ -36,6 +43,10 @@ public class SettingsActivity extends AppCompatActivity {
mNumericProgressSwitch.setChecked(mConfig.getIsNumericProgressEnabled());
}
private void setupSorting() {
mSortingSpinner.setSelection(mConfig.getSorting());
}
@OnClick(R.id.settings_shuffle_holder)
public void handleShuffle() {
mShuffleSwitch.setChecked(!mShuffleSwitch.isChecked());
@ -47,4 +58,17 @@ public class SettingsActivity extends AppCompatActivity {
mNumericProgressSwitch.setChecked(!mNumericProgressSwitch.isChecked());
mConfig.setIsNumericProgressEnabled(mNumericProgressSwitch.isChecked());
}
@OnItemSelected(R.id.settings_sorting)
public void handleSorting() {
mConfig.setSorting(mSortingSpinner.getSelectedItemPosition());
updatePlaylist();
}
private void updatePlaylist() {
final Intent intent = new Intent(this, MusicService.class);
intent.putExtra(Constants.UPDATE_ACTIVITY, true);
intent.setAction(Constants.REFRESH_LIST);
startService(intent);
}
}

View file

@ -57,4 +57,31 @@
android:clickable="false"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_sorting_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/settings_padding"
android:paddingBottom="@dimen/activity_margin"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/settings_padding"
android:paddingTop="@dimen/activity_margin">
<TextView
android:id="@+id/settings_sorting_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="@dimen/settings_padding"
android:text="@string/sort_by"/>
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/settings_sorting"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:entries="@array/sorting"/>
</RelativeLayout>
</LinearLayout>

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sorting">
<item>@string/title</item>
<item>@string/artist</item>
</string-array>
</resources>

View file

@ -23,6 +23,7 @@
<string name="settings">Settings</string>
<string name="shuffle">Shuffle</string>
<string name="numeric_progress">Display progress with numbers too</string>
<string name="sort_by">Sort tracks by</string>
<!-- About -->
<string name="about">About</string>

View file

@ -6,6 +6,9 @@
<item name="colorAccent">@color/colorAccent</item>
<item name="actionBarStyle">@style/AppTheme.ActionBarStyle</item>
<item name="android:textSize">@dimen/normal_text_size</item>
<item name="android:spinnerItemStyle">@style/SpinnerItem</item>
<item name="spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
<item name="android:spinnerDropDownItemStyle">@style/SpinnerItem.DropDownItem</item>
</style>
<style name="MyWidgetConfigTheme" parent="@style/AppTheme">
@ -26,4 +29,12 @@
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">20sp</item>
</style>
<style name="SpinnerItem" parent="@android:style/Widget.TextView"/>
<style name="SpinnerItem.DropDownItem" parent="@android:style/Widget.TextView">
<item name="android:gravity">center_vertical</item>
<item name="android:paddingLeft">@dimen/activity_margin</item>
<item name="android:paddingRight">@dimen/activity_margin</item>
</style>
</resources>