2021-05-12 13:28:33 +02:00
|
|
|
package org.moire.ultrasonic.fragment
|
|
|
|
|
|
|
|
import android.app.Application
|
|
|
|
import android.content.Context
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Handler
|
|
|
|
import android.os.Looper
|
|
|
|
import androidx.lifecycle.AndroidViewModel
|
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import org.koin.core.component.KoinComponent
|
|
|
|
import org.koin.core.component.inject
|
|
|
|
import org.moire.ultrasonic.data.ActiveServerProvider
|
|
|
|
import org.moire.ultrasonic.data.ServerSetting
|
|
|
|
import org.moire.ultrasonic.domain.MusicFolder
|
2021-11-13 12:57:14 +01:00
|
|
|
import org.moire.ultrasonic.service.CommunicationErrorUtil
|
2021-05-12 13:28:33 +02:00
|
|
|
import org.moire.ultrasonic.service.MusicService
|
|
|
|
import org.moire.ultrasonic.service.MusicServiceFactory
|
2021-09-24 18:20:53 +02:00
|
|
|
import org.moire.ultrasonic.util.Settings
|
2021-05-12 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* An abstract Model, which can be extended to retrieve a list of items from the API
|
|
|
|
*/
|
2021-05-16 20:00:28 +02:00
|
|
|
open class GenericListModel(application: Application) :
|
2021-05-12 13:28:33 +02:00
|
|
|
AndroidViewModel(application), KoinComponent {
|
|
|
|
|
|
|
|
val activeServerProvider: ActiveServerProvider by inject()
|
|
|
|
|
|
|
|
val activeServer: ServerSetting
|
|
|
|
get() = activeServerProvider.getActiveServer()
|
|
|
|
|
|
|
|
val context: Context
|
|
|
|
get() = getApplication<Application>().applicationContext
|
|
|
|
|
|
|
|
var currentListIsSortable = true
|
|
|
|
var showHeader = true
|
2021-05-16 20:00:28 +02:00
|
|
|
|
|
|
|
@Suppress("UNUSED_PARAMETER")
|
|
|
|
open fun showSelectFolderHeader(args: Bundle?): Boolean {
|
|
|
|
return true
|
|
|
|
}
|
2021-05-12 13:28:33 +02:00
|
|
|
|
2021-06-20 16:31:08 +02:00
|
|
|
internal val musicFolders: MutableLiveData<List<MusicFolder>> = MutableLiveData(listOf())
|
2021-05-12 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to check online status
|
|
|
|
*/
|
|
|
|
fun isOffline(): Boolean {
|
|
|
|
return ActiveServerProvider.isOffline()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refreshes the cached items from the server
|
|
|
|
*/
|
|
|
|
fun refresh(swipe: SwipeRefreshLayout, bundle: Bundle?) {
|
|
|
|
backgroundLoadFromServer(true, swipe, bundle ?: Bundle())
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger a load() and notify the UI that we are loading
|
|
|
|
*/
|
|
|
|
fun backgroundLoadFromServer(
|
|
|
|
refresh: Boolean,
|
|
|
|
swipe: SwipeRefreshLayout,
|
|
|
|
bundle: Bundle = Bundle()
|
|
|
|
) {
|
|
|
|
viewModelScope.launch {
|
|
|
|
swipe.isRefreshing = true
|
|
|
|
loadFromServer(refresh, swipe, bundle)
|
|
|
|
swipe.isRefreshing = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calls the load() function with error handling
|
|
|
|
*/
|
|
|
|
suspend fun loadFromServer(refresh: Boolean, swipe: SwipeRefreshLayout, bundle: Bundle) =
|
|
|
|
withContext(Dispatchers.IO) {
|
|
|
|
val musicService = MusicServiceFactory.getMusicService()
|
|
|
|
val isOffline = ActiveServerProvider.isOffline()
|
2021-09-24 18:20:53 +02:00
|
|
|
val useId3Tags = Settings.shouldUseId3Tags
|
2021-05-12 13:28:33 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
load(isOffline, useId3Tags, musicService, refresh, bundle)
|
2021-06-09 20:24:22 +02:00
|
|
|
} catch (all: Exception) {
|
|
|
|
handleException(all, swipe.context)
|
2021-05-12 13:28:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-21 23:26:13 +02:00
|
|
|
private fun handleException(exception: Exception, context: Context) {
|
|
|
|
Handler(Looper.getMainLooper()).post {
|
2021-11-13 12:57:14 +01:00
|
|
|
CommunicationErrorUtil.handleError(exception, context)
|
2021-05-21 23:26:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 13:28:33 +02:00
|
|
|
/**
|
|
|
|
* This is the central function you need to implement if you want to extend this class
|
|
|
|
*/
|
2021-05-16 20:00:28 +02:00
|
|
|
open fun load(
|
2021-05-12 13:28:33 +02:00
|
|
|
isOffline: Boolean,
|
|
|
|
useId3Tags: Boolean,
|
|
|
|
musicService: MusicService,
|
|
|
|
refresh: Boolean,
|
|
|
|
args: Bundle
|
2021-05-16 20:00:28 +02:00
|
|
|
) {
|
|
|
|
// Update the list of available folders if enabled
|
|
|
|
if (showSelectFolderHeader(args) && !isOffline && !useId3Tags) {
|
|
|
|
musicFolders.postValue(
|
|
|
|
musicService.getMusicFolders(refresh)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 13:28:33 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the available Music Folders in a LiveData
|
|
|
|
*/
|
|
|
|
fun getMusicFolders(): LiveData<List<MusicFolder>> {
|
|
|
|
return musicFolders
|
|
|
|
}
|
|
|
|
}
|