Glimpse: Use AndroidViewModel

Change-Id: Ifad97f71c39eaae5e2fbc55f152388c8402412a2
This commit is contained in:
Luca Stefani 2023-09-07 09:24:23 +02:00
parent 3addfcb9a2
commit c028c1faba
10 changed files with 61 additions and 75 deletions

View File

@ -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()

View File

@ -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

View File

@ -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<RecyclerView>(R.id.albumsRecyclerView)

View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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,
)
}
}
}
}

View File

@ -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<Application>().applicationContext
}

View File

@ -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,
)
}
}
}
}

View File

@ -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<Int>(MEDIA_POSITION_KEY)
val mediaPositionLiveData: LiveData<Int> = 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,
)
}
}
}
}