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

ViewModels: helper functions

parent 6e8b3d21
......@@ -41,7 +41,6 @@ import androidx.appcompat.view.ActionMode
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.paging.PagedList
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
......@@ -70,6 +69,7 @@ import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.media.PlaylistManager
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.mobile.PlaylistViewModel
import org.videolan.vlc.viewmodels.mobile.getViewModel
import java.lang.Runnable
import java.util.*
......@@ -105,7 +105,7 @@ open class PlaylistActivity : AudioPlayerContainerActivity(), IEventsHandler, IL
}
isPlaylist = playlist.itemType == MediaLibraryItem.TYPE_PLAYLIST
binding.playlist = playlist
viewModel = ViewModelProviders.of(this, PlaylistViewModel.Factory(this, playlist)).get(PlaylistViewModel::class.java)
viewModel = getViewModel(playlist)
viewModel.tracksProvider.pagedList.observe(this, Observer { tracks ->
if (tracks != null) {
@Suppress("UNCHECKED_CAST")
......
......@@ -31,7 +31,6 @@ import android.view.ViewGroup
import androidx.appcompat.view.ActionMode
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.paging.PagedList
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
......@@ -57,6 +56,7 @@ import org.videolan.vlc.reloadLibrary
import org.videolan.vlc.util.CTX_PLAY_ALL
import org.videolan.vlc.util.getScreenWidth
import org.videolan.vlc.viewmodels.mobile.PlaylistsViewModel
import org.videolan.vlc.viewmodels.mobile.getViewModel
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
......@@ -69,8 +69,7 @@ class PlaylistFragment : BaseAudioBrowser<PlaylistsViewModel>(), SwipeRefreshLay
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProviders.of(requireActivity(), PlaylistsViewModel.Factory(requireContext())).get(PlaylistsViewModel::class.java)
viewModel = getViewModel()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
binding = PlaylistsFragmentBinding.inflate(inflater, container, false)
......
......@@ -28,7 +28,6 @@ import android.os.Parcelable
import android.view.*
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.paging.PagedList
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
......@@ -50,6 +49,7 @@ import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.util.CTX_PLAY_ALL
import org.videolan.vlc.util.Util
import org.videolan.vlc.viewmodels.mobile.AlbumSongsViewModel
import org.videolan.vlc.viewmodels.mobile.getViewModel
private const val TAG = "VLC/AudioAlbumsSongsFragment"
......@@ -86,7 +86,7 @@ class AudioAlbumsSongsFragment : BaseAudioBrowser<AlbumSongsViewModel>(), SwipeR
savedInstanceState.getParcelable<Parcelable>(AudioBrowserFragment.TAG_ITEM) as MediaLibraryItem
else
arguments!!.getParcelable<Parcelable>(AudioBrowserFragment.TAG_ITEM) as MediaLibraryItem
viewModel = ViewModelProviders.of(requireActivity(), AlbumSongsViewModel.Factory(requireContext(), item)).get(AlbumSongsViewModel::class.java)
viewModel = getViewModel(item)
}
override fun getTitle(): String = viewModel.parent.title
......
......@@ -29,7 +29,6 @@ import android.widget.Button
import android.widget.TextView
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.paging.PagedList
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
......@@ -58,6 +57,7 @@ import org.videolan.vlc.util.KEY_AUDIO_CURRENT_TAB
import org.videolan.vlc.util.Settings
import org.videolan.vlc.util.WeakHandler
import org.videolan.vlc.viewmodels.mobile.AudioBrowserViewModel
import org.videolan.vlc.viewmodels.mobile.getViewModel
import java.util.*
@ObsoleteCoroutinesApi
......@@ -166,7 +166,7 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>(), SwipeRef
}
private fun setupModels() {
viewModel = ViewModelProviders.of(requireActivity(), AudioBrowserViewModel.Factory(requireContext())).get(AudioBrowserViewModel::class.java)
viewModel = getViewModel()
artistsAdapter = AudioBrowserAdapter(MediaLibraryItem.TYPE_ARTIST, this)
albumsAdapter = AudioBrowserAdapter(MediaLibraryItem.TYPE_ALBUM, this)
songsAdapter = AudioBrowserAdapter(MediaLibraryItem.TYPE_MEDIA, this)
......
......@@ -5,7 +5,6 @@ import android.os.Bundle
import android.view.*
import androidx.appcompat.view.ActionMode
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.folders_fragment.*
......@@ -24,6 +23,7 @@ import org.videolan.vlc.media.getAll
import org.videolan.vlc.reloadLibrary
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.mobile.FoldersViewModel
import org.videolan.vlc.viewmodels.mobile.getViewModel
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
......@@ -60,7 +60,7 @@ class FoldersFragment : MediaBrowserFragment<FoldersViewModel>(), CtxActionRecei
super.onCreate(savedInstanceState)
if (!this::adapter.isInitialized) {
adapter = FoldersAdapter(actor)
viewModel = ViewModelProviders.of(requireActivity(), FoldersViewModel.Factory(requireContext(), Folder.TYPE_FOLDER_VIDEO)).get(FoldersViewModel::class.java)
viewModel = getViewModel()
viewModel.provider.pagedList.observe(requireActivity(), Observer {
swipeRefreshLayout?.isRefreshing = false
adapter.submitList(it)
......
......@@ -40,7 +40,6 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import androidx.leanback.app.BackgroundManager
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.paging.PagedList
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
......@@ -60,6 +59,7 @@ import org.videolan.vlc.gui.view.RecyclerSectionItemGridDecoration
import org.videolan.vlc.interfaces.IEventsHandler
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.tv.MediaBrowserViewModel
import org.videolan.vlc.viewmodels.tv.getMediaBrowserModel
import java.util.*
private const val TAG = "MediaBrowserTvFragment"
......@@ -102,7 +102,7 @@ class MediaBrowserTvFragment : Fragment(), BrowserFragmentInterface, IEventsHand
val category = arguments?.getLong(AUDIO_CATEGORY, CATEGORY_SONGS) ?: CATEGORY_SONGS
viewModel = ViewModelProviders.of(requireActivity(), MediaBrowserViewModel.Factory(requireContext(), category)).get(MediaBrowserViewModel::class.java)
viewModel = getMediaBrowserModel(category)
viewModel.currentItem = if (savedInstanceState != null) savedInstanceState.getParcelable<Parcelable>(AUDIO_ITEM) as? MediaLibraryItem
else requireActivity().intent.getParcelableExtra<Parcelable>(AUDIO_ITEM) as? MediaLibraryItem
......
......@@ -32,7 +32,6 @@ 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
......@@ -61,6 +60,7 @@ import org.videolan.vlc.media.PlaylistManager
import org.videolan.vlc.reloadLibrary
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.mobile.VideosViewModel
import org.videolan.vlc.viewmodels.mobile.getViewModel
import java.lang.ref.WeakReference
import java.util.*
......@@ -109,7 +109,7 @@ class VideoGridFragment : MediaBrowserFragment<VideosViewModel>(), SwipeRefreshL
multiSelectHelper = videoListAdapter.multiSelectHelper
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 = getViewModel(folder)
viewModel.provider.pagedList.observe(this, this)
}
}
......
......@@ -51,9 +51,9 @@ import kotlinx.coroutines.channels.actor
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.BuildConfig
import org.videolan.vlc.R
import org.videolan.vlc.providers.medialibrary.HeadersIndex
import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
import org.videolan.vlc.util.WeakHandler
import org.videolan.vlc.viewmodels.paged.HeadersIndex
import java.util.concurrent.atomic.AtomicBoolean
private const val TAG = "FastScroller"
......
......@@ -22,6 +22,7 @@ package org.videolan.vlc.providers.medialibrary
import android.content.Context
import androidx.annotation.MainThread
import androidx.collection.SparseArrayCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.paging.Config
......@@ -35,9 +36,8 @@ import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.SortableModel
import org.videolan.vlc.viewmodels.paged.HeadersIndex
typealias HeadersIndex = SparseArrayCompat<String>
abstract class MedialibraryProvider<T : MediaLibraryItem>(val context: Context, val scope: SortableModel) {
protected val medialibrary = Medialibrary.getInstance()
val loading = MutableLiveData<Boolean>().apply { value = false }
......
......@@ -23,11 +23,14 @@ package org.videolan.vlc.viewmodels.mobile
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.Album
import org.videolan.medialibrary.media.Artist
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.gui.audio.AudioAlbumsSongsFragment
import org.videolan.vlc.providers.medialibrary.AlbumsProvider
import org.videolan.vlc.providers.medialibrary.TracksProvider
import org.videolan.vlc.util.EmptyMLCallbacks
......@@ -77,4 +80,8 @@ class AlbumSongsViewModel(context: Context, val parent: MediaLibraryItem) : Medi
return AlbumSongsViewModel(context.applicationContext, parent) as T
}
}
}
\ No newline at end of file
}
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
internal fun AudioAlbumsSongsFragment.getViewModel(item : MediaLibraryItem) = ViewModelProviders.of(requireActivity(), AlbumSongsViewModel.Factory(requireContext(), item)).get(AlbumSongsViewModel::class.java)
......@@ -23,8 +23,11 @@ package org.videolan.vlc.viewmodels.mobile
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.medialibrary.Medialibrary
import org.videolan.vlc.gui.audio.AudioBrowserFragment
import org.videolan.vlc.providers.medialibrary.AlbumsProvider
import org.videolan.vlc.providers.medialibrary.ArtistsProvider
import org.videolan.vlc.providers.medialibrary.GenresProvider
......@@ -93,4 +96,8 @@ class AudioBrowserViewModel(context: Context) : MedialibraryViewModel(context),
return AudioBrowserViewModel(context.applicationContext) as T
}
}
}
\ No newline at end of file
}
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
internal fun AudioBrowserFragment.getViewModel() = ViewModelProviders.of(requireActivity(), AudioBrowserViewModel.Factory(requireContext())).get(AudioBrowserViewModel::class.java)
\ No newline at end of file
......@@ -23,9 +23,11 @@ package org.videolan.vlc.viewmodels.mobile
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import kotlinx.coroutines.*
import org.videolan.medialibrary.media.Folder
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.gui.folders.FoldersFragment
import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.media.getAll
import org.videolan.vlc.providers.medialibrary.FoldersProvider
......@@ -69,4 +71,8 @@ class FoldersViewModel(context: Context, val type : Int) : MedialibraryViewModel
return FoldersViewModel(context.applicationContext, type) as T
}
}
}
\ No newline at end of file
}
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
internal fun FoldersFragment.getViewModel() = ViewModelProviders.of(requireActivity(), FoldersViewModel.Factory(requireContext(), Folder.TYPE_FOLDER_VIDEO)).get(FoldersViewModel::class.java)
......@@ -23,11 +23,14 @@ package org.videolan.vlc.viewmodels.mobile
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.Album
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.medialibrary.media.Playlist
import org.videolan.vlc.gui.PlaylistActivity
import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
import org.videolan.vlc.providers.medialibrary.TracksProvider
import org.videolan.vlc.util.EmptyMLCallbacks
......@@ -78,4 +81,8 @@ class PlaylistViewModel(context: Context, val playlist: MediaLibraryItem) : Medi
return PlaylistViewModel(context.applicationContext, playlist) as T
}
}
}
\ No newline at end of file
}
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
internal fun PlaylistActivity.getViewModel(playlist: MediaLibraryItem) = ViewModelProviders.of(this, PlaylistViewModel.Factory(this, playlist)).get(PlaylistViewModel::class.java)
\ No newline at end of file
......@@ -23,12 +23,17 @@ package org.videolan.vlc.viewmodels.mobile
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.gui.PlaylistFragment
import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
import org.videolan.vlc.providers.medialibrary.PlaylistsProvider
import org.videolan.vlc.viewmodels.MedialibraryViewModel
@ExperimentalCoroutinesApi
class PlaylistsViewModel(context: Context) : MedialibraryViewModel(context) {
val provider = PlaylistsProvider(context, this)
override val providers : Array<MedialibraryProvider<out MediaLibraryItem>> = arrayOf(provider)
......@@ -43,4 +48,8 @@ class PlaylistsViewModel(context: Context) : MedialibraryViewModel(context) {
return PlaylistsViewModel(context.applicationContext) as T
}
}
}
\ No newline at end of file
}
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
internal fun PlaylistFragment.getViewModel() = ViewModelProviders.of(requireActivity(), PlaylistsViewModel.Factory(requireContext())).get(PlaylistsViewModel::class.java)
\ No newline at end of file
......@@ -23,9 +23,12 @@ package org.videolan.vlc.viewmodels.mobile
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.medialibrary.media.Folder
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.gui.video.VideoGridFragment
import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
import org.videolan.vlc.providers.medialibrary.VideosProvider
import org.videolan.vlc.viewmodels.MedialibraryViewModel
......@@ -45,4 +48,8 @@ class VideosViewModel(context: Context, val folder: Folder?) : MedialibraryViewM
return VideosViewModel(context.applicationContext, folder) as T
}
}
}
\ No newline at end of file
}
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
internal fun VideoGridFragment.getViewModel(folder: Folder?) = ViewModelProviders.of(requireActivity(), VideosViewModel.Factory(requireContext(), folder)).get(VideosViewModel::class.java)
\ No newline at end of file
package org.videolan.vlc.viewmodels.tv
import android.content.Context
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.R
......@@ -35,4 +37,7 @@ class MediaBrowserViewModel(context: Context, category: Long) : MedialibraryView
return MediaBrowserViewModel(context.applicationContext, category) as T
}
}
}
\ No newline at end of file
}
@ExperimentalCoroutinesApi
fun Fragment.getMediaBrowserModel(category: Long) = ViewModelProviders.of(requireActivity(), MediaBrowserViewModel.Factory(requireContext(), category)).get(MediaBrowserViewModel::class.java)
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