Skip to content
Snippets Groups Projects
Commit 1b680520 authored by Geoffrey Métais's avatar Geoffrey Métais
Browse files

Extend OnDeviceChangeListener to all ViewModels

parent b9067087
No related branches found
No related tags found
No related merge requests found
......@@ -21,7 +21,6 @@
package org.videolan.vlc.viewmodels
import android.content.Context
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaLibraryItem
......@@ -31,9 +30,12 @@ abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseM
val medialibrary = Medialibrary.getInstance()
override fun fetch() {
init {
medialibrary.addOnMedialibraryReadyListener(this)
medialibrary.addOnDeviceChangeListener(this)
}
override fun fetch() {
if (medialibrary.isStarted) onMedialibraryReady()
}
......@@ -47,7 +49,6 @@ abstract class MedialibraryModel<T : MediaLibraryItem>(context: Context) : BaseM
override fun onDeviceChange() {
launch {
delay(1000L)
refresh()
}
}
......
......@@ -11,7 +11,7 @@ import org.videolan.medialibrary.Medialibrary
import org.videolan.medialibrary.media.MediaLibraryItem
import org.videolan.vlc.viewmodels.SortableModel
abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableModel(context), Medialibrary.OnMedialibraryReadyListener {
abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableModel(context), Medialibrary.OnMedialibraryReadyListener, Medialibrary.OnDeviceChangeListener {
protected val medialibrary = Medialibrary.getInstance()
protected var filter : String? = null
val loading = MutableLiveData<Boolean>().apply { value = false }
......@@ -28,6 +28,8 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
init {
@Suppress("LeakingThis")
medialibrary.addOnMedialibraryReadyListener(this)
@Suppress("LeakingThis")
medialibrary.addOnDeviceChangeListener(this)
if (medialibrary.isStarted) onMedialibraryReady()
}
......@@ -39,8 +41,15 @@ abstract class MLPagedModel<T : MediaLibraryItem>(context: Context) : SortableMo
launch { refresh() }
}
override fun onDeviceChange() {
launch {
refresh()
}
}
override fun onCleared() {
medialibrary.removeOnMedialibraryReadyListener(this)
medialibrary.removeOnDeviceChangeListener(this)
super.onCleared()
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment