[Android] Correctly navigate to parent even if it's not in backstack

Nicolas Pomepuy git at videolan.org
Mon Oct 7 11:59:06 CEST 2019


vlc-android | branch: master | Nicolas Pomepuy <nicolas.pomepuy at gmail.com> | Fri Sep 13 13:50:28 2019 +0200| [04c022e2e50f8e0c206f76182fa0f7c9dfad3ad9] | committer: Geoffrey Métais

Correctly navigate to parent even if it's not in backstack

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

 .../vlc/gui/browser/BaseBrowserFragment.kt         | 21 +++++++++++++++++-
 .../org/videolan/vlc/gui/browser/PathAdapter.kt    |  2 +-
 .../vlc/gui/tv/browser/FileBrowserTvFragment.kt    | 21 +++++++++++++++++-
 .../viewmodels/browser/PathOperationDelegate.kt    | 25 +++++++++++-----------
 4 files changed, 54 insertions(+), 15 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
index a4e82949b..70507396a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/BaseBrowserFragment.kt
@@ -42,6 +42,7 @@ import com.google.android.material.snackbar.Snackbar
 import kotlinx.coroutines.*
 import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 import org.videolan.medialibrary.media.MediaLibraryItem
+import org.videolan.medialibrary.media.MediaWrapper
 import org.videolan.tools.MultiSelectHelper
 import org.videolan.tools.isStarted
 import org.videolan.vlc.BuildConfig
@@ -59,7 +60,9 @@ import org.videolan.vlc.gui.helpers.hf.OTG_SCHEME
 import org.videolan.vlc.gui.view.VLCDividerItemDecoration
 import org.videolan.vlc.interfaces.IEventsHandler
 import org.videolan.vlc.interfaces.IRefreshable
+import org.videolan.vlc.media.MediaSessionBrowser.browse
 import org.videolan.vlc.media.MediaUtils
+import org.videolan.vlc.media.MediaUtils.playAll
 import org.videolan.vlc.media.PlaylistManager
 import org.videolan.vlc.repository.BrowserFavRepository
 import org.videolan.vlc.util.*
@@ -168,7 +171,20 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
     }
 
     override fun backTo(tag: String) {
-        requireActivity().supportFragmentManager.popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE)
+        val supportFragmentManager = requireActivity().supportFragmentManager
+        var poped = false
+        for (i in 0 until supportFragmentManager.backStackEntryCount) {
+            if (tag == supportFragmentManager.getBackStackEntryAt(i).name) {
+                supportFragmentManager.popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE)
+                poped = true
+                break
+            }
+        }
+        if (!poped) {
+            viewModel.setDestination(MediaWrapper(Uri.parse(tag)))
+            supportFragmentManager.popBackStackImmediate()
+        }
+
     }
 
     override fun currentContext() = requireContext()
@@ -189,6 +205,9 @@ abstract class BaseBrowserFragment : MediaBrowserFragment<BrowserModel>(), IRefr
     override fun onResume() {
         super.onResume()
         if (goBack) goBack()
+        viewModel.getAndRemoveDestination()?.let {
+            browse(it, true)
+        }
     }
 
     override fun onStop() {
diff --git a/vlc-android/src/org/videolan/vlc/gui/browser/PathAdapter.kt b/vlc-android/src/org/videolan/vlc/gui/browser/PathAdapter.kt
index e53088320..de7cc7a3d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/browser/PathAdapter.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/browser/PathAdapter.kt
@@ -77,7 +77,7 @@ class PathAdapter(val browser: PathAdapterListener, media: AbstractMediaWrapper)
         val pathParts = string.split('/').filter { it.isNotEmpty() }
         for (index in pathParts.indices) {
             //start creating the Uri
-            val currentPathUri = Uri.Builder().scheme(uri.scheme).authority(uri.authority)
+            val currentPathUri = Uri.Builder().scheme(uri.scheme).encodedAuthority(uri.authority)
             //append all the previous paths and the current one
             for (i in 0..index) pathOperationDelegate.appendPathToUri(pathParts[i], currentPathUri)
             list.add(currentPathUri.toString())
diff --git a/vlc-android/src/org/videolan/vlc/gui/tv/browser/FileBrowserTvFragment.kt b/vlc-android/src/org/videolan/vlc/gui/tv/browser/FileBrowserTvFragment.kt
index 33f1ab2fb..cd23904aa 100644
--- a/vlc-android/src/org/videolan/vlc/gui/tv/browser/FileBrowserTvFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/tv/browser/FileBrowserTvFragment.kt
@@ -2,6 +2,7 @@ package org.videolan.vlc.gui.tv.browser
 
 import android.content.Context
 import android.graphics.Rect
+import android.net.Uri
 import android.os.Bundle
 import android.os.Parcelable
 import android.util.Log
@@ -170,7 +171,22 @@ class FileBrowserTvFragment : BaseBrowserTvFragment(), PathAdapterListener {
             requireActivity().finish()
             return
         }
-        requireActivity().supportFragmentManager.popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE)
+        val supportFragmentManager = requireActivity().supportFragmentManager
+        var poped = false
+        for (i in 0 until supportFragmentManager.backStackEntryCount) {
+            if (tag == supportFragmentManager.getBackStackEntryAt(i).name) {
+                supportFragmentManager.popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE)
+                poped = true
+                break
+            }
+        }
+        if (!poped) {
+            if (supportFragmentManager.backStackEntryCount == 0) browse(MediaWrapper(Uri.parse(tag)), false)
+            else {
+                (viewModel as IPathOperationDelegate).setDestination(MediaWrapper(Uri.parse(tag)))
+                supportFragmentManager.popBackStack()
+            }
+        }
     }
 
     override fun currentContext(): Context = requireActivity()
@@ -215,6 +231,9 @@ class FileBrowserTvFragment : BaseBrowserTvFragment(), PathAdapterListener {
         super.onResume()
         if (item == null) (viewModel.provider as BrowserProvider).browseRoot()
         else refresh()
+        (viewModel as IPathOperationDelegate).getAndRemoveDestination()?.let {
+            browse(it, true)
+        }
     }
 
     override fun onSaveInstanceState(outState: Bundle) {
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/browser/PathOperationDelegate.kt b/vlc-android/src/org/videolan/vlc/viewmodels/browser/PathOperationDelegate.kt
index ee8762c94..310da80e2 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/browser/PathOperationDelegate.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/browser/PathOperationDelegate.kt
@@ -3,34 +3,35 @@ package org.videolan.vlc.viewmodels.browser
 import android.net.Uri
 import android.util.Base64
 import androidx.collection.SimpleArrayMap
+import org.videolan.medialibrary.interfaces.media.AbstractMediaWrapper
 
 interface IPathOperationDelegate {
 
-    //    fun getBackstack(from: String, to: String): SimpleArrayMap<String, Uri>
     fun appendPathToUri(path: String, uri: Uri.Builder)
 
     fun replaceStoragePath(path: String): String
     fun makePathSafe(path: String): String
     fun retrieveSafePath(encoded: String): String
+    fun setDestination(media: AbstractMediaWrapper?)
+    fun getAndRemoveDestination(): AbstractMediaWrapper?
 }
 
 class PathOperationDelegate : IPathOperationDelegate {
+    override fun setDestination(media: AbstractMediaWrapper?) {
+        privateDestination = media
+    }
+
+    override fun getAndRemoveDestination(): AbstractMediaWrapper? {
+        val destination = privateDestination
+        privateDestination = null
+        return destination
+    }
 
     companion object {
         val storages = SimpleArrayMap<String, String>()
+        private var privateDestination: AbstractMediaWrapper? = null
     }
 
-//    override fun getBackstack(from: String, to: String): SimpleArrayMap<String, Uri> {
-//        val result = SimpleArrayMap<String, Uri>()
-//        val fromUri = if (from == "root") null else Uri.parse(from)
-//        val toUri = Uri.parse(to)
-//        if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "PathAdapter From: $fromUri")
-//        if (BuildConfig.DEBUG) Log.d(this::class.java.simpleName, "PathAdapter To: $toUri")
-//
-//
-//
-//        return result
-//    }
 
     /**
      * Append a path to the Uri from a String



More information about the Android mailing list