TracksModel.kt 3.3 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
/*****************************************************************************
 * TracksModel.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.
 *****************************************************************************/

Geoffrey Métais's avatar
Geoffrey Métais committed
21 22
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
Geoffrey Métais's avatar
Geoffrey Métais committed
28 29
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.*
30
import org.videolan.vlc.util.Settings
Geoffrey Métais's avatar
Geoffrey Métais committed
31

32
class TracksModel(context: Context, val parent: MediaLibraryItem? = null): AudioModel(context), Medialibrary.MediaCb {
Geoffrey Métais's avatar
Geoffrey Métais committed
33

34
    override val sortKey = "${super.sortKey}_${parent?.javaClass?.simpleName}"
Geoffrey Métais's avatar
Geoffrey Métais committed
35 36 37 38
    override fun canSortByDuration() = true
    override fun canSortByAlbum() = parent !== null

    init {
39 40
        sort = Settings.getInstance(context).getInt(sortKey, Medialibrary.SORT_ALPHA)
        desc = Settings.getInstance(context).getBoolean("${sortKey}_desc", false)
41
        if (sort == Medialibrary.SORT_ALPHA) sort = when (parent) {
Geoffrey Métais's avatar
Geoffrey Métais committed
42 43 44 45
            is Artist -> Medialibrary.SORT_ALBUM
            is Album -> Medialibrary.SORT_DEFAULT
            else -> Medialibrary.SORT_ALPHA
        }
46
        if (medialibrary.isStarted) refresh()
Geoffrey Métais's avatar
Geoffrey Métais committed
47
    }
Geoffrey Métais's avatar
Geoffrey Métais committed
48

49 50 51 52 53 54 55
    override fun onMedialibraryReady() {
        super.onMedialibraryReady()
        medialibrary.addMediaCb(this)
    }

    override fun onCleared() {
        medialibrary.removeMediaCb(this)
56
        super.onCleared()
57 58 59 60 61 62 63
    }

    override fun onMediaAdded() {
        refresh()
    }

    override fun onMediaModified() {
Geoffrey Métais's avatar
Geoffrey Métais committed
64 65 66
        refresh()
    }

67
    override fun onMediaDeleted() {
Geoffrey Métais's avatar
Geoffrey Métais committed
68 69
        refresh()
    }
70

71
    @Suppress("UNCHECKED_CAST")
Geoffrey Métais's avatar
Geoffrey Métais committed
72
    override suspend fun updateList() {
73
        dataset.value = withContext(Dispatchers.IO) {
74 75 76 77 78 79 80
            when (parent) {
                is Artist -> parent.getTracks(sort, desc)
                is Album -> parent.getTracks(sort, desc)
                is Genre -> parent.getTracks(sort, desc)
                is Playlist -> parent.getTracks()
                else -> medialibrary.getAudio(sort, desc)
            }.toMutableList() as MutableList<MediaLibraryItem>
Geoffrey Métais's avatar
Geoffrey Métais committed
81
        }
82 83
    }

84
    class Factory(private val context: Context, private val parent: MediaLibraryItem?): ViewModelProvider.NewInstanceFactory() {
85 86
        override fun <T : ViewModel> create(modelClass: Class<T>): T {
            @Suppress("UNCHECKED_CAST")
87
            return TracksModel(context.applicationContext, parent) as T
88
        }
Geoffrey Métais's avatar
Geoffrey Métais committed
89
    }
90
}