[Android] Add a confirmation for playlist deletion on TV

Nicolas Pomepuy git at videolan.org
Wed Jul 10 08:45:22 CEST 2019


vlc-android | branch: playlist_confirmation_tv | Nicolas Pomepuy <nicolas.pomepuy at gmail.com> | Wed Jul 10 08:45:18 2019 +0200| [7410825b97d581ce359ebbc83228c4e64b4e2a73] | committer: Nicolas Pomepuy

Add a confirmation for playlist deletion on TV

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

 vlc-android/AndroidManifest.xml                    | 12 ++++--
 vlc-android/res/values/strings.xml                 |  2 +
 .../vlc/gui/tv/details/MediaListActivity.kt        | 28 ++++++++++---
 .../vlc/gui/tv/dialogs/ConfirmationTvActivity.kt   | 28 +++++++++++++
 .../vlc/gui/tv/dialogs/ConfirmationTvDialog.kt     | 49 ++++++++++++++++++++++
 5 files changed, 109 insertions(+), 10 deletions(-)

diff --git a/vlc-android/AndroidManifest.xml b/vlc-android/AndroidManifest.xml
index a41d73683..f7feeb8f4 100644
--- a/vlc-android/AndroidManifest.xml
+++ b/vlc-android/AndroidManifest.xml
@@ -566,10 +566,14 @@
             android:screenOrientation="landscape"
             android:theme="@style/Theme.VLC.TV" />
         <activity
-            android:name=".gui.tv.DetailsActivity"
-            android:exported="true"
-            android:screenOrientation="landscape"
-            android:theme="@style/Theme.Leanback.Details" />
+                android:name=".gui.tv.dialogs.ConfirmationTvActivity"
+                android:screenOrientation="landscape"
+                android:theme="@style/Theme.VLC.TV"/>
+        <activity
+                android:name=".gui.tv.DetailsActivity"
+                android:exported="true"
+                android:screenOrientation="landscape"
+                android:theme="@style/Theme.Leanback.Details"/>
         <activity
             android:name=".gui.tv.browser.VerticalGridActivity"
             android:exported="true"
diff --git a/vlc-android/res/values/strings.xml b/vlc-android/res/values/strings.xml
index e1196e3b2..ab3fff35c 100644
--- a/vlc-android/res/values/strings.xml
+++ b/vlc-android/res/values/strings.xml
@@ -716,4 +716,6 @@
     <string name="appended">Track(s) appended</string>
     <string name="inserted">Track(s) inserted</string>
     <string name="remove_current">Remove current</string>
+    <string name="validation_delete_playlist">Are you sure you want to delete this playlist?</string>
+    <string name="validation_delete_playlist_text">This action cannot be undone.</string>
 </resources>
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/details/MediaListActivity.kt b/vlc-android/src/org/videolan/vlc/gui/tv/details/MediaListActivity.kt
index 48560c4a3..60eb59870 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/details/MediaListActivity.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/details/MediaListActivity.kt
@@ -1,5 +1,6 @@
 package org.videolan.vlc.gui.tv.details
 
+import android.content.Intent
 import android.os.Bundle
 import android.os.Parcelable
 import android.view.View
@@ -21,13 +22,15 @@ import org.videolan.vlc.gui.dialogs.SavePlaylistDialog
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.gui.tv.TvUtil
 import org.videolan.vlc.gui.tv.browser.BaseTvActivity
+import org.videolan.vlc.gui.tv.dialogs.ConfirmationTvActivity
+import org.videolan.vlc.gui.tv.dialogs.ConfirmationTvActivity.Companion.CONFIRMATION_DIALOG_TEXT
+import org.videolan.vlc.gui.tv.dialogs.ConfirmationTvActivity.Companion.CONFIRMATION_DIALOG_TITLE
 import org.videolan.vlc.gui.tv.updateBackground
 import org.videolan.vlc.interfaces.ITVEventsHandler
 import org.videolan.vlc.media.MediaUtils
 import org.videolan.vlc.util.ITEM
 import org.videolan.vlc.viewmodels.mobile.PlaylistViewModel
 
-
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
 class MediaListActivity : BaseTvActivity(), ITVEventsHandler, CoroutineScope by MainScope() {
@@ -41,7 +44,6 @@ class MediaListActivity : BaseTvActivity(), ITVEventsHandler, CoroutineScope by
     private lateinit var viewModel: PlaylistViewModel
     private var lateSelectedItem: MediaLibraryItem? = null
 
-
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
         binding = DataBindingUtil.setContentView(this, R.layout.activity_media_list_tv)
@@ -74,7 +76,6 @@ class MediaListActivity : BaseTvActivity(), ITVEventsHandler, CoroutineScope by
             binding.subtitle = item.description
         } else {
             binding.albumSubtitle.visibility = View.GONE
-
         }
 
         binding.totalTime = Tools.millisToString(item.tracks.sumByDouble { it.length.toDouble() }.toLong())
@@ -86,8 +87,10 @@ class MediaListActivity : BaseTvActivity(), ITVEventsHandler, CoroutineScope by
         binding.insertNext.setOnClickListener { MediaUtils.insertNext(this, item.tracks) }
         binding.addPlaylist.setOnClickListener { UiTools.addToPlaylist(this, item.tracks, SavePlaylistDialog.KEY_NEW_TRACKS) }
         binding.delete.setOnClickListener {
-            (viewModel.playlist as Playlist).delete()
-            finish()
+            val intent = Intent(this, ConfirmationTvActivity::class.java)
+            intent.putExtra(CONFIRMATION_DIALOG_TITLE, getString(R.string.validation_delete_playlist))
+            intent.putExtra(CONFIRMATION_DIALOG_TEXT, getString(R.string.validation_delete_playlist_text))
+            startActivityForResult(intent, REQUEST_DELETE_PLAYLIST)
         }
 
         if (item.itemType == MediaLibraryItem.TYPE_PLAYLIST) {
@@ -100,8 +103,18 @@ class MediaListActivity : BaseTvActivity(), ITVEventsHandler, CoroutineScope by
         } else {
             binding.delete.visibility = View.GONE
         }
+    }
+
+    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
 
+        super.onActivityResult(requestCode, resultCode, data)
 
+        if (requestCode == REQUEST_DELETE_PLAYLIST) {
+            if (resultCode == ConfirmationTvActivity.ACTION_ID_POSITIVE) {
+                (viewModel.playlist as Playlist).delete()
+                finish()
+            }
+        }
     }
 
     override fun onResume() {
@@ -138,7 +151,6 @@ class MediaListActivity : BaseTvActivity(), ITVEventsHandler, CoroutineScope by
     override fun onClickMoveUp(v: View, position: Int) {
 
         (viewModel.playlist as Playlist).move(position, position - 1)
-
     }
 
     override fun onClickMoveDown(v: View, position: Int) {
@@ -153,6 +165,10 @@ class MediaListActivity : BaseTvActivity(), ITVEventsHandler, CoroutineScope by
         if (item != lateSelectedItem) updateBackground(this, backgroundManager, item)
         lateSelectedItem = item
     }
+
+    companion object {
+        private const val REQUEST_DELETE_PLAYLIST = 1
+    }
 }
 
 @ObsoleteCoroutinesApi
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/dialogs/ConfirmationTvActivity.kt b/vlc-android/src/org/videolan/vlc/gui/tv/dialogs/ConfirmationTvActivity.kt
new file mode 100644
index 000000000..14d74c583
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/dialogs/ConfirmationTvActivity.kt
@@ -0,0 +1,28 @@
+package org.videolan.vlc.gui.tv.dialogs
+
+import android.os.Bundle
+import androidx.leanback.app.GuidedStepSupportFragment
+import org.videolan.vlc.gui.tv.browser.BaseTvActivity
+
+class ConfirmationTvActivity : BaseTvActivity() {
+
+    public override fun onCreate(savedInstanceState: Bundle?) {
+        super.onCreate(savedInstanceState)
+
+        if (savedInstanceState == null) {
+            val title = intent.getStringExtra(CONFIRMATION_DIALOG_TITLE)
+            val text = intent.getStringExtra(CONFIRMATION_DIALOG_TEXT)
+            val fragment = ConfirmationTvDialog.newInstance(title, text)
+            GuidedStepSupportFragment.addAsRoot(this, fragment, android.R.id.content)
+        }
+    }
+
+    companion object {
+        const val CONFIRMATION_DIALOG_TITLE = "confirmation_dialog_title"
+        const val CONFIRMATION_DIALOG_TEXT = "confirmation_dialog_text"
+        const val ACTION_ID_POSITIVE = 1
+        const val ACTION_ID_NEGATIVE = ACTION_ID_POSITIVE + 1
+    }
+
+    override fun refresh() {}
+}
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/dialogs/ConfirmationTvDialog.kt b/vlc-android/src/org/videolan/vlc/gui/tv/dialogs/ConfirmationTvDialog.kt
new file mode 100644
index 000000000..8547414c5
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/dialogs/ConfirmationTvDialog.kt
@@ -0,0 +1,49 @@
+package org.videolan.vlc.gui.tv.dialogs
+
+import android.os.Bundle
+import androidx.leanback.app.GuidedStepSupportFragment
+import androidx.leanback.widget.GuidanceStylist
+import androidx.leanback.widget.GuidedAction
+import org.videolan.vlc.R
+import org.videolan.vlc.gui.tv.dialogs.ConfirmationTvActivity.Companion.ACTION_ID_NEGATIVE
+import org.videolan.vlc.gui.tv.dialogs.ConfirmationTvActivity.Companion.ACTION_ID_POSITIVE
+import org.videolan.vlc.gui.tv.dialogs.ConfirmationTvActivity.Companion.CONFIRMATION_DIALOG_TEXT
+import org.videolan.vlc.gui.tv.dialogs.ConfirmationTvActivity.Companion.CONFIRMATION_DIALOG_TITLE
+
+class ConfirmationTvDialog : GuidedStepSupportFragment() {
+
+    override fun onCreateGuidance(savedInstanceState: Bundle?): GuidanceStylist.Guidance {
+        return GuidanceStylist.Guidance(arguments!!.getString(CONFIRMATION_DIALOG_TITLE),
+                arguments!!.getString(CONFIRMATION_DIALOG_TEXT),
+                "", null)
+    }
+
+    override fun onCreateActions(actions: MutableList<GuidedAction>, savedInstanceState: Bundle?) {
+        var action = GuidedAction.Builder(requireActivity())
+                .id(ACTION_ID_POSITIVE.toLong())
+                .title(getString(R.string.yes)).build()
+        actions.add(action)
+        action = GuidedAction.Builder(requireActivity())
+                .id(ACTION_ID_NEGATIVE.toLong())
+                .title(getString(R.string.no)).build()
+        actions.add(action)
+    }
+
+    override fun onGuidedActionClicked(action: GuidedAction?) {
+        if (ACTION_ID_POSITIVE.toLong() == action!!.id) {
+            requireActivity().setResult(ACTION_ID_POSITIVE)
+        } else {
+            requireActivity().setResult(ACTION_ID_NEGATIVE)
+        }
+        requireActivity().finish()
+    }
+
+    companion object {
+        fun newInstance(title: String, text: String): ConfirmationTvDialog = ConfirmationTvDialog().also {
+            val args = Bundle()
+            args.putString(CONFIRMATION_DIALOG_TITLE, title)
+            args.putString(CONFIRMATION_DIALOG_TEXT, text)
+            it.arguments = args
+        }
+    }
+}
\ No newline at end of file



More information about the Android mailing list