[Android] FoldersViewModel for FoldersFragment

Geoffrey Métais git at videolan.org
Tue May 14 13:33:08 CEST 2019


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Mon May 13 18:18:42 2019 +0200| [606ec3cf0888e7f87f3c7b2f8400bc081909f2e2] | committer: Geoffrey Métais

FoldersViewModel for FoldersFragment

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

 .../videolan/vlc/gui/folders/FoldersFragment.kt    | 12 ++--
 .../vlc/viewmodels/mobile/FoldersViewModel.kt      | 72 ++++++++++++++++++++++
 .../vlc/viewmodels/mobile/PlaylistsViewModel.kt    |  4 ++
 .../vlc/viewmodels/mobile/VideosViewModel.kt       |  4 ++
 4 files changed, 86 insertions(+), 6 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 b540a15a3..fbb7f3289 100644
--- a/vlc-android/src/org/videolan/vlc/gui/folders/FoldersFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/folders/FoldersFragment.kt
@@ -23,11 +23,11 @@ import org.videolan.vlc.media.PlaylistManager
 import org.videolan.vlc.media.getAll
 import org.videolan.vlc.reloadLibrary
 import org.videolan.vlc.util.*
-import org.videolan.vlc.viewmodels.paged.PagedFoldersModel
+import org.videolan.vlc.viewmodels.mobile.FoldersViewModel
 
 @ExperimentalCoroutinesApi
 @ObsoleteCoroutinesApi
-class FoldersFragment : MediaBrowserFragment<PagedFoldersModel>(), CtxActionReceiver {
+class FoldersFragment : MediaBrowserFragment<FoldersViewModel>(), CtxActionReceiver {
 
     private lateinit var adapter: FoldersAdapter
 
@@ -60,8 +60,8 @@ class FoldersFragment : MediaBrowserFragment<PagedFoldersModel>(), CtxActionRece
         super.onCreate(savedInstanceState)
         if (!this::adapter.isInitialized) {
             adapter = FoldersAdapter(actor)
-            viewModel = ViewModelProviders.of(requireActivity(), PagedFoldersModel.Factory(requireContext(), Folder.TYPE_FOLDER_VIDEO)).get(PagedFoldersModel::class.java)
-            viewModel.pagedList.observe(requireActivity(), Observer {
+            viewModel = ViewModelProviders.of(requireActivity(), FoldersViewModel.Factory(requireContext(), Folder.TYPE_FOLDER_VIDEO)).get(FoldersViewModel::class.java)
+            viewModel.provider.pagedList.observe(requireActivity(), Observer {
                 swipeRefreshLayout?.isRefreshing = false
                 adapter.submitList(it)
             })
@@ -107,7 +107,7 @@ class FoldersFragment : MediaBrowserFragment<PagedFoldersModel>(), CtxActionRece
         when (option) {
             CTX_PLAY -> launch { viewModel.play(position) }
             CTX_APPEND -> launch { viewModel.append(position) }
-            CTX_ADD_TO_PLAYLIST -> viewModel.pagedList.value?.get(position)?.let { UiTools.addToPlaylist(requireActivity(), it.getAll()) }
+            CTX_ADD_TO_PLAYLIST -> viewModel.provider.pagedList.value?.get(position)?.let { UiTools.addToPlaylist(requireActivity(), it.getAll()) }
         }
     }
 
@@ -138,7 +138,7 @@ class FoldersFragment : MediaBrowserFragment<PagedFoldersModel>(), CtxActionRece
             R.id.action_folder_add_playlist -> launch { UiTools.addToPlaylist(requireActivity(), withContext(Dispatchers.Default) { selection.getAll() }) }
             else -> return false
         }
-        stopActionMode();
+        stopActionMode()
         return true
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/FoldersViewModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/FoldersViewModel.kt
new file mode 100644
index 000000000..1ba5f69aa
--- /dev/null
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/FoldersViewModel.kt
@@ -0,0 +1,72 @@
+/*****************************************************************************
+ * FoldersViewModel.kt
+ *****************************************************************************
+ * Copyright © 2019 VLC authors and VideoLAN
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+package org.videolan.vlc.viewmodels.mobile
+
+import android.content.Context
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.ViewModelProvider
+import kotlinx.coroutines.*
+import org.videolan.medialibrary.media.Folder
+import org.videolan.medialibrary.media.MediaLibraryItem
+import org.videolan.vlc.media.MediaUtils
+import org.videolan.vlc.media.getAll
+import org.videolan.vlc.providers.medialibrary.FoldersProvider
+import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
+import org.videolan.vlc.viewmodels.MedialibraryViewModel
+
+
+ at ExperimentalCoroutinesApi
+ at ObsoleteCoroutinesApi
+class FoldersViewModel(context: Context, val type : Int) : MedialibraryViewModel(context) {
+    val provider = FoldersProvider(context, this, type)
+    override val providers: Array<MedialibraryProvider<out MediaLibraryItem>> = arrayOf(provider)
+
+    init {
+        if (medialibrary.isStarted) refresh()
+    }
+
+    suspend fun play(position: Int) {
+        val list = withContext(Dispatchers.IO) { provider.pagedList.value?.get(position)?.getAll()}
+        list?.let { MediaUtils.openList(context, it, 0) }
+    }
+
+    suspend fun append(position: Int) {
+        val list = withContext(Dispatchers.IO) { provider.pagedList.value?.get(position)?.getAll()}
+        list?.let { MediaUtils.appendMedia(context, it) }
+    }
+
+    fun playSelection(selection: List<Folder>) = launch {
+        val list = selection.flatMap { it.getAll() }
+        MediaUtils.openList(context, list, 0)
+    }
+
+    fun appendSelection(selection: List<Folder>) = launch {
+        val list = selection.flatMap { it.getAll() }
+        MediaUtils.appendMedia(context, list)
+    }
+
+    class Factory(val context: Context, val type : Int): ViewModelProvider.NewInstanceFactory() {
+        override fun <T : ViewModel> create(modelClass: Class<T>): T {
+            @Suppress("UNCHECKED_CAST")
+            return FoldersViewModel(context.applicationContext, type) as T
+        }
+    }
+}
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/PlaylistsViewModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/PlaylistsViewModel.kt
index dcd78f2fa..9defa6434 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/PlaylistsViewModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/PlaylistsViewModel.kt
@@ -33,6 +33,10 @@ class PlaylistsViewModel(context: Context) : MedialibraryViewModel(context) {
     val provider = PlaylistsProvider(context, this)
     override val providers : Array<MedialibraryProvider<out MediaLibraryItem>> = arrayOf(provider)
 
+    init {
+        if (medialibrary.isStarted) refresh()
+    }
+
     class Factory(val context: Context): ViewModelProvider.NewInstanceFactory() {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
             @Suppress("UNCHECKED_CAST")
diff --git a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
index ea664208d..965c24f29 100644
--- a/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
+++ b/vlc-android/src/org/videolan/vlc/viewmodels/mobile/VideosViewModel.kt
@@ -35,6 +35,10 @@ class VideosViewModel(context: Context, val folder: Folder?) : MedialibraryViewM
     val provider = VideosProvider(folder, context, this)
     override val providers: Array<MedialibraryProvider<out MediaLibraryItem>> = arrayOf(provider)
 
+    init {
+        if (medialibrary.isStarted) refresh()
+    }
+
     class Factory(val context: Context, val folder: Folder?): ViewModelProvider.NewInstanceFactory() {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
             @Suppress("UNCHECKED_CAST")



More information about the Android mailing list