Commit d99591ff authored by Geoffrey Métais's avatar Geoffrey Métais Committed by Geoffrey Métais

VideosViewModel for VideoGridFragment

parent 36bc38c2
......@@ -33,7 +33,6 @@ import androidx.fragment.app.Fragment
import com.google.android.material.appbar.AppBarLayout
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.medialibrary.media.Folder
import org.videolan.vlc.R
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.gui.audio.AudioAlbumsSongsFragment
......@@ -46,6 +45,7 @@ import org.videolan.vlc.gui.video.VideoGridFragment
import org.videolan.vlc.reloadLibrary
import org.videolan.vlc.rescan
import org.videolan.vlc.util.AndroidDevices
import org.videolan.vlc.util.KEY_FOLDER
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
......@@ -123,9 +123,11 @@ class SecondaryActivity : ContentActivity() {
}
ABOUT -> fragment = AboutFragment()
VIDEO_GROUP_LIST -> {
fragment = VideoGridFragment()
(fragment as VideoGridFragment).setGroup(intent.getStringExtra("param"))
(fragment as VideoGridFragment).setFolder(intent.getParcelableExtra<Parcelable>("folder") as Folder)
fragment = VideoGridFragment().apply {
arguments = Bundle(1).apply {
putParcelable(KEY_FOLDER, intent.getParcelableExtra<Parcelable>(KEY_FOLDER))
}
}
}
STORAGE_BROWSER -> fragment = StorageBrowserFragment()
else -> throw IllegalArgumentException("Wrong fragment id.")
......
......@@ -40,7 +40,7 @@ class FoldersFragment : MediaBrowserFragment<PagedFoldersModel>(), CtxActionRece
} else {
val i = Intent(activity, SecondaryActivity::class.java)
i.putExtra("fragment", SecondaryActivity.VIDEO_GROUP_LIST)
i.putExtra("folder", action.folder)
i.putExtra(KEY_FOLDER, action.folder)
activity?.startActivityForResult(i, SecondaryActivity.ACTIVITY_RESULT_SECONDARY)
}
}
......
/*****************************************************************************
* VideoGridFragment.java
* VideoGridFragment.kt
*
* Copyright © 2011-2019 VLC authors and VideoLAN
* 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
......@@ -32,6 +32,7 @@ import android.view.*
import androidx.annotation.MainThread
import androidx.appcompat.view.ActionMode
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.paging.PagedList
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
......@@ -59,19 +60,23 @@ import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.media.PlaylistManager
import org.videolan.vlc.reloadLibrary
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.paged.PagedVideosModel
import org.videolan.vlc.viewmodels.mobile.VideosViewModel
import java.lang.ref.WeakReference
import java.util.*
private const val TAG = "VLC/VideoListFragment"
private const val UPDATE_LIST = 14
private const val SET_REFRESHING = 15
private const val UNSET_REFRESHING = 16
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
class VideoGridFragment : MediaBrowserFragment<PagedVideosModel>(), SwipeRefreshLayout.OnRefreshListener, IEventsHandler, Observer<PagedList<MediaWrapper>>, CtxActionReceiver {
class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshLayout.OnRefreshListener, IEventsHandler, Observer<PagedList<MediaWrapper>>, CtxActionReceiver {
private lateinit var videoListAdapter: VideoListAdapter
private lateinit var multiSelectHelper: MultiSelectHelper<MediaWrapper>
private lateinit var binding: VideoGridBinding
private var videoGroup: String? = null
private var videoFolder: Folder? = null
private var gridItemDecoration: RecyclerView.ItemDecoration? = null
......@@ -102,10 +107,11 @@ class VideoGridFragment : MediaBrowserFragment<PagedVideosModel>(), SwipeRefresh
val seenMarkVisible = preferences.getBoolean("media_seen", true)
videoListAdapter = VideoListAdapter(this, seenMarkVisible)
multiSelectHelper = videoListAdapter.multiSelectHelper
viewModel = PagedVideosModel.get(this, videoFolder)
viewModel.pagedList.observe(this, this)
val folder = if (savedInstanceState != null ) savedInstanceState.getParcelable<Folder>(KEY_FOLDER)
else arguments?.getParcelable(KEY_FOLDER)
viewModel = ViewModelProviders.of(requireActivity(), VideosViewModel.Factory(requireContext(), folder)).get(VideosViewModel::class.java)
viewModel.provider.pagedList.observe(this, this)
}
if (savedInstanceState != null) setGroup(savedInstanceState.getString(KEY_GROUP))
}
override fun onPrepareOptionsMenu(menu: Menu) {
......@@ -165,7 +171,7 @@ class VideoGridFragment : MediaBrowserFragment<PagedVideosModel>(), SwipeRefresh
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putString(KEY_GROUP, videoGroup)
outState.putParcelable(KEY_FOLDER, viewModel.folder)
}
override fun onDestroy() {
......@@ -179,9 +185,7 @@ class VideoGridFragment : MediaBrowserFragment<PagedVideosModel>(), SwipeRefresh
if (list != null) videoListAdapter.submitList(list)
}
override fun getTitle(): String {
return if (videoGroup == null) if (videoFolder == null) getString(R.string.video) else videoFolder!!.title else videoGroup!! + "\u2026"
}
override fun getTitle() = viewModel.folder?.title ?: getString(R.string.video)
private fun updateViewMode() {
if (view == null || activity == null) {
......@@ -237,14 +241,6 @@ class VideoGridFragment : MediaBrowserFragment<PagedVideosModel>(), SwipeRefresh
binding.empty = empty && !working
}
fun setGroup(prefix: String?) {
videoGroup = prefix
}
fun setFolder(folder: Folder) {
videoFolder = folder
}
override fun onRefresh() {
val activity = activity
activity?.reloadLibrary()
......@@ -394,13 +390,4 @@ class VideoGridFragment : MediaBrowserFragment<PagedVideosModel>(), SwipeRefresh
CTX_ADD_TO_PLAYLIST -> UiTools.addToPlaylist(requireActivity(), media.tracks, SavePlaylistDialog.KEY_NEW_TRACKS)
}
}
companion object {
private const val TAG = "VLC/VideoListFragment"
private const val UPDATE_LIST = 14
private const val SET_REFRESHING = 15
private const val UNSET_REFRESHING = 16
}
}
......@@ -131,6 +131,7 @@ const val CATEGORY_VIDEOS = 25L
const val AUDIO_CATEGORY = "category"
const val AUDIO_ITEM = "item"
const val KEY_GROUP = "key_group"
const val KEY_FOLDER = "key_folder"
// Items updates
const val UPDATE_SELECTION = 0
......
......@@ -18,6 +18,8 @@ abstract class MedialibraryViewModel(context: Context) : SortableModel(context),
override fun refresh() = providers.forEach { it.refresh() }
fun isEmpty() = providers.all { it.isEmpty() }
override fun restore() {
if (filterQuery !== null) filter(null)
}
......
/*****************************************************************************
* VideosViewModel.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.ExperimentalCoroutinesApi
import org.videolan.medialibrary.media.Folder
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
import org.videolan.vlc.providers.medialibrary.VideosProvider
import org.videolan.vlc.viewmodels.MedialibraryViewModel
@ExperimentalCoroutinesApi
class VideosViewModel(context: Context, val folder: Folder?) : MedialibraryViewModel(context) {
val provider = VideosProvider(folder, context, this)
override val providers: Array<MedialibraryProvider<out MediaLibraryItem>> = arrayOf(provider)
class Factory(val context: Context, val folder: Folder?): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return VideosViewModel(context.applicationContext, folder) as T
}
}
}
\ No newline at end of file
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