From c028c1fababa16b039275e481bf2f1fb08b3965d Mon Sep 17 00:00:00 2001 From: Luca Stefani Date: Thu, 7 Sep 2023 09:24:23 +0200 Subject: [PATCH] Glimpse: Use AndroidViewModel Change-Id: Ifad97f71c39eaae5e2fbc55f152388c8402412a2 --- .../lineageos/glimpse/GlimpseApplication.kt | 4 --- .../glimpse/fragments/AlbumFragment.kt | 2 +- .../glimpse/fragments/AlbumsFragment.kt | 4 +-- .../glimpse/fragments/MediaViewerFragment.kt | 4 +-- .../glimpse/fragments/ReelsFragment.kt | 2 +- .../glimpse/repository/MediaRepository.kt | 14 +++++--- .../glimpse/viewmodels/AlbumsViewModel.kt | 28 ++++------------ .../glimpse/viewmodels/GlimpseViewModel.kt | 15 +++++++++ .../glimpse/viewmodels/MediaViewModel.kt | 33 ++++++++----------- .../viewmodels/MediaViewerViewModel.kt | 30 +++++++---------- 10 files changed, 61 insertions(+), 75 deletions(-) create mode 100644 app/src/main/java/org/lineageos/glimpse/viewmodels/GlimpseViewModel.kt diff --git a/app/src/main/java/org/lineageos/glimpse/GlimpseApplication.kt b/app/src/main/java/org/lineageos/glimpse/GlimpseApplication.kt index 1e89b94..ca6e7c6 100644 --- a/app/src/main/java/org/lineageos/glimpse/GlimpseApplication.kt +++ b/app/src/main/java/org/lineageos/glimpse/GlimpseApplication.kt @@ -6,7 +6,6 @@ package org.lineageos.glimpse import android.app.Application -import android.os.Build import coil.ImageLoader import coil.ImageLoaderFactory import coil.decode.GifDecoder @@ -14,11 +13,8 @@ import coil.decode.ImageDecoderDecoder import coil.decode.VideoFrameDecoder import coil.memory.MemoryCache import com.google.android.material.color.DynamicColors -import org.lineageos.glimpse.repository.MediaRepository class GlimpseApplication : Application(), ImageLoaderFactory { - val mediaRepository = MediaRepository(this) - override fun onCreate() { super.onCreate() diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/AlbumFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/AlbumFragment.kt index 8b49558..ddddcb5 100644 --- a/app/src/main/java/org/lineageos/glimpse/fragments/AlbumFragment.kt +++ b/app/src/main/java/org/lineageos/glimpse/fragments/AlbumFragment.kt @@ -45,7 +45,7 @@ import org.lineageos.glimpse.viewmodels.MediaViewModel class AlbumFragment : Fragment(R.layout.fragment_album) { // View models private val mediaViewModel: MediaViewModel by viewModels { - MediaViewModel.factory(lifecycleScope, album.id) + MediaViewModel.factory(requireActivity().application, album.id) } // Views diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt index 72dc569..7a0ad5b 100644 --- a/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt +++ b/app/src/main/java/org/lineageos/glimpse/fragments/AlbumsFragment.kt @@ -40,9 +40,7 @@ import org.lineageos.glimpse.viewmodels.AlbumsViewModel */ class AlbumsFragment : Fragment() { // View models - private val albumsViewModel: AlbumsViewModel by viewModels { - AlbumsViewModel.factory(lifecycleScope) - } + private val albumsViewModel: AlbumsViewModel by viewModels() // Views private val albumsRecyclerView by getViewProperty(R.id.albumsRecyclerView) diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt index beef8d6..930c6fa 100644 --- a/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt +++ b/app/src/main/java/org/lineageos/glimpse/fragments/MediaViewerFragment.kt @@ -58,8 +58,8 @@ class MediaViewerFragment : Fragment(R.layout.fragment_media_viewer) { // View models private val mediaViewModel: MediaViewerViewModel by viewModels { albumId?.let { - MediaViewerViewModel.factory(lifecycleScope, it) - } ?: MediaViewerViewModel.factory(lifecycleScope) + MediaViewerViewModel.factory(requireActivity().application, it) + } ?: MediaViewerViewModel.factory(requireActivity().application) } // Views diff --git a/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt b/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt index c8ff81a..a2f47af 100644 --- a/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt +++ b/app/src/main/java/org/lineageos/glimpse/fragments/ReelsFragment.kt @@ -41,7 +41,7 @@ import org.lineageos.glimpse.viewmodels.MediaViewModel class ReelsFragment : Fragment(R.layout.fragment_reels) { // View models private val mediaViewModel: MediaViewModel by viewModels { - MediaViewModel.factory(lifecycleScope) + MediaViewModel.factory(requireActivity().application) } // Views diff --git a/app/src/main/java/org/lineageos/glimpse/repository/MediaRepository.kt b/app/src/main/java/org/lineageos/glimpse/repository/MediaRepository.kt index 75b33d5..b51ea46 100644 --- a/app/src/main/java/org/lineageos/glimpse/repository/MediaRepository.kt +++ b/app/src/main/java/org/lineageos/glimpse/repository/MediaRepository.kt @@ -12,9 +12,13 @@ import org.lineageos.glimpse.flow.AlbumsFlow import org.lineageos.glimpse.flow.MediaFlow @Suppress("Unused") -class MediaRepository(private val context: Context) { - fun media(bucketId: Int) = MediaFlow(context, bucketId).flowData().flowOn(Dispatchers.IO) - fun mediaCursor(bucketId: Int) = MediaFlow(context, bucketId).flowCursor().flowOn(Dispatchers.IO) - fun albums() = AlbumsFlow(context).flowData().flowOn(Dispatchers.IO) - fun albumsCursor() = AlbumsFlow(context).flowCursor().flowOn(Dispatchers.IO) +object MediaRepository { + fun media(context: Context, bucketId: Int) = + MediaFlow(context, bucketId).flowData().flowOn(Dispatchers.IO) + + fun mediaCursor(context: Context, bucketId: Int) = + MediaFlow(context, bucketId).flowCursor().flowOn(Dispatchers.IO) + + fun albums(context: Context) = AlbumsFlow(context).flowData().flowOn(Dispatchers.IO) + fun albumsCursor(context: Context) = AlbumsFlow(context).flowCursor().flowOn(Dispatchers.IO) } diff --git a/app/src/main/java/org/lineageos/glimpse/viewmodels/AlbumsViewModel.kt b/app/src/main/java/org/lineageos/glimpse/viewmodels/AlbumsViewModel.kt index fac20b4..11f9859 100644 --- a/app/src/main/java/org/lineageos/glimpse/viewmodels/AlbumsViewModel.kt +++ b/app/src/main/java/org/lineageos/glimpse/viewmodels/AlbumsViewModel.kt @@ -5,34 +5,18 @@ package org.lineageos.glimpse.viewmodels -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY -import androidx.lifecycle.viewmodel.initializer -import androidx.lifecycle.viewmodel.viewModelFactory -import kotlinx.coroutines.CoroutineScope +import android.app.Application +import androidx.lifecycle.viewModelScope import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.shareIn -import org.lineageos.glimpse.GlimpseApplication import org.lineageos.glimpse.repository.MediaRepository open class AlbumsViewModel( - private val mediaRepository: MediaRepository, - private val externalScope: CoroutineScope, -) : ViewModel() { - val albums = mediaRepository.albums().shareIn( - externalScope, + application: Application, +) : GlimpseViewModel(application) { + val albums = MediaRepository.albums(context).shareIn( + viewModelScope, replay = 1, started = SharingStarted.WhileSubscribed() ) - - companion object { - fun factory(externalScope: CoroutineScope) = viewModelFactory { - initializer { - AlbumsViewModel( - mediaRepository = (this[APPLICATION_KEY] as GlimpseApplication).mediaRepository, - externalScope = externalScope, - ) - } - } - } } diff --git a/app/src/main/java/org/lineageos/glimpse/viewmodels/GlimpseViewModel.kt b/app/src/main/java/org/lineageos/glimpse/viewmodels/GlimpseViewModel.kt new file mode 100644 index 0000000..35ddcbf --- /dev/null +++ b/app/src/main/java/org/lineageos/glimpse/viewmodels/GlimpseViewModel.kt @@ -0,0 +1,15 @@ +/* + * SPDX-FileCopyrightText: 2023 The LineageOS Project + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.lineageos.glimpse.viewmodels + +import android.app.Application +import android.content.Context +import androidx.lifecycle.AndroidViewModel + +abstract class GlimpseViewModel(application: Application) : AndroidViewModel(application) { + protected val context: Context + get() = getApplication().applicationContext +} diff --git a/app/src/main/java/org/lineageos/glimpse/viewmodels/MediaViewModel.kt b/app/src/main/java/org/lineageos/glimpse/viewmodels/MediaViewModel.kt index ec50b6d..f93fe68 100644 --- a/app/src/main/java/org/lineageos/glimpse/viewmodels/MediaViewModel.kt +++ b/app/src/main/java/org/lineageos/glimpse/viewmodels/MediaViewModel.kt @@ -5,41 +5,36 @@ package org.lineageos.glimpse.viewmodels -import androidx.lifecycle.ViewModel -import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY +import android.app.Application +import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.shareIn -import org.lineageos.glimpse.GlimpseApplication import org.lineageos.glimpse.repository.MediaRepository import org.lineageos.glimpse.utils.MediaStoreBuckets open class MediaViewModel( - private val mediaRepository: MediaRepository, - private val externalScope: CoroutineScope, + application: Application, private val bucketId: Int -) : ViewModel() { - val media = mediaRepository.media(bucketId).shareIn( - externalScope, +) : GlimpseViewModel(application) { + val media = MediaRepository.media(context, bucketId).shareIn( + viewModelScope, replay = 1, started = SharingStarted.WhileSubscribed() ) companion object { fun factory( - externalScope: CoroutineScope, + application: Application, bucketId: Int = MediaStoreBuckets.MEDIA_STORE_BUCKET_REELS.id - ) = - viewModelFactory { - initializer { - MediaViewModel( - mediaRepository = (this[APPLICATION_KEY] as GlimpseApplication).mediaRepository, - externalScope = externalScope, - bucketId = bucketId, - ) - } + ) = viewModelFactory { + initializer { + MediaViewModel( + application = application, + bucketId = bucketId, + ) } + } } } diff --git a/app/src/main/java/org/lineageos/glimpse/viewmodels/MediaViewerViewModel.kt b/app/src/main/java/org/lineageos/glimpse/viewmodels/MediaViewerViewModel.kt index 18dabec..f560845 100644 --- a/app/src/main/java/org/lineageos/glimpse/viewmodels/MediaViewerViewModel.kt +++ b/app/src/main/java/org/lineageos/glimpse/viewmodels/MediaViewerViewModel.kt @@ -5,24 +5,20 @@ package org.lineageos.glimpse.viewmodels +import android.app.Application import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.SavedStateHandle -import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY import androidx.lifecycle.createSavedStateHandle import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory -import kotlinx.coroutines.CoroutineScope -import org.lineageos.glimpse.GlimpseApplication -import org.lineageos.glimpse.repository.MediaRepository import org.lineageos.glimpse.utils.MediaStoreBuckets class MediaViewerViewModel( + application: Application, savedStateHandle: SavedStateHandle, - mediaRepository: MediaRepository, - externalScope: CoroutineScope, bucketId: Int, -) : MediaViewModel(mediaRepository, externalScope, bucketId) { +) : MediaViewModel(application, bucketId) { private val mediaPositionInternal = savedStateHandle.getLiveData(MEDIA_POSITION_KEY) val mediaPositionLiveData: LiveData = mediaPositionInternal var mediaPosition: Int @@ -55,18 +51,16 @@ class MediaViewerViewModel( private const val MEDIA_POSITION_KEY = "position" fun factory( - externalScope: CoroutineScope, + application: Application, bucketId: Int = MediaStoreBuckets.MEDIA_STORE_BUCKET_REELS.id - ) = - viewModelFactory { - initializer { - MediaViewerViewModel( - savedStateHandle = createSavedStateHandle(), - mediaRepository = (this[APPLICATION_KEY] as GlimpseApplication).mediaRepository, - externalScope = externalScope, - bucketId = bucketId, - ) - } + ) = viewModelFactory { + initializer { + MediaViewerViewModel( + application = application, + savedStateHandle = createSavedStateHandle(), + bucketId = bucketId, + ) } + } } }