MedialibraryModel.kt 2.02 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
/*****************************************************************************
 * MedialibraryModel.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.
 *****************************************************************************/

21 22
package org.videolan.vlc.viewmodels

23
import android.content.Context
Geoffrey Métais's avatar
Geoffrey Métais committed
24
import kotlinx.coroutines.launch
25
import org.videolan.medialibrary.Medialibrary
Geoffrey Métais's avatar
Geoffrey Métais committed
26
import org.videolan.medialibrary.media.MediaLibraryItem
27

28
abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseModel<T>(context), Medialibrary.OnMedialibraryReadyListener, Medialibrary.OnDeviceChangeListener {
29

30
    val medialibrary = Medialibrary.getInstance()
31

32
    init {
33 34 35 36
        medialibrary.apply {
            addOnMedialibraryReadyListener(this@MedialibraryModel)
            medialibrary.addOnDeviceChangeListener(this@MedialibraryModel)
        }
37 38 39
    }

    override fun onMedialibraryReady() {
40
        launch { refresh() }
41 42 43
    }

    override fun onMedialibraryIdle() {
44
        launch { refresh() }
45 46
    }

47
    override fun onDeviceChange() {
48
        launch { refresh() }
49 50
    }

51 52
    override fun onCleared() {
        medialibrary.removeOnMedialibraryReadyListener(this)
53
        medialibrary.removeOnDeviceChangeListener(this)
54
        super.onCleared()
55 56
    }
}