[Android] Allow deleting a ML custom path

Nicolas Pomepuy git at videolan.org
Tue Apr 4 15:15:53 UTC 2023


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Mon Mar 20 09:22:49 2023 +0100| [639dba3fdb03e5008579330e87a3854f86455893] | committer: Duncan McNamara

Allow deleting a ML custom path

Fixes #2761

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

 .../vlc/gui/browser/MLStorageBrowserFragment.kt    | 28 +++++++++++++++++++---
 .../vlc/gui/browser/StorageBrowserAdapter.kt       |  3 ++-
 .../vlc/gui/browser/StorageBrowserFragment.kt      | 22 +----------------
 .../org/videolan/vlc/gui/dialogs/ContextSheet.kt   |  1 +
 4 files changed, 29 insertions(+), 25 deletions(-)

diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/MLStorageBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/MLStorageBrowserFragment.kt
index 815271c89e..acbb1b8240 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/MLStorageBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/MLStorageBrowserFragment.kt
@@ -40,11 +40,16 @@ import org.videolan.medialibrary.MLServiceLocator
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.Storage
+import org.videolan.resources.CTX_CUSTOM_REMOVE
 import org.videolan.tools.NetworkMonitor
 import org.videolan.tools.setGone
+import org.videolan.tools.stripTrailingSlash
 import org.videolan.vlc.R
 import org.videolan.vlc.databinding.BrowserItemBinding
+import org.videolan.vlc.gui.AudioPlayerContainerActivity
 import org.videolan.vlc.gui.BaseFragment
+import org.videolan.vlc.gui.dialogs.CtxActionReceiver
+import org.videolan.vlc.gui.dialogs.showContext
 import org.videolan.vlc.gui.helpers.ThreeStatesCheckbox
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.gui.view.EmptyLoadingState
@@ -58,13 +63,14 @@ import java.io.File
 
 private const val FROM_ONBOARDING = "from_onboarding"
 
-class MLStorageBrowserFragment : BaseFragment(), IStorageFragmentDelegate by StorageFragmentDelegate() {
+class MLStorageBrowserFragment : BaseFragment(), IStorageFragmentDelegate by StorageFragmentDelegate(), CtxActionReceiver {
 
     private lateinit var localEntry: TitleListView
     private lateinit var networkEntry: TitleListView
     private lateinit var networkMonitor: NetworkMonitor
     private lateinit var localViewModel: BrowserModel
     private lateinit var networkViewModel: BrowserModel
+    private lateinit var storageBrowserAdapter: StorageBrowserAdapter
 
     private var alertDialog: AlertDialog? = null
 
@@ -105,7 +111,7 @@ class MLStorageBrowserFragment : BaseFragment(), IStorageFragmentDelegate by Sto
         favoritesEntry.setGone()
 
         localEntry = view.findViewById(R.id.local_browser_entry)
-        val storageBrowserAdapter = StorageBrowserAdapter(getBrowserContainer(false))
+        storageBrowserAdapter = StorageBrowserAdapter(getBrowserContainer(false))
         localEntry.list.adapter = storageBrowserAdapter
         localViewModel = getBrowserModel(category = TYPE_STORAGE, url = null)
         localViewModel.dataset.observe(viewLifecycleOwner) { list ->
@@ -244,7 +250,16 @@ class MLStorageBrowserFragment : BaseFragment(), IStorageFragmentDelegate by Sto
 
         override fun onImageClick(v: View, position: Int, item: MediaLibraryItem) {}
 
-        override fun onCtxClick(v: View, position: Int, item: MediaLibraryItem) {}
+        override fun onCtxClick(v: View, position: Int, item: MediaLibraryItem) {
+            if (isRootDirectory) {
+                val storage = storageBrowserAdapter.getItem(position) as Storage
+                val path = storage.uri.path ?: return
+                lifecycleScope.launchWhenStarted {
+                    val isCustom = localViewModel.customDirectoryExists(path.stripTrailingSlash())
+                    if (isCustom && isAdded) showContext(requireActivity(), this at MLStorageBrowserFragment, position, item, CTX_CUSTOM_REMOVE)
+                }
+            }
+        }
 
         override fun onUpdateFinished(adapter: RecyclerView.Adapter<*>) { }
 
@@ -252,6 +267,13 @@ class MLStorageBrowserFragment : BaseFragment(), IStorageFragmentDelegate by Sto
 
         override fun onItemFocused(v: View, item: MediaLibraryItem) {}
     }
+    override fun onCtxAction(position: Int, option: Long) {
+        val storage = storageBrowserAdapter.getItem(position) as Storage
+        val path = storage.uri.path ?: return
+        localViewModel.deleteCustomDirectory(path)
+        localViewModel.remove(storage)
+        (activity as AudioPlayerContainerActivity).updateLib()
+    }
 
     companion object {
         fun newInstance(onboarding:Boolean) = MLStorageBrowserFragment().apply { arguments = Bundle().apply { putBoolean(FROM_ONBOARDING, onboarding) } }
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
index 6c9104471c..d05075214b 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserAdapter.kt
@@ -39,6 +39,7 @@ import org.videolan.medialibrary.media.Storage
 import org.videolan.resources.AndroidDevices
 import org.videolan.tools.containsPath
 import org.videolan.tools.removeFileScheme
+import org.videolan.tools.stripTrailingSlash
 import org.videolan.vlc.MediaParsingService
 import org.videolan.vlc.R
 import org.videolan.vlc.gui.helpers.MedialibraryUtils
@@ -72,7 +73,7 @@ class StorageBrowserAdapter(browserContainer: BrowserContainer<MediaLibraryItem>
             vh.bindingContainer.setIsTv(AndroidDevices.isTv)
             updateJob?.join()
             if (updateJob?.isCancelled == true) return at launch
-            val hasContextMenu = customDirsLocation.contains(storagePath) && !multiSelectHelper.inActionMode
+            val hasContextMenu = customDirsLocation.contains(storagePath.stripTrailingSlash()) && !multiSelectHelper.inActionMode
             val checked = browserContainer.scannedDirectory || mediaDirsLocation.containsPath(storagePath)
             vh.bindingContainer.setHasContextMenu(hasContextMenu)
             val banned = MedialibraryUtils.isBanned(uri, bannedFolders)
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.kt b/application/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.kt
index 20700f09af..a1e4c56caa 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/browser/StorageBrowserFragment.kt
@@ -46,13 +46,10 @@ import org.videolan.medialibrary.interfaces.Medialibrary
 import org.videolan.medialibrary.interfaces.media.MediaWrapper
 import org.videolan.medialibrary.media.MediaLibraryItem
 import org.videolan.medialibrary.media.Storage
-import org.videolan.resources.CTX_CUSTOM_REMOVE
 import org.videolan.tools.Settings
 import org.videolan.vlc.R
 import org.videolan.vlc.databinding.BrowserItemBinding
-import org.videolan.vlc.gui.AudioPlayerContainerActivity
 import org.videolan.vlc.gui.SecondaryActivity
-import org.videolan.vlc.gui.dialogs.showContext
 import org.videolan.vlc.gui.helpers.ThreeStatesCheckbox
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.viewmodels.browser.TYPE_STORAGE
@@ -146,24 +143,7 @@ class StorageBrowserFragment : FileBrowserFragment(), BrowserContainer<MediaLibr
     }
 
 
-    override fun onCtxClick(v: View, position: Int, item: MediaLibraryItem) {
-        if (isRootDirectory) {
-            val storage = adapter.getItem(position) as Storage
-            val path = storage.uri.path ?: return
-            lifecycleScope.launchWhenStarted {
-                val isCustom = viewModel.customDirectoryExists(path)
-                if (isCustom && isAdded) showContext(requireActivity(), this at StorageBrowserFragment, position, item, CTX_CUSTOM_REMOVE)
-            }
-        }
-    }
-
-    override fun onCtxAction(position: Int, option: Long) {
-        val storage = adapter.getItem(position) as Storage
-        val path = storage.uri.path ?: return
-        viewModel.deleteCustomDirectory(path)
-        viewModel.remove(storage)
-        (activity as AudioPlayerContainerActivity).updateLib()
-    }
+    override fun onCtxClick(v: View, position: Int, item: MediaLibraryItem) {}
 
     override fun onClick(v: View, position: Int, item: MediaLibraryItem) {
         val mw = (item as? Storage)?.let { MLServiceLocator.getAbstractMediaWrapper(it.uri) } ?: return
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/ContextSheet.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/ContextSheet.kt
index 03303168f4..702d9218b2 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/ContextSheet.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/ContextSheet.kt
@@ -145,6 +145,7 @@ class ContextSheet : VLCBottomSheetDialogFragment() {
         if (flags and CTX_MARK_AS_UNPLAYED != 0L) add(Simple(CTX_MARK_AS_UNPLAYED, getString(R.string.mark_as_not_played), R.drawable.ic_ctx_mark_as_not_played))
         if (flags and CTX_MARK_ALL_AS_PLAYED != 0L) add(Simple(CTX_MARK_ALL_AS_PLAYED, getString(R.string.mark_all_as_played), R.drawable.ic_ctx_mark_all_as_played))
         if (flags and CTX_GO_TO_FOLDER != 0L) add(Simple(CTX_GO_TO_FOLDER, getString(R.string.go_to_folder), R.drawable.ic_ctx_folder))
+        if (flags and CTX_CUSTOM_REMOVE != 0L) add(Simple(CTX_CUSTOM_REMOVE, getString(R.string.remove_custom_path), R.drawable.ic_ctx_delete))
     }
 
     inner class ContextAdapter : RecyclerView.Adapter<ContextAdapter.ViewHolder>() {



More information about the Android mailing list