[Android] Folders: Implement FAB

Geoffrey Métais git at videolan.org
Tue Feb 5 17:45:20 CET 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb  5 17:44:47 2019 +0100| [60a65906927e70b27720dd1f6e308e8012ff1988] | committer: Geoffrey Métais

Folders: Implement FAB

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

 .../videolan/vlc/gui/folders/FoldersFragment.kt    | 13 +++++++--
 .../src/org/videolan/vlc/media/MediaUtils.kt       | 34 ++++++++++++++++++++++
 .../vlc/viewmodels/paged/PagedFoldersModel.kt      |  2 +-
 3 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/folders/FoldersFragment.kt b/vlc-android/src/org/videolan/vlc/gui/folders/FoldersFragment.kt
index e98cba00c..cc7badf6f 100644
--- a/vlc-android/src/org/videolan/vlc/gui/folders/FoldersFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/folders/FoldersFragment.kt
@@ -83,13 +83,18 @@ class FoldersFragment : MediaBrowserFragment<PagedFoldersModel>(), CoroutineScop
         mSwipeRefreshLayout.setOnRefreshListener { activity?.reload() }
     }
 
+    override fun onStart() {
+        super.onStart()
+        setFabPlayVisibility(true);
+        mFabPlay.setImageResource(R.drawable.ic_fab_play);
+    }
+
     override fun getTitle() = getString(R.string.video)
 
     override fun onRefresh() {
         viewModel.refresh()
     }
 
-
     override fun onPrepareOptionsMenu(menu: Menu) {
         super.onPrepareOptionsMenu(menu)
         menu.findItem(R.id.ml_menu_last_playlist).isVisible = true
@@ -111,6 +116,10 @@ class FoldersFragment : MediaBrowserFragment<PagedFoldersModel>(), CoroutineScop
         }
     }
 
+    override fun onFabPlayClick(view: View?) {
+        MediaUtils.playAllTracks(context, viewModel, 0, false)
+    }
+
     override fun onCreateActionMode(mode: ActionMode?, menu: Menu?) : Boolean {
         mode?.apply { menuInflater.inflate(R.menu.action_mode_folder, menu) }
         return true
@@ -142,8 +151,6 @@ class FoldersFragment : MediaBrowserFragment<PagedFoldersModel>(), CoroutineScop
         mActionMode = null
         adapter.multiSelectHelper.clearSelection()
     }
-
-    override fun hasFAB() = false
 }
 
 sealed class FolderAction
diff --git a/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt b/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
index ed847fa00..daf4ee934 100644
--- a/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
+++ b/vlc-android/src/org/videolan/vlc/media/MediaUtils.kt
@@ -28,6 +28,7 @@ import org.videolan.vlc.gui.DialogActivity
 import org.videolan.vlc.gui.dialogs.SubtitleDownloaderDialogFragment
 import org.videolan.vlc.util.*
 import org.videolan.vlc.viewmodels.paged.MLPagedModel
+import org.videolan.vlc.viewmodels.paged.PagedFoldersModel
 import java.util.*
 import kotlin.collections.ArrayList
 import kotlin.math.min
@@ -194,6 +195,39 @@ object MediaUtils : CoroutineScope {
         }
     }
 
+    fun playAllTracks(context: Context?, model: PagedFoldersModel, position: Int, shuffle: Boolean) {
+        if (context == null) return
+        SuspendDialogCallback(context) { service ->
+            val count = withContext(Dispatchers.IO) { model.getTotalCount() }
+            fun play(list : List<MediaWrapper>) {
+                service.load(list, if (shuffle) Random().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
+                if (shuffle && !service.isShuffling) service.shuffle()
+            }
+            when (count) {
+                0 -> return at SuspendDialogCallback
+                in 1..MEDIALIBRARY_PAGE_SIZE -> play(withContext(Dispatchers.IO) {
+                    model.getAll().flatMap {
+                        it.media(model.type, Medialibrary.SORT_DEFAULT, false, it.mediaCount(model.type), 0).toList()
+                    }
+                })
+                else -> {
+                    var index = 0
+                    while (index < count) {
+                        val pageCount = min(MEDIALIBRARY_PAGE_SIZE, count - index)
+                        val list = withContext(Dispatchers.IO) {
+                            model.getPage(pageCount, index).flatMap {
+                                it.media(model.type, Medialibrary.SORT_DEFAULT, false, it.mediaCount(model.type), 0).toList()
+                            }
+                        }
+                        if (index == 0) play(list)
+                        else service.append(list)
+                        index += pageCount
+                    }
+                }
+            }
+        }
+    }
+
     @JvmOverloads
     fun openList(context: Context?, list: List<MediaWrapper>, position: Int, shuffle: Boolean = false) {
         if (Util.isListEmpty(list) || context == null) return
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedFoldersModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedFoldersModel.kt
index e4741eeb0..d65a5b7e6 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedFoldersModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/paged/PagedFoldersModel.kt
@@ -16,7 +16,7 @@ class PagedFoldersModel(context: Context, val type: Int) : MLPagedModel<Folder>(
         if (medialibrary.isStarted) refresh()
     }
 
-    override fun getAll() = emptyArray<Folder>()
+    override fun getAll() : Array<Folder> = medialibrary.getFolders(type, sort, desc, getTotalCount(), 0)
 
     override fun getTotalCount() = medialibrary.getFoldersCount(type)
 



More information about the Android mailing list