-
Notifications
You must be signed in to change notification settings - Fork 223
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix crash when chapters have 0 length durations (#2060)
Co-authored-by: Tiago Marques <[email protected]>
- Loading branch information
Showing
10 changed files
with
375 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 44 additions & 27 deletions
71
...s/src/main/java/au/com/shiftyjelly/pocketcasts/repositories/podcast/ChapterManagerImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,65 @@ | ||
package au.com.shiftyjelly.pocketcasts.repositories.podcast | ||
|
||
import au.com.shiftyjelly.pocketcasts.models.db.dao.ChapterDao | ||
import au.com.shiftyjelly.pocketcasts.models.db.dao.EpisodeDao | ||
import au.com.shiftyjelly.pocketcasts.models.entity.BaseEpisode | ||
import au.com.shiftyjelly.pocketcasts.models.to.Chapter | ||
import au.com.shiftyjelly.pocketcasts.models.to.Chapters | ||
import au.com.shiftyjelly.pocketcasts.models.to.DbChapter | ||
import javax.inject.Inject | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.milliseconds | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.distinctUntilChangedBy | ||
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull | ||
|
||
class ChapterManagerImpl @Inject constructor( | ||
private val chapterDao: ChapterDao, | ||
private val episodeDao: EpisodeDao, | ||
private val episodeManager: EpisodeManager, | ||
) : ChapterManager { | ||
override suspend fun updateChapters(episodeUuid: String, chapters: List<DbChapter>) { | ||
chapterDao.replaceAllChapters(episodeUuid, chapters) | ||
override suspend fun updateChapters( | ||
episodeUuid: String, | ||
chapters: List<DbChapter>, | ||
forceUpdate: Boolean, | ||
) { | ||
if (forceUpdate) { | ||
chapterDao.replaceAllChapters(episodeUuid, chapters) | ||
} else { | ||
chapterDao.replaceAllChaptersIfMoreIsPassed(episodeUuid, chapters) | ||
} | ||
} | ||
|
||
override fun observerChaptersForEpisode(episodeUuid: String) = chapterDao | ||
.observerChaptersForEpisode(episodeUuid) | ||
.map { it.toChapters(episodeUuid) } | ||
override fun observerChaptersForEpisode(episodeUuid: String) = combine( | ||
episodeManager.observeEpisodeByUuid(episodeUuid).distinctUntilChangedBy(BaseEpisode::deselectedChapters), | ||
chapterDao.observerChaptersForEpisode(episodeUuid), | ||
) { episode, dbChapters -> dbChapters.toChapters(episode) } | ||
|
||
private fun List<DbChapter>.toChapters(episode: BaseEpisode): Chapters { | ||
val chaptersList = asSequence() | ||
.fixChapterTimestamps(episode) | ||
.filter { it.duration > Duration.ZERO } | ||
.mapIndexed { index, chapter -> chapter.copy(index = index + 1) } | ||
.toList() | ||
return Chapters(chaptersList) | ||
} | ||
|
||
private suspend fun List<DbChapter>.toChapters(episodeUuid: String): Chapters { | ||
val deselectedChapters = episodeDao.findByUuid(episodeUuid)?.deselectedChapters.orEmpty() | ||
val chaptersList = withIndex().windowed(size = 2, partialWindows = true) { window -> | ||
val index = window[0].index + 1 | ||
val firstChapter = window[0].value | ||
val secondChapter = window.getOrNull(1)?.value | ||
private fun Sequence<DbChapter>.fixChapterTimestamps(episode: BaseEpisode) = withIndex().windowed(size = 2, partialWindows = true) { window -> | ||
val index = window[0].index | ||
val chapterIndex = window[0].index + 1 | ||
val firstChapter = window[0].value | ||
val secondChapter = window.getOrNull(1)?.value | ||
|
||
val secondEndTime = secondChapter?.startTimeMs?.milliseconds ?: Duration.INFINITE | ||
val fixedEndTime = firstChapter.endTimeMs?.milliseconds?.takeIf { it <= secondEndTime } ?: secondEndTime | ||
val newStartTime = if (index == 0) Duration.ZERO else firstChapter.startTimeMs.milliseconds | ||
val secondStartTime = secondChapter?.startTimeMs?.milliseconds ?: episode.durationMs.milliseconds | ||
val newEndTime = firstChapter.endTimeMs?.milliseconds?.takeIf { it <= secondStartTime && it > newStartTime } ?: secondStartTime | ||
|
||
Chapter( | ||
title = firstChapter.title.orEmpty(), | ||
startTime = firstChapter.startTimeMs.milliseconds, | ||
endTime = fixedEndTime, | ||
url = firstChapter.url?.toHttpUrlOrNull(), | ||
imagePath = firstChapter.imageUrl, | ||
index = index, | ||
selected = index !in deselectedChapters, | ||
) | ||
} | ||
return Chapters(chaptersList) | ||
Chapter( | ||
title = firstChapter.title.orEmpty(), | ||
startTime = newStartTime, | ||
endTime = newEndTime, | ||
url = firstChapter.url?.toHttpUrlOrNull(), | ||
imagePath = firstChapter.imageUrl, | ||
index = chapterIndex, | ||
selected = chapterIndex !in episode.deselectedChapters, | ||
) | ||
} | ||
} |
Oops, something went wrong.