[Android] Improve the playlist creation/addition UX

Nicolas Pomepuy git at videolan.org
Fri Mar 13 14:04:48 CET 2020


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Mar 12 14:30:53 2020 +0100| [af112f5e088d6ae2cfb21b232892411f59fac657] | committer: Geoffrey Métais

Improve the playlist creation/addition UX

> https://code.videolan.org/videolan/vlc-android/commit/af112f5e088d6ae2cfb21b232892411f59fac657
---

 .../resources/src/main/res/values/strings.xml      |  1 +
 .../vlc-android/res/layout/dialog_playlist.xml     |  6 ++---
 .../videolan/vlc/gui/dialogs/SavePlaylistDialog.kt | 29 ++++++++++++----------
 3 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/application/resources/src/main/res/values/strings.xml b/application/resources/src/main/res/values/strings.xml
index 585f4535c..f89d14f8d 100644
--- a/application/resources/src/main/res/values/strings.xml
+++ b/application/resources/src/main/res/values/strings.xml
@@ -810,4 +810,5 @@
     <string name="sortby_track">Track</string>
     <string name="this_folder">Add this folder</string>
     <string name="all_subfolders">Add this folder and subfolders</string>
+    <string name="playlist_existing">Playlist %s already exists</string>
 </resources>
diff --git a/application/vlc-android/res/layout/dialog_playlist.xml b/application/vlc-android/res/layout/dialog_playlist.xml
index 5d3a6959c..643d5ee00 100644
--- a/application/vlc-android/res/layout/dialog_playlist.xml
+++ b/application/vlc-android/res/layout/dialog_playlist.xml
@@ -115,7 +115,7 @@
                     android:layout_marginStart="8dp"
                     android:layout_marginTop="8dp"
                     android:layout_marginEnd="8dp"
-                    android:layout_marginBottom="8dp"
+                    android:layout_marginBottom="24dp"
                     app:layout_constraintBottom_toBottomOf="parent"
                     app:layout_constraintEnd_toStartOf="@+id/dialog_playlist_save"
                     app:layout_constraintStart_toStartOf="parent"
@@ -138,9 +138,9 @@
                     android:layout_below="@+id/dialog_playlist_name"
                     android:layout_alignParentEnd="true"
                     android:layout_marginEnd="8dp"
-                    android:text="@string/add"
                     android:enabled="@{!isLoading}"
-                    app:layout_constraintBottom_toBottomOf="@+id/dialog_playlist_name"
+                    android:text="@string/add"
+                    app:layout_constraintBaseline_toBaselineOf="@+id/dialog_playlist_name"
                     app:layout_constraintEnd_toEndOf="parent" />
         </androidx.constraintlayout.widget.ConstraintLayout>
 
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SavePlaylistDialog.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SavePlaylistDialog.kt
index f79c81078..d2626bb08 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SavePlaylistDialog.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SavePlaylistDialog.kt
@@ -72,7 +72,6 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
     private lateinit var adapter: SimpleAdapter
     private lateinit var newTrack: Array<MediaWrapper>
     private lateinit var medialibrary: Medialibrary
-    private var currentPLaylist: Playlist? = null
 
     private val coroutineContextProvider: CoroutineContextProvider
 
@@ -144,24 +143,29 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
     }
 
     override fun onClick(v: View) {
-        savePlaylist()
+        addToNewPlaylist()
     }
 
     override fun onEditorAction(v: TextView, actionId: Int, event: KeyEvent?): Boolean {
-        if (actionId == EditorInfo.IME_ACTION_SEND) savePlaylist()
+        if (actionId == EditorInfo.IME_ACTION_SEND) addToNewPlaylist()
         return false
     }
 
-    private fun savePlaylist() {
+    private fun addToNewPlaylist() {
         val name = binding.dialogPlaylistName.editText?.text?.toString()?.trim { it <= ' ' }
                 ?: return
-        AppScope.launch(coroutineContextProvider.IO) {
-            var playlist = if (currentPLaylist?.title == name) {
-                medialibrary.getPlaylist(currentPLaylist?.id ?: return at launch)
-            } else {
-                medialibrary.getPlaylistByName(name) ?: medialibrary.createPlaylist(name)
-                ?: return at launch
+        lifecycleScope.launch {
+            withContext(Dispatchers.IO) { medialibrary.getPlaylistByName(name) }?.let {
+                binding.dialogPlaylistName.error = getString(R.string.playlist_existing, it.title)
+                return at launch
             }
+            dismiss()
+            savePlaylist(medialibrary.createPlaylist(name) ?: return at launch)
+        }
+    }
+
+    private fun savePlaylist(playlist: Playlist) {
+        AppScope.launch(coroutineContextProvider.IO) {
             if (newTrack.isEmpty()) return at launch
             val ids = LinkedList<Long>()
             for (mw in newTrack) {
@@ -183,8 +187,7 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
     }
 
     override fun onClick(item: MediaLibraryItem) {
-        currentPLaylist = item as Playlist
-        binding.dialogPlaylistName.editText?.setText(item.title)
+        savePlaylist(item as Playlist)
     }
 
     companion object : DependencyProvider<Any>() {
@@ -197,4 +200,4 @@ class SavePlaylistDialog : VLCBottomSheetDialogFragment(), View.OnClickListener,
     }
 }
 
-fun Medialibrary.getPlaylistByName(name: String)= playlists.firstOrNull { it.title == name }
+fun Medialibrary.getPlaylistByName(name: String) = playlists.firstOrNull { it.title == name }



More information about the Android mailing list