Commit acc0f1c0 authored by Nicolas Pomepuy's avatar Nicolas Pomepuy Committed by Geoffrey Métais

Use the new media library playlist API

parent 1aa02306
......@@ -454,15 +454,15 @@ AndroidMediaLibrary::playlistAdd(int64_t playlistId, int64_t mediaId, unsigned i
}
bool
AndroidMediaLibrary::playlistMove(int64_t playlistId, int64_t mediaId, unsigned int position) {
AndroidMediaLibrary::playlistMove(int64_t playlistId, unsigned int oldPosition, unsigned int newPosition) {
medialibrary::PlaylistPtr playlist = p_ml->playlist(playlistId);
return playlist == nullptr ? false : playlist->move(mediaId, position);
return playlist == nullptr ? false : playlist->move(oldPosition, newPosition);
}
bool
AndroidMediaLibrary::playlistRemove(int64_t playlistId, int64_t mediaId) {
AndroidMediaLibrary::playlistRemove(int64_t playlistId, unsigned int position) {
medialibrary::PlaylistPtr playlist = p_ml->playlist(playlistId);
return playlist == nullptr ? false : playlist->remove(mediaId);
return playlist == nullptr ? false : playlist->remove(position);
}
bool
......
......@@ -99,8 +99,8 @@ public:
//PLaylists
bool playlistAppend(int64_t playlistId, int64_t mediaId);
bool playlistAdd(int64_t playlistId, int64_t mediaId, unsigned int position);
bool playlistMove(int64_t playlistId, int64_t mediaId, unsigned int position);
bool playlistRemove(int64_t playlistId, int64_t mediaId);
bool playlistMove(int64_t playlistId, unsigned int oldPosition, unsigned int newPosition);
bool playlistRemove(int64_t playlistId, unsigned int position);
bool PlaylistDelete( int64_t playlistId );
void requestThumbnail( int64_t media_id );
......
......@@ -1572,17 +1572,17 @@ playlistAdd(JNIEnv* env, jobject thiz, jobject medialibrary, jlong playlistId, j
}
jboolean
playlistMove(JNIEnv* env, jobject thiz, jobject medialibrary, jlong playlistId, jlong mediaId, jint position)
playlistMove(JNIEnv* env, jobject thiz, jobject medialibrary, jlong playlistId, jint oldPosition, jint newPosition)
{
AndroidMediaLibrary *aml = MediaLibrary_getInstance(env, medialibrary);
return aml->playlistMove(playlistId, mediaId, position);
return aml->playlistMove(playlistId, oldPosition, newPosition);
}
jboolean
playlistRemove(JNIEnv* env, jobject thiz, jobject medialibrary, jlong playlistId, jlong mediaId)
playlistRemove(JNIEnv* env, jobject thiz, jobject medialibrary, jlong playlistId, jint position)
{
AndroidMediaLibrary *aml = MediaLibrary_getInstance(env, medialibrary);
return aml->playlistRemove(playlistId, mediaId);
return aml->playlistRemove(playlistId, position);
}
jboolean
......@@ -1819,8 +1819,8 @@ static JNINativeMethod playlist_methods[] = {
{"nativePlaylistAppend", "(Lorg/videolan/medialibrary/Medialibrary;JJ)Z", (void*)playlistAppend },
{"nativePlaylistAppendGroup", "(Lorg/videolan/medialibrary/Medialibrary;J[J)Z", (void*)playlistAppendGroup },
{"nativePlaylistAdd", "(Lorg/videolan/medialibrary/Medialibrary;JJI)Z", (void*)playlistAdd },
{"nativePlaylistMove", "(Lorg/videolan/medialibrary/Medialibrary;JJI)Z", (void*)playlistMove },
{"nativePlaylistRemove", "(Lorg/videolan/medialibrary/Medialibrary;JJ)Z", (void*)playlistRemove },
{"nativePlaylistMove", "(Lorg/videolan/medialibrary/Medialibrary;JII)Z", (void*)playlistMove },
{"nativePlaylistRemove", "(Lorg/videolan/medialibrary/Medialibrary;JI)Z", (void*)playlistRemove },
{"nativePlaylistDelete", "(Lorg/videolan/medialibrary/Medialibrary;J)Z", (void*)playlistDelete },
};
......
......@@ -68,14 +68,14 @@ public class Playlist extends MediaLibraryItem {
return ml.isInitiated() && nativePlaylistAdd(ml, mId, mediaId, position);
}
public boolean move(long mediaId, int position) {
public boolean move(int oldPosition, int newPosition) {
Medialibrary ml = Medialibrary.getInstance();
return ml.isInitiated() && nativePlaylistMove(ml, mId, mediaId, position);
return ml.isInitiated() && nativePlaylistMove(ml, mId, oldPosition, newPosition);
}
public boolean remove(long mediaId) {
public boolean remove(int position) {
Medialibrary ml = Medialibrary.getInstance();
return ml.isInitiated() && nativePlaylistRemove(ml, mId, mediaId);
return ml.isInitiated() && nativePlaylistRemove(ml, mId, position);
}
public boolean delete() {
......@@ -125,7 +125,9 @@ public class Playlist extends MediaLibraryItem {
private native boolean nativePlaylistAppend(Medialibrary ml, long id, long mediaId);
private native boolean nativePlaylistAppendGroup(Medialibrary ml, long id, long[] mediaIds);
private native boolean nativePlaylistAdd(Medialibrary ml, long id, long mediaId, int position);
private native boolean nativePlaylistMove(Medialibrary ml, long id, long mediaId, int position);
private native boolean nativePlaylistRemove(Medialibrary ml, long id, long mediaId);
private native boolean nativePlaylistMove(Medialibrary ml, long id, int oldPosition, int position);
private native boolean nativePlaylistRemove(Medialibrary ml, long id, int position);
private native boolean nativePlaylistDelete(Medialibrary ml, long id);
}
......@@ -227,13 +227,13 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
@Override
public void onRemove(int position, @NotNull final MediaLibraryItem item) {
ArrayList<MediaWrapper> tracks = new ArrayList<MediaWrapper>(Arrays.asList(item.getTracks()));
removeFromPlaylist(tracks);
removeFromPlaylist(tracks, new ArrayList(Arrays.asList(position)));
}
@Override
public void onMove(int position, @NotNull MediaLibraryItem item) {
if (BuildConfig.DEBUG) Log.d(TAG, "Moving item: " + item.getId() + " to " + (position + 1));
((Playlist) mPlaylist).move(item.getId(), position + 1);
public void onMove(int oldPosition, int newPosition) {
if (BuildConfig.DEBUG) Log.d(TAG, "Moving item from " + oldPosition + " to " + newPosition);
((Playlist) mPlaylist).move(oldPosition, newPosition);
}
......@@ -308,6 +308,11 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
Toast.LENGTH_SHORT).show();
return true;
}
List<Integer> indexes = new ArrayList<Integer>();
for (int i = 0; i < mAdapter.getMultiSelectHelper().getSelectionMap().size(); i++) {
indexes.add(mAdapter.getMultiSelectHelper().getSelectionMap().keyAt(i));
}
stopActionMode();
switch (item.getItemId()) {
case R.id.action_mode_audio_play:
......@@ -326,7 +331,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
AudioUtil.setRingtone((MediaWrapper) list.get(0), this);
break;
case R.id.action_mode_audio_delete:
removeFromPlaylist(tracks);
removeFromPlaylist(tracks, indexes);
break;
default:
return false;
......@@ -347,7 +352,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
}
@Override
public void onCtxAction(int position, int option) {
public void onCtxAction(final int position, int option) {
if (position >= mAdapter.getItemCount()) return;
final MediaWrapper media = (MediaWrapper) mAdapter.getItem(position);
if (media == null) return;
......@@ -356,7 +361,14 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
showInfoDialog(media);
break;
case Constants.CTX_DELETE:
removeItem(media);
final int resId = mIsPlaylist ? R.string.confirm_remove_from_playlist : R.string.confirm_delete;
UiTools.snackerConfirm(mBinding.getRoot(), getString(resId, media.getTitle()), new Runnable() {
@Override
public void run() {
if (mIsPlaylist) ((Playlist) mPlaylist).remove(position);
else removeItem(media);
}
});
break;
case Constants.CTX_APPEND:
MediaUtils.INSTANCE.appendMedia(this, media.getTracks());
......@@ -422,7 +434,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
MediaUtils.INSTANCE.playTracks(this, mPlaylist, 0);
}
private void removeFromPlaylist(final List<MediaWrapper> list) {
private void removeFromPlaylist(final List<MediaWrapper> list, List<Integer> indexes) {
final HashMap<Integer, Long> itemsRemoved = new HashMap<>();
final Playlist playlist = (Playlist) this.mPlaylist;
......@@ -430,10 +442,13 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
for (MediaLibraryItem mediaItem : list) {
for (int i = 0; i < playlist.getTracks().length; i++) {
if (playlist.getTracks()[i].getId() == mediaItem.getId()) {
itemsRemoved.put(i + 1, mediaItem.getId());
itemsRemoved.put(i, mediaItem.getId());
}
}
playlist.remove(mediaItem.getId());
}
for (Integer index : indexes) {
playlist.remove(index);
}
UiTools.snackerWithCancel(mBinding.getRoot(), getString(R.string.removed_from_playlist_anonymous), null, new Runnable() {
......
......@@ -72,6 +72,10 @@ public class AudioBrowserAdapter extends PagedListAdapter<MediaLibraryItem, Audi
private final int mType;
private final boolean mHasSections;
private final BitmapDrawable mDefaultCover;
/**
* Awful hack to workaround the {@link PagedListAdapter} not keeping track of notifyItemMoved operations
*/
private static boolean preventNextAnim;
public AudioBrowserAdapter(int type, IEventsHandler eventsHandler, IListEventsHandler listEventsHandler, boolean sections, boolean canBeReordered) {
super(DIFF_CALLBACK);
......@@ -87,7 +91,7 @@ public class AudioBrowserAdapter extends PagedListAdapter<MediaLibraryItem, Audi
}
public AudioBrowserAdapter(int type, IEventsHandler eventsHandler) {
this(type, eventsHandler, null, true, false);
this(type, eventsHandler, null, true, false);
}
@NonNull
......@@ -193,8 +197,8 @@ public class AudioBrowserAdapter extends PagedListAdapter<MediaLibraryItem, Audi
@Override
public void onItemMoved(int dragFrom, int dragTo) {
final MediaLibraryItem item = getItem(dragFrom);
mListEventsHandler.onMove(dragTo, item);
mListEventsHandler.onMove(dragFrom, dragTo);
preventNextAnim = true;
}
@Override
......@@ -291,6 +295,9 @@ public class AudioBrowserAdapter extends PagedListAdapter<MediaLibraryItem, Audi
@Override
public boolean areItemsTheSame(
@NonNull MediaLibraryItem oldMedia, @NonNull MediaLibraryItem newMedia) {
if (preventNextAnim) {
return true;
}
return oldMedia == newMedia || (oldMedia.getItemType() == newMedia.getItemType() && oldMedia.equals(newMedia));
}
......@@ -302,6 +309,7 @@ public class AudioBrowserAdapter extends PagedListAdapter<MediaLibraryItem, Audi
@Override
public Object getChangePayload(@NotNull MediaLibraryItem oldItem, @NotNull MediaLibraryItem newItem) {
preventNextAnim = false;
return UPDATE_PAYLOAD;
}
};
......
......@@ -34,8 +34,8 @@ public class SwipeDragItemTouchHelperCallback extends ItemTouchHelper.Callback {
private static final String TAG = SwipeDragItemTouchHelperCallback.class.getSimpleName();
private final SwipeDragHelperAdapter mAdapter;
private int dragFrom;
private int dragTo;
private int dragFrom = -1;
private int dragTo = -1;
public SwipeDragItemTouchHelperCallback(SwipeDragHelperAdapter mAdapter) {
this.mAdapter = mAdapter;
......
......@@ -5,6 +5,6 @@ import org.videolan.medialibrary.media.MediaLibraryItem
interface IListEventsHandler {
fun onRemove(position: Int, item: MediaLibraryItem)
fun onMove(position: Int, item: MediaLibraryItem)
fun onMove(oldPosition: Int, newPosition: Int)
fun onStartDrag(viewHolder: RecyclerView.ViewHolder )
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment