[Cleanup] ViewModel cleanup, fixed coroutine dispatchers for some functions
This commit is contained in:
parent
8b100b4a17
commit
eb21c07f5b
10 changed files with 55 additions and 93 deletions
|
@ -69,41 +69,23 @@ class LibraryViewModel(
|
|||
|
||||
fun getSearchResult(): LiveData<List<Any>> = searchResults
|
||||
|
||||
fun getSongs(): LiveData<List<Song>> {
|
||||
return songs
|
||||
}
|
||||
fun getSongs(): LiveData<List<Song>> = songs
|
||||
|
||||
fun getAlbums(): LiveData<List<Album>> {
|
||||
return albums
|
||||
}
|
||||
fun getAlbums(): LiveData<List<Album>> = albums
|
||||
|
||||
fun getArtists(): LiveData<List<Artist>> {
|
||||
return artists
|
||||
}
|
||||
fun getArtists(): LiveData<List<Artist>> = artists
|
||||
|
||||
fun getPlaylists(): LiveData<List<PlaylistWithSongs>> {
|
||||
return playlists
|
||||
}
|
||||
fun getPlaylists(): LiveData<List<PlaylistWithSongs>> = playlists
|
||||
|
||||
fun getLegacyPlaylist(): LiveData<List<Playlist>> {
|
||||
return legacyPlaylists
|
||||
}
|
||||
fun getLegacyPlaylist(): LiveData<List<Playlist>> = legacyPlaylists
|
||||
|
||||
fun getGenre(): LiveData<List<Genre>> {
|
||||
return genres
|
||||
}
|
||||
fun getGenre(): LiveData<List<Genre>> = genres
|
||||
|
||||
fun getHome(): LiveData<List<Home>> {
|
||||
return home
|
||||
}
|
||||
fun getHome(): LiveData<List<Home>> = home
|
||||
|
||||
fun getSuggestions(): LiveData<List<Song>> {
|
||||
return suggestions
|
||||
}
|
||||
fun getSuggestions(): LiveData<List<Song>> = suggestions
|
||||
|
||||
fun getFabMargin(): LiveData<Int> {
|
||||
return fabMargin
|
||||
}
|
||||
fun getFabMargin(): LiveData<Int> = fabMargin
|
||||
|
||||
private suspend fun fetchSongs() {
|
||||
songs.postValue(repository.allSongs())
|
||||
|
@ -111,7 +93,6 @@ class LibraryViewModel(
|
|||
|
||||
private suspend fun fetchAlbums() {
|
||||
albums.postValue(repository.fetchAlbums())
|
||||
|
||||
}
|
||||
|
||||
private suspend fun fetchArtists() {
|
||||
|
@ -146,7 +127,7 @@ class LibraryViewModel(
|
|||
|
||||
fun search(query: String?, filter: Filter) =
|
||||
viewModelScope.launch(IO) {
|
||||
val result =repository.search(query, filter)
|
||||
val result = repository.search(query, filter)
|
||||
searchResults.postValue(result)
|
||||
}
|
||||
|
||||
|
@ -273,29 +254,22 @@ class LibraryViewModel(
|
|||
loadLibraryContent()
|
||||
}
|
||||
|
||||
fun recentSongs(): LiveData<List<Song>> = liveData {
|
||||
fun recentSongs(): LiveData<List<Song>> = liveData(IO) {
|
||||
emit(repository.recentSongs())
|
||||
}
|
||||
|
||||
fun playCountSongs(): LiveData<List<Song>> = liveData {
|
||||
val songs = repository.playCountSongs().map {
|
||||
it.toSong()
|
||||
}
|
||||
emit(songs)
|
||||
// Cleaning up deleted or moved songs
|
||||
withContext(IO) {
|
||||
songs.forEach { song ->
|
||||
fun playCountSongs(): LiveData<List<Song>> = liveData(IO) {
|
||||
repository.playCountSongs().forEach { song ->
|
||||
if (!File(song.data).exists() || song.id == -1L) {
|
||||
repository.deleteSongInPlayCount(song.toPlayCount())
|
||||
repository.deleteSongInPlayCount(song)
|
||||
}
|
||||
}
|
||||
emit(repository.playCountSongs().map {
|
||||
it.toSong()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fun artists(type: Int): LiveData<List<Artist>> = liveData {
|
||||
fun artists(type: Int): LiveData<List<Artist>> = liveData(IO) {
|
||||
when (type) {
|
||||
TOP_ARTISTS -> emit(repository.topArtists())
|
||||
RECENT_ARTISTS -> {
|
||||
|
@ -304,7 +278,7 @@ class LibraryViewModel(
|
|||
}
|
||||
}
|
||||
|
||||
fun albums(type: Int): LiveData<List<Album>> = liveData {
|
||||
fun albums(type: Int): LiveData<List<Album>> = liveData(IO) {
|
||||
when (type) {
|
||||
TOP_ALBUMS -> emit(repository.topAlbums())
|
||||
RECENT_ALBUMS -> {
|
||||
|
@ -313,29 +287,25 @@ class LibraryViewModel(
|
|||
}
|
||||
}
|
||||
|
||||
fun artist(artistId: Long): LiveData<Artist> = liveData {
|
||||
fun artist(artistId: Long): LiveData<Artist> = liveData(IO) {
|
||||
emit(repository.artistById(artistId))
|
||||
}
|
||||
|
||||
fun fetchContributors(): LiveData<List<Contributor>> = liveData {
|
||||
fun fetchContributors(): LiveData<List<Contributor>> = liveData(IO) {
|
||||
emit(repository.contributor())
|
||||
}
|
||||
|
||||
fun observableHistorySongs(): LiveData<List<Song>> {
|
||||
val songs = repository.historySong().map {
|
||||
it.toSong()
|
||||
}
|
||||
songHistory.value = songs
|
||||
// Cleaning up deleted or moved songs
|
||||
viewModelScope.launch {
|
||||
songs.forEach { song ->
|
||||
viewModelScope.launch(IO) {
|
||||
repository.historySong().forEach { song ->
|
||||
if (!File(song.data).exists() || song.id == -1L) {
|
||||
repository.deleteSongInHistory(song.id)
|
||||
}
|
||||
}
|
||||
}
|
||||
songHistory.value = repository.historySong().map {
|
||||
|
||||
songHistory.postValue(repository.historySong().map {
|
||||
it.toSong()
|
||||
})
|
||||
}
|
||||
return songHistory
|
||||
}
|
||||
|
@ -366,9 +336,7 @@ class LibraryViewModel(
|
|||
fun favorites() = repository.favorites()
|
||||
|
||||
fun clearSearchResult() {
|
||||
viewModelScope.launch {
|
||||
searchResults.postValue(emptyList())
|
||||
}
|
||||
searchResults.value = emptyList()
|
||||
}
|
||||
|
||||
fun addToPlaylist(playlistName: String, songs: List<Song>) {
|
||||
|
@ -396,11 +364,13 @@ class LibraryViewModel(
|
|||
}
|
||||
forceReload(Playlists)
|
||||
withContext(Main) {
|
||||
Toast.makeText(App.getContext(), App.getContext().getString(
|
||||
Toast.makeText(
|
||||
App.getContext(), App.getContext().getString(
|
||||
R.string.added_song_count_to_playlist,
|
||||
songs.size,
|
||||
playlistName
|
||||
), Toast.LENGTH_SHORT).show()
|
||||
), Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ class AlbumDetailsViewModel(
|
|||
emit(artist)
|
||||
}
|
||||
|
||||
fun getAlbumInfo(album: Album): LiveData<Result<LastFmAlbum>> = liveData {
|
||||
fun getAlbumInfo(album: Album): LiveData<Result<LastFmAlbum>> = liveData(IO) {
|
||||
emit(Result.Loading)
|
||||
emit(repository.albumInfo(album.artistName, album.title))
|
||||
}
|
||||
|
|
|
@ -47,7 +47,7 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
|||
else
|
||||
backupAdapter?.swapDataset(listOf())
|
||||
}
|
||||
backupViewModel.loadBackups(requireContext())
|
||||
backupViewModel.loadBackups()
|
||||
val openFilePicker = registerForActivityResult(ActivityResultContracts.OpenDocument()) {
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
it?.let {
|
||||
|
@ -98,7 +98,7 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
|||
// Text submitted with the action button
|
||||
lifecycleScope.launch {
|
||||
BackupHelper.createBackup(requireContext(), text.sanitize())
|
||||
backupViewModel.loadBackups(requireContext())
|
||||
backupViewModel.loadBackups()
|
||||
}
|
||||
}
|
||||
positiveButton(android.R.string.ok)
|
||||
|
@ -128,7 +128,7 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
|||
Toast.LENGTH_SHORT
|
||||
).show()
|
||||
}
|
||||
backupViewModel.loadBackups(requireContext())
|
||||
backupViewModel.loadBackups()
|
||||
return true
|
||||
}
|
||||
R.id.action_share -> {
|
||||
|
@ -149,7 +149,7 @@ class BackupFragment : Fragment(R.layout.fragment_backup), BackupAdapter.BackupC
|
|||
File(file.parent, "$text${BackupHelper.APPEND_EXTENSION}")
|
||||
if (!renamedFile.exists()) {
|
||||
file.renameTo(renamedFile)
|
||||
backupViewModel.loadBackups(requireContext())
|
||||
backupViewModel.loadBackups()
|
||||
} else {
|
||||
Toast.makeText(
|
||||
requireContext(),
|
||||
|
|
|
@ -20,7 +20,7 @@ class BackupViewModel : ViewModel() {
|
|||
private val backupsMutableLiveData = MutableLiveData<List<File>>()
|
||||
val backupsLiveData: LiveData<List<File>> = backupsMutableLiveData
|
||||
|
||||
fun loadBackups(context: Context) {
|
||||
fun loadBackups() {
|
||||
BackupHelper.getBackupRoot().listFiles { _, name ->
|
||||
return@listFiles name.endsWith(BackupHelper.BACKUP_EXTENSION)
|
||||
}?.toList()?.let {
|
||||
|
|
|
@ -22,6 +22,7 @@ import code.name.monkey.retromusic.interfaces.IMusicServiceEventListener
|
|||
import code.name.monkey.retromusic.model.Genre
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.repository.RealRepository
|
||||
import kotlinx.coroutines.Dispatchers.IO
|
||||
import kotlinx.coroutines.Dispatchers.Main
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
|
@ -44,7 +45,7 @@ class GenreDetailsViewModel(
|
|||
loadGenreSongs(genre)
|
||||
}
|
||||
|
||||
private fun loadGenreSongs(genre: Genre) = viewModelScope.launch {
|
||||
private fun loadGenreSongs(genre: Genre) = viewModelScope.launch(IO) {
|
||||
val songs = realRepository.getGenre(genre.id)
|
||||
withContext(Main) { _playListSongs.postValue(songs) }
|
||||
}
|
||||
|
|
|
@ -21,6 +21,7 @@ import android.view.MenuItem
|
|||
import android.view.MenuItem.SHOW_AS_ACTION_IF_ROOM
|
||||
import android.view.View
|
||||
import androidx.activity.addCallback
|
||||
import androidx.coordinatorlayout.widget.CoordinatorLayout
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.core.text.parseAsHtml
|
||||
import androidx.core.view.doOnLayout
|
||||
|
@ -29,7 +30,6 @@ import androidx.core.view.isVisible
|
|||
import androidx.navigation.fragment.FragmentNavigatorExtras
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import code.name.monkey.appthemehelper.ThemeStore
|
||||
import code.name.monkey.appthemehelper.common.ATHToolbarActivity
|
||||
import code.name.monkey.appthemehelper.util.ColorUtil
|
||||
import code.name.monkey.appthemehelper.util.ToolbarContentTintHelper
|
||||
|
@ -77,12 +77,12 @@ class HomeFragment :
|
|||
layoutManager = LinearLayoutManager(mainActivity)
|
||||
adapter = homeAdapter
|
||||
}
|
||||
libraryViewModel.getHome().observe(viewLifecycleOwner) {
|
||||
homeAdapter.swapData(it)
|
||||
}
|
||||
libraryViewModel.getSuggestions().observe(viewLifecycleOwner) {
|
||||
loadSuggestions(it)
|
||||
}
|
||||
libraryViewModel.getHome().observe(viewLifecycleOwner) {
|
||||
homeAdapter.swapData(it)
|
||||
}
|
||||
|
||||
loadProfile()
|
||||
setupTitle()
|
||||
|
@ -110,7 +110,6 @@ class HomeFragment :
|
|||
button.setLines(maxLineCount)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun setupListeners() {
|
||||
|
@ -218,16 +217,12 @@ class HomeFragment :
|
|||
}
|
||||
|
||||
fun setSharedAxisXTransitions() {
|
||||
exitTransition = MaterialSharedAxis(MaterialSharedAxis.X, true).apply {
|
||||
addTarget(binding.root)
|
||||
}
|
||||
exitTransition = MaterialSharedAxis(MaterialSharedAxis.X, true).addTarget(CoordinatorLayout::class.java)
|
||||
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.X, false)
|
||||
}
|
||||
|
||||
private fun setSharedAxisYTransitions() {
|
||||
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Y, true).apply {
|
||||
addTarget(binding.root)
|
||||
}
|
||||
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Y, true).addTarget(CoordinatorLayout::class.java)
|
||||
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Y, false)
|
||||
}
|
||||
|
||||
|
@ -246,7 +241,7 @@ class HomeFragment :
|
|||
binding.suggestions.image7,
|
||||
binding.suggestions.image8
|
||||
)
|
||||
val color = ThemeStore.accentColor(requireContext())
|
||||
val color = accentColor()
|
||||
binding.suggestions.message.apply {
|
||||
setTextColor(color)
|
||||
setOnClickListener {
|
||||
|
|
|
@ -80,7 +80,7 @@ class PlayerAlbumCoverFragment : AbsMusicServiceFragment(R.layout.fragment_playe
|
|||
lifecycleScope.launchWhenStarted {
|
||||
viewPager.setPageTransformer(false, transformer)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateLyrics() {
|
||||
binding.lyricsView.setLabel(context?.getString(R.string.no_lyrics_found))
|
||||
|
@ -286,7 +286,7 @@ class PlayerAlbumCoverFragment : AbsMusicServiceFragment(R.layout.fragment_playe
|
|||
} else {
|
||||
surfaceColor()
|
||||
}
|
||||
Color ,Classic -> color.backgroundColor
|
||||
Color, Classic -> color.backgroundColor
|
||||
Blur -> Color.BLACK
|
||||
else -> surfaceColor()
|
||||
}
|
||||
|
|
|
@ -15,14 +15,10 @@
|
|||
package code.name.monkey.retromusic.fragments.playlists
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import code.name.monkey.retromusic.db.PlaylistWithSongs
|
||||
import code.name.monkey.retromusic.db.SongEntity
|
||||
import code.name.monkey.retromusic.interfaces.IMusicServiceEventListener
|
||||
import code.name.monkey.retromusic.model.Song
|
||||
import code.name.monkey.retromusic.repository.RealRepository
|
||||
import code.name.monkey.retromusic.repository.RealRoomRepository
|
||||
|
||||
class PlaylistDetailsViewModel(
|
||||
private val realRepository: RealRepository,
|
||||
|
|
|
@ -585,7 +585,7 @@ object PreferenceUtil {
|
|||
4 -> VerticalFlipTransformation()
|
||||
5 -> HingeTransformation()
|
||||
6 -> VerticalStackTransformer()
|
||||
else -> NormalPageTransformer()
|
||||
else -> ViewPager.PageTransformer { _, _ -> }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
<item>@string/horizontal_flip</item>
|
||||
<item>@string/hinge</item>
|
||||
<item>@string/stack</item>
|
||||
<item>@string/classic</item>
|
||||
<item>@string/simple</item>
|
||||
</array>
|
||||
|
||||
<string-array name="pref_album_cover_transform_values">
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue