Commit 606ec3cf authored by Geoffrey Métais's avatar Geoffrey Métais Committed by Geoffrey Métais

FoldersViewModel for FoldersFragment

parent d99591ff
......@@ -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
}
......
/*****************************************************************************
* 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
@ExperimentalCoroutinesApi
@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
......@@ -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")
......
......@@ -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")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment