package io.hefuyi.listener.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
/**
* Created by hefuyi on 2016/11/3.
*/
public class PreferencesUtility {
private static final String ARTIST_SORT_ORDER = "artist_sort_order";
private static final String ARTIST_SONG_SORT_ORDER = "artist_song_sort_order";
private static final String ARTIST_ART_URL = "artist_art_url_";
private static final String ALBUM_SORT_ORDER = "album_sort_order";
private static final String ALBUM_SONG_SORT_ORDER = "album_song_sort_order";
private static final String SONG_SORT_ORDER = "song_sort_order";
private static final String TOGGLE_ARTIST_GRID = "toggle_artist_grid";
private static final String TOGGLE_ALBUM_GRID = "toggle_album_grid";
private static final String TOGGLE_PLAYLIST_VIEW = "toggle_playlist_view";
private static final String START_PAGE_INDEX = "start_page_index";
private static PreferencesUtility sInstance;
private static volatile SharedPreferences mPreferences;
public PreferencesUtility(final Context context) {
mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
}
public static PreferencesUtility getInstance(final Context context) {
if (sInstance == null) {
synchronized (PreferencesUtility.class) {
if (sInstance == null) {
sInstance = new PreferencesUtility(context.getApplicationContext());
}
}
}
return sInstance;
}
public void setOnSharedPreferenceChangeListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
mPreferences.registerOnSharedPreferenceChangeListener(listener);
}
public boolean isArtistsInGrid() {
return mPreferences.getBoolean(TOGGLE_ARTIST_GRID, true);
}
public void setArtistsInGrid(final boolean b) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean(TOGGLE_ARTIST_GRID, b);
editor.apply();
}
public boolean isAlbumsInGrid() {
return mPreferences.getBoolean(TOGGLE_ALBUM_GRID, true);
}
public void setAlbumsInGrid(final boolean b) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean(TOGGLE_ALBUM_GRID, b);
editor.apply();
}
public int getStartPageIndex() {
return mPreferences.getInt(START_PAGE_INDEX, 0);
}
public void setStartPageIndex(final int index) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(final Void... unused) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putInt(START_PAGE_INDEX, index);
editor.apply();
return null;
}
}.execute();
}
private void setSortOrder(final String key, final String value) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putString(key, value);
editor.apply();
}
public final String getArtistSortOrder() {
return mPreferences.getString(ARTIST_SORT_ORDER, SortOrder.ArtistSortOrder.ARTIST_A_Z);
}
public void setArtistSortOrder(final String value) {
setSortOrder(ARTIST_SORT_ORDER, value);
}
public final String getArtistSongSortOrder() {
return mPreferences.getString(ARTIST_SONG_SORT_ORDER,
SortOrder.ArtistSongSortOrder.SONG_A_Z);
}
public final String getAlbumSortOrder() {
return mPreferences.getString(ALBUM_SORT_ORDER, SortOrder.AlbumSortOrder.ALBUM_A_Z);
}
public void setAlbumSortOrder(final String value) {
setSortOrder(ALBUM_SORT_ORDER, value);
}
public final String getAlbumSongSortOrder() {
return mPreferences.getString(ALBUM_SONG_SORT_ORDER,
SortOrder.AlbumSongSortOrder.SONG_TRACK_LIST);
}
public void setAlbumSongSortOrder(final String value) {
setSortOrder(ALBUM_SONG_SORT_ORDER, value);
}
public final String getSongSortOrder() {
return mPreferences.getString(SONG_SORT_ORDER, SortOrder.SongSortOrder.SONG_A_Z);
}
public void setSongSortOrder(final String value) {
setSortOrder(SONG_SORT_ORDER, value);
}
public void setArtistArt(long artistID, String jsonString) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putString(ARTIST_ART_URL + artistID, jsonString);
editor.apply();
}
public String getArtistArt(long artistID) {
return mPreferences.getString(ARTIST_ART_URL+artistID,"");
}
public int getPlaylistView() {
return mPreferences.getInt(TOGGLE_PLAYLIST_VIEW ,0);
}
public void setPlaylistView(final int i) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putInt(TOGGLE_PLAYLIST_VIEW, i);
editor.apply();
}
}