[Android] Change playlist/Album tracks deletion process
Geoffrey Métais
git at videolan.org
Thu Sep 13 15:06:35 CEST 2018
vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Sep 13 14:36:06 2018 +0200| [1c6b7577127d433c7669a5e0d93e9f71f57f2487] | committer: Geoffrey Métais
Change playlist/Album tracks deletion process
> https://code.videolan.org/videolan/vlc-android/commit/1c6b7577127d433c7669a5e0d93e9f71f57f2487
---
vlc-android/res/values/strings.xml | 2 +
.../src/org/videolan/vlc/gui/PlaylistActivity.java | 47 ++++---------
.../vlc/viewmodels/paged/PagedTracksModel.kt | 78 +++++++++++++++++-----
3 files changed, 75 insertions(+), 52 deletions(-)
diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index 80a7c125b..47fa0290d 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -55,6 +55,8 @@
<string name="confirm_delete">Delete the file \'%1$s\'?</string>
<string name="confirm_delete_folder">Delete the folder \'%1$s\' and all its contents?</string>
<string name="confirm_delete_playlist">Delete playlist \'%1$s\'?</string>
+ <string name="confirm_remove_from_playlist">Remove \'%1$s\' from playlist?</string>
+ <string name="confirm_remove_from_playlist_anonymous">Remove from playlist?</string>
<string name="ringtone_set">The file \'%1$s\' was set as the ringtone.</string>
<string name="ringtone_error">An error occurred while setting the ringtone.</string>
<string name="pause">Pause</string>
diff --git a/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java b/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
index ed5a9f234..178ac744f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/PlaylistActivity.java
@@ -317,20 +317,14 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
showInfoDialog(media);
break;
case Constants.CTX_DELETE:
- tracksModel.remove(media);
- final Runnable cancel = new Runnable() {
- @Override
- public void run() {
- tracksModel.refresh();
- }
- };
- UiTools.snackerWithCancel(mBinding.getRoot(), getString(R.string.file_deleted), new Runnable() {
+ 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(media.getId());
- else deleteMedia(media, cancel);
+ else deleteMedia(media);
}
- }, cancel);
+ });
break;
case Constants.CTX_APPEND:
MediaUtils.INSTANCE.appendMedia(this, media.getTracks());
@@ -342,13 +336,13 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
UiTools.addToPlaylist(this, media.getTracks(), SavePlaylistDialog.KEY_NEW_TRACKS);
break;
case Constants.CTX_SET_RINGTONE:
- AudioUtil.setRingtone((MediaWrapper) media, this);
+ AudioUtil.setRingtone(media, this);
break;
}
}
- protected void deleteMedia(final MediaLibraryItem mw, final Runnable cancel) {
+ protected void deleteMedia(final MediaLibraryItem mw) {
WorkersKt.runBackground(new Runnable() {
@Override
public void run() {
@@ -362,21 +356,7 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
mediaPaths.add(media.getLocation());
} else UiTools.snacker(mBinding.getRoot(), getString(R.string.msg_delete_failed, media.getTitle()));
}
- for (String folder : foldersToReload)
- mMediaLibrary.reload(folder);
- if (PlaylistManager.Companion.hasMedia()) {
- WorkersKt.runOnMainThread(new Runnable() {
- @Override
- public void run() {
- if (mediaPaths.isEmpty()) cancel.run();
- //TODO
-// else {
-// for (String path : mediaPaths)
-// mService.removeLocation(path);
-// }
- }
- });
- }
+ for (String folder : foldersToReload) mMediaLibrary.reload(folder);
}
});
}
@@ -387,17 +367,14 @@ public class PlaylistActivity extends AudioPlayerContainerActivity implements IE
}
private void removeFromPlaylist(final List<MediaWrapper> list){
- for (MediaLibraryItem mediaItem : list) tracksModel.remove(mediaItem);
- UiTools.snackerWithCancel(mBinding.getRoot(), getString(R.string.file_deleted), new Runnable() {
+ UiTools.snackerConfirm(mBinding.getRoot(), getString(R.string.confirm_remove_from_playlist_anonymous), new Runnable() {
@Override
public void run() {
for (MediaLibraryItem mediaItem : list) ((Playlist) mPlaylist).remove(mediaItem.getId());
- if (mPlaylist.getTracks().length == 0) ((Playlist) mPlaylist).delete();
- }
- }, new Runnable() {
- @Override
- public void run() {
- tracksModel.refresh();
+ if (mPlaylist.getTracks().length == 0) {
+ ((Playlist) mPlaylist).delete();
+ finish();
+ }
}
});
}
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedTracksModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedTracksModel.kt
index 1fb5bf5fa..37e5851f8 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedTracksModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedTracksModel.kt
@@ -3,11 +3,20 @@ package org.videolan.vlc.viewmodels.paged
import android.arch.lifecycle.ViewModel
import android.arch.lifecycle.ViewModelProvider
import android.content.Context
+import kotlinx.coroutines.experimental.Dispatchers
+import kotlinx.coroutines.experimental.android.Main
+import kotlinx.coroutines.experimental.launch
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.*
+import org.videolan.vlc.util.EmptyMLCallbacks
import org.videolan.vlc.util.Settings
-class PagedTracksModel(context: Context, val parent: MediaLibraryItem? = null): MLPagedModel<MediaWrapper>(context), Medialibrary.MediaCb {
+class PagedTracksModel(context: Context, val parent: MediaLibraryItem? = null): MLPagedModel<MediaWrapper>(context),
+ Medialibrary.MediaCb,
+ Medialibrary.ArtistsCb by EmptyMLCallbacks,
+ Medialibrary.AlbumsCb by EmptyMLCallbacks,
+ Medialibrary.GenresCb by EmptyMLCallbacks,
+ Medialibrary.PlaylistsCb by EmptyMLCallbacks {
override val sortKey = "${super.sortKey}_${parent?.javaClass?.simpleName}"
override fun canSortByDuration() = true
@@ -26,24 +35,26 @@ class PagedTracksModel(context: Context, val parent: MediaLibraryItem? = null):
override fun onMedialibraryReady() {
super.onMedialibraryReady()
- medialibrary.addMediaCb(this)
+ launch(Dispatchers.Main) {
+ when (parent) {
+ is Artist -> medialibrary.addArtistsCb(this at PagedTracksModel)
+ is Album -> medialibrary.addAlbumsCb(this at PagedTracksModel)
+ is Genre -> medialibrary.addGenreCb(this at PagedTracksModel)
+ is Playlist -> medialibrary.addPlaylistCb(this at PagedTracksModel)
+ else -> medialibrary.addMediaCb(this at PagedTracksModel)
+ }
+ }
}
override fun onCleared() {
super.onCleared()
- medialibrary.removeMediaCb(this)
- }
-
- override fun onMediaAdded() {
- refresh()
- }
-
- override fun onMediaModified() {
- refresh()
- }
-
- override fun onMediaDeleted() {
- refresh()
+ when (parent) {
+ is Artist -> medialibrary.removeArtistsCb(this)
+ is Album -> medialibrary.removeAlbumsCb(this)
+ is Genre -> medialibrary.removeGenreCb(this)
+ is Playlist -> medialibrary.removePlaylistCb(this)
+ else -> medialibrary.removeMediaCb(this)
+ }
}
override fun getAll(): Array<MediaWrapper> = parent?.tracks ?: medialibrary.getAudio(sort, desc)
@@ -62,8 +73,13 @@ class PagedTracksModel(context: Context, val parent: MediaLibraryItem? = null):
else -> medialibrary.searchAudio(filter, sort, desc, loadSize, startposition)
}
- override fun getTotalCount() = if (filter == null) parent?.tracksCount ?: medialibrary.audioCount
- else when(parent) {
+ override fun getTotalCount() = if (filter == null) when (parent) {
+ is Album -> parent.realTracksCount
+ is Playlist -> parent.realTracksCount
+ is Artist,
+ is Genre -> parent.tracksCount
+ else -> medialibrary.audioCount
+ } else when(parent) {
is Artist -> parent.searchTracksCount(filter)
is Album -> parent.searchTracksCount(filter)
is Genre -> parent.searchTracksCount(filter)
@@ -77,4 +93,32 @@ class PagedTracksModel(context: Context, val parent: MediaLibraryItem? = null):
return PagedTracksModel(context.applicationContext, parent) as T
}
}
+
+ override fun onMediaAdded() {
+ refresh()
+ }
+
+ override fun onMediaModified() {
+ refresh()
+ }
+
+ override fun onMediaDeleted() {
+ refresh()
+ }
+
+ override fun onArtistsModified() {
+ refresh()
+ }
+
+ override fun onAlbumsModified() {
+ refresh()
+ }
+
+ override fun onGenresModified() {
+ refresh()
+ }
+
+ override fun onPlaylistsModified() {
+ refresh()
+ }
}
More information about the Android
mailing list