PlaylistsModel.kt 2.43 KB
Newer Older
Geoffrey Métais's avatar
Geoffrey Métais committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*****************************************************************************
 * PlaylistsModel.kt
 *****************************************************************************
 * Copyright © 2018 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.audio

23 24
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
25
import android.content.Context
Geoffrey Métais's avatar
Geoffrey Métais committed
26 27
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
28
import org.videolan.medialibrary.Medialibrary
Geoffrey Métais's avatar
Geoffrey Métais committed
29
import org.videolan.medialibrary.media.MediaLibraryItem
30
import org.videolan.vlc.util.EmptyMLCallbacks
Geoffrey Métais's avatar
Geoffrey Métais committed
31 32


33 34
class PlaylistsModel(context: Context): AudioModel(context), Medialibrary.PlaylistsCb by EmptyMLCallbacks {

35 36 37 38
    init {
        if (medialibrary.isStarted) refresh()
    }

39 40 41 42 43 44 45
    override fun onMedialibraryReady() {
        super.onMedialibraryReady()
        medialibrary.addPlaylistCb(this)
    }

    override fun onCleared() {
        medialibrary.removePlaylistCb(this)
46
        super.onCleared()
47
    }
Geoffrey Métais's avatar
Geoffrey Métais committed
48 49 50 51 52

    override fun canSortByDuration() = true

    @Suppress("UNCHECKED_CAST")
    override suspend fun updateList() {
53
        dataset.value = withContext(Dispatchers.IO) { medialibrary.getPlaylists(sort, desc).toMutableList() as MutableList<MediaLibraryItem> }
Geoffrey Métais's avatar
Geoffrey Métais committed
54
    }
55 56 57 58 59 60 61

    class Factory(private val context: Context): ViewModelProvider.NewInstanceFactory() {
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            @Suppress("UNCHECKED_CAST")
            return PlaylistsModel(context.applicationContext) as T
        }
    }
62 63 64 65 66 67 68 69

    override fun onPlaylistsAdded() {
        refresh()
    }

    override fun onPlaylistsDeleted() {
        refresh()
    }
Geoffrey Métais's avatar
Geoffrey Métais committed
70
}