PagedPlaylistsModel.kt 1.65 KB
Newer Older
1 2
package org.videolan.vlc.viewmodels.paged

3 4
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
5
import android.content.Context
6
import org.videolan.medialibrary.Medialibrary
7 8
import org.videolan.medialibrary.media.Playlist

9 10
class PagedPlaylistsModel(context: Context): MLPagedModel<Playlist>(context), Medialibrary.PlaylistsCb {

11 12 13 14
    init {
        if (medialibrary.isStarted) refresh()
    }

15 16 17 18 19 20 21
    override fun onMedialibraryReady() {
        super.onMedialibraryReady()
        medialibrary.addPlaylistCb(this)
    }

    override fun onCleared() {
        medialibrary.removePlaylistCb(this)
22
        super.onCleared()
23
    }
24 25 26

    override fun canSortByDuration() = true

27
    override fun getAll() : Array<Playlist> = medialibrary.getPlaylists(sort, desc)
28

29
    override fun getPage(loadSize: Int, startposition: Int)  : Array<Playlist> {
30 31
        return if (filterQuery == null) medialibrary.getPagedPlaylists(sort, desc, loadSize, startposition)
        else medialibrary.searchPlaylist(filterQuery, sort, desc, loadSize, startposition)
32
    }
33

34
    override fun getTotalCount() = if (filterQuery == null) medialibrary.playlistsCount else medialibrary.getPlaylistsCount(filterQuery)
35

36 37 38 39 40 41 42 43 44 45 46 47
    override fun onPlaylistsAdded() {
        refresh()
    }

    override fun onPlaylistsModified() {
        refresh()
    }

    override fun onPlaylistsDeleted() {
        refresh()
    }

48 49 50 51 52 53 54
    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
        }
    }
}