Skip to content

Commit

Permalink
Let ReadingProgressManager handle dispatcher when stopping tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
xizzhu committed May 21, 2020
1 parent 6fa0d83 commit bfe007d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,15 @@ class ReadingProgressManager(private val bibleReadingRepository: BibleReadingRep
}
}

suspend fun stopTracking() {
trackReadingProgress()
currentVerseIndex = VerseIndex.INVALID
lastTimestamp = 0L
currentVerseIndexObserver = null
fun stopTracking() {
// uses GlobalScope to make sure this will be executed without being canceled
// uses Dispatchers.Main.immediate to make sure this will be executed immediately
GlobalScope.launch(Dispatchers.Main.immediate) {
trackReadingProgress()
currentVerseIndex = VerseIndex.INVALID
lastTimestamp = 0L
currentVerseIndexObserver = null
}
}

suspend fun read(): ReadingProgress = readingProgressRepository.read()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,7 @@ class ReadingViewModel(
bibleReadingManager.clearParallelTranslation()
}

fun currentVerseIndex(): Flow<VerseIndex> =
bibleReadingManager.currentVerseIndex().filterIsValid()
fun currentVerseIndex(): Flow<VerseIndex> = bibleReadingManager.currentVerseIndex().filterIsValid()

suspend fun saveCurrentVerseIndex(verseIndex: VerseIndex) {
bibleReadingManager.saveCurrentVerseIndex(verseIndex)
Expand All @@ -136,15 +135,13 @@ class ReadingViewModel(
)
}

fun chapterList(): Flow<ChapterListViewData> {
val currentVerseIndexFlow = bibleReadingManager.currentVerseIndex().filterIsValid()
val bookNamesFlow = bibleReadingManager.currentTranslation()
.filterNotEmpty()
.map { bibleReadingManager.readBookNames(it) }
return combine(currentVerseIndexFlow, bookNamesFlow) { currentVerseIndex, bookNames ->
ChapterListViewData(currentVerseIndex, bookNames)
}
}
fun chapterList(): Flow<ChapterListViewData> =
combine(bibleReadingManager.currentVerseIndex().filterIsValid(),
bibleReadingManager.currentTranslation()
.filterNotEmpty()
.map { bibleReadingManager.readBookNames(it) }) { currentVerseIndex, bookNames ->
ChapterListViewData(currentVerseIndex, bookNames)
}

fun bookName(translationShortName: String, bookIndex: Int): Flow<String> = flow {
emit(bibleReadingManager.readBookNames(translationShortName)[bookIndex])
Expand Down Expand Up @@ -286,9 +283,7 @@ class ReadingViewModel(
}

fun stopTracking() {
// uses GlobalScope to make sure this will be executed without being canceled
// uses Dispatchers.Main.immediate to make sure this will be executed immediately
GlobalScope.launch(Dispatchers.Main.immediate) { readingProgressManager.stopTracking() }
readingProgressManager.stopTracking()
}

fun verseUpdates(): Flow<VerseUpdate> = verseUpdates.asFlow()
Expand Down

0 comments on commit bfe007d

Please sign in to comment.