mirror of
https://github.com/bbara04/Purefin.git
synced 2026-07-23 19:26:50 +00:00
refactor: moved CompositeMediaRepository to the domain level + changes in connected classes
This commit is contained in:
@@ -1,7 +0,0 @@
|
||||
package hu.bbara.purefin.data
|
||||
|
||||
import java.util.UUID
|
||||
|
||||
interface EpisodeSeriesLookup {
|
||||
suspend fun preferenceKeyFor(mediaId: UUID): String
|
||||
}
|
||||
@@ -3,10 +3,11 @@ package hu.bbara.purefin.data
|
||||
import hu.bbara.purefin.model.Episode
|
||||
import hu.bbara.purefin.model.Movie
|
||||
import hu.bbara.purefin.model.Series
|
||||
import java.util.UUID
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import java.util.UUID
|
||||
|
||||
//TODO rename MediaRepository
|
||||
interface MediaCatalogReader {
|
||||
val movies: StateFlow<Map<UUID, Movie>>
|
||||
val series: StateFlow<Map<UUID, Series>>
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
package hu.bbara.purefin.data.catalog
|
||||
|
||||
import hu.bbara.purefin.data.MediaCatalogReader
|
||||
import hu.bbara.purefin.data.MediaProgressWriter
|
||||
import hu.bbara.purefin.data.NetworkMonitor
|
||||
import hu.bbara.purefin.model.Episode
|
||||
import hu.bbara.purefin.model.Movie
|
||||
import hu.bbara.purefin.model.Series
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flatMapLatest
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
@Singleton
|
||||
class CompositeMediaRepository @Inject constructor(
|
||||
private val offlineRepository: OfflineMediaRepository,
|
||||
private val onlineRepository: InMemoryMediaRepository,
|
||||
private val networkMonitor: NetworkMonitor,
|
||||
) : MediaCatalogReader, MediaProgressWriter {
|
||||
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
|
||||
private val activeRepository: Flow<MediaCatalogReader> =
|
||||
networkMonitor.isOnline.flatMapLatest { online ->
|
||||
flowOf(if (online) onlineRepository else offlineRepository)
|
||||
}
|
||||
|
||||
override val movies: StateFlow<Map<UUID, Movie>> = activeRepository
|
||||
.flatMapLatest { it.movies }
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyMap())
|
||||
|
||||
override val series: StateFlow<Map<UUID, Series>> = activeRepository
|
||||
.flatMapLatest { it.series }
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyMap())
|
||||
|
||||
override val episodes: StateFlow<Map<UUID, Episode>> = activeRepository
|
||||
.flatMapLatest { it.episodes }
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyMap())
|
||||
|
||||
override fun observeSeriesWithContent(seriesId: UUID): Flow<Series?> {
|
||||
return activeRepository.flatMapLatest { it.observeSeriesWithContent(seriesId) }
|
||||
}
|
||||
|
||||
override suspend fun updateWatchProgress(mediaId: UUID, positionMs: Long, durationMs: Long) {
|
||||
val repository = if (networkMonitor.isOnline.first()) onlineRepository else offlineRepository
|
||||
repository.updateWatchProgress(mediaId, positionMs, durationMs)
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package hu.bbara.purefin.data.catalog
|
||||
|
||||
import hu.bbara.purefin.data.EpisodeSeriesLookup
|
||||
import hu.bbara.purefin.data.MediaCatalogReader
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class DefaultEpisodeSeriesLookup @Inject constructor(
|
||||
private val mediaCatalogReader: MediaCatalogReader,
|
||||
) : EpisodeSeriesLookup {
|
||||
override suspend fun preferenceKeyFor(mediaId: UUID): String {
|
||||
return mediaCatalogReader.episodes.value[mediaId]?.seriesId?.toString() ?: mediaId.toString()
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
package hu.bbara.purefin.data.catalog
|
||||
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import hu.bbara.purefin.data.EpisodeSeriesLookup
|
||||
import hu.bbara.purefin.data.HomeRepository
|
||||
import hu.bbara.purefin.data.MediaCatalogReader
|
||||
import hu.bbara.purefin.data.MediaProgressWriter
|
||||
import hu.bbara.purefin.data.OfflineCatalogReader
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
abstract class MediaRepositoryModule {
|
||||
|
||||
@Binds
|
||||
abstract fun bindHomeRepository(impl: InMemoryAppContentRepository): HomeRepository
|
||||
|
||||
@Binds
|
||||
abstract fun bindMediaCatalogReader(impl: CompositeMediaRepository): MediaCatalogReader
|
||||
|
||||
@Binds
|
||||
abstract fun bindMediaProgressWriter(impl: CompositeMediaRepository): MediaProgressWriter
|
||||
|
||||
@Binds
|
||||
abstract fun bindOfflineCatalogReader(impl: OfflineMediaRepository): OfflineCatalogReader
|
||||
|
||||
@Binds
|
||||
abstract fun bindEpisodeSeriesLookup(impl: DefaultEpisodeSeriesLookup): EpisodeSeriesLookup
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package hu.bbara.purefin.data.module
|
||||
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import hu.bbara.purefin.data.OfflineCatalogReader
|
||||
import hu.bbara.purefin.data.catalog.OfflineMediaRepository
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
abstract class MediaRepositoryModule {
|
||||
|
||||
@Binds
|
||||
abstract fun bindOfflineCatalogReader(impl: OfflineMediaRepository): OfflineCatalogReader
|
||||
}
|
||||
Reference in New Issue
Block a user