Fix favorite toggling
This commit is contained in:
parent
c77e6f132a
commit
1f37982536
4 changed files with 57 additions and 40 deletions
|
|
@ -6,6 +6,7 @@ import android.support.annotation.NonNull;
|
|||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.DialogFragment;
|
||||
import android.text.InputType;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.afollestad.materialdialogs.MaterialDialog;
|
||||
import com.kabouzeid.gramophone.R;
|
||||
|
|
@ -19,6 +20,8 @@ import java.util.ArrayList;
|
|||
*/
|
||||
public class CreatePlaylistDialog extends DialogFragment {
|
||||
|
||||
private static final String SONGS = "songs";
|
||||
|
||||
@NonNull
|
||||
public static CreatePlaylistDialog create() {
|
||||
return create((Song) null);
|
||||
|
|
@ -36,7 +39,7 @@ public class CreatePlaylistDialog extends DialogFragment {
|
|||
public static CreatePlaylistDialog create(ArrayList<Song> songs) {
|
||||
CreatePlaylistDialog dialog = new CreatePlaylistDialog();
|
||||
Bundle args = new Bundle();
|
||||
args.putParcelableArrayList("songs", songs);
|
||||
args.putParcelableArrayList(SONGS, songs);
|
||||
dialog.setArguments(args);
|
||||
return dialog;
|
||||
}
|
||||
|
|
@ -53,20 +56,27 @@ public class CreatePlaylistDialog extends DialogFragment {
|
|||
InputType.TYPE_TEXT_FLAG_CAP_WORDS)
|
||||
.input(R.string.playlist_name_empty, 0, false, new MaterialDialog.InputCallback() {
|
||||
@Override
|
||||
public void onInput(MaterialDialog materialDialog, @NonNull CharSequence charSequence) {
|
||||
public void onInput(@NonNull MaterialDialog materialDialog, @NonNull CharSequence charSequence) {
|
||||
if (getActivity() == null)
|
||||
return;
|
||||
if (!charSequence.toString().trim().isEmpty()) {
|
||||
final int playlistId = PlaylistsUtil.createPlaylist(getActivity(), charSequence.toString());
|
||||
if (playlistId != -1 && getActivity() != null) {
|
||||
//noinspection unchecked
|
||||
ArrayList<Song> songs = getArguments().getParcelableArrayList("songs");
|
||||
if (songs != null) {
|
||||
PlaylistsUtil.addToPlaylist(getActivity(), songs, playlistId, true);
|
||||
final String name = charSequence.toString().trim();
|
||||
if (!name.isEmpty()) {
|
||||
if (!PlaylistsUtil.doesPlaylistExist(getActivity(), name)) {
|
||||
final int playlistId = PlaylistsUtil.createPlaylist(getActivity(), name);
|
||||
if (getActivity() != null) {
|
||||
//noinspection unchecked
|
||||
ArrayList<Song> songs = getArguments().getParcelableArrayList(SONGS);
|
||||
if (songs != null && !songs.isEmpty()) {
|
||||
PlaylistsUtil.addToPlaylist(getActivity(), songs, playlistId, true);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Toast.makeText(getActivity(), getActivity().getResources().getString(
|
||||
R.string.playlist_exists, name), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
}).build();
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
|
@ -15,11 +15,13 @@ import com.kabouzeid.gramophone.util.PlaylistsUtil;
|
|||
*/
|
||||
public class RenamePlaylistDialog extends DialogFragment {
|
||||
|
||||
private static final String PLAYLIST_ID = "playlist_id";
|
||||
|
||||
@NonNull
|
||||
public static RenamePlaylistDialog create(long playlistId) {
|
||||
RenamePlaylistDialog dialog = new RenamePlaylistDialog();
|
||||
Bundle args = new Bundle();
|
||||
args.putLong("playlist_id", playlistId);
|
||||
args.putLong(PLAYLIST_ID, playlistId);
|
||||
dialog.setArguments(args);
|
||||
return dialog;
|
||||
}
|
||||
|
|
@ -27,7 +29,7 @@ public class RenamePlaylistDialog extends DialogFragment {
|
|||
@NonNull
|
||||
@Override
|
||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||
long playlistId = getArguments().getLong("playlist_id");
|
||||
long playlistId = getArguments().getLong(PLAYLIST_ID);
|
||||
return new MaterialDialog.Builder(getActivity())
|
||||
.title(R.string.rename_playlist_title)
|
||||
.positiveText(R.string.rename_action)
|
||||
|
|
@ -38,10 +40,11 @@ public class RenamePlaylistDialog extends DialogFragment {
|
|||
.input(getString(R.string.playlist_name_empty), PlaylistsUtil.getNameForPlaylist(getActivity(), playlistId), false,
|
||||
new MaterialDialog.InputCallback() {
|
||||
@Override
|
||||
public void onInput(MaterialDialog materialDialog, @NonNull CharSequence charSequence) {
|
||||
if (!charSequence.toString().trim().equals("")) {
|
||||
long playlistId = getArguments().getLong("playlist_id");
|
||||
PlaylistsUtil.renamePlaylist(getActivity(), playlistId, charSequence.toString());
|
||||
public void onInput(@NonNull MaterialDialog materialDialog, @NonNull CharSequence charSequence) {
|
||||
final String name = charSequence.toString().trim();
|
||||
if (!name.isEmpty()) {
|
||||
long playlistId = getArguments().getLong(PLAYLIST_ID);
|
||||
PlaylistsUtil.renamePlaylist(getActivity(), playlistId, name);
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
|
|||
|
|
@ -27,8 +27,6 @@ import java.util.ArrayList;
|
|||
* @author Karim Abou Zeid (kabouzeid)
|
||||
*/
|
||||
public class PlaylistMenuHelper {
|
||||
public static final int MENU_RES = R.menu.menu_item_playlist;
|
||||
|
||||
public static boolean handleMenuClick(@NonNull AppCompatActivity activity, @NonNull final Playlist playlist, @NonNull MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_play:
|
||||
|
|
@ -50,7 +48,8 @@ public class PlaylistMenuHelper {
|
|||
DeletePlaylistDialog.create(playlist).show(activity.getSupportFragmentManager(), "DELETE_PLAYLIST");
|
||||
return true;
|
||||
case R.id.action_save_playlist:
|
||||
@SuppressLint("ShowToast") final Toast toast = Toast.makeText(activity, R.string.saving_to_file, Toast.LENGTH_SHORT);
|
||||
@SuppressLint("ShowToast")
|
||||
final Toast toast = Toast.makeText(activity, R.string.saving_to_file, Toast.LENGTH_SHORT);
|
||||
new AsyncTask<Context, Void, String>() {
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
|
|
|
|||
|
|
@ -31,20 +31,15 @@ import static android.provider.MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
|
|||
public class PlaylistsUtil {
|
||||
|
||||
public static boolean doesPlaylistExist(@NonNull final Context context, final int playlistId) {
|
||||
if (playlistId == -1) {
|
||||
return false;
|
||||
}
|
||||
return playlistId != -1 && doesPlaylistExist(context,
|
||||
MediaStore.Audio.Playlists._ID + "=?",
|
||||
new String[]{String.valueOf(playlistId)});
|
||||
}
|
||||
|
||||
Cursor cursor = context.getContentResolver().query(
|
||||
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
|
||||
new String[]{}, null, null, null);
|
||||
|
||||
if (cursor == null || cursor.getCount() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
cursor.close();
|
||||
return true;
|
||||
public static boolean doesPlaylistExist(@NonNull final Context context, final String name) {
|
||||
return doesPlaylistExist(context,
|
||||
MediaStore.Audio.PlaylistsColumns.NAME + "=?",
|
||||
new String[]{name});
|
||||
}
|
||||
|
||||
public static int createPlaylist(@NonNull final Context context, @Nullable final String name) {
|
||||
|
|
@ -52,7 +47,9 @@ public class PlaylistsUtil {
|
|||
if (name != null && name.length() > 0) {
|
||||
try {
|
||||
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
|
||||
new String[]{MediaStore.Audio.Playlists._ID}, MediaStore.Audio.PlaylistsColumns.NAME + "=?", new String[]{name}, null);
|
||||
new String[]{MediaStore.Audio.Playlists._ID},
|
||||
MediaStore.Audio.PlaylistsColumns.NAME + "=?", new String[]{name},
|
||||
null);
|
||||
if (cursor == null || cursor.getCount() < 1) {
|
||||
final ContentValues values = new ContentValues(1);
|
||||
values.put(MediaStore.Audio.PlaylistsColumns.NAME, name);
|
||||
|
|
@ -60,7 +57,7 @@ public class PlaylistsUtil {
|
|||
EXTERNAL_CONTENT_URI,
|
||||
values);
|
||||
if (uri != null) {
|
||||
// necessary because somehow the MediaStoreObserver is not notified when adding a playlist
|
||||
// Necessary because somehow the MediaStoreObserver is not notified when adding a playlist
|
||||
context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
|
||||
Toast.makeText(context, context.getResources().getString(
|
||||
R.string.created_playlist_x, name), Toast.LENGTH_SHORT).show();
|
||||
|
|
@ -69,10 +66,7 @@ public class PlaylistsUtil {
|
|||
} else {
|
||||
// Playlist exists
|
||||
if (cursor.moveToFirst()) {
|
||||
Toast.makeText(context, context.getResources().getString(
|
||||
R.string.playlist_exists, name), Toast.LENGTH_SHORT).show();
|
||||
cursor.close();
|
||||
return -1;
|
||||
id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists._ID));
|
||||
}
|
||||
}
|
||||
if (cursor != null) {
|
||||
|
|
@ -231,8 +225,7 @@ public class PlaylistsUtil {
|
|||
|
||||
public static String getNameForPlaylist(@NonNull final Context context, final long id) {
|
||||
try {
|
||||
Cursor cursor = context.getContentResolver().query(
|
||||
EXTERNAL_CONTENT_URI,
|
||||
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
|
||||
new String[]{MediaStore.Audio.PlaylistsColumns.NAME},
|
||||
BaseColumns._ID + "=?",
|
||||
new String[]{String.valueOf(id)},
|
||||
|
|
@ -254,4 +247,16 @@ public class PlaylistsUtil {
|
|||
public static File savePlaylist(Context context, Playlist playlist) throws IOException {
|
||||
return M3UWriter.write(context, new File(Environment.getExternalStorageDirectory(), "Playlists"), playlist);
|
||||
}
|
||||
|
||||
private static boolean doesPlaylistExist(@NonNull Context context, @NonNull final String selection, @NonNull final String[] values) {
|
||||
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
|
||||
new String[]{}, selection, values, null);
|
||||
|
||||
boolean exists = false;
|
||||
if (cursor != null) {
|
||||
exists = cursor.getCount() != 0;
|
||||
cursor.close();
|
||||
}
|
||||
return exists;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue