2020-11-16 19:14:44 +01:00
|
|
|
/*
|
|
|
|
This file is part of Subsonic.
|
|
|
|
|
|
|
|
Subsonic 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Subsonic 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 Subsonic. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
Copyright 2020 (C) Jozsef Varga
|
|
|
|
*/
|
2021-02-08 21:46:31 +01:00
|
|
|
package org.moire.ultrasonic.fragment
|
2020-11-16 19:14:44 +01:00
|
|
|
|
|
|
|
import android.os.Handler
|
|
|
|
import android.os.Looper
|
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.MutableLiveData
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
import androidx.lifecycle.viewModelScope
|
|
|
|
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import org.moire.ultrasonic.data.ActiveServerProvider
|
|
|
|
import org.moire.ultrasonic.domain.Artist
|
|
|
|
import org.moire.ultrasonic.domain.MusicFolder
|
|
|
|
import org.moire.ultrasonic.service.CommunicationErrorHandler
|
|
|
|
import org.moire.ultrasonic.service.MusicServiceFactory
|
|
|
|
import org.moire.ultrasonic.util.Util
|
|
|
|
|
2020-11-18 21:46:46 +01:00
|
|
|
/**
|
|
|
|
* Provides ViewModel which contains the list of available Artists
|
|
|
|
*/
|
2020-11-16 19:14:44 +01:00
|
|
|
class ArtistListModel(
|
2021-05-09 10:57:36 +02:00
|
|
|
private val activeServerProvider: ActiveServerProvider
|
2020-11-16 19:14:44 +01:00
|
|
|
) : ViewModel() {
|
|
|
|
private val musicFolders: MutableLiveData<List<MusicFolder>> = MutableLiveData()
|
|
|
|
private val artists: MutableLiveData<List<Artist>> = MutableLiveData()
|
|
|
|
|
2020-11-18 21:46:46 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the available Artists in a LiveData
|
|
|
|
*/
|
2020-11-16 19:14:44 +01:00
|
|
|
fun getArtists(refresh: Boolean, swipe: SwipeRefreshLayout): LiveData<List<Artist>> {
|
|
|
|
backgroundLoadFromServer(refresh, swipe)
|
|
|
|
return artists
|
|
|
|
}
|
|
|
|
|
2020-11-18 21:46:46 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the available Music Folders in a LiveData
|
|
|
|
*/
|
|
|
|
fun getMusicFolders(): LiveData<List<MusicFolder>> {
|
2020-11-16 19:14:44 +01:00
|
|
|
return musicFolders
|
|
|
|
}
|
|
|
|
|
2020-11-18 21:46:46 +01:00
|
|
|
/**
|
|
|
|
* Refreshes the cached Artists from the server
|
|
|
|
*/
|
2020-11-16 19:14:44 +01:00
|
|
|
fun refresh(swipe: SwipeRefreshLayout) {
|
|
|
|
backgroundLoadFromServer(true, swipe)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun backgroundLoadFromServer(refresh: Boolean, swipe: SwipeRefreshLayout) {
|
|
|
|
viewModelScope.launch {
|
2020-11-18 21:46:46 +01:00
|
|
|
swipe.isRefreshing = true
|
2020-11-16 19:14:44 +01:00
|
|
|
loadFromServer(refresh, swipe)
|
|
|
|
swipe.isRefreshing = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private suspend fun loadFromServer(refresh: Boolean, swipe: SwipeRefreshLayout) =
|
|
|
|
withContext(Dispatchers.IO) {
|
2021-05-09 10:38:03 +02:00
|
|
|
val musicService = MusicServiceFactory.getMusicService()
|
2021-05-09 10:25:04 +02:00
|
|
|
val isOffline = ActiveServerProvider.isOffline()
|
2021-05-09 08:35:15 +02:00
|
|
|
val useId3Tags = Util.getShouldUseId3Tags()
|
2020-11-16 19:14:44 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (!isOffline && !useId3Tags) {
|
|
|
|
musicFolders.postValue(
|
2021-05-09 09:24:05 +02:00
|
|
|
musicService.getMusicFolders(refresh)
|
2020-11-16 19:14:44 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
val musicFolderId = activeServerProvider.getActiveServer().musicFolderId
|
|
|
|
|
|
|
|
val result = if (!isOffline && useId3Tags)
|
2021-05-09 09:24:05 +02:00
|
|
|
musicService.getArtists(refresh)
|
2021-05-09 10:57:36 +02:00
|
|
|
else musicService.getIndexes(musicFolderId, refresh)
|
2020-11-16 19:14:44 +01:00
|
|
|
|
|
|
|
val retrievedArtists: MutableList<Artist> =
|
|
|
|
ArrayList(result.shortcuts.size + result.artists.size)
|
|
|
|
retrievedArtists.addAll(result.shortcuts)
|
|
|
|
retrievedArtists.addAll(result.artists)
|
|
|
|
artists.postValue(retrievedArtists)
|
|
|
|
} catch (exception: Exception) {
|
|
|
|
Handler(Looper.getMainLooper()).post {
|
|
|
|
CommunicationErrorHandler.handleError(exception, swipe.context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|