Commit 6e8b3d21 authored by Geoffrey Métais's avatar Geoffrey Métais Committed by Geoffrey Métais

Remove Paged ViewModel classes

parent 606ec3cf
......@@ -168,7 +168,7 @@ class AudioAlbumsSongsFragment : BaseAudioBrowser<AlbumSongsViewModel>(), SwipeR
menu.findItem(R.id.ml_menu_sortby_last_modified).isVisible = canSortByLastModified()
menu.findItem(R.id.ml_menu_sortby_number).isVisible = false
}
super.onPrepareOptionsMenu(menu)
sortMenuTitles()
}
override fun onUpdateFinished(adapter: RecyclerView.Adapter<*>) {
......
......@@ -205,7 +205,7 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>(), SwipeRef
menu.findItem(R.id.ml_menu_sortby_last_modified).isVisible = canSortByLastModified()
menu.findItem(R.id.ml_menu_sortby_number).isVisible = false
}
super.onPrepareOptionsMenu(menu)
sortMenuTitles()
}
override fun onFabPlayClick(view: View) {
......
......@@ -52,8 +52,8 @@ import org.videolan.vlc.gui.view.SwipeRefreshLayout
import org.videolan.vlc.interfaces.Filterable
import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.MedialibraryViewModel
import org.videolan.vlc.viewmodels.SortableModel
import org.videolan.vlc.viewmodels.paged.MLPagedModel
import java.lang.Runnable
import java.util.*
......@@ -202,7 +202,7 @@ abstract class MediaBrowserFragment<T : SortableModel> : Fragment(), ActionMode.
override fun onPrepareOptionsMenu(menu: Menu) {
super.onPrepareOptionsMenu(menu)
(viewModel as? MLPagedModel<*>)?.run {
(viewModel as? MedialibraryViewModel)?.run {
menu.findItem(R.id.ml_menu_sortby).isVisible = canSortByName()
menu.findItem(R.id.ml_menu_sortby_filename).isVisible = canSortByFileNameName()
menu.findItem(R.id.ml_menu_sortby_artist_name).isVisible = canSortByArtist()
......@@ -215,7 +215,11 @@ abstract class MediaBrowserFragment<T : SortableModel> : Fragment(), ActionMode.
sortMenuTitles()
}
open fun sortMenuTitles() = UiTools.updateSortTitles(this)
open fun sortMenuTitles() {
(viewModel as? MedialibraryViewModel)?.let { model ->
menu?.let { UiTools.updateSortTitles(it, model.providers[0]) }
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
......
......@@ -54,7 +54,6 @@ import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
import org.videolan.vlc.util.*
import org.videolan.vlc.viewmodels.BaseModel
import org.videolan.vlc.viewmodels.paged.MLPagedModel
@ExperimentalCoroutinesApi
@ObsoleteCoroutinesApi
......
/*****************************************************************************
* MLPagedModel.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.paged
import android.content.Context
import androidx.collection.SparseArrayCompat
import androidx.lifecycle.LiveData
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.providers.medialibrary.MedialibraryProvider
import org.videolan.vlc.util.Settings
import org.videolan.vlc.viewmodels.SortableModel
typealias HeadersIndex = SparseArrayCompat<String>
@Suppress("LeakingThis")
abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableModel(context), Medialibrary.OnMedialibraryReadyListener, Medialibrary.OnDeviceChangeListener {
protected val medialibrary = Medialibrary.getInstance()
abstract val provider : MedialibraryProvider<T>
private lateinit var restoreJob: Job
val liveHeaders
get() = provider.liveHeaders
val pagedList
get() = provider.pagedList
val loading : LiveData<Boolean>
get() = provider.loading
init {
medialibrary.addOnMedialibraryReadyListener(this)
medialibrary.addOnDeviceChangeListener(this)
}
override fun canSortByName() = provider.canSortByName()
override fun canSortByFileNameName() = provider.canSortByFileNameName()
override fun canSortByDuration() = provider.canSortByDuration()
override fun canSortByInsertionDate() = provider.canSortByInsertionDate()
override fun canSortByLastModified() = provider.canSortByLastModified()
override fun canSortByReleaseDate() = provider.canSortByReleaseDate()
override fun canSortByFileSize() = provider.canSortByFileSize()
override fun canSortByArtist() = provider.canSortByArtist()
override fun canSortByAlbum () = provider.canSortByAlbum ()
override fun canSortByPlayCount() = provider.canSortByPlayCount()
override fun onMedialibraryReady() { refresh() }
override fun onMedialibraryIdle() { refresh() }
override fun onDeviceChange() { refresh() }
override fun onCleared() {
medialibrary.removeOnMedialibraryReadyListener(this)
medialibrary.removeOnDeviceChangeListener(this)
super.onCleared()
}
override fun sort(sort: Int) {
if (this.sort != sort) {
this.sort = sort
desc = false
} else desc = !desc
refresh()
Settings.getInstance(context).edit()
.putInt(sortKey, sort)
.putBoolean("${sortKey}_desc", desc)
.apply()
}
fun isFiltering() = filterQuery != null
override fun filter(query: String?) {
filterQuery = query
refresh()
}
override fun restore() {
restoreJob = launch {
delay(500L)
if (filterQuery != null) {
filterQuery = null
refresh()
}
}
}
fun isEmpty() = provider.isEmpty()
override fun refresh() {
if (this::restoreJob.isInitialized && restoreJob.isActive) restoreJob.cancel()
launch { provider.refresh() }
}
}
/*****************************************************************************
* PagedAlbumsModel.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.paged
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import kotlinx.coroutines.ExperimentalCoroutinesApi
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.providers.medialibrary.AlbumsProvider
import org.videolan.vlc.util.EmptyMLCallbacks
import org.videolan.vlc.util.Settings
@ExperimentalCoroutinesApi
class PagedAlbumsModel(context: Context, val parent: MediaLibraryItem? = null) : MLPagedModel<Album>(context), Medialibrary.AlbumsCb by EmptyMLCallbacks {
override val provider = AlbumsProvider(parent, context, this)
override val sortKey = "${super.sortKey}_${parent?.javaClass?.simpleName}"
init {
sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", false)
if (sort == Medialibrary.SORT_ALPHA && parent is Artist) sort = Medialibrary.SORT_RELEASEDATE
medialibrary.addAlbumsCb(this)
if (medialibrary.isStarted) refresh()
}
override fun onAlbumsAdded() {
refresh()
}
override fun onCleared() {
medialibrary.removeAlbumsCb(this)
super.onCleared()
}
class Factory(private val context: Context, val parent: MediaLibraryItem?) : ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return PagedAlbumsModel(context.applicationContext, parent) as T
}
}
}
\ No newline at end of file
/*****************************************************************************
* PagedArtistsModel.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.paged
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.Artist
import org.videolan.vlc.providers.medialibrary.ArtistsProvider
import org.videolan.vlc.util.EmptyMLCallbacks
@ExperimentalCoroutinesApi
class PagedArtistsModel(context: Context, showAll: Boolean = false): MLPagedModel<Artist>(context), Medialibrary.ArtistsCb by EmptyMLCallbacks {
override val provider = ArtistsProvider(context, this, showAll)
var showAll : Boolean
get() = provider.showAll
set(value) {
provider.showAll = value
}
init {
medialibrary.addArtistsCb(this)
if (medialibrary.isStarted) refresh()
}
override fun onArtistsAdded() {
refresh()
}
override fun onCleared() {
medialibrary.removeArtistsCb(this)
super.onCleared()
}
class Factory(private val context: Context, private val showAll: Boolean): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return PagedArtistsModel(context, showAll) as T
}
}
}
\ No newline at end of file
/*****************************************************************************
* PagedFoldersModel.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.paged
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import kotlinx.coroutines.*
import org.videolan.medialibrary.media.Folder
import org.videolan.vlc.media.MediaUtils
import org.videolan.vlc.media.getAll
import org.videolan.vlc.providers.medialibrary.FoldersProvider
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
class PagedFoldersModel(context: Context, val type: Int) : MLPagedModel<Folder>(context) {
override val provider = FoldersProvider(context, this, type)
init {
if (medialibrary.isStarted) refresh()
}
suspend fun play(position: Int) {
val list = withContext(Dispatchers.IO) { pagedList.value?.get(position)?.getAll()}
list?.let { MediaUtils.openList(context, it, 0) }
}
suspend fun append(position: Int) {
val list = withContext(Dispatchers.IO) { 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(private val context: Context, val type: Int): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return PagedFoldersModel(context.applicationContext, type) as T
}
}
}
\ No newline at end of file
/*****************************************************************************
* PagedGenresModel.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.paged
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.Genre
import org.videolan.vlc.providers.medialibrary.GenresProvider
import org.videolan.vlc.util.EmptyMLCallbacks
import org.videolan.vlc.util.Settings
@ExperimentalCoroutinesApi
class PagedGenresModel(context: Context): MLPagedModel<Genre>(context), Medialibrary.GenresCb by EmptyMLCallbacks {
override val provider = GenresProvider(context, this)
init {
sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", false)
medialibrary.addGenreCb(this)
if (medialibrary.isStarted) refresh()
}
override fun onCleared() {
medialibrary.removeGenreCb(this)
super.onCleared()
}
override fun onGenresAdded() {
refresh()
}
override fun onGenresDeleted() {
refresh()
}
class Factory(private val context: Context): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return PagedGenresModel(context.applicationContext) as T
}
}
}
\ No newline at end of file
/*****************************************************************************
* PagedPlaylistsModel.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.paged
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.Playlist
import org.videolan.vlc.providers.medialibrary.PlaylistsProvider
@ExperimentalCoroutinesApi
class PagedPlaylistsModel(context: Context): MLPagedModel<Playlist>(context), Medialibrary.PlaylistsCb {
override val provider = PlaylistsProvider(context, this)
init {
medialibrary.addPlaylistCb(this)
if (medialibrary.isStarted) refresh()
}
override fun onCleared() {
medialibrary.removePlaylistCb(this)
super.onCleared()
}
override fun canSortByDuration() = true
override fun onPlaylistsAdded() {
refresh()
}
override fun onPlaylistsModified() {
refresh()
}
override fun onPlaylistsDeleted() {
refresh()
}
class Factory(private val context: Context): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return PagedPlaylistsModel(context.applicationContext) as T
}
}
}
\ No newline at end of file
/*****************************************************************************
* PagedTracksModel.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.paged
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.*
import org.videolan.vlc.providers.medialibrary.TracksProvider
import org.videolan.vlc.util.EmptyMLCallbacks
import org.videolan.vlc.util.Settings
@ExperimentalCoroutinesApi
class PagedTracksModel(context: Context, val parent: MediaLibraryItem? = null): MLPagedModel<MediaWrapper>(context),
Medialibrary.MediaCb,
Medialibrary.ArtistsCb by EmptyMLCallbacks,
Medialibrary.AlbumsCb by EmptyMLCallbacks,
Medialibrary.GenresCb by EmptyMLCallbacks,
Medialibrary.PlaylistsCb by EmptyMLCallbacks {
override val provider = TracksProvider(parent, context, this)
init {
sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", parent is Artist)
if (sort == Medialibrary.SORT_ALPHA) sort = when (parent) {
is Artist -> Medialibrary.SORT_ALBUM
is Album -> Medialibrary.SORT_DEFAULT
else -> Medialibrary.SORT_ALPHA
}
if (medialibrary.isStarted) refresh()
when (parent) {
is Artist -> medialibrary.addArtistsCb(this@PagedTracksModel)
is Album -> medialibrary.addAlbumsCb(this@PagedTracksModel)
is Genre -> medialibrary.addGenreCb(this@PagedTracksModel)
is Playlist -> medialibrary.addPlaylistCb(this@PagedTracksModel)
else -> medialibrary.addMediaCb(this@PagedTracksModel)
}
}
override fun onCleared() {
when (parent) {
is Artist -> medialibrary.removeArtistsCb(this)
is Album -> medialibrary.removeAlbumsCb(this)
is Genre -> medialibrary.removeGenreCb(this)
is Playlist -> medialibrary.removePlaylistCb(this)
else -> medialibrary.removeMediaCb(this)
}
super.onCleared()
}
class Factory(private val context: Context, private val parent: MediaLibraryItem?): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
@Suppress("UNCHECKED_CAST")
return PagedTracksModel(context.applicationContext, parent) as T
}
}
override fun onMediaAdded() {
refresh()
}
override fun onMediaModified() {
refresh()
}
override fun onMediaDeleted() {
refresh()
}
override fun onArtistsModified() {
refresh()
}
override fun onAlbumsModified() {
refresh()
}
override fun onGenresModified() {
refresh()
}
override fun onPlaylistsModified() {
refresh()
if ((parent as Playlist).realTracksCount == 0) parent.delete()
}
}
/*****************************************************************************
* PagedVideosModel.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.paged
import android.annotation.TargetApi
import android.content.Context
import android.os.Build
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ObsoleteCoroutinesApi
import org.videolan.libvlc.util.AndroidUtil
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.Folder
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.vlc.providers.medialibrary.VideosProvider
import org.videolan.vlc.util.AndroidDevices
import org.videolan.vlc.util.Settings
import org.videolan.vlc.util.launchChannelUpdate
@ObsoleteCoroutinesApi
@ExperimentalCoroutinesApi
class PagedVideosModel(
context: Context,
val folder : Folder?,
customSort : Int,
customDesc: Boolean?
) : MLPagedModel<MediaWrapper>(context), Medialibrary.MediaCb {
override val provider = VideosProvider(folder, context, this)
init {
sort = if (customSort != Medialibrary.SORT_DEFAULT) customSort
else Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
desc = customDesc ?: Settings.getInstance(context).getBoolean(sortKey + "_desc", false)
if (medialibrary.isStarted) {
medialibrary.addMediaCb(this)
}
}
@TargetApi(Build.VERSION_CODES.O)
override fun onMedialibraryIdle() {
super.onMedialibraryIdle()
if (AndroidDevices.isAndroidTv && AndroidUtil.isOOrLater) context.launchChannelUpdate()
}
override fun onMediaAdded() {
refresh()
}
override fun onMediaModified() {
refresh()
}
override fun onMediaDeleted() {
refresh()