Skip to content

Commit

Permalink
Fix a rare crash when the position was updated but in the mean time t…
Browse files Browse the repository at this point in the history
…he chapter was removed.
  • Loading branch information
PaulWoitaschek committed Jun 6, 2023
1 parent 6a461fa commit fdabca4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
7 changes: 3 additions & 4 deletions data/src/main/kotlin/voice/data/BookContent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ data class BookContent(
) {

@Ignore
val currentChapterIndex = chapters.indexOf(currentChapter).also { require(it != -1) }
val currentChapterIndex = chapters.indexOf(currentChapter)

init {
require(currentChapter in chapters)
require(positionInChapter >= 0) {
"invalid position$positionInChapter"
require(currentChapter in chapters && positionInChapter >= 0) {
"invalid data in $this"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import voice.data.repo.BookRepository
import voice.logging.core.Logger
import voice.playback.session.MediaId
import voice.playback.session.toMediaIdOrNull
import javax.inject.Inject
Expand Down Expand Up @@ -53,11 +54,17 @@ class PositionUpdater
.takeIf { it >= 0 } ?: return
val mediaId = mediaItem.mediaId.toMediaIdOrNull() ?: return
mediaId as MediaId.Chapter
val chapterId = mediaId.chapterId
bookRepo.updateBook(mediaId.bookId) { content ->
content.copy(
currentChapter = mediaId.chapterId,
positionInChapter = currentPosition,
)
if (chapterId in content.chapters) {
content.copy(
currentChapter = chapterId,
positionInChapter = currentPosition,
)
} else {
Logger.e("$mediaId not in $content")
content
}
}
}
}

0 comments on commit fdabca4

Please sign in to comment.