feat: Implement Episode download functionality

This commit is contained in:
2026-02-22 15:41:42 +01:00
parent 9ec09a0e94
commit 8c7ddab9c2
9 changed files with 220 additions and 12 deletions

View File

@@ -213,18 +213,18 @@ class InMemoryAppContentRepository @Inject constructor(
}
}
suspend fun loadMovie(movie: Movie): Movie {
val movieItem = jellyfinApiClient.getItemInfo(movie.id)
suspend fun loadMovie(movieId: UUID): Movie {
val movieItem = jellyfinApiClient.getItemInfo(movieId)
?: throw RuntimeException("Movie not found")
val updatedMovie = movieItem.toMovie(serverUrl(), movie.libraryId)
val updatedMovie = movieItem.toMovie(serverUrl(), movieItem.parentId!!)
mediaRepository._movies.update { it + (updatedMovie.id to updatedMovie) }
return updatedMovie
}
suspend fun loadSeries(series: Series): Series {
val seriesItem = jellyfinApiClient.getItemInfo(series.id)
suspend fun loadSeries(seriesId: UUID): Series {
val seriesItem = jellyfinApiClient.getItemInfo(seriesId)
?: throw RuntimeException("Series not found")
val updatedSeries = seriesItem.toSeries(serverUrl(), series.libraryId)
val updatedSeries = seriesItem.toSeries(serverUrl(), seriesItem.parentId!!)
mediaRepository._series.update { it + (updatedSeries.id to updatedSeries) }
return updatedSeries
}

View File

@@ -41,4 +41,10 @@ interface EpisodeDao {
@Query("DELETE FROM episodes WHERE seasonId = :seasonId")
suspend fun deleteBySeasonId(seasonId: UUID)
@Query("DELETE FROM episodes WHERE id = :id")
suspend fun deleteById(id: UUID)
@Query("SELECT COUNT(*) FROM episodes WHERE seasonId = :seasonId")
suspend fun countBySeasonId(seasonId: UUID): Int
}

View File

@@ -25,4 +25,10 @@ interface SeasonDao {
@Query("DELETE FROM seasons WHERE seriesId = :seriesId")
suspend fun deleteBySeriesId(seriesId: UUID)
@Query("DELETE FROM seasons WHERE id = :id")
suspend fun deleteById(id: UUID)
@Query("SELECT COUNT(*) FROM seasons WHERE seriesId = :seriesId")
suspend fun countBySeriesId(seriesId: UUID): Int
}

View File

@@ -35,4 +35,7 @@ interface SeriesDao {
@Query("DELETE FROM series")
suspend fun clear()
@Query("DELETE FROM series WHERE id = :id")
suspend fun deleteById(id: UUID)
}

View File

@@ -80,6 +80,14 @@ class OfflineRoomMediaLocalDataSource(
}
}
suspend fun saveSeason(season: Season) {
database.withTransaction {
seriesDao.getById(season.seriesId)
?: throw RuntimeException("Cannot add season without series. Season: $season")
seasonDao.upsert(season.toEntity())
}
}
suspend fun saveEpisode(episode: Episode) {
database.withTransaction {
seriesDao.getById(episode.seriesId)
@@ -89,6 +97,23 @@ class OfflineRoomMediaLocalDataSource(
}
}
suspend fun deleteEpisodeAndCleanup(episodeId: UUID) {
database.withTransaction {
val episode = episodeDao.getById(episodeId) ?: return@withTransaction
episodeDao.deleteById(episodeId)
val remainingEpisodesInSeason = episodeDao.countBySeasonId(episode.seasonId)
if (remainingEpisodesInSeason == 0) {
seasonDao.deleteById(episode.seasonId)
val remainingSeasonsInSeries = seasonDao.countBySeriesId(episode.seriesId)
if (remainingSeasonsInSeries == 0) {
seriesDao.deleteById(episode.seriesId)
}
}
}
}
suspend fun getMovies(): List<Movie> {
val movies = movieDao.getAll()
return movies.map { entity ->