mirror of
https://github.com/bbara04/Purefin.git
synced 2026-03-31 17:10:08 +02:00
refactor: modularize app into multi-module architecture
This commit is contained in:
52
core/data/build.gradle.kts
Normal file
52
core/data/build.gradle.kts
Normal file
@@ -0,0 +1,52 @@
|
||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||
|
||||
plugins {
|
||||
alias(libs.plugins.android.library)
|
||||
alias(libs.plugins.kotlin.android)
|
||||
alias(libs.plugins.kotlin.compose)
|
||||
alias(libs.plugins.kotlin.serialization)
|
||||
alias(libs.plugins.hilt)
|
||||
alias(libs.plugins.ksp)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "hu.bbara.purefin.core.data"
|
||||
compileSdk = 36
|
||||
|
||||
defaultConfig {
|
||||
minSdk = 29
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_11
|
||||
targetCompatibility = JavaVersion.VERSION_11
|
||||
}
|
||||
|
||||
buildFeatures {
|
||||
compose = true
|
||||
}
|
||||
}
|
||||
|
||||
kotlin {
|
||||
compilerOptions {
|
||||
jvmTarget.set(JvmTarget.JVM_11)
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":core:model"))
|
||||
implementation(libs.jellyfin.core)
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
implementation(libs.hilt)
|
||||
ksp(libs.hilt.compiler)
|
||||
implementation(libs.datastore)
|
||||
implementation(libs.okhttp)
|
||||
implementation(libs.logging.interceptor)
|
||||
implementation(libs.coil.compose)
|
||||
implementation(libs.coil.network.okhttp)
|
||||
implementation(libs.androidx.room.ktx)
|
||||
ksp(libs.androidx.room.compiler)
|
||||
implementation(libs.androidx.navigation3.runtime)
|
||||
implementation(platform(libs.androidx.compose.bom))
|
||||
implementation(libs.androidx.compose.ui)
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package hu.bbara.purefin.core.data
|
||||
|
||||
import hu.bbara.purefin.core.data.local.room.OfflineRepository
|
||||
import hu.bbara.purefin.core.data.local.room.OnlineRepository
|
||||
import hu.bbara.purefin.core.data.session.UserSessionRepository
|
||||
import hu.bbara.purefin.core.model.Episode
|
||||
import hu.bbara.purefin.core.model.Library
|
||||
import hu.bbara.purefin.core.model.Media
|
||||
import hu.bbara.purefin.core.model.MediaRepositoryState
|
||||
import hu.bbara.purefin.core.model.Movie
|
||||
import hu.bbara.purefin.core.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.flatMapLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Active media repository that delegates to either online or offline repository
|
||||
* based on user preference.
|
||||
*/
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
@Singleton
|
||||
class ActiveMediaRepository @Inject constructor(
|
||||
@OnlineRepository private val onlineRepository: MediaRepository,
|
||||
@OfflineRepository private val offlineRepository: MediaRepository,
|
||||
private val userSessionRepository: UserSessionRepository
|
||||
) : MediaRepository {
|
||||
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
|
||||
// Switch between repositories based on offline mode preference
|
||||
private val activeRepository: StateFlow<MediaRepository> =
|
||||
userSessionRepository.isOfflineMode
|
||||
.map { isOffline ->
|
||||
if (isOffline) offlineRepository else onlineRepository
|
||||
}
|
||||
.stateIn(scope, SharingStarted.Eagerly, onlineRepository)
|
||||
|
||||
// Delegate all MediaRepository interface methods to the active repository
|
||||
override val libraries: StateFlow<List<Library>> =
|
||||
activeRepository.flatMapLatest { it.libraries }
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyList())
|
||||
|
||||
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 val state: StateFlow<MediaRepositoryState> =
|
||||
activeRepository.flatMapLatest { it.state }
|
||||
.stateIn(scope, SharingStarted.Eagerly, MediaRepositoryState.Loading)
|
||||
|
||||
override val continueWatching: StateFlow<List<Media>> =
|
||||
activeRepository.flatMapLatest { it.continueWatching }
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyList())
|
||||
|
||||
override val nextUp: StateFlow<List<Media>> =
|
||||
activeRepository.flatMapLatest { it.nextUp }
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyList())
|
||||
|
||||
override val latestLibraryContent: StateFlow<Map<UUID, List<Media>>> =
|
||||
activeRepository.flatMapLatest { it.latestLibraryContent }
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyMap())
|
||||
|
||||
override fun observeSeriesWithContent(seriesId: UUID): Flow<Series?> =
|
||||
activeRepository.flatMapLatest { it.observeSeriesWithContent(seriesId) }
|
||||
|
||||
override suspend fun ensureReady() {
|
||||
activeRepository.value.ensureReady()
|
||||
}
|
||||
|
||||
override suspend fun updateWatchProgress(mediaId: UUID, positionMs: Long, durationMs: Long) {
|
||||
activeRepository.value.updateWatchProgress(mediaId, positionMs, durationMs)
|
||||
}
|
||||
|
||||
override suspend fun refreshHomeData() {
|
||||
activeRepository.value.refreshHomeData()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,489 @@
|
||||
package hu.bbara.purefin.core.data
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import hu.bbara.purefin.core.data.cache.CachedMediaItem
|
||||
import hu.bbara.purefin.core.data.cache.HomeCache
|
||||
import hu.bbara.purefin.core.data.client.JellyfinApiClient
|
||||
import hu.bbara.purefin.core.data.image.JellyfinImageHelper
|
||||
import hu.bbara.purefin.core.data.local.room.OfflineDatabase
|
||||
import hu.bbara.purefin.core.data.local.room.OfflineRoomMediaLocalDataSource
|
||||
import hu.bbara.purefin.core.data.local.room.RoomMediaLocalDataSource
|
||||
import hu.bbara.purefin.core.data.session.UserSessionRepository
|
||||
import hu.bbara.purefin.core.model.Episode
|
||||
import hu.bbara.purefin.core.model.Library
|
||||
import hu.bbara.purefin.core.model.Media
|
||||
import hu.bbara.purefin.core.model.MediaRepositoryState
|
||||
import hu.bbara.purefin.core.model.Movie
|
||||
import hu.bbara.purefin.core.model.Season
|
||||
import hu.bbara.purefin.core.model.Series
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import org.jellyfin.sdk.model.api.BaseItemDto
|
||||
import org.jellyfin.sdk.model.api.BaseItemKind
|
||||
import org.jellyfin.sdk.model.api.CollectionType
|
||||
import org.jellyfin.sdk.model.api.ImageType
|
||||
import java.time.LocalDateTime
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.util.Locale
|
||||
import java.util.UUID
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class InMemoryMediaRepository @Inject constructor(
|
||||
val userSessionRepository: UserSessionRepository,
|
||||
val jellyfinApiClient: JellyfinApiClient,
|
||||
private val localDataSource: RoomMediaLocalDataSource,
|
||||
@OfflineDatabase private val offlineDataSource: OfflineRoomMediaLocalDataSource,
|
||||
private val homeCacheDataStore: DataStore<HomeCache>
|
||||
) : MediaRepository {
|
||||
|
||||
private val ready = CompletableDeferred<Unit>()
|
||||
private val readyMutex = Mutex()
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
private var initialLoadTimestamp = 0L
|
||||
|
||||
private val _state: MutableStateFlow<MediaRepositoryState> = MutableStateFlow(MediaRepositoryState.Loading)
|
||||
override val state: StateFlow<MediaRepositoryState> = _state.asStateFlow()
|
||||
|
||||
override val libraries: StateFlow<List<Library>> = localDataSource.librariesFlow
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyList())
|
||||
override val movies: StateFlow<Map<UUID, Movie>> = localDataSource.moviesFlow
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyMap())
|
||||
|
||||
override val series: StateFlow<Map<UUID, Series>> = localDataSource.seriesFlow
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyMap())
|
||||
|
||||
override val episodes: StateFlow<Map<UUID, Episode>> = localDataSource.episodesFlow
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyMap())
|
||||
|
||||
override fun observeSeriesWithContent(seriesId: UUID): Flow<Series?> {
|
||||
scope.launch {
|
||||
awaitReady()
|
||||
ensureSeriesContentLoaded(seriesId)
|
||||
}
|
||||
return localDataSource.observeSeriesWithContent(seriesId)
|
||||
}
|
||||
|
||||
private val _continueWatching: MutableStateFlow<List<Media>> = MutableStateFlow(emptyList())
|
||||
override val continueWatching: StateFlow<List<Media>> = _continueWatching.asStateFlow()
|
||||
|
||||
private val _nextUp: MutableStateFlow<List<Media>> = MutableStateFlow(emptyList())
|
||||
override val nextUp: StateFlow<List<Media>> = _nextUp.asStateFlow()
|
||||
|
||||
private val _latestLibraryContent: MutableStateFlow<Map<UUID, List<Media>>> = MutableStateFlow(emptyMap())
|
||||
override val latestLibraryContent: StateFlow<Map<UUID, List<Media>>> = _latestLibraryContent.asStateFlow()
|
||||
|
||||
init {
|
||||
scope.launch {
|
||||
loadFromCache()
|
||||
runCatching { ensureReady() }
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun loadFromCache() {
|
||||
val cache = homeCacheDataStore.data.first()
|
||||
if (cache.continueWatching.isNotEmpty()) {
|
||||
_continueWatching.value = cache.continueWatching.mapNotNull { it.toMedia() }
|
||||
}
|
||||
if (cache.nextUp.isNotEmpty()) {
|
||||
_nextUp.value = cache.nextUp.mapNotNull { it.toMedia() }
|
||||
}
|
||||
if (cache.latestLibraryContent.isNotEmpty()) {
|
||||
_latestLibraryContent.value = cache.latestLibraryContent.mapNotNull { (key, items) ->
|
||||
val uuid = runCatching { UUID.fromString(key) }.getOrNull() ?: return@mapNotNull null
|
||||
uuid to items.mapNotNull { it.toMedia() }
|
||||
}.toMap()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun persistHomeCache() {
|
||||
val cache = HomeCache(
|
||||
continueWatching = _continueWatching.value.map { it.toCachedItem() },
|
||||
nextUp = _nextUp.value.map { it.toCachedItem() },
|
||||
latestLibraryContent = _latestLibraryContent.value.map { (uuid, items) ->
|
||||
uuid.toString() to items.map { it.toCachedItem() }
|
||||
}.toMap()
|
||||
)
|
||||
homeCacheDataStore.updateData { cache }
|
||||
}
|
||||
|
||||
private fun Media.toCachedItem(): CachedMediaItem = when (this) {
|
||||
is Media.MovieMedia -> CachedMediaItem(type = "MOVIE", id = movieId.toString())
|
||||
is Media.SeriesMedia -> CachedMediaItem(type = "SERIES", id = seriesId.toString())
|
||||
is Media.SeasonMedia -> CachedMediaItem(type = "SEASON", id = seasonId.toString(), seriesId = seriesId.toString())
|
||||
is Media.EpisodeMedia -> CachedMediaItem(type = "EPISODE", id = episodeId.toString(), seriesId = seriesId.toString())
|
||||
}
|
||||
|
||||
private fun CachedMediaItem.toMedia(): Media? {
|
||||
val uuid = runCatching { UUID.fromString(id) }.getOrNull() ?: return null
|
||||
val seriesUuid = seriesId?.let { runCatching { UUID.fromString(it) }.getOrNull() }
|
||||
return when (type) {
|
||||
"MOVIE" -> Media.MovieMedia(movieId = uuid)
|
||||
"SERIES" -> Media.SeriesMedia(seriesId = uuid)
|
||||
"SEASON" -> Media.SeasonMedia(seasonId = uuid, seriesId = seriesUuid ?: return null)
|
||||
"EPISODE" -> Media.EpisodeMedia(episodeId = uuid, seriesId = seriesUuid ?: return null)
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun ensureReady() {
|
||||
if (ready.isCompleted) {
|
||||
ready.await() // rethrows if completed exceptionally
|
||||
return
|
||||
}
|
||||
|
||||
// Only the first caller runs the loading logic; others wait on the deferred.
|
||||
if (readyMutex.tryLock()) {
|
||||
try {
|
||||
if (ready.isCompleted) {
|
||||
ready.await()
|
||||
return
|
||||
}
|
||||
loadLibraries()
|
||||
loadContinueWatching()
|
||||
loadNextUp()
|
||||
loadLatestLibraryContent()
|
||||
persistHomeCache()
|
||||
_state.value = MediaRepositoryState.Ready
|
||||
initialLoadTimestamp = System.currentTimeMillis()
|
||||
ready.complete(Unit)
|
||||
} catch (t: Throwable) {
|
||||
_state.value = MediaRepositoryState.Error(t)
|
||||
ready.completeExceptionally(t)
|
||||
throw t
|
||||
} finally {
|
||||
readyMutex.unlock()
|
||||
}
|
||||
} else {
|
||||
ready.await()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun awaitReady() {
|
||||
ready.await()
|
||||
}
|
||||
|
||||
suspend fun loadLibraries() {
|
||||
val librariesItem = jellyfinApiClient.getLibraries()
|
||||
//TODO add support for playlists
|
||||
val filteredLibraries =
|
||||
librariesItem.filter { it.collectionType == CollectionType.MOVIES || it.collectionType == CollectionType.TVSHOWS }
|
||||
val emptyLibraries = filteredLibraries.map {
|
||||
it.toLibrary()
|
||||
}
|
||||
localDataSource.saveLibraries(emptyLibraries)
|
||||
offlineDataSource.saveLibraries(emptyLibraries)
|
||||
|
||||
val filledLibraries = emptyLibraries.map { library ->
|
||||
return@map loadLibrary(library)
|
||||
}
|
||||
val movies = filledLibraries.filter { it.type == CollectionType.MOVIES }.flatMap { it.movies.orEmpty() }
|
||||
localDataSource.saveMovies(movies)
|
||||
|
||||
val series = filledLibraries.filter { it.type == CollectionType.TVSHOWS }.flatMap { it.series.orEmpty() }
|
||||
localDataSource.saveSeries(series)
|
||||
}
|
||||
|
||||
suspend fun loadLibrary(library: Library): Library {
|
||||
val contentItem = jellyfinApiClient.getLibraryContent(library.id)
|
||||
when (library.type) {
|
||||
CollectionType.MOVIES -> {
|
||||
val movies = contentItem.map { it.toMovie(serverUrl(), library.id) }
|
||||
return library.copy(movies = movies)
|
||||
}
|
||||
CollectionType.TVSHOWS -> {
|
||||
val series = contentItem.map { it.toSeries(serverUrl(), library.id) }
|
||||
return library.copy(series = series)
|
||||
}
|
||||
else -> throw UnsupportedOperationException("Unsupported library type: ${library.type}")
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun loadMovie(movie: Movie) : Movie {
|
||||
val movieItem = jellyfinApiClient.getItemInfo(movie.id)
|
||||
?: throw RuntimeException("Movie not found")
|
||||
val updatedMovie = movieItem.toMovie(serverUrl(), movie.libraryId)
|
||||
localDataSource.saveMovies(listOf(updatedMovie))
|
||||
return updatedMovie
|
||||
}
|
||||
|
||||
suspend fun loadSeries(series: Series) : Series {
|
||||
val seriesItem = jellyfinApiClient.getItemInfo(series.id)
|
||||
?: throw RuntimeException("Series not found")
|
||||
val updatedSeries = seriesItem.toSeries(serverUrl(), series.libraryId)
|
||||
localDataSource.saveSeries(listOf(updatedSeries))
|
||||
return updatedSeries
|
||||
}
|
||||
|
||||
suspend fun loadContinueWatching() {
|
||||
val continueWatchingItems = jellyfinApiClient.getContinueWatching()
|
||||
val items = continueWatchingItems.mapNotNull { item ->
|
||||
when (item.type) {
|
||||
BaseItemKind.MOVIE -> Media.MovieMedia(movieId = item.id)
|
||||
BaseItemKind.EPISODE -> Media.EpisodeMedia(
|
||||
episodeId = item.id,
|
||||
seriesId = item.seriesId!!
|
||||
)
|
||||
else -> throw UnsupportedOperationException("Unsupported item type: ${item.type}")
|
||||
}
|
||||
}
|
||||
_continueWatching.value = items
|
||||
|
||||
//Load episodes, Movies are already loaded at this point.
|
||||
continueWatchingItems.forEach { item ->
|
||||
when (item.type) {
|
||||
BaseItemKind.EPISODE -> {
|
||||
val episode = item.toEpisode(serverUrl())
|
||||
localDataSource.saveEpisode(episode)
|
||||
}
|
||||
else -> { /* Do nothing */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun loadNextUp() {
|
||||
val nextUpItems = jellyfinApiClient.getNextUpEpisodes()
|
||||
val items = nextUpItems.map { item ->
|
||||
Media.EpisodeMedia(
|
||||
episodeId = item.id,
|
||||
seriesId = item.seriesId!!
|
||||
)
|
||||
}
|
||||
_nextUp.value = items
|
||||
|
||||
// Load episodes
|
||||
nextUpItems.forEach { item ->
|
||||
val episode = item.toEpisode(serverUrl())
|
||||
localDataSource.saveEpisode(episode)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun loadLatestLibraryContent() {
|
||||
// TODO Make libraries accessible in a field or something that is not this ugly.
|
||||
val librariesItem = jellyfinApiClient.getLibraries()
|
||||
val filterLibraries =
|
||||
librariesItem.filter { it.collectionType == CollectionType.MOVIES || it.collectionType == CollectionType.TVSHOWS }
|
||||
val latestLibraryContents = filterLibraries.associate { library ->
|
||||
val latestFromLibrary = jellyfinApiClient.getLatestFromLibrary(library.id)
|
||||
library.id to when (library.collectionType) {
|
||||
CollectionType.MOVIES -> {
|
||||
latestFromLibrary.map {
|
||||
val movie = it.toMovie(serverUrl(), library.id)
|
||||
Media.MovieMedia(movieId = movie.id)
|
||||
}
|
||||
}
|
||||
CollectionType.TVSHOWS -> {
|
||||
latestFromLibrary.map {
|
||||
when (it.type) {
|
||||
BaseItemKind.SERIES -> {
|
||||
val series = it.toSeries(serverUrl(), library.id)
|
||||
Media.SeriesMedia(seriesId = series.id)
|
||||
}
|
||||
BaseItemKind.SEASON -> {
|
||||
val season = it.toSeason(serverUrl())
|
||||
Media.SeasonMedia(seasonId = season.id, seriesId = season.seriesId)
|
||||
}
|
||||
BaseItemKind.EPISODE -> {
|
||||
val episode = it.toEpisode(serverUrl())
|
||||
Media.EpisodeMedia(episodeId = episode.id, seriesId = episode.seriesId)
|
||||
} else -> throw UnsupportedOperationException("Unsupported item type: ${it.type}")
|
||||
}
|
||||
}
|
||||
}
|
||||
else -> throw UnsupportedOperationException("Unsupported library type: ${library.collectionType}")
|
||||
}
|
||||
}
|
||||
_latestLibraryContent.value = latestLibraryContents
|
||||
|
||||
//TODO Load seasons and episodes, other types are already loaded at this point.
|
||||
}
|
||||
|
||||
private suspend fun ensureSeriesContentLoaded(seriesId: UUID) {
|
||||
awaitReady()
|
||||
// Skip if content is already cached in Room
|
||||
localDataSource.getSeriesWithContent(seriesId)?.takeIf { it.seasons.isNotEmpty() }?.let {
|
||||
return
|
||||
}
|
||||
|
||||
val series = this.series.value[seriesId] ?: throw RuntimeException("Series not found")
|
||||
|
||||
val emptySeasonsItem = jellyfinApiClient.getSeasons(seriesId)
|
||||
val emptySeasons = emptySeasonsItem.map { it.toSeason(serverUrl()) }
|
||||
val filledSeasons = emptySeasons.map { season ->
|
||||
val episodesItem = jellyfinApiClient.getEpisodesInSeason(seriesId, season.id)
|
||||
val episodes = episodesItem.map { it.toEpisode(serverUrl()) }
|
||||
season.copy(episodes = episodes)
|
||||
}
|
||||
val updatedSeries = series.copy(seasons = filledSeasons)
|
||||
localDataSource.saveSeries(listOf(updatedSeries))
|
||||
localDataSource.saveSeriesContent(updatedSeries)
|
||||
}
|
||||
|
||||
override suspend fun updateWatchProgress(mediaId: UUID, positionMs: Long, durationMs: Long) {
|
||||
if (durationMs <= 0) return
|
||||
val progressPercent = (positionMs.toDouble() / durationMs.toDouble()) * 100.0
|
||||
val watched = progressPercent >= 90.0
|
||||
// Write to Room — the reactive Flows propagate changes to UI automatically
|
||||
localDataSource.updateWatchProgress(mediaId, progressPercent, watched)
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val REFRESH_MIN_INTERVAL_MS = 30_000L
|
||||
}
|
||||
|
||||
override suspend fun refreshHomeData() {
|
||||
awaitReady()
|
||||
// Skip refresh if the initial load (or last refresh) just happened
|
||||
val elapsed = System.currentTimeMillis() - initialLoadTimestamp
|
||||
if (elapsed < REFRESH_MIN_INTERVAL_MS) return
|
||||
|
||||
loadLibraries()
|
||||
loadContinueWatching()
|
||||
loadNextUp()
|
||||
loadLatestLibraryContent()
|
||||
persistHomeCache()
|
||||
initialLoadTimestamp = System.currentTimeMillis()
|
||||
}
|
||||
|
||||
private suspend fun serverUrl(): String {
|
||||
return userSessionRepository.serverUrl.first()
|
||||
}
|
||||
|
||||
private fun BaseItemDto.toLibrary(): Library {
|
||||
return when (this.collectionType) {
|
||||
CollectionType.MOVIES -> Library(
|
||||
id = this.id,
|
||||
name = this.name!!,
|
||||
type = CollectionType.MOVIES,
|
||||
movies = emptyList()
|
||||
)
|
||||
CollectionType.TVSHOWS -> Library(
|
||||
id = this.id,
|
||||
name = this.name!!,
|
||||
type = CollectionType.TVSHOWS,
|
||||
series = emptyList()
|
||||
)
|
||||
else -> throw UnsupportedOperationException("Unsupported library type: ${this.collectionType}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun BaseItemDto.toMovie(serverUrl: String, libraryId: UUID) : Movie {
|
||||
return Movie(
|
||||
id = this.id,
|
||||
libraryId = libraryId,
|
||||
title = this.name ?: "Unknown title",
|
||||
progress = this.userData!!.playedPercentage,
|
||||
watched = this.userData!!.played,
|
||||
year = this.productionYear?.toString() ?: premiereDate?.year?.toString().orEmpty(),
|
||||
rating = this.officialRating
|
||||
?: "NR",
|
||||
runtime = formatRuntime(this.runTimeTicks),
|
||||
synopsis = this.overview ?: "No synopsis available",
|
||||
format = container?.uppercase() ?: "VIDEO",
|
||||
heroImageUrl = JellyfinImageHelper.toImageUrl(
|
||||
url = serverUrl,
|
||||
itemId = this.id,
|
||||
type = ImageType.PRIMARY
|
||||
),
|
||||
subtitles = "ENG",
|
||||
audioTrack = "ENG",
|
||||
cast = emptyList()
|
||||
)
|
||||
}
|
||||
|
||||
private fun BaseItemDto.toSeries(serverUrl: String, libraryId: UUID): Series {
|
||||
return Series(
|
||||
id = this.id,
|
||||
libraryId = libraryId,
|
||||
name = this.name ?: "Unknown",
|
||||
synopsis = this.overview ?: "No synopsis available",
|
||||
year = this.productionYear?.toString()
|
||||
?: this.premiereDate?.year?.toString().orEmpty(),
|
||||
heroImageUrl = JellyfinImageHelper.toImageUrl(
|
||||
url = serverUrl,
|
||||
itemId = this.id,
|
||||
type = ImageType.PRIMARY
|
||||
),
|
||||
unwatchedEpisodeCount = this.userData!!.unplayedItemCount!!,
|
||||
seasonCount = this.childCount!!,
|
||||
seasons = emptyList(),
|
||||
cast = emptyList()
|
||||
)
|
||||
}
|
||||
|
||||
private fun BaseItemDto.toSeason(serverUrl: String): Season {
|
||||
return Season(
|
||||
id = this.id,
|
||||
seriesId = this.seriesId!!,
|
||||
name = this.name ?: "Unknown",
|
||||
index = this.indexNumber ?: 0,
|
||||
unwatchedEpisodeCount = this.userData!!.unplayedItemCount!!,
|
||||
episodeCount = this.childCount!!,
|
||||
episodes = emptyList()
|
||||
)
|
||||
}
|
||||
|
||||
private fun BaseItemDto.toEpisode(serverUrl: String): Episode {
|
||||
val releaseDate = formatReleaseDate(premiereDate, productionYear)
|
||||
val rating = officialRating ?: "NR"
|
||||
val runtime = formatRuntime(runTimeTicks)
|
||||
val format = container?.uppercase() ?: "VIDEO"
|
||||
val synopsis = overview ?: "No synopsis available."
|
||||
val heroImageUrl = id?.let { itemId ->
|
||||
JellyfinImageHelper.toImageUrl(
|
||||
url = serverUrl,
|
||||
itemId = itemId,
|
||||
type = ImageType.PRIMARY
|
||||
)
|
||||
} ?: ""
|
||||
return Episode(
|
||||
id = id,
|
||||
seriesId = seriesId!!,
|
||||
seasonId = parentId!!,
|
||||
title = name ?: "Unknown title",
|
||||
index = indexNumber!!,
|
||||
releaseDate = releaseDate,
|
||||
rating = rating,
|
||||
runtime = runtime,
|
||||
progress = userData!!.playedPercentage,
|
||||
watched = userData!!.played,
|
||||
format = format,
|
||||
synopsis = synopsis,
|
||||
heroImageUrl = heroImageUrl,
|
||||
cast = emptyList()
|
||||
)
|
||||
}
|
||||
|
||||
private fun formatReleaseDate(date: LocalDateTime?, fallbackYear: Int?): String {
|
||||
if (date == null) {
|
||||
return fallbackYear?.toString() ?: "—"
|
||||
}
|
||||
val formatter = DateTimeFormatter.ofPattern("MMM d, yyyy", Locale.getDefault())
|
||||
return date.toLocalDate().format(formatter)
|
||||
}
|
||||
|
||||
private fun formatRuntime(ticks: Long?): String {
|
||||
if (ticks == null || ticks <= 0) return "—"
|
||||
val totalSeconds = ticks / 10_000_000
|
||||
val hours = TimeUnit.SECONDS.toHours(totalSeconds)
|
||||
val minutes = TimeUnit.SECONDS.toMinutes(totalSeconds) % 60
|
||||
return if (hours > 0) {
|
||||
"${hours}h ${minutes}m"
|
||||
} else {
|
||||
"${minutes}m"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package hu.bbara.purefin.core.data
|
||||
|
||||
import hu.bbara.purefin.core.model.Episode
|
||||
import hu.bbara.purefin.core.model.Library
|
||||
import hu.bbara.purefin.core.model.Media
|
||||
import hu.bbara.purefin.core.model.MediaRepositoryState
|
||||
import hu.bbara.purefin.core.model.Movie
|
||||
import hu.bbara.purefin.core.model.Series
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import java.util.UUID
|
||||
|
||||
interface MediaRepository {
|
||||
|
||||
val libraries: StateFlow<List<Library>>
|
||||
val movies: StateFlow<Map<UUID, Movie>>
|
||||
val series: StateFlow<Map<UUID, Series>>
|
||||
val episodes: StateFlow<Map<UUID, Episode>>
|
||||
val state: StateFlow<MediaRepositoryState>
|
||||
|
||||
val continueWatching: StateFlow<List<Media>>
|
||||
val nextUp: StateFlow<List<Media>>
|
||||
val latestLibraryContent: StateFlow<Map<UUID, List<Media>>>
|
||||
|
||||
fun observeSeriesWithContent(seriesId: UUID): Flow<Series?>
|
||||
|
||||
suspend fun ensureReady()
|
||||
|
||||
suspend fun updateWatchProgress(mediaId: UUID, positionMs: Long, durationMs: Long)
|
||||
suspend fun refreshHomeData()
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package hu.bbara.purefin.core.data
|
||||
|
||||
import dagger.Binds
|
||||
import dagger.Module
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import hu.bbara.purefin.core.data.local.room.OfflineRepository
|
||||
import hu.bbara.purefin.core.data.local.room.OnlineRepository
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
abstract class MediaRepositoryModule {
|
||||
|
||||
@Binds
|
||||
@OnlineRepository
|
||||
abstract fun bindOnlineMediaRepository(impl: InMemoryMediaRepository): MediaRepository
|
||||
|
||||
@Binds
|
||||
@OfflineRepository
|
||||
abstract fun bindOfflineMediaRepository(impl: OfflineMediaRepository): MediaRepository
|
||||
|
||||
// Default binding delegates to online/offline based on user preference
|
||||
@Binds
|
||||
abstract fun bindDefaultMediaRepository(impl: ActiveMediaRepository): MediaRepository
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package hu.bbara.purefin.core.data
|
||||
|
||||
import hu.bbara.purefin.core.data.local.room.OfflineDatabase
|
||||
import hu.bbara.purefin.core.data.local.room.OfflineRoomMediaLocalDataSource
|
||||
import hu.bbara.purefin.core.model.Episode
|
||||
import hu.bbara.purefin.core.model.Library
|
||||
import hu.bbara.purefin.core.model.Media
|
||||
import hu.bbara.purefin.core.model.MediaRepositoryState
|
||||
import hu.bbara.purefin.core.model.Movie
|
||||
import hu.bbara.purefin.core.model.Series
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Offline media repository for managing downloaded content.
|
||||
* This repository only accesses the local offline database and does not make network calls.
|
||||
*/
|
||||
@Singleton
|
||||
class OfflineMediaRepository @Inject constructor(
|
||||
@OfflineDatabase private val localDataSource: OfflineRoomMediaLocalDataSource
|
||||
) : MediaRepository {
|
||||
|
||||
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
|
||||
|
||||
// Offline repository is always ready (no network loading required)
|
||||
private val _state: MutableStateFlow<MediaRepositoryState> = MutableStateFlow(MediaRepositoryState.Ready)
|
||||
override val state: StateFlow<MediaRepositoryState> = _state.asStateFlow()
|
||||
|
||||
override val libraries: StateFlow<List<Library>> = localDataSource.librariesFlow
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyList())
|
||||
|
||||
override val movies: StateFlow<Map<UUID, Movie>> = localDataSource.moviesFlow
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyMap())
|
||||
|
||||
override val series: StateFlow<Map<UUID, Series>> = localDataSource.seriesFlow
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyMap())
|
||||
|
||||
override val episodes: StateFlow<Map<UUID, Episode>> = localDataSource.episodesFlow
|
||||
.stateIn(scope, SharingStarted.Eagerly, emptyMap())
|
||||
|
||||
// Offline mode doesn't support these server-side features
|
||||
override val continueWatching: StateFlow<List<Media>> = MutableStateFlow(emptyList())
|
||||
override val nextUp: StateFlow<List<Media>> = MutableStateFlow(emptyList())
|
||||
override val latestLibraryContent: StateFlow<Map<UUID, List<Media>>> = MutableStateFlow(emptyMap())
|
||||
|
||||
override fun observeSeriesWithContent(seriesId: UUID): Flow<Series?> {
|
||||
return localDataSource.observeSeriesWithContent(seriesId)
|
||||
}
|
||||
|
||||
override suspend fun ensureReady() {
|
||||
// Offline repository is always ready - no initialization needed
|
||||
}
|
||||
|
||||
override suspend fun updateWatchProgress(mediaId: UUID, positionMs: Long, durationMs: Long) {
|
||||
if (durationMs <= 0) return
|
||||
val progressPercent = (positionMs.toDouble() / durationMs.toDouble()) * 100.0
|
||||
val watched = progressPercent >= 90.0
|
||||
// Write to offline database - the reactive Flows propagate changes to UI automatically
|
||||
localDataSource.updateWatchProgress(mediaId, progressPercent, watched)
|
||||
}
|
||||
|
||||
override suspend fun refreshHomeData() {
|
||||
// No-op for offline repository - no network refresh available
|
||||
}
|
||||
}
|
||||
17
core/data/src/main/java/hu/bbara/purefin/core/data/cache/HomeCache.kt
vendored
Normal file
17
core/data/src/main/java/hu/bbara/purefin/core/data/cache/HomeCache.kt
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
package hu.bbara.purefin.core.data.cache
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class CachedMediaItem(
|
||||
val type: String,
|
||||
val id: String,
|
||||
val seriesId: String? = null
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class HomeCache(
|
||||
val continueWatching: List<CachedMediaItem> = emptyList(),
|
||||
val nextUp: List<CachedMediaItem> = emptyList(),
|
||||
val latestLibraryContent: Map<String, List<CachedMediaItem>> = emptyMap()
|
||||
)
|
||||
32
core/data/src/main/java/hu/bbara/purefin/core/data/cache/HomeCacheModule.kt
vendored
Normal file
32
core/data/src/main/java/hu/bbara/purefin/core/data/cache/HomeCacheModule.kt
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
package hu.bbara.purefin.core.data.cache
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.core.DataStoreFactory
|
||||
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
|
||||
import androidx.datastore.dataStoreFile
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
class HomeCacheModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideHomeCacheDataStore(
|
||||
@ApplicationContext context: Context
|
||||
): DataStore<HomeCache> {
|
||||
return DataStoreFactory.create(
|
||||
serializer = HomeCacheSerializer,
|
||||
produceFile = { context.dataStoreFile("home_cache.json") },
|
||||
corruptionHandler = ReplaceFileCorruptionHandler(
|
||||
produceNewData = { HomeCacheSerializer.defaultValue }
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
30
core/data/src/main/java/hu/bbara/purefin/core/data/cache/HomeCacheSerializer.kt
vendored
Normal file
30
core/data/src/main/java/hu/bbara/purefin/core/data/cache/HomeCacheSerializer.kt
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
package hu.bbara.purefin.core.data.cache
|
||||
|
||||
import androidx.datastore.core.CorruptionException
|
||||
import androidx.datastore.core.Serializer
|
||||
import kotlinx.serialization.SerializationException
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
object HomeCacheSerializer : Serializer<HomeCache> {
|
||||
override val defaultValue: HomeCache
|
||||
get() = HomeCache()
|
||||
|
||||
override suspend fun readFrom(input: InputStream): HomeCache {
|
||||
try {
|
||||
return Json.decodeFromString<HomeCache>(
|
||||
input.readBytes().decodeToString()
|
||||
)
|
||||
} catch (serialization: SerializationException) {
|
||||
throw CorruptionException("proto", serialization)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun writeTo(t: HomeCache, output: OutputStream) {
|
||||
output.write(
|
||||
Json.encodeToString(t)
|
||||
.encodeToByteArray()
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
package hu.bbara.purefin.core.data.client
|
||||
|
||||
import android.media.MediaCodecList
|
||||
import android.util.Log
|
||||
import org.jellyfin.sdk.model.api.DeviceProfile
|
||||
import org.jellyfin.sdk.model.api.DirectPlayProfile
|
||||
import org.jellyfin.sdk.model.api.DlnaProfileType
|
||||
import org.jellyfin.sdk.model.api.SubtitleDeliveryMethod
|
||||
import org.jellyfin.sdk.model.api.SubtitleProfile
|
||||
|
||||
/**
|
||||
* Creates a DeviceProfile for Android devices with proper codec support detection.
|
||||
* This prevents playback failures by requesting transcoding for unsupported formats like DTS-HD.
|
||||
*/
|
||||
object AndroidDeviceProfile {
|
||||
|
||||
fun create(): DeviceProfile {
|
||||
// Debug: Log all available decoders
|
||||
CodecDebugHelper.logAvailableDecoders()
|
||||
|
||||
val audioCodecs = getAudioCodecs()
|
||||
val videoCodecs = getVideoCodecs()
|
||||
|
||||
Log.d("AndroidDeviceProfile", "Supported audio codecs: ${audioCodecs.joinToString()}")
|
||||
Log.d("AndroidDeviceProfile", "Supported video codecs: ${videoCodecs.joinToString()}")
|
||||
|
||||
// Check specifically for DTS
|
||||
val hasDTS = CodecDebugHelper.hasDecoderFor("audio/vnd.dts")
|
||||
val hasDTSHD = CodecDebugHelper.hasDecoderFor("audio/vnd.dts.hd")
|
||||
Log.d("AndroidDeviceProfile", "Has DTS decoder: $hasDTS, Has DTS-HD decoder: $hasDTSHD")
|
||||
|
||||
return DeviceProfile(
|
||||
name = "Android",
|
||||
maxStaticBitrate = 100_000_000,
|
||||
maxStreamingBitrate = 100_000_000,
|
||||
|
||||
// Direct play profiles - what we can play natively
|
||||
// By specifying supported codecs, Jellyfin will transcode unsupported formats like DTS-HD
|
||||
directPlayProfiles = listOf(
|
||||
DirectPlayProfile(
|
||||
type = DlnaProfileType.VIDEO,
|
||||
container = "mp4,m4v,mkv,webm",
|
||||
videoCodec = videoCodecs.joinToString(","),
|
||||
audioCodec = audioCodecs.joinToString(",")
|
||||
)
|
||||
),
|
||||
|
||||
// Empty transcoding profiles - Jellyfin will use its defaults
|
||||
transcodingProfiles = emptyList(),
|
||||
|
||||
codecProfiles = emptyList(),
|
||||
|
||||
subtitleProfiles = listOf(
|
||||
// Prefer EMBED so subtitles stay in the container — this gives
|
||||
// correct cues after seeking (Media3 parses them at extraction time).
|
||||
SubtitleProfile("srt", SubtitleDeliveryMethod.EMBED),
|
||||
SubtitleProfile("ass", SubtitleDeliveryMethod.EMBED),
|
||||
SubtitleProfile("ssa", SubtitleDeliveryMethod.EMBED),
|
||||
SubtitleProfile("subrip", SubtitleDeliveryMethod.EMBED),
|
||||
SubtitleProfile("sub", SubtitleDeliveryMethod.EMBED),
|
||||
// EXTERNAL fallback for when embedding isn't possible
|
||||
SubtitleProfile("srt", SubtitleDeliveryMethod.EXTERNAL),
|
||||
SubtitleProfile("ass", SubtitleDeliveryMethod.EXTERNAL),
|
||||
SubtitleProfile("ssa", SubtitleDeliveryMethod.EXTERNAL),
|
||||
SubtitleProfile("vtt", SubtitleDeliveryMethod.EXTERNAL),
|
||||
SubtitleProfile("sub", SubtitleDeliveryMethod.EXTERNAL)
|
||||
),
|
||||
|
||||
containerProfiles = emptyList()
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of supported audio codecs on this device.
|
||||
* Excludes unsupported formats like DTS, DTS-HD, TrueHD which commonly cause playback failures.
|
||||
*/
|
||||
private fun getAudioCodecs(): List<String> {
|
||||
val supportedCodecs = mutableListOf<String>()
|
||||
|
||||
// Common codecs supported on most Android devices
|
||||
val commonCodecs = listOf(
|
||||
"aac" to android.media.MediaFormat.MIMETYPE_AUDIO_AAC,
|
||||
"mp3" to android.media.MediaFormat.MIMETYPE_AUDIO_MPEG,
|
||||
"ac3" to android.media.MediaFormat.MIMETYPE_AUDIO_AC3,
|
||||
"eac3" to android.media.MediaFormat.MIMETYPE_AUDIO_EAC3,
|
||||
"flac" to android.media.MediaFormat.MIMETYPE_AUDIO_FLAC,
|
||||
"vorbis" to android.media.MediaFormat.MIMETYPE_AUDIO_VORBIS,
|
||||
"opus" to android.media.MediaFormat.MIMETYPE_AUDIO_OPUS
|
||||
)
|
||||
|
||||
for ((codecName, mimeType) in commonCodecs) {
|
||||
if (isCodecSupported(mimeType)) {
|
||||
supportedCodecs.add(codecName)
|
||||
}
|
||||
}
|
||||
|
||||
// AAC is mandatory on Android - ensure it's always included
|
||||
if (!supportedCodecs.contains("aac")) {
|
||||
supportedCodecs.add("aac")
|
||||
}
|
||||
|
||||
return supportedCodecs
|
||||
}
|
||||
|
||||
/**
|
||||
* Get list of supported video codecs on this device.
|
||||
*/
|
||||
private fun getVideoCodecs(): List<String> {
|
||||
val supportedCodecs = mutableListOf<String>()
|
||||
|
||||
val commonCodecs = listOf(
|
||||
"h264" to android.media.MediaFormat.MIMETYPE_VIDEO_AVC,
|
||||
"hevc" to android.media.MediaFormat.MIMETYPE_VIDEO_HEVC,
|
||||
"vp9" to android.media.MediaFormat.MIMETYPE_VIDEO_VP9,
|
||||
"vp8" to android.media.MediaFormat.MIMETYPE_VIDEO_VP8,
|
||||
"mpeg4" to android.media.MediaFormat.MIMETYPE_VIDEO_MPEG4,
|
||||
"av1" to android.media.MediaFormat.MIMETYPE_VIDEO_AV1
|
||||
)
|
||||
|
||||
for ((codecName, mimeType) in commonCodecs) {
|
||||
if (isCodecSupported(mimeType)) {
|
||||
supportedCodecs.add(codecName)
|
||||
}
|
||||
}
|
||||
|
||||
// H.264 is mandatory on Android - ensure it's always included
|
||||
if (!supportedCodecs.contains("h264")) {
|
||||
supportedCodecs.add("h264")
|
||||
}
|
||||
|
||||
return supportedCodecs
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a specific decoder (not encoder) is supported on this device.
|
||||
*/
|
||||
private fun isCodecSupported(mimeType: String): Boolean {
|
||||
return try {
|
||||
val codecList = MediaCodecList(MediaCodecList.ALL_CODECS)
|
||||
codecList.codecInfos.any { codecInfo ->
|
||||
!codecInfo.isEncoder &&
|
||||
codecInfo.supportedTypes.any { it.equals(mimeType, ignoreCase = true) }
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
// If we can't determine, assume not supported
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package hu.bbara.purefin.core.data.client
|
||||
|
||||
import android.media.MediaCodecList
|
||||
import android.util.Log
|
||||
|
||||
/**
|
||||
* Helper to debug available audio/video codecs on the device.
|
||||
*/
|
||||
object CodecDebugHelper {
|
||||
|
||||
private const val TAG = "CodecDebug"
|
||||
|
||||
/**
|
||||
* Logs all available decoders on this device.
|
||||
* Call this to understand what your device can actually decode.
|
||||
*/
|
||||
fun logAvailableDecoders() {
|
||||
try {
|
||||
val codecList = MediaCodecList(MediaCodecList.ALL_CODECS)
|
||||
Log.d(TAG, "=== Available Audio Decoders ===")
|
||||
|
||||
codecList.codecInfos
|
||||
.filter { !it.isEncoder }
|
||||
.forEach { codecInfo ->
|
||||
codecInfo.supportedTypes.forEach { mimeType ->
|
||||
if (mimeType.startsWith("audio/")) {
|
||||
Log.d(TAG, "${codecInfo.name}: $mimeType")
|
||||
if (mimeType.contains("dts", ignoreCase = true) ||
|
||||
mimeType.contains("truehd", ignoreCase = true)) {
|
||||
Log.w(TAG, " ^^^ DTS/TrueHD decoder found! ^^^")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Log.d(TAG, "=== Available Video Decoders ===")
|
||||
codecList.codecInfos
|
||||
.filter { !it.isEncoder }
|
||||
.forEach { codecInfo ->
|
||||
codecInfo.supportedTypes.forEach { mimeType ->
|
||||
if (mimeType.startsWith("video/")) {
|
||||
Log.d(TAG, "${codecInfo.name}: $mimeType")
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Failed to list codecs", e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a specific MIME type has a decoder available.
|
||||
*/
|
||||
fun hasDecoderFor(mimeType: String): Boolean {
|
||||
return try {
|
||||
val codecList = MediaCodecList(MediaCodecList.ALL_CODECS)
|
||||
codecList.codecInfos.any { codecInfo ->
|
||||
!codecInfo.isEncoder &&
|
||||
codecInfo.supportedTypes.any { it.equals(mimeType, ignoreCase = true) }
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,332 @@
|
||||
package hu.bbara.purefin.core.data.client
|
||||
|
||||
import android.content.Context
|
||||
import android.util.Log
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import hu.bbara.purefin.core.data.session.UserSessionRepository
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.jellyfin.sdk.api.client.Response
|
||||
import org.jellyfin.sdk.api.client.extensions.authenticateUserByName
|
||||
import org.jellyfin.sdk.api.client.extensions.itemsApi
|
||||
import org.jellyfin.sdk.api.client.extensions.mediaInfoApi
|
||||
import org.jellyfin.sdk.api.client.extensions.playStateApi
|
||||
import org.jellyfin.sdk.api.client.extensions.tvShowsApi
|
||||
import org.jellyfin.sdk.api.client.extensions.userApi
|
||||
import org.jellyfin.sdk.api.client.extensions.userLibraryApi
|
||||
import org.jellyfin.sdk.api.client.extensions.userViewsApi
|
||||
import org.jellyfin.sdk.api.client.extensions.videosApi
|
||||
import org.jellyfin.sdk.createJellyfin
|
||||
import org.jellyfin.sdk.model.ClientInfo
|
||||
import org.jellyfin.sdk.model.api.BaseItemDto
|
||||
import org.jellyfin.sdk.model.api.BaseItemDtoQueryResult
|
||||
import org.jellyfin.sdk.model.api.BaseItemKind
|
||||
import org.jellyfin.sdk.model.api.CollectionType
|
||||
import org.jellyfin.sdk.model.api.ItemFields
|
||||
import org.jellyfin.sdk.model.api.MediaSourceInfo
|
||||
import org.jellyfin.sdk.model.api.PlayMethod
|
||||
import org.jellyfin.sdk.model.api.PlaybackInfoDto
|
||||
import org.jellyfin.sdk.model.api.PlaybackOrder
|
||||
import org.jellyfin.sdk.model.api.PlaybackProgressInfo
|
||||
import org.jellyfin.sdk.model.api.PlaybackStartInfo
|
||||
import org.jellyfin.sdk.model.api.PlaybackStopInfo
|
||||
import org.jellyfin.sdk.model.api.RepeatMode
|
||||
import org.jellyfin.sdk.model.api.request.GetItemsRequest
|
||||
import org.jellyfin.sdk.model.api.request.GetNextUpRequest
|
||||
import org.jellyfin.sdk.model.api.request.GetResumeItemsRequest
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class JellyfinApiClient @Inject constructor(
|
||||
@ApplicationContext private val applicationContext: Context,
|
||||
private val userSessionRepository: UserSessionRepository,
|
||||
) {
|
||||
private val jellyfin = createJellyfin {
|
||||
context = applicationContext
|
||||
clientInfo = ClientInfo(name = "Purefin", version = "0.0.1")
|
||||
}
|
||||
|
||||
private val api = jellyfin.createApi()
|
||||
|
||||
private suspend fun getUserId(): UUID? = userSessionRepository.userId.first()
|
||||
|
||||
private suspend fun ensureConfigured(): Boolean {
|
||||
val serverUrl = userSessionRepository.serverUrl.first().trim()
|
||||
val accessToken = userSessionRepository.accessToken.first().trim()
|
||||
if (serverUrl.isBlank() || accessToken.isBlank()) {
|
||||
userSessionRepository.setLoggedIn(false)
|
||||
return false
|
||||
}
|
||||
api.update(baseUrl = serverUrl, accessToken = accessToken)
|
||||
return true
|
||||
}
|
||||
|
||||
suspend fun login(url: String, username: String, password: String): Boolean = withContext(Dispatchers.IO) {
|
||||
val trimmedUrl = url.trim()
|
||||
if (trimmedUrl.isBlank()) {
|
||||
return@withContext false
|
||||
}
|
||||
api.update(baseUrl = trimmedUrl)
|
||||
try {
|
||||
val response = api.userApi.authenticateUserByName(username = username, password = password)
|
||||
val authResult = response.content
|
||||
|
||||
val token = authResult.accessToken ?: return@withContext false
|
||||
val userId = authResult.user?.id ?: return@withContext false
|
||||
userSessionRepository.setAccessToken(accessToken = token)
|
||||
userSessionRepository.setUserId(userId)
|
||||
userSessionRepository.setLoggedIn(true)
|
||||
api.update(accessToken = token)
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
Log.e("JellyfinApiClient", "Login failed", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun updateApiClient() = withContext(Dispatchers.IO) {
|
||||
ensureConfigured()
|
||||
}
|
||||
|
||||
suspend fun getLibraries(): List<BaseItemDto> = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) {
|
||||
return@withContext emptyList()
|
||||
}
|
||||
val response = api.userViewsApi.getUserViews(
|
||||
userId = getUserId(),
|
||||
presetViews = listOf(CollectionType.MOVIES, CollectionType.TVSHOWS),
|
||||
includeHidden = false,
|
||||
)
|
||||
Log.d("getLibraries", response.content.toString())
|
||||
response.content.items
|
||||
}
|
||||
|
||||
private val itemFields =
|
||||
listOf(
|
||||
ItemFields.CHILD_COUNT,
|
||||
ItemFields.PARENT_ID,
|
||||
ItemFields.DATE_LAST_REFRESHED,
|
||||
ItemFields.OVERVIEW,
|
||||
ItemFields.SEASON_USER_DATA
|
||||
)
|
||||
|
||||
suspend fun getLibraryContent(libraryId: UUID): List<BaseItemDto> = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) {
|
||||
return@withContext emptyList()
|
||||
}
|
||||
val getItemsRequest = GetItemsRequest(
|
||||
userId = getUserId(),
|
||||
enableImages = false,
|
||||
parentId = libraryId,
|
||||
fields = itemFields,
|
||||
enableUserData = true,
|
||||
includeItemTypes = listOf(BaseItemKind.MOVIE, BaseItemKind.SERIES),
|
||||
recursive = true,
|
||||
)
|
||||
val response = api.itemsApi.getItems(getItemsRequest)
|
||||
Log.d("getLibraryContent", response.content.toString())
|
||||
response.content.items
|
||||
}
|
||||
|
||||
suspend fun getContinueWatching(): List<BaseItemDto> = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) {
|
||||
return@withContext emptyList()
|
||||
}
|
||||
val userId = getUserId()
|
||||
if (userId == null) {
|
||||
return@withContext emptyList()
|
||||
}
|
||||
val getResumeItemsRequest = GetResumeItemsRequest(
|
||||
userId = userId,
|
||||
fields = itemFields,
|
||||
includeItemTypes = listOf(BaseItemKind.MOVIE, BaseItemKind.EPISODE),
|
||||
enableUserData = true,
|
||||
startIndex = 0,
|
||||
)
|
||||
val response: Response<BaseItemDtoQueryResult> = api.itemsApi.getResumeItems(getResumeItemsRequest)
|
||||
Log.d("getContinueWatching", response.content.toString())
|
||||
response.content.items
|
||||
}
|
||||
|
||||
suspend fun getNextUpEpisodes(): List<BaseItemDto> = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) {
|
||||
throw IllegalStateException("Not configured")
|
||||
}
|
||||
val getNextUpRequest = GetNextUpRequest(
|
||||
userId = getUserId(),
|
||||
fields = itemFields,
|
||||
enableResumable = false,
|
||||
)
|
||||
val result = api.tvShowsApi.getNextUp(getNextUpRequest)
|
||||
Log.d("getNextUpEpisodes", result.content.toString())
|
||||
result.content.items
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the latest media items from a specified library including Movie, Episode, Season.
|
||||
*
|
||||
* @param libraryId The UUID of the library to fetch from
|
||||
* @return A list of [BaseItemDto] representing the latest media items that includes Movie, Episode, Season, or an empty list if not configured
|
||||
*/
|
||||
suspend fun getLatestFromLibrary(libraryId: UUID): List<BaseItemDto> = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) {
|
||||
return@withContext emptyList()
|
||||
}
|
||||
val response = api.userLibraryApi.getLatestMedia(
|
||||
userId = getUserId(),
|
||||
parentId = libraryId,
|
||||
fields = itemFields,
|
||||
includeItemTypes = listOf(BaseItemKind.MOVIE, BaseItemKind.EPISODE, BaseItemKind.SEASON),
|
||||
limit = 10
|
||||
)
|
||||
Log.d("getLatestFromLibrary", response.content.toString())
|
||||
response.content
|
||||
}
|
||||
|
||||
suspend fun getItemInfo(mediaId: UUID): BaseItemDto? = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) {
|
||||
return@withContext null
|
||||
}
|
||||
val result = api.userLibraryApi.getItem(
|
||||
itemId = mediaId,
|
||||
userId = getUserId()
|
||||
)
|
||||
Log.d("getItemInfo", result.content.toString())
|
||||
result.content
|
||||
}
|
||||
|
||||
suspend fun getSeasons(seriesId: UUID): List<BaseItemDto> = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) {
|
||||
return@withContext emptyList()
|
||||
}
|
||||
val result = api.tvShowsApi.getSeasons(
|
||||
userId = getUserId(),
|
||||
seriesId = seriesId,
|
||||
fields = itemFields,
|
||||
enableUserData = true
|
||||
)
|
||||
Log.d("getSeasons", result.content.toString())
|
||||
result.content.items
|
||||
}
|
||||
|
||||
suspend fun getEpisodesInSeason(seriesId: UUID, seasonId: UUID): List<BaseItemDto> = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) {
|
||||
return@withContext emptyList()
|
||||
}
|
||||
val result = api.tvShowsApi.getEpisodes(
|
||||
userId = getUserId(),
|
||||
seriesId = seriesId,
|
||||
seasonId = seasonId,
|
||||
fields = itemFields,
|
||||
enableUserData = true
|
||||
)
|
||||
Log.d("getEpisodesInSeason", result.content.toString())
|
||||
result.content.items
|
||||
}
|
||||
|
||||
suspend fun getNextEpisodes(episodeId: UUID, count: Int = 10): List<BaseItemDto> = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) {
|
||||
return@withContext emptyList()
|
||||
}
|
||||
// TODO pass complete Episode object not only an id
|
||||
val episodeInfo = getItemInfo(episodeId) ?: return@withContext emptyList()
|
||||
val seriesId = episodeInfo.seriesId ?: return@withContext emptyList()
|
||||
val nextUpEpisodesResult = api.tvShowsApi.getEpisodes(
|
||||
userId = getUserId(),
|
||||
seriesId = seriesId,
|
||||
enableUserData = true,
|
||||
startItemId = episodeId,
|
||||
limit = count + 1
|
||||
)
|
||||
//Remove first element as we need only the next episodes
|
||||
val nextUpEpisodes = nextUpEpisodesResult.content.items.drop(1)
|
||||
Log.d("getNextEpisodes", nextUpEpisodes.toString())
|
||||
nextUpEpisodes
|
||||
}
|
||||
|
||||
suspend fun getMediaSources(mediaId: UUID): List<MediaSourceInfo> = withContext(Dispatchers.IO) {
|
||||
val result = api.mediaInfoApi
|
||||
.getPostedPlaybackInfo(
|
||||
mediaId,
|
||||
PlaybackInfoDto(
|
||||
userId = getUserId(),
|
||||
deviceProfile = AndroidDeviceProfile.create(),
|
||||
maxStreamingBitrate = 100_000_000,
|
||||
),
|
||||
)
|
||||
Log.d("getMediaSources", result.toString())
|
||||
result.content.mediaSources
|
||||
}
|
||||
|
||||
suspend fun getMediaPlaybackUrl(mediaId: UUID, mediaSource: MediaSourceInfo): String? = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) {
|
||||
return@withContext null
|
||||
}
|
||||
|
||||
// Check if transcoding is required based on the MediaSourceInfo from getMediaSources
|
||||
val shouldTranscode = mediaSource.supportsTranscoding == true &&
|
||||
(mediaSource.supportsDirectPlay == false || mediaSource.transcodingUrl != null)
|
||||
|
||||
val url = if (shouldTranscode && !mediaSource.transcodingUrl.isNullOrBlank()) {
|
||||
// Use transcoding URL
|
||||
val baseUrl = userSessionRepository.serverUrl.first().trim().trimEnd('/')
|
||||
"$baseUrl${mediaSource.transcodingUrl}"
|
||||
} else {
|
||||
// Use direct play URL
|
||||
api.videosApi.getVideoStreamUrl(
|
||||
itemId = mediaId,
|
||||
static = true,
|
||||
mediaSourceId = mediaSource.id,
|
||||
)
|
||||
}
|
||||
|
||||
Log.d("getMediaPlaybackUrl", "Direct play: ${!shouldTranscode}, URL: $url")
|
||||
url
|
||||
}
|
||||
|
||||
suspend fun reportPlaybackStart(itemId: UUID, positionTicks: Long = 0L) = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) return@withContext
|
||||
api.playStateApi.reportPlaybackStart(
|
||||
PlaybackStartInfo(
|
||||
itemId = itemId,
|
||||
positionTicks = positionTicks,
|
||||
canSeek = true,
|
||||
isPaused = false,
|
||||
isMuted = false,
|
||||
playMethod = PlayMethod.DIRECT_PLAY,
|
||||
repeatMode = RepeatMode.REPEAT_NONE,
|
||||
playbackOrder = PlaybackOrder.DEFAULT
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun reportPlaybackProgress(itemId: UUID, positionTicks: Long, isPaused: Boolean) = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) return@withContext
|
||||
api.playStateApi.reportPlaybackProgress(
|
||||
PlaybackProgressInfo(
|
||||
itemId = itemId,
|
||||
positionTicks = positionTicks,
|
||||
canSeek = true,
|
||||
isPaused = isPaused,
|
||||
isMuted = false,
|
||||
playMethod = PlayMethod.DIRECT_PLAY,
|
||||
repeatMode = RepeatMode.REPEAT_NONE,
|
||||
playbackOrder = PlaybackOrder.DEFAULT
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun reportPlaybackStopped(itemId: UUID, positionTicks: Long) = withContext(Dispatchers.IO) {
|
||||
if (!ensureConfigured()) return@withContext
|
||||
api.playStateApi.reportPlaybackStopped(
|
||||
PlaybackStopInfo(
|
||||
itemId = itemId,
|
||||
positionTicks = positionTicks,
|
||||
failed = false
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package hu.bbara.purefin.core.data.client
|
||||
|
||||
import hu.bbara.purefin.core.data.session.UserSessionRepository
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.SupervisorJob
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import okhttp3.Interceptor
|
||||
import okhttp3.Response
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class JellyfinAuthInterceptor @Inject constructor(
|
||||
userSessionRepository: UserSessionRepository
|
||||
) : Interceptor {
|
||||
|
||||
@Volatile
|
||||
private var cachedToken: String = ""
|
||||
|
||||
init {
|
||||
userSessionRepository.accessToken
|
||||
.onEach { cachedToken = it }
|
||||
.launchIn(CoroutineScope(SupervisorJob() + Dispatchers.IO))
|
||||
}
|
||||
|
||||
override fun intercept(chain: Interceptor.Chain): Response {
|
||||
val token = cachedToken
|
||||
val request = chain.request().newBuilder()
|
||||
.addHeader("X-Emby-Token", token)
|
||||
.build()
|
||||
return chain.proceed(request)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package hu.bbara.purefin.core.data.domain.usecase
|
||||
|
||||
import hu.bbara.purefin.core.data.MediaRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
class RefreshHomeDataUseCase @Inject constructor(
|
||||
private val repository: MediaRepository
|
||||
) {
|
||||
suspend operator fun invoke() {
|
||||
repository.refreshHomeData()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package hu.bbara.purefin.core.data.domain.usecase
|
||||
|
||||
import hu.bbara.purefin.core.data.MediaRepository
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
class UpdateWatchProgressUseCase @Inject constructor(
|
||||
private val repository: MediaRepository
|
||||
) {
|
||||
suspend operator fun invoke(mediaId: UUID, positionMs: Long, durationMs: Long) {
|
||||
repository.updateWatchProgress(mediaId, positionMs, durationMs)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package hu.bbara.purefin.core.data.image
|
||||
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import hu.bbara.purefin.core.data.client.JellyfinAuthInterceptor
|
||||
import okhttp3.OkHttpClient
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object ImageModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideOkHttpClient(authInterceptor: JellyfinAuthInterceptor): OkHttpClient {
|
||||
return OkHttpClient.Builder()
|
||||
.addInterceptor(authInterceptor)
|
||||
.build()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package hu.bbara.purefin.core.data.image
|
||||
|
||||
import org.jellyfin.sdk.model.UUID
|
||||
import org.jellyfin.sdk.model.api.ImageType
|
||||
|
||||
class JellyfinImageHelper {
|
||||
companion object {
|
||||
fun toImageUrl(url: String, itemId: UUID, type: ImageType): String {
|
||||
if (url.isEmpty()) {
|
||||
return ""
|
||||
}
|
||||
return StringBuilder()
|
||||
.append(url)
|
||||
.append("/Items/")
|
||||
.append(itemId)
|
||||
.append("/Images/")
|
||||
.append(type.serialName)
|
||||
.toString()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import java.util.UUID
|
||||
|
||||
@Entity(
|
||||
tableName = "cast_members",
|
||||
indices = [Index("movieId"), Index("seriesId"), Index("episodeId")]
|
||||
)
|
||||
data class CastMemberEntity(
|
||||
@PrimaryKey(autoGenerate = true) val id: Long = 0,
|
||||
val name: String,
|
||||
val role: String,
|
||||
val imageUrl: String?,
|
||||
val movieId: UUID? = null,
|
||||
val seriesId: UUID? = null,
|
||||
val episodeId: UUID? = null
|
||||
)
|
||||
@@ -0,0 +1,35 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import javax.inject.Qualifier
|
||||
|
||||
/**
|
||||
* Qualifier for online database and its components.
|
||||
* Used for the primary MediaDatabase that syncs with the Jellyfin server.
|
||||
*/
|
||||
@Qualifier
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
annotation class OnlineDatabase
|
||||
|
||||
/**
|
||||
* Qualifier for offline database and its components.
|
||||
* Used for the OfflineMediaDatabase that stores downloaded content for offline viewing.
|
||||
*/
|
||||
@Qualifier
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
annotation class OfflineDatabase
|
||||
|
||||
/**
|
||||
* Qualifier for the online media repository.
|
||||
* Provides access to media synced from the Jellyfin server.
|
||||
*/
|
||||
@Qualifier
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
annotation class OnlineRepository
|
||||
|
||||
/**
|
||||
* Qualifier for the offline media repository.
|
||||
* Provides access to media downloaded for offline viewing.
|
||||
*/
|
||||
@Qualifier
|
||||
@Retention(AnnotationRetention.BINARY)
|
||||
annotation class OfflineRepository
|
||||
@@ -0,0 +1,35 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import java.util.UUID
|
||||
|
||||
@Entity(
|
||||
tableName = "episodes",
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = SeriesEntity::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["seriesId"],
|
||||
onDelete = ForeignKey.CASCADE
|
||||
)
|
||||
],
|
||||
indices = [Index("seriesId"), Index("seasonId")]
|
||||
)
|
||||
data class EpisodeEntity(
|
||||
@PrimaryKey val id: UUID,
|
||||
val seriesId: UUID,
|
||||
val seasonId: UUID,
|
||||
val index: Int,
|
||||
val title: String,
|
||||
val synopsis: String,
|
||||
val releaseDate: String,
|
||||
val rating: String,
|
||||
val runtime: String,
|
||||
val progress: Double?,
|
||||
val watched: Boolean,
|
||||
val format: String,
|
||||
val heroImageUrl: String
|
||||
)
|
||||
@@ -0,0 +1,13 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
import java.util.UUID
|
||||
|
||||
@Entity(tableName = "library")
|
||||
data class LibraryEntity (
|
||||
@PrimaryKey
|
||||
val id: UUID,
|
||||
val name: String,
|
||||
val type: String,
|
||||
)
|
||||
@@ -0,0 +1,33 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import hu.bbara.purefin.core.data.local.room.dao.CastMemberDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.EpisodeDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.LibraryDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.MovieDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.SeasonDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.SeriesDao
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
MovieEntity::class,
|
||||
SeriesEntity::class,
|
||||
SeasonEntity::class,
|
||||
EpisodeEntity::class,
|
||||
LibraryEntity::class,
|
||||
CastMemberEntity::class
|
||||
],
|
||||
version = 3,
|
||||
exportSchema = false
|
||||
)
|
||||
@TypeConverters(UuidConverters::class)
|
||||
abstract class MediaDatabase : RoomDatabase() {
|
||||
abstract fun movieDao(): MovieDao
|
||||
abstract fun seriesDao(): SeriesDao
|
||||
abstract fun seasonDao(): SeasonDao
|
||||
abstract fun episodeDao(): EpisodeDao
|
||||
abstract fun libraryDao(): LibraryDao
|
||||
abstract fun castMemberDao(): CastMemberDao
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import hu.bbara.purefin.core.data.local.room.dao.CastMemberDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.EpisodeDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.LibraryDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.MovieDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.SeasonDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.SeriesDao
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object MediaDatabaseModule {
|
||||
|
||||
// Online Database and DAOs
|
||||
@Provides
|
||||
@Singleton
|
||||
@OnlineDatabase
|
||||
fun provideOnlineDatabase(@ApplicationContext context: Context): MediaDatabase =
|
||||
Room.inMemoryDatabaseBuilder(context, MediaDatabase::class.java)
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
|
||||
@Provides
|
||||
@OnlineDatabase
|
||||
fun provideOnlineMovieDao(@OnlineDatabase db: MediaDatabase) = db.movieDao()
|
||||
|
||||
@Provides
|
||||
@OnlineDatabase
|
||||
fun provideOnlineSeriesDao(@OnlineDatabase db: MediaDatabase) = db.seriesDao()
|
||||
|
||||
@Provides
|
||||
@OnlineDatabase
|
||||
fun provideOnlineSeasonDao(@OnlineDatabase db: MediaDatabase) = db.seasonDao()
|
||||
|
||||
@Provides
|
||||
@OnlineDatabase
|
||||
fun provideOnlineEpisodeDao(@OnlineDatabase db: MediaDatabase) = db.episodeDao()
|
||||
|
||||
@Provides
|
||||
@OnlineDatabase
|
||||
fun provideOnlineCastMemberDao(@OnlineDatabase db: MediaDatabase) = db.castMemberDao()
|
||||
|
||||
@Provides
|
||||
@OnlineDatabase
|
||||
fun provideOnlineLibraryDao(@OnlineDatabase db: MediaDatabase) = db.libraryDao()
|
||||
|
||||
// Offline Database and DAOs
|
||||
@Provides
|
||||
@Singleton
|
||||
@OfflineDatabase
|
||||
fun provideOfflineDatabase(@ApplicationContext context: Context): OfflineMediaDatabase =
|
||||
Room.databaseBuilder(context, OfflineMediaDatabase::class.java, "offline_media_database")
|
||||
.fallbackToDestructiveMigration()
|
||||
.build()
|
||||
|
||||
@Provides
|
||||
@OfflineDatabase
|
||||
fun provideOfflineMovieDao(@OfflineDatabase db: OfflineMediaDatabase) = db.movieDao()
|
||||
|
||||
@Provides
|
||||
@OfflineDatabase
|
||||
fun provideOfflineSeriesDao(@OfflineDatabase db: OfflineMediaDatabase) = db.seriesDao()
|
||||
|
||||
@Provides
|
||||
@OfflineDatabase
|
||||
fun provideOfflineSeasonDao(@OfflineDatabase db: OfflineMediaDatabase) = db.seasonDao()
|
||||
|
||||
@Provides
|
||||
@OfflineDatabase
|
||||
fun provideOfflineEpisodeDao(@OfflineDatabase db: OfflineMediaDatabase) = db.episodeDao()
|
||||
|
||||
@Provides
|
||||
@OfflineDatabase
|
||||
fun provideOfflineCastMemberDao(@OfflineDatabase db: OfflineMediaDatabase) = db.castMemberDao()
|
||||
|
||||
@Provides
|
||||
@OfflineDatabase
|
||||
fun provideOfflineLibraryDao(@OfflineDatabase db: OfflineMediaDatabase) = db.libraryDao()
|
||||
|
||||
// Data Sources
|
||||
@Provides
|
||||
@Singleton
|
||||
@OnlineDatabase
|
||||
fun provideOnlineDataSource(
|
||||
@OnlineDatabase database: MediaDatabase,
|
||||
@OnlineDatabase movieDao: MovieDao,
|
||||
@OnlineDatabase seriesDao: SeriesDao,
|
||||
@OnlineDatabase seasonDao: SeasonDao,
|
||||
@OnlineDatabase episodeDao: EpisodeDao,
|
||||
@OnlineDatabase castMemberDao: CastMemberDao,
|
||||
@OnlineDatabase libraryDao: LibraryDao
|
||||
): RoomMediaLocalDataSource = RoomMediaLocalDataSource(
|
||||
database, movieDao, seriesDao, seasonDao, episodeDao, castMemberDao, libraryDao
|
||||
)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
@OfflineDatabase
|
||||
fun provideOfflineDataSource(
|
||||
@OfflineDatabase database: OfflineMediaDatabase,
|
||||
@OfflineDatabase movieDao: MovieDao,
|
||||
@OfflineDatabase seriesDao: SeriesDao,
|
||||
@OfflineDatabase seasonDao: SeasonDao,
|
||||
@OfflineDatabase episodeDao: EpisodeDao,
|
||||
@OfflineDatabase castMemberDao: CastMemberDao,
|
||||
@OfflineDatabase libraryDao: LibraryDao
|
||||
): OfflineRoomMediaLocalDataSource = OfflineRoomMediaLocalDataSource(
|
||||
database, movieDao, seriesDao, seasonDao, episodeDao, castMemberDao, libraryDao
|
||||
)
|
||||
|
||||
// Default (unqualified) data source for backward compatibility
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideDefaultDataSource(
|
||||
@OnlineDatabase dataSource: RoomMediaLocalDataSource
|
||||
): RoomMediaLocalDataSource = dataSource
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import java.util.UUID
|
||||
|
||||
@Entity(
|
||||
tableName = "movies",
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = LibraryEntity::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["libraryId"],
|
||||
),
|
||||
],
|
||||
indices = [Index("libraryId")]
|
||||
)
|
||||
data class MovieEntity(
|
||||
@PrimaryKey val id: UUID,
|
||||
val libraryId: UUID,
|
||||
val title: String,
|
||||
val progress: Double?,
|
||||
val watched: Boolean,
|
||||
val year: String,
|
||||
val rating: String,
|
||||
val runtime: String,
|
||||
val format: String,
|
||||
val synopsis: String,
|
||||
val heroImageUrl: String,
|
||||
val audioTrack: String,
|
||||
val subtitles: String
|
||||
)
|
||||
@@ -0,0 +1,33 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.Database
|
||||
import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import hu.bbara.purefin.core.data.local.room.dao.CastMemberDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.EpisodeDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.LibraryDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.MovieDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.SeasonDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.SeriesDao
|
||||
|
||||
@Database(
|
||||
entities = [
|
||||
MovieEntity::class,
|
||||
SeriesEntity::class,
|
||||
SeasonEntity::class,
|
||||
EpisodeEntity::class,
|
||||
LibraryEntity::class,
|
||||
CastMemberEntity::class
|
||||
],
|
||||
version = 3,
|
||||
exportSchema = false
|
||||
)
|
||||
@TypeConverters(UuidConverters::class)
|
||||
abstract class OfflineMediaDatabase : RoomDatabase() {
|
||||
abstract fun movieDao(): MovieDao
|
||||
abstract fun seriesDao(): SeriesDao
|
||||
abstract fun seasonDao(): SeasonDao
|
||||
abstract fun episodeDao(): EpisodeDao
|
||||
abstract fun libraryDao(): LibraryDao
|
||||
abstract fun castMemberDao(): CastMemberDao
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.withTransaction
|
||||
import hu.bbara.purefin.core.data.local.room.dao.CastMemberDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.EpisodeDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.LibraryDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.MovieDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.SeasonDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.SeriesDao
|
||||
import hu.bbara.purefin.core.model.CastMember
|
||||
import hu.bbara.purefin.core.model.Episode
|
||||
import hu.bbara.purefin.core.model.Library
|
||||
import hu.bbara.purefin.core.model.Movie
|
||||
import hu.bbara.purefin.core.model.Season
|
||||
import hu.bbara.purefin.core.model.Series
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import org.jellyfin.sdk.model.api.CollectionType
|
||||
import java.util.UUID
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class OfflineRoomMediaLocalDataSource(
|
||||
private val database: OfflineMediaDatabase,
|
||||
private val movieDao: MovieDao,
|
||||
private val seriesDao: SeriesDao,
|
||||
private val seasonDao: SeasonDao,
|
||||
private val episodeDao: EpisodeDao,
|
||||
private val castMemberDao: CastMemberDao,
|
||||
private val libraryDao: LibraryDao
|
||||
) {
|
||||
|
||||
// Lightweight Flows for list screens (home, library)
|
||||
val librariesFlow: Flow<List<Library>> = libraryDao.observeAllWithContent()
|
||||
.map { relation ->
|
||||
relation.map {
|
||||
it.library.toDomain(
|
||||
movies = it.movies.map { e -> e.toDomain(cast = emptyList()) },
|
||||
series = it.series.map { e -> e.toDomain(seasons = emptyList(), cast = emptyList()) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val moviesFlow: Flow<Map<UUID, Movie>> = movieDao.observeAll()
|
||||
.map { entities -> entities.associate { it.id to it.toDomain(cast = emptyList()) } }
|
||||
|
||||
val seriesFlow: Flow<Map<UUID, Series>> = seriesDao.observeAll()
|
||||
.map { entities ->
|
||||
entities.associate { it.id to it.toDomain(seasons = emptyList(), cast = emptyList()) }
|
||||
}
|
||||
|
||||
val episodesFlow: Flow<Map<UUID, Episode>> = episodeDao.observeAll()
|
||||
.map { entities -> entities.associate { it.id to it.toDomain(cast = emptyList()) } }
|
||||
|
||||
// Full content Flow for series detail screen (scoped to one series)
|
||||
fun observeSeriesWithContent(seriesId: UUID): Flow<Series?> =
|
||||
seriesDao.observeWithContent(seriesId).map { relation ->
|
||||
relation?.let {
|
||||
it.series.toDomain(
|
||||
seasons = it.seasons.map { swe ->
|
||||
swe.season.toDomain(
|
||||
episodes = swe.episodes.map { ep -> ep.toDomain(cast = emptyList()) }
|
||||
)
|
||||
},
|
||||
cast = emptyList()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveLibraries(libraries: List<Library>) {
|
||||
database.withTransaction {
|
||||
libraryDao.deleteAll()
|
||||
libraryDao.upsertAll(libraries.map { it.toEntity() })
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveMovies(movies: List<Movie>) {
|
||||
database.withTransaction {
|
||||
movieDao.upsertAll(movies.map { it.toEntity() })
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveSeries(seriesList: List<Series>) {
|
||||
database.withTransaction {
|
||||
seriesDao.upsertAll(seriesList.map { it.toEntity() })
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveSeriesContent(series: Series) {
|
||||
database.withTransaction {
|
||||
// First ensure the series exists before adding seasons/episodes/cast
|
||||
seriesDao.upsert(series.toEntity())
|
||||
|
||||
episodeDao.deleteBySeriesId(series.id)
|
||||
seasonDao.deleteBySeriesId(series.id)
|
||||
|
||||
series.seasons.forEach { season ->
|
||||
seasonDao.upsert(season.toEntity())
|
||||
season.episodes.forEach { episode ->
|
||||
episodeDao.upsert(episode.toEntity())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveEpisode(episode: Episode) {
|
||||
database.withTransaction {
|
||||
seriesDao.getById(episode.seriesId)
|
||||
?: throw RuntimeException("Cannot add episode without series. Episode: $episode")
|
||||
|
||||
episodeDao.upsert(episode.toEntity())
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getMovies(): List<Movie> {
|
||||
val movies = movieDao.getAll()
|
||||
return movies.map { entity ->
|
||||
val cast = castMemberDao.getByMovieId(entity.id).map { it.toDomain() }
|
||||
entity.toDomain(cast)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getMovie(id: UUID): Movie? {
|
||||
val entity = movieDao.getById(id) ?: return null
|
||||
val cast = castMemberDao.getByMovieId(id).map { it.toDomain() }
|
||||
return entity.toDomain(cast)
|
||||
}
|
||||
|
||||
suspend fun getSeries(): List<Series> {
|
||||
return seriesDao.getAll().mapNotNull { entity -> getSeriesInternal(entity.id, includeContent = false) }
|
||||
}
|
||||
|
||||
suspend fun getSeriesBasic(id: UUID): Series? = getSeriesInternal(id, includeContent = false)
|
||||
|
||||
suspend fun getSeriesWithContent(id: UUID): Series? = getSeriesInternal(id, includeContent = true)
|
||||
|
||||
private suspend fun getSeriesInternal(id: UUID, includeContent: Boolean): Series? {
|
||||
val entity = seriesDao.getById(id) ?: return null
|
||||
val cast = castMemberDao.getBySeriesId(id).map { it.toDomain() }
|
||||
val seasons = if (includeContent) {
|
||||
seasonDao.getBySeriesId(id).map { seasonEntity ->
|
||||
val episodes = episodeDao.getBySeasonId(seasonEntity.id).map { episodeEntity ->
|
||||
val episodeCast = castMemberDao.getByEpisodeId(episodeEntity.id).map { it.toDomain() }
|
||||
episodeEntity.toDomain(episodeCast)
|
||||
}
|
||||
seasonEntity.toDomain(episodes)
|
||||
}
|
||||
} else emptyList()
|
||||
return entity.toDomain(seasons, cast)
|
||||
}
|
||||
|
||||
suspend fun getSeason(seriesId: UUID, seasonId: UUID): Season? {
|
||||
val seasonEntity = seasonDao.getById(seasonId) ?: return null
|
||||
val episodes = episodeDao.getBySeasonId(seasonId).map { episodeEntity ->
|
||||
val episodeCast = castMemberDao.getByEpisodeId(episodeEntity.id).map { it.toDomain() }
|
||||
episodeEntity.toDomain(episodeCast)
|
||||
}
|
||||
return seasonEntity.toDomain(episodes)
|
||||
}
|
||||
|
||||
suspend fun getSeasons(seriesId: UUID): List<Season> {
|
||||
return seasonDao.getBySeriesId(seriesId).map { seasonEntity ->
|
||||
val episodes = episodeDao.getBySeasonId(seasonEntity.id).map { episodeEntity ->
|
||||
val episodeCast = castMemberDao.getByEpisodeId(episodeEntity.id).map { it.toDomain() }
|
||||
episodeEntity.toDomain(episodeCast)
|
||||
}
|
||||
seasonEntity.toDomain(episodes)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getEpisode(seriesId: UUID, seasonId: UUID, episodeId: UUID): Episode? {
|
||||
val episodeEntity = episodeDao.getById(episodeId) ?: return null
|
||||
val cast = castMemberDao.getByEpisodeId(episodeId).map { it.toDomain() }
|
||||
return episodeEntity.toDomain(cast)
|
||||
}
|
||||
|
||||
suspend fun getEpisodeById(episodeId: UUID): Episode? {
|
||||
val episodeEntity = episodeDao.getById(episodeId) ?: return null
|
||||
val cast = castMemberDao.getByEpisodeId(episodeId).map { it.toDomain() }
|
||||
return episodeEntity.toDomain(cast)
|
||||
}
|
||||
|
||||
suspend fun updateWatchProgress(mediaId: UUID, progress: Double?, watched: Boolean) {
|
||||
movieDao.getById(mediaId)?.let {
|
||||
movieDao.updateProgress(mediaId, progress, watched)
|
||||
return
|
||||
}
|
||||
|
||||
episodeDao.getById(mediaId)?.let { episode ->
|
||||
database.withTransaction {
|
||||
episodeDao.updateProgress(mediaId, progress, watched)
|
||||
val seasonUnwatched = episodeDao.countUnwatchedBySeason(episode.seasonId)
|
||||
seasonDao.updateUnwatchedCount(episode.seasonId, seasonUnwatched)
|
||||
val seriesUnwatched = episodeDao.countUnwatchedBySeries(episode.seriesId)
|
||||
seriesDao.updateUnwatchedCount(episode.seriesId, seriesUnwatched)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getEpisodesBySeries(seriesId: UUID): List<Episode> {
|
||||
return episodeDao.getBySeriesId(seriesId).map { episodeEntity ->
|
||||
val cast = castMemberDao.getByEpisodeId(episodeEntity.id).map { it.toDomain() }
|
||||
episodeEntity.toDomain(cast)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Library.toEntity() = LibraryEntity(
|
||||
id = id,
|
||||
name = name,
|
||||
type = when (type) {
|
||||
CollectionType.MOVIES -> "MOVIES"
|
||||
CollectionType.TVSHOWS -> "TVSHOWS"
|
||||
else -> throw UnsupportedOperationException("Unsupported library type: $type")
|
||||
}
|
||||
)
|
||||
|
||||
private fun LibraryEntity.toDomain(series: List<Series>, movies: List<Movie>) = Library(
|
||||
id = id,
|
||||
name = name,
|
||||
type = when (type) {
|
||||
"MOVIES" -> CollectionType.MOVIES
|
||||
"TVSHOWS" -> CollectionType.TVSHOWS
|
||||
else -> throw UnsupportedOperationException("Unsupported library type: $type")
|
||||
},
|
||||
movies = if (type == "MOVIES") movies else null,
|
||||
series = if (type == "TVSHOWS") series else null,
|
||||
)
|
||||
|
||||
private fun Movie.toEntity() = MovieEntity(
|
||||
id = id,
|
||||
libraryId = libraryId,
|
||||
title = title,
|
||||
progress = progress,
|
||||
watched = watched,
|
||||
year = year,
|
||||
rating = rating,
|
||||
runtime = runtime,
|
||||
format = format,
|
||||
synopsis = synopsis,
|
||||
heroImageUrl = heroImageUrl,
|
||||
audioTrack = audioTrack,
|
||||
subtitles = subtitles
|
||||
)
|
||||
|
||||
private fun Series.toEntity() = SeriesEntity(
|
||||
id = id,
|
||||
libraryId = libraryId,
|
||||
name = name,
|
||||
synopsis = synopsis,
|
||||
year = year,
|
||||
heroImageUrl = heroImageUrl,
|
||||
unwatchedEpisodeCount = unwatchedEpisodeCount,
|
||||
seasonCount = seasonCount
|
||||
)
|
||||
|
||||
private fun Season.toEntity() = SeasonEntity(
|
||||
id = id,
|
||||
seriesId = seriesId,
|
||||
name = name,
|
||||
index = index,
|
||||
unwatchedEpisodeCount = unwatchedEpisodeCount,
|
||||
episodeCount = episodeCount
|
||||
)
|
||||
|
||||
private fun Episode.toEntity() = EpisodeEntity(
|
||||
id = id,
|
||||
seriesId = seriesId,
|
||||
seasonId = seasonId,
|
||||
index = index,
|
||||
title = title,
|
||||
synopsis = synopsis,
|
||||
releaseDate = releaseDate,
|
||||
rating = rating,
|
||||
runtime = runtime,
|
||||
progress = progress,
|
||||
watched = watched,
|
||||
format = format,
|
||||
heroImageUrl = heroImageUrl
|
||||
)
|
||||
|
||||
private fun MovieEntity.toDomain(cast: List<CastMember>) = Movie(
|
||||
id = id,
|
||||
libraryId = libraryId,
|
||||
title = title,
|
||||
progress = progress,
|
||||
watched = watched,
|
||||
year = year,
|
||||
rating = rating,
|
||||
runtime = runtime,
|
||||
format = format,
|
||||
synopsis = synopsis,
|
||||
heroImageUrl = heroImageUrl,
|
||||
audioTrack = audioTrack,
|
||||
subtitles = subtitles,
|
||||
cast = cast
|
||||
)
|
||||
|
||||
private fun SeriesEntity.toDomain(seasons: List<Season>, cast: List<CastMember>) = Series(
|
||||
id = id,
|
||||
libraryId = libraryId,
|
||||
name = name,
|
||||
synopsis = synopsis,
|
||||
year = year,
|
||||
heroImageUrl = heroImageUrl,
|
||||
unwatchedEpisodeCount = unwatchedEpisodeCount,
|
||||
seasonCount = seasonCount,
|
||||
seasons = seasons,
|
||||
cast = cast
|
||||
)
|
||||
|
||||
private fun SeasonEntity.toDomain(episodes: List<Episode>) = Season(
|
||||
id = id,
|
||||
seriesId = seriesId,
|
||||
name = name,
|
||||
index = index,
|
||||
unwatchedEpisodeCount = unwatchedEpisodeCount,
|
||||
episodeCount = episodeCount,
|
||||
episodes = episodes
|
||||
)
|
||||
|
||||
private fun EpisodeEntity.toDomain(cast: List<CastMember>) = Episode(
|
||||
id = id,
|
||||
seriesId = seriesId,
|
||||
seasonId = seasonId,
|
||||
index = index,
|
||||
title = title,
|
||||
synopsis = synopsis,
|
||||
releaseDate = releaseDate,
|
||||
rating = rating,
|
||||
runtime = runtime,
|
||||
progress = progress,
|
||||
watched = watched,
|
||||
format = format,
|
||||
heroImageUrl = heroImageUrl,
|
||||
cast = cast
|
||||
)
|
||||
|
||||
private fun CastMember.toMovieEntity(movieId: UUID) = CastMemberEntity(
|
||||
name = name,
|
||||
role = role,
|
||||
imageUrl = imageUrl,
|
||||
movieId = movieId
|
||||
)
|
||||
|
||||
private fun CastMember.toSeriesEntity(seriesId: UUID) = CastMemberEntity(
|
||||
name = name,
|
||||
role = role,
|
||||
imageUrl = imageUrl,
|
||||
seriesId = seriesId
|
||||
)
|
||||
|
||||
private fun CastMember.toEpisodeEntity(episodeId: UUID) = CastMemberEntity(
|
||||
name = name,
|
||||
role = role,
|
||||
imageUrl = imageUrl,
|
||||
episodeId = episodeId
|
||||
)
|
||||
|
||||
private fun CastMemberEntity.toDomain() = CastMember(
|
||||
name = name,
|
||||
role = role,
|
||||
imageUrl = imageUrl
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.withTransaction
|
||||
import hu.bbara.purefin.core.data.local.room.dao.CastMemberDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.EpisodeDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.LibraryDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.MovieDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.SeasonDao
|
||||
import hu.bbara.purefin.core.data.local.room.dao.SeriesDao
|
||||
import hu.bbara.purefin.core.model.CastMember
|
||||
import hu.bbara.purefin.core.model.Episode
|
||||
import hu.bbara.purefin.core.model.Library
|
||||
import hu.bbara.purefin.core.model.Movie
|
||||
import hu.bbara.purefin.core.model.Season
|
||||
import hu.bbara.purefin.core.model.Series
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import org.jellyfin.sdk.model.api.CollectionType
|
||||
import java.util.UUID
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Singleton
|
||||
class RoomMediaLocalDataSource(
|
||||
private val database: MediaDatabase,
|
||||
private val movieDao: MovieDao,
|
||||
private val seriesDao: SeriesDao,
|
||||
private val seasonDao: SeasonDao,
|
||||
private val episodeDao: EpisodeDao,
|
||||
private val castMemberDao: CastMemberDao,
|
||||
private val libraryDao: LibraryDao
|
||||
) {
|
||||
|
||||
// Lightweight Flows for list screens (home, library)
|
||||
val librariesFlow: Flow<List<Library>> = libraryDao.observeAllWithContent()
|
||||
.map { relation ->
|
||||
relation.map { libraryEntity ->
|
||||
libraryEntity.library.toDomain(
|
||||
movies = libraryEntity.movies.map { it.toDomain(listOf()) },
|
||||
series = libraryEntity.series.map { it.toDomain(listOf(), listOf()) }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val moviesFlow: Flow<Map<UUID, Movie>> = movieDao.observeAll()
|
||||
.map { entities -> entities.associate { it.id to it.toDomain(cast = emptyList()) } }
|
||||
|
||||
val seriesFlow: Flow<Map<UUID, Series>> = seriesDao.observeAll()
|
||||
.map { entities ->
|
||||
entities.associate { it.id to it.toDomain(seasons = emptyList(), cast = emptyList()) }
|
||||
}
|
||||
|
||||
val episodesFlow: Flow<Map<UUID, Episode>> = episodeDao.observeAll()
|
||||
.map { entities -> entities.associate { it.id to it.toDomain(cast = emptyList()) } }
|
||||
|
||||
// Full content Flow for series detail screen (scoped to one series)
|
||||
fun observeSeriesWithContent(seriesId: UUID): Flow<Series?> =
|
||||
seriesDao.observeWithContent(seriesId).map { relation ->
|
||||
relation?.let {
|
||||
it.series.toDomain(
|
||||
seasons = it.seasons.map { swe ->
|
||||
swe.season.toDomain(
|
||||
episodes = swe.episodes.map { ep -> ep.toDomain(cast = emptyList()) }
|
||||
)
|
||||
},
|
||||
cast = emptyList()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveLibraries(libraries: List<Library>) {
|
||||
database.withTransaction {
|
||||
libraryDao.deleteAll()
|
||||
libraryDao.upsertAll(libraries.map { it.toEntity() })
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveMovies(movies: List<Movie>) {
|
||||
database.withTransaction {
|
||||
movieDao.upsertAll(movies.map { it.toEntity() })
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveSeries(seriesList: List<Series>) {
|
||||
database.withTransaction {
|
||||
seriesDao.upsertAll(seriesList.map { it.toEntity() })
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveSeriesContent(series: Series) {
|
||||
database.withTransaction {
|
||||
// First ensure the series exists before adding seasons/episodes/cast
|
||||
seriesDao.upsert(series.toEntity())
|
||||
|
||||
episodeDao.deleteBySeriesId(series.id)
|
||||
seasonDao.deleteBySeriesId(series.id)
|
||||
|
||||
series.seasons.forEach { season ->
|
||||
seasonDao.upsert(season.toEntity())
|
||||
season.episodes.forEach { episode ->
|
||||
episodeDao.upsert(episode.toEntity())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun saveEpisode(episode: Episode) {
|
||||
database.withTransaction {
|
||||
seriesDao.getById(episode.seriesId)
|
||||
?: throw RuntimeException("Cannot add episode without series. Episode: $episode")
|
||||
|
||||
episodeDao.upsert(episode.toEntity())
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getMovies(): List<Movie> {
|
||||
val movies = movieDao.getAll()
|
||||
return movies.map { entity ->
|
||||
val cast = castMemberDao.getByMovieId(entity.id).map { it.toDomain() }
|
||||
entity.toDomain(cast)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getMovie(id: UUID): Movie? {
|
||||
val entity = movieDao.getById(id) ?: return null
|
||||
val cast = castMemberDao.getByMovieId(id).map { it.toDomain() }
|
||||
return entity.toDomain(cast)
|
||||
}
|
||||
|
||||
suspend fun getSeries(): List<Series> {
|
||||
return seriesDao.getAll().mapNotNull { entity -> getSeriesInternal(entity.id, includeContent = false) }
|
||||
}
|
||||
|
||||
suspend fun getSeriesBasic(id: UUID): Series? = getSeriesInternal(id, includeContent = false)
|
||||
|
||||
suspend fun getSeriesWithContent(id: UUID): Series? = getSeriesInternal(id, includeContent = true)
|
||||
|
||||
private suspend fun getSeriesInternal(id: UUID, includeContent: Boolean): Series? {
|
||||
val entity = seriesDao.getById(id) ?: return null
|
||||
val cast = castMemberDao.getBySeriesId(id).map { it.toDomain() }
|
||||
val seasons = if (includeContent) {
|
||||
seasonDao.getBySeriesId(id).map { seasonEntity ->
|
||||
val episodes = episodeDao.getBySeasonId(seasonEntity.id).map { episodeEntity ->
|
||||
val episodeCast = castMemberDao.getByEpisodeId(episodeEntity.id).map { it.toDomain() }
|
||||
episodeEntity.toDomain(episodeCast)
|
||||
}
|
||||
seasonEntity.toDomain(episodes)
|
||||
}
|
||||
} else emptyList()
|
||||
return entity.toDomain(seasons, cast)
|
||||
}
|
||||
|
||||
suspend fun getSeason(seriesId: UUID, seasonId: UUID): Season? {
|
||||
val seasonEntity = seasonDao.getById(seasonId) ?: return null
|
||||
val episodes = episodeDao.getBySeasonId(seasonId).map { episodeEntity ->
|
||||
val episodeCast = castMemberDao.getByEpisodeId(episodeEntity.id).map { it.toDomain() }
|
||||
episodeEntity.toDomain(episodeCast)
|
||||
}
|
||||
return seasonEntity.toDomain(episodes)
|
||||
}
|
||||
|
||||
suspend fun getSeasons(seriesId: UUID): List<Season> {
|
||||
return seasonDao.getBySeriesId(seriesId).map { seasonEntity ->
|
||||
val episodes = episodeDao.getBySeasonId(seasonEntity.id).map { episodeEntity ->
|
||||
val episodeCast = castMemberDao.getByEpisodeId(episodeEntity.id).map { it.toDomain() }
|
||||
episodeEntity.toDomain(episodeCast)
|
||||
}
|
||||
seasonEntity.toDomain(episodes)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getEpisode(seriesId: UUID, seasonId: UUID, episodeId: UUID): Episode? {
|
||||
val episodeEntity = episodeDao.getById(episodeId) ?: return null
|
||||
val cast = castMemberDao.getByEpisodeId(episodeId).map { it.toDomain() }
|
||||
return episodeEntity.toDomain(cast)
|
||||
}
|
||||
|
||||
suspend fun getEpisodeById(episodeId: UUID): Episode? {
|
||||
val episodeEntity = episodeDao.getById(episodeId) ?: return null
|
||||
val cast = castMemberDao.getByEpisodeId(episodeId).map { it.toDomain() }
|
||||
return episodeEntity.toDomain(cast)
|
||||
}
|
||||
|
||||
suspend fun updateWatchProgress(mediaId: UUID, progress: Double?, watched: Boolean) {
|
||||
movieDao.getById(mediaId)?.let {
|
||||
movieDao.updateProgress(mediaId, progress, watched)
|
||||
return
|
||||
}
|
||||
|
||||
episodeDao.getById(mediaId)?.let { episode ->
|
||||
database.withTransaction {
|
||||
episodeDao.updateProgress(mediaId, progress, watched)
|
||||
val seasonUnwatched = episodeDao.countUnwatchedBySeason(episode.seasonId)
|
||||
seasonDao.updateUnwatchedCount(episode.seasonId, seasonUnwatched)
|
||||
val seriesUnwatched = episodeDao.countUnwatchedBySeries(episode.seriesId)
|
||||
seriesDao.updateUnwatchedCount(episode.seriesId, seriesUnwatched)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getEpisodesBySeries(seriesId: UUID): List<Episode> {
|
||||
return episodeDao.getBySeriesId(seriesId).map { episodeEntity ->
|
||||
val cast = castMemberDao.getByEpisodeId(episodeEntity.id).map { it.toDomain() }
|
||||
episodeEntity.toDomain(cast)
|
||||
}
|
||||
}
|
||||
|
||||
private fun Library.toEntity() = LibraryEntity(
|
||||
id = id,
|
||||
name = name,
|
||||
type = when (type) {
|
||||
CollectionType.MOVIES -> "MOVIES"
|
||||
CollectionType.TVSHOWS -> "TVSHOWS"
|
||||
else -> throw UnsupportedOperationException("Unsupported library type: $type")
|
||||
}
|
||||
)
|
||||
|
||||
private fun LibraryEntity.toDomain(series: List<Series>, movies: List<Movie>) = Library(
|
||||
id = id,
|
||||
name = name,
|
||||
type = when (type) {
|
||||
"MOVIES" -> CollectionType.MOVIES
|
||||
"TVSHOWS" -> CollectionType.TVSHOWS
|
||||
else -> throw UnsupportedOperationException("Unsupported library type: $type")
|
||||
},
|
||||
movies = if (type == "MOVIES") movies else null,
|
||||
series = if (type == "TVSHOWS") series else null,
|
||||
)
|
||||
|
||||
private fun Movie.toEntity() = MovieEntity(
|
||||
id = id,
|
||||
libraryId = libraryId,
|
||||
title = title,
|
||||
progress = progress,
|
||||
watched = watched,
|
||||
year = year,
|
||||
rating = rating,
|
||||
runtime = runtime,
|
||||
format = format,
|
||||
synopsis = synopsis,
|
||||
heroImageUrl = heroImageUrl,
|
||||
audioTrack = audioTrack,
|
||||
subtitles = subtitles
|
||||
)
|
||||
|
||||
private fun Series.toEntity() = SeriesEntity(
|
||||
id = id,
|
||||
libraryId = libraryId,
|
||||
name = name,
|
||||
synopsis = synopsis,
|
||||
year = year,
|
||||
heroImageUrl = heroImageUrl,
|
||||
unwatchedEpisodeCount = unwatchedEpisodeCount,
|
||||
seasonCount = seasonCount
|
||||
)
|
||||
|
||||
private fun Season.toEntity() = SeasonEntity(
|
||||
id = id,
|
||||
seriesId = seriesId,
|
||||
name = name,
|
||||
index = index,
|
||||
unwatchedEpisodeCount = unwatchedEpisodeCount,
|
||||
episodeCount = episodeCount
|
||||
)
|
||||
|
||||
private fun Episode.toEntity() = EpisodeEntity(
|
||||
id = id,
|
||||
seriesId = seriesId,
|
||||
seasonId = seasonId,
|
||||
index = index,
|
||||
title = title,
|
||||
synopsis = synopsis,
|
||||
releaseDate = releaseDate,
|
||||
rating = rating,
|
||||
runtime = runtime,
|
||||
progress = progress,
|
||||
watched = watched,
|
||||
format = format,
|
||||
heroImageUrl = heroImageUrl
|
||||
)
|
||||
|
||||
private fun MovieEntity.toDomain(cast: List<CastMember>) = Movie(
|
||||
id = id,
|
||||
libraryId = libraryId,
|
||||
title = title,
|
||||
progress = progress,
|
||||
watched = watched,
|
||||
year = year,
|
||||
rating = rating,
|
||||
runtime = runtime,
|
||||
format = format,
|
||||
synopsis = synopsis,
|
||||
heroImageUrl = heroImageUrl,
|
||||
audioTrack = audioTrack,
|
||||
subtitles = subtitles,
|
||||
cast = cast
|
||||
)
|
||||
|
||||
private fun SeriesEntity.toDomain(seasons: List<Season>, cast: List<CastMember>) = Series(
|
||||
id = id,
|
||||
libraryId = libraryId,
|
||||
name = name,
|
||||
synopsis = synopsis,
|
||||
year = year,
|
||||
heroImageUrl = heroImageUrl,
|
||||
unwatchedEpisodeCount = unwatchedEpisodeCount,
|
||||
seasonCount = seasonCount,
|
||||
seasons = seasons,
|
||||
cast = cast
|
||||
)
|
||||
|
||||
private fun SeasonEntity.toDomain(episodes: List<Episode>) = Season(
|
||||
id = id,
|
||||
seriesId = seriesId,
|
||||
name = name,
|
||||
index = index,
|
||||
unwatchedEpisodeCount = unwatchedEpisodeCount,
|
||||
episodeCount = episodeCount,
|
||||
episodes = episodes
|
||||
)
|
||||
|
||||
private fun EpisodeEntity.toDomain(cast: List<CastMember>) = Episode(
|
||||
id = id,
|
||||
seriesId = seriesId,
|
||||
seasonId = seasonId,
|
||||
index = index,
|
||||
title = title,
|
||||
synopsis = synopsis,
|
||||
releaseDate = releaseDate,
|
||||
rating = rating,
|
||||
runtime = runtime,
|
||||
progress = progress,
|
||||
watched = watched,
|
||||
format = format,
|
||||
heroImageUrl = heroImageUrl,
|
||||
cast = cast
|
||||
)
|
||||
|
||||
private fun CastMember.toMovieEntity(movieId: UUID) = CastMemberEntity(
|
||||
name = name,
|
||||
role = role,
|
||||
imageUrl = imageUrl,
|
||||
movieId = movieId
|
||||
)
|
||||
|
||||
private fun CastMember.toSeriesEntity(seriesId: UUID) = CastMemberEntity(
|
||||
name = name,
|
||||
role = role,
|
||||
imageUrl = imageUrl,
|
||||
seriesId = seriesId
|
||||
)
|
||||
|
||||
private fun CastMember.toEpisodeEntity(episodeId: UUID) = CastMemberEntity(
|
||||
name = name,
|
||||
role = role,
|
||||
imageUrl = imageUrl,
|
||||
episodeId = episodeId
|
||||
)
|
||||
|
||||
private fun CastMemberEntity.toDomain() = CastMember(
|
||||
name = name,
|
||||
role = role,
|
||||
imageUrl = imageUrl
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.Embedded
|
||||
import androidx.room.Relation
|
||||
|
||||
data class SeasonWithEpisodes(
|
||||
@Embedded val season: SeasonEntity,
|
||||
@Relation(
|
||||
parentColumn = "id",
|
||||
entityColumn = "seasonId"
|
||||
)
|
||||
val episodes: List<EpisodeEntity>
|
||||
)
|
||||
|
||||
data class SeriesWithSeasonsAndEpisodes(
|
||||
@Embedded val series: SeriesEntity,
|
||||
@Relation(
|
||||
entity = SeasonEntity::class,
|
||||
parentColumn = "id",
|
||||
entityColumn = "seriesId"
|
||||
)
|
||||
val seasons: List<SeasonWithEpisodes>
|
||||
)
|
||||
|
||||
data class LibraryWithContent(
|
||||
@Embedded val library: LibraryEntity,
|
||||
@Relation(
|
||||
parentColumn = "id",
|
||||
entityColumn = "libraryId"
|
||||
)
|
||||
val series: List<SeriesEntity>,
|
||||
@Relation(
|
||||
parentColumn = "id",
|
||||
entityColumn = "libraryId"
|
||||
)
|
||||
val movies: List<MovieEntity>
|
||||
)
|
||||
@@ -0,0 +1,28 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import java.util.UUID
|
||||
|
||||
@Entity(
|
||||
tableName = "seasons",
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = SeriesEntity::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["seriesId"],
|
||||
onDelete = ForeignKey.CASCADE
|
||||
)
|
||||
],
|
||||
indices = [Index("seriesId")]
|
||||
)
|
||||
data class SeasonEntity(
|
||||
@PrimaryKey val id: UUID,
|
||||
val seriesId: UUID,
|
||||
val name: String,
|
||||
val index: Int,
|
||||
val unwatchedEpisodeCount: Int,
|
||||
val episodeCount: Int
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.Entity
|
||||
import androidx.room.ForeignKey
|
||||
import androidx.room.Index
|
||||
import androidx.room.PrimaryKey
|
||||
import java.util.UUID
|
||||
|
||||
@Entity(
|
||||
tableName = "series",
|
||||
foreignKeys = [
|
||||
ForeignKey(
|
||||
entity = LibraryEntity::class,
|
||||
parentColumns = ["id"],
|
||||
childColumns = ["libraryId"]
|
||||
),
|
||||
],
|
||||
indices = [Index("libraryId")]
|
||||
)
|
||||
data class SeriesEntity(
|
||||
@PrimaryKey val id: UUID,
|
||||
val libraryId: UUID,
|
||||
val name: String,
|
||||
val synopsis: String,
|
||||
val year: String,
|
||||
val heroImageUrl: String,
|
||||
val unwatchedEpisodeCount: Int,
|
||||
val seasonCount: Int
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
package hu.bbara.purefin.core.data.local.room
|
||||
|
||||
import androidx.room.TypeConverter
|
||||
import java.util.UUID
|
||||
|
||||
/**
|
||||
* Stores UUIDs as strings for Room in-memory database.
|
||||
*/
|
||||
class UuidConverters {
|
||||
@TypeConverter
|
||||
fun fromString(value: String?): UUID? = value?.let(UUID::fromString)
|
||||
|
||||
@TypeConverter
|
||||
fun uuidToString(uuid: UUID?): String? = uuid?.toString()
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package hu.bbara.purefin.core.data.local.room.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import androidx.room.Upsert
|
||||
import hu.bbara.purefin.core.data.local.room.CastMemberEntity
|
||||
import java.util.UUID
|
||||
|
||||
@Dao
|
||||
interface CastMemberDao {
|
||||
@Upsert
|
||||
suspend fun upsertAll(cast: List<CastMemberEntity>)
|
||||
|
||||
@Query("SELECT * FROM cast_members WHERE movieId = :movieId")
|
||||
suspend fun getByMovieId(movieId: UUID): List<CastMemberEntity>
|
||||
|
||||
@Query("SELECT * FROM cast_members WHERE seriesId = :seriesId")
|
||||
suspend fun getBySeriesId(seriesId: UUID): List<CastMemberEntity>
|
||||
|
||||
@Query("SELECT * FROM cast_members WHERE episodeId = :episodeId")
|
||||
suspend fun getByEpisodeId(episodeId: UUID): List<CastMemberEntity>
|
||||
|
||||
@Query("DELETE FROM cast_members WHERE movieId = :movieId")
|
||||
suspend fun deleteByMovieId(movieId: UUID)
|
||||
|
||||
@Query("DELETE FROM cast_members WHERE seriesId = :seriesId")
|
||||
suspend fun deleteBySeriesId(seriesId: UUID)
|
||||
|
||||
@Query("DELETE FROM cast_members WHERE episodeId = :episodeId")
|
||||
suspend fun deleteByEpisodeId(episodeId: UUID)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package hu.bbara.purefin.core.data.local.room.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import androidx.room.Upsert
|
||||
import hu.bbara.purefin.core.data.local.room.EpisodeEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.util.UUID
|
||||
|
||||
@Dao
|
||||
interface EpisodeDao {
|
||||
@Upsert
|
||||
suspend fun upsert(episode: EpisodeEntity)
|
||||
|
||||
@Upsert
|
||||
suspend fun upsertAll(episodes: List<EpisodeEntity>)
|
||||
|
||||
@Query("SELECT * FROM episodes WHERE seriesId = :seriesId")
|
||||
suspend fun getBySeriesId(seriesId: UUID): List<EpisodeEntity>
|
||||
|
||||
@Query("SELECT * FROM episodes WHERE seasonId = :seasonId")
|
||||
suspend fun getBySeasonId(seasonId: UUID): List<EpisodeEntity>
|
||||
|
||||
@Query("SELECT * FROM episodes")
|
||||
fun observeAll(): Flow<List<EpisodeEntity>>
|
||||
|
||||
@Query("SELECT * FROM episodes WHERE id = :id")
|
||||
suspend fun getById(id: UUID): EpisodeEntity?
|
||||
|
||||
@Query("UPDATE episodes SET progress = :progress, watched = :watched WHERE id = :id")
|
||||
suspend fun updateProgress(id: UUID, progress: Double?, watched: Boolean)
|
||||
|
||||
@Query("SELECT COUNT(*) FROM episodes WHERE seriesId = :seriesId AND watched = 0")
|
||||
suspend fun countUnwatchedBySeries(seriesId: UUID): Int
|
||||
|
||||
@Query("SELECT COUNT(*) FROM episodes WHERE seasonId = :seasonId AND watched = 0")
|
||||
suspend fun countUnwatchedBySeason(seasonId: UUID): Int
|
||||
|
||||
@Query("DELETE FROM episodes WHERE seriesId = :seriesId")
|
||||
suspend fun deleteBySeriesId(seriesId: UUID)
|
||||
|
||||
@Query("DELETE FROM episodes WHERE seasonId = :seasonId")
|
||||
suspend fun deleteBySeasonId(seasonId: UUID)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package hu.bbara.purefin.core.data.local.room.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import androidx.room.Upsert
|
||||
import hu.bbara.purefin.core.data.local.room.LibraryEntity
|
||||
import hu.bbara.purefin.core.data.local.room.LibraryWithContent
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
@Dao
|
||||
interface LibraryDao {
|
||||
@Upsert
|
||||
suspend fun upsert(library: LibraryEntity)
|
||||
|
||||
@Upsert
|
||||
suspend fun upsertAll(libraries: List<LibraryEntity>)
|
||||
|
||||
@Query("SELECT * FROM library")
|
||||
fun observeAll(): Flow<List<LibraryEntity>>
|
||||
|
||||
@Query("SELECT * FROM library")
|
||||
fun observeAllWithContent(): Flow<List<LibraryWithContent>>
|
||||
|
||||
@Query("SELECT * FROM library")
|
||||
suspend fun getAll(): List<LibraryEntity>
|
||||
|
||||
@Query("DELETE FROM library")
|
||||
suspend fun deleteAll()
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package hu.bbara.purefin.core.data.local.room.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import androidx.room.Upsert
|
||||
import hu.bbara.purefin.core.data.local.room.MovieEntity
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.util.UUID
|
||||
|
||||
@Dao
|
||||
interface MovieDao {
|
||||
@Upsert
|
||||
suspend fun upsert(movie: MovieEntity)
|
||||
|
||||
@Upsert
|
||||
suspend fun upsertAll(movies: List<MovieEntity>)
|
||||
|
||||
@Query("SELECT * FROM movies")
|
||||
suspend fun getAll(): List<MovieEntity>
|
||||
|
||||
@Query("SELECT * FROM movies")
|
||||
fun observeAll(): Flow<List<MovieEntity>>
|
||||
|
||||
@Query("SELECT * FROM movies WHERE id = :id")
|
||||
suspend fun getById(id: UUID): MovieEntity?
|
||||
|
||||
@Query("UPDATE movies SET progress = :progress, watched = :watched WHERE id = :id")
|
||||
suspend fun updateProgress(id: UUID, progress: Double?, watched: Boolean)
|
||||
|
||||
@Query("DELETE FROM movies WHERE id = :id")
|
||||
suspend fun deleteById(id: UUID)
|
||||
|
||||
@Query("DELETE FROM movies")
|
||||
suspend fun clear()
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package hu.bbara.purefin.core.data.local.room.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import androidx.room.Upsert
|
||||
import hu.bbara.purefin.core.data.local.room.SeasonEntity
|
||||
import java.util.UUID
|
||||
|
||||
@Dao
|
||||
interface SeasonDao {
|
||||
@Upsert
|
||||
suspend fun upsert(season: SeasonEntity)
|
||||
|
||||
@Upsert
|
||||
suspend fun upsertAll(seasons: List<SeasonEntity>)
|
||||
|
||||
@Query("SELECT * FROM seasons WHERE seriesId = :seriesId")
|
||||
suspend fun getBySeriesId(seriesId: UUID): List<SeasonEntity>
|
||||
|
||||
@Query("SELECT * FROM seasons WHERE id = :id")
|
||||
suspend fun getById(id: UUID): SeasonEntity?
|
||||
|
||||
@Query("UPDATE seasons SET unwatchedEpisodeCount = :count WHERE id = :id")
|
||||
suspend fun updateUnwatchedCount(id: UUID, count: Int)
|
||||
|
||||
@Query("DELETE FROM seasons WHERE seriesId = :seriesId")
|
||||
suspend fun deleteBySeriesId(seriesId: UUID)
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package hu.bbara.purefin.core.data.local.room.dao
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Query
|
||||
import androidx.room.Transaction
|
||||
import androidx.room.Upsert
|
||||
import hu.bbara.purefin.core.data.local.room.SeriesEntity
|
||||
import hu.bbara.purefin.core.data.local.room.SeriesWithSeasonsAndEpisodes
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import java.util.UUID
|
||||
|
||||
@Dao
|
||||
interface SeriesDao {
|
||||
@Upsert
|
||||
suspend fun upsert(series: SeriesEntity)
|
||||
|
||||
@Upsert
|
||||
suspend fun upsertAll(series: List<SeriesEntity>)
|
||||
|
||||
@Query("SELECT * FROM series")
|
||||
suspend fun getAll(): List<SeriesEntity>
|
||||
|
||||
@Query("SELECT * FROM series")
|
||||
fun observeAll(): Flow<List<SeriesEntity>>
|
||||
|
||||
@Transaction
|
||||
@Query("SELECT * FROM series WHERE id = :id")
|
||||
fun observeWithContent(id: UUID): Flow<SeriesWithSeasonsAndEpisodes?>
|
||||
|
||||
@Query("SELECT * FROM series WHERE id = :id")
|
||||
suspend fun getById(id: UUID): SeriesEntity?
|
||||
|
||||
@Query("UPDATE series SET unwatchedEpisodeCount = :count WHERE id = :id")
|
||||
suspend fun updateUnwatchedCount(id: UUID, count: Int)
|
||||
|
||||
@Query("DELETE FROM series")
|
||||
suspend fun clear()
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package hu.bbara.purefin.core.data.navigation
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.jellyfin.sdk.model.serializer.UUIDSerializer
|
||||
import java.util.UUID
|
||||
|
||||
@Serializable
|
||||
data class EpisodeDto(
|
||||
@Serializable(with = UUIDSerializer::class)
|
||||
val id: UUID,
|
||||
@Serializable(with = UUIDSerializer::class)
|
||||
val seasonId: UUID,
|
||||
@Serializable(with = UUIDSerializer::class)
|
||||
val seriesId: UUID,
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
package hu.bbara.purefin.core.data.navigation
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.jellyfin.sdk.model.UUID
|
||||
import org.jellyfin.sdk.model.serializer.UUIDSerializer
|
||||
|
||||
@Serializable
|
||||
data class LibraryDto (
|
||||
@Serializable(with = UUIDSerializer::class)
|
||||
val id: UUID,
|
||||
val name: String
|
||||
)
|
||||
@@ -0,0 +1,11 @@
|
||||
package hu.bbara.purefin.core.data.navigation
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.jellyfin.sdk.model.serializer.UUIDSerializer
|
||||
import java.util.UUID
|
||||
|
||||
@Serializable
|
||||
data class MovieDto(
|
||||
@Serializable(with = UUIDSerializer::class)
|
||||
val id: UUID
|
||||
)
|
||||
@@ -0,0 +1,45 @@
|
||||
package hu.bbara.purefin.core.data.navigation
|
||||
|
||||
import androidx.compose.runtime.ProvidableCompositionLocal
|
||||
import androidx.compose.runtime.staticCompositionLocalOf
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.SharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
|
||||
sealed interface NavigationCommand {
|
||||
data class Navigate(val route: Route) : NavigationCommand
|
||||
data class ReplaceAll(val route: Route) : NavigationCommand
|
||||
data object Pop : NavigationCommand
|
||||
}
|
||||
|
||||
interface NavigationManager {
|
||||
val commands: SharedFlow<NavigationCommand>
|
||||
fun navigate(route: Route)
|
||||
fun replaceAll(route: Route)
|
||||
fun pop()
|
||||
}
|
||||
|
||||
class DefaultNavigationManager : NavigationManager {
|
||||
private val _commands =
|
||||
MutableSharedFlow<NavigationCommand>(
|
||||
extraBufferCapacity = 64,
|
||||
onBufferOverflow = BufferOverflow.DROP_OLDEST
|
||||
)
|
||||
override val commands: SharedFlow<NavigationCommand> = _commands.asSharedFlow()
|
||||
|
||||
override fun navigate(route: Route) {
|
||||
_commands.tryEmit(NavigationCommand.Navigate(route))
|
||||
}
|
||||
|
||||
override fun replaceAll(route: Route) {
|
||||
_commands.tryEmit(NavigationCommand.ReplaceAll(route))
|
||||
}
|
||||
|
||||
override fun pop() {
|
||||
_commands.tryEmit(NavigationCommand.Pop)
|
||||
}
|
||||
}
|
||||
|
||||
val LocalNavigationManager: ProvidableCompositionLocal<NavigationManager> =
|
||||
staticCompositionLocalOf { error("NavigationManager not provided") }
|
||||
@@ -0,0 +1,15 @@
|
||||
package hu.bbara.purefin.core.data.navigation
|
||||
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
object NavigationManagerModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideNavigationManager(): NavigationManager = DefaultNavigationManager()
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package hu.bbara.purefin.core.data.navigation
|
||||
|
||||
import androidx.navigation3.runtime.NavKey
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
sealed interface Route : NavKey {
|
||||
@Serializable
|
||||
data object Home: Route
|
||||
|
||||
@Serializable
|
||||
data class MovieRoute(val item : MovieDto) : Route
|
||||
|
||||
@Serializable
|
||||
data class SeriesRoute(val item : SeriesDto) : Route
|
||||
|
||||
@Serializable
|
||||
data class EpisodeRoute(val item : EpisodeDto) : Route
|
||||
|
||||
@Serializable
|
||||
data class LibraryRoute(val library : LibraryDto) : Route
|
||||
|
||||
@Serializable
|
||||
data object LoginRoute : Route
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package hu.bbara.purefin.core.data.navigation
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.jellyfin.sdk.model.serializer.UUIDSerializer
|
||||
import java.util.UUID
|
||||
|
||||
@Serializable
|
||||
data class SeriesDto(
|
||||
@Serializable(with = UUIDSerializer::class)
|
||||
val id: UUID
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
package hu.bbara.purefin.core.data.session
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import org.jellyfin.sdk.model.serializer.UUIDSerializer
|
||||
import java.util.UUID
|
||||
|
||||
@Serializable
|
||||
data class UserSession(
|
||||
val accessToken: String,
|
||||
val url: String,
|
||||
@Serializable(with = UUIDSerializer::class)
|
||||
val userId: UUID?,
|
||||
val loggedIn: Boolean,
|
||||
val isOfflineMode: Boolean = false
|
||||
)
|
||||
@@ -0,0 +1,40 @@
|
||||
package hu.bbara.purefin.core.data.session
|
||||
|
||||
import android.content.Context
|
||||
import androidx.datastore.core.DataStore
|
||||
import androidx.datastore.core.DataStoreFactory
|
||||
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler
|
||||
import androidx.datastore.dataStoreFile
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import dagger.hilt.components.SingletonComponent
|
||||
import javax.inject.Singleton
|
||||
|
||||
@Module
|
||||
@InstallIn(SingletonComponent::class)
|
||||
class UserSessionModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideUserProfileDataStore(
|
||||
@ApplicationContext context: Context
|
||||
): DataStore<UserSession> {
|
||||
return DataStoreFactory.create(
|
||||
serializer = UserSessionSerializer,
|
||||
produceFile = { context.dataStoreFile("user_session.json") },
|
||||
corruptionHandler = ReplaceFileCorruptionHandler(
|
||||
produceNewData = { UserSessionSerializer.defaultValue }
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideUserSessionRepository(
|
||||
userSessionDataStore: DataStore<UserSession>
|
||||
): UserSessionRepository {
|
||||
return UserSessionRepository(userSessionDataStore)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package hu.bbara.purefin.core.data.session
|
||||
|
||||
import androidx.datastore.core.DataStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import java.util.UUID
|
||||
import javax.inject.Inject
|
||||
|
||||
class UserSessionRepository @Inject constructor(
|
||||
private val userSessionDataStore: DataStore<UserSession>
|
||||
) {
|
||||
val session: Flow<UserSession> = userSessionDataStore.data
|
||||
|
||||
val serverUrl: Flow<String> = session
|
||||
.map { it.url }
|
||||
|
||||
suspend fun setServerUrl(serverUrl: String) {
|
||||
userSessionDataStore.updateData {
|
||||
it.copy(url = serverUrl)
|
||||
}
|
||||
}
|
||||
|
||||
val accessToken: Flow<String> = session
|
||||
.map { it.accessToken }
|
||||
|
||||
suspend fun setAccessToken(accessToken: String) {
|
||||
userSessionDataStore.updateData {
|
||||
it.copy(accessToken = accessToken)
|
||||
}
|
||||
}
|
||||
|
||||
val userId: Flow<UUID?> = session
|
||||
.map { it.userId }
|
||||
|
||||
suspend fun setUserId(userId: UUID?) {
|
||||
userSessionDataStore.updateData {
|
||||
it.copy(userId = userId)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getUserId(): UUID? = userId.first()
|
||||
|
||||
val isLoggedIn: Flow<Boolean> = session.map { it.loggedIn }.distinctUntilChanged()
|
||||
|
||||
suspend fun setLoggedIn(isLoggedIn: Boolean) {
|
||||
userSessionDataStore.updateData {
|
||||
it.copy(loggedIn = isLoggedIn)
|
||||
}
|
||||
}
|
||||
|
||||
val isOfflineMode: Flow<Boolean> = session.map { it.isOfflineMode }.distinctUntilChanged()
|
||||
|
||||
suspend fun setOfflineMode(isOffline: Boolean) {
|
||||
userSessionDataStore.updateData {
|
||||
it.copy(isOfflineMode = isOffline)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package hu.bbara.purefin.core.data.session
|
||||
|
||||
import androidx.datastore.core.CorruptionException
|
||||
import androidx.datastore.core.Serializer
|
||||
import kotlinx.serialization.SerializationException
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
object UserSessionSerializer : Serializer<UserSession> {
|
||||
override val defaultValue: UserSession
|
||||
get() = UserSession(accessToken = "", url = "", loggedIn = false, userId = null, isOfflineMode = false)
|
||||
|
||||
override suspend fun readFrom(input: InputStream): UserSession {
|
||||
try {
|
||||
return Json.decodeFromString<UserSession>(
|
||||
input.readBytes().decodeToString()
|
||||
)
|
||||
} catch (serialization: SerializationException) {
|
||||
throw CorruptionException("proto", serialization)
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun writeTo(t: UserSession, output: OutputStream) {
|
||||
output.write(
|
||||
Json.encodeToString(t)
|
||||
.encodeToByteArray()
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user