Glimpse: Move albums logic to AlbumsViewModel

Change-Id: I07d2dab157358ba42ebe03e83e6e27594e199fbd
This commit is contained in:
Luca Stefani 2023-09-05 10:28:22 +02:00
parent ed99cf3094
commit 065618e18b
3 changed files with 33 additions and 4 deletions

View File

@ -31,7 +31,7 @@ import org.lineageos.glimpse.ext.*
import org.lineageos.glimpse.recyclerview.AlbumThumbnailAdapter
import org.lineageos.glimpse.recyclerview.AlbumThumbnailLayoutManager
import org.lineageos.glimpse.utils.PermissionsGatedCallback
import org.lineageos.glimpse.viewmodels.MediaViewModel
import org.lineageos.glimpse.viewmodels.AlbumsViewModel
/**
* An albums list visualizer.
@ -40,7 +40,7 @@ import org.lineageos.glimpse.viewmodels.MediaViewModel
*/
class AlbumsFragment : Fragment() {
// View models
private val mediaViewModel: MediaViewModel by viewModels { MediaViewModel.Factory }
private val albumsViewModel: AlbumsViewModel by viewModels { AlbumsViewModel.Factory }
// Views
private val albumsRecyclerView by getViewProperty<RecyclerView>(R.id.albumsRecyclerView)
@ -55,7 +55,7 @@ class AlbumsFragment : Fragment() {
private val permissionsGatedCallback = PermissionsGatedCallback(this) {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
mediaViewModel.albums.collectLatest {
albumsViewModel.albums.collectLatest {
albumThumbnailAdapter.data = it.toTypedArray()
}
}

View File

@ -0,0 +1,30 @@
/*
* SPDX-FileCopyrightText: 2023 The LineageOS Project
* SPDX-License-Identifier: Apache-2.0
*/
package org.lineageos.glimpse.viewmodels
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import org.lineageos.glimpse.GlimpseApplication
import org.lineageos.glimpse.repository.MediaRepository
open class AlbumsViewModel(
private val mediaRepository: MediaRepository
) : ViewModel() {
val albums = mediaRepository.albums()
companion object {
val Factory: ViewModelProvider.Factory = viewModelFactory {
initializer {
AlbumsViewModel(
mediaRepository = (this[APPLICATION_KEY] as GlimpseApplication).mediaRepository,
)
}
}
}
}

View File

@ -21,7 +21,6 @@ open class MediaViewModel(
private val mediaRepository: MediaRepository
) : ViewModel() {
val media = mediaRepository.media(MediaStoreBuckets.MEDIA_STORE_BUCKET_REELS.id)
val albums = mediaRepository.albums()
private val bucketId = MutableStateFlow(MediaStoreBuckets.MEDIA_STORE_BUCKET_REELS.id)
fun setBucketId(bucketId: Int) {