feat: add smart download management with enable and delete options

This commit is contained in:
2026-05-14 14:05:51 +02:00
parent f60aba5a72
commit a0a51ef436
7 changed files with 66 additions and 7 deletions

View File

@@ -13,5 +13,7 @@ interface MediaDownloadController {
suspend fun downloadEpisodes(episodeIds: List<UUID>)
suspend fun cancelEpisodeDownload(episodeId: UUID)
suspend fun enableSmartDownload(seriesId: UUID)
suspend fun deleteSmartDownloads(seriesId: UUID)
fun isSmartDownloadEnabled(seriesId: UUID): Flow<Boolean>
suspend fun syncSmartDownloads()
}

View File

@@ -206,11 +206,16 @@ class MediaDownloadManager @Inject constructor(
syncSmartDownloadsForSeries(seriesId)
}
suspend fun disableSmartDownload(seriesId: UUID) {
smartDownloadStore.disable(seriesId)
override suspend fun deleteSmartDownloads(seriesId: UUID) {
withContext(Dispatchers.IO) {
smartDownloadStore.disable(seriesId)
offlineCatalogStore.getEpisodesBySeries(seriesId).forEach { episode ->
cancelEpisodeDownload(episode.id)
}
}
}
fun isSmartDownloadEnabled(seriesId: UUID): Flow<Boolean> = smartDownloadStore.observe(seriesId)
override fun isSmartDownloadEnabled(seriesId: UUID): Flow<Boolean> = smartDownloadStore.observe(seriesId)
override suspend fun syncSmartDownloads() {
withContext(Dispatchers.IO) {

View File

@@ -88,9 +88,15 @@ class EpisodeScreenViewModel @Inject constructor(
is DownloadState.NotDownloaded, is DownloadState.Failed -> {
mediaDownloadManager.downloadEpisode(episodeId)
}
is DownloadState.Downloading, is DownloadState.Downloaded -> {
is DownloadState.Downloading -> {
mediaDownloadManager.cancelEpisodeDownload(episodeId)
}
is DownloadState.Downloaded -> {
mediaDownloadManager.cancelEpisodeDownload(episodeId)
if (_episode.value?.offline == true) {
navigationManager.pop()
}
}
}
}
}

View File

@@ -80,6 +80,9 @@ class MovieScreenViewModel @Inject constructor(
}
is DownloadState.Downloaded -> {
mediaDownloadManager.cancelDownload(movieId)
if (_movie.value?.offline == true) {
navigationManager.pop()
}
}
}
}

View File

@@ -48,6 +48,17 @@ class SeriesViewModel @Inject constructor(
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null)
@OptIn(ExperimentalCoroutinesApi::class)
val isSmartDownloadEnabled: StateFlow<Boolean> = _series
.flatMapLatest { series ->
if (series == null) {
flowOf(false)
} else {
mediaDownloadManager.isSmartDownloadEnabled(series.id)
}
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), false)
private val _seriesDownloadState = MutableStateFlow<DownloadState>(DownloadState.NotDownloaded)
val seriesDownloadState: StateFlow<DownloadState> = _seriesDownloadState
@@ -111,6 +122,15 @@ class SeriesViewModel @Inject constructor(
}
}
fun deleteSmartDownloads(seriesId: UUID) {
viewModelScope.launch {
mediaDownloadManager.deleteSmartDownloads(seriesId)
if (_series.value?.offline == true) {
navigationManager.pop()
}
}
}
fun downloadSeries(seriesData: Series) {
viewModelScope.launch {
val mediaCatalogReader = selectedMediaCatalogReader()