refactor(stats): Remove some runBlocking usage

This commit is contained in:
Ahmad Ansori Palembani 2024-11-29 15:21:10 +07:00
parent e25f330118
commit 8cef33d9c6
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 30 additions and 21 deletions

View file

@ -238,9 +238,7 @@ class StatsDetailsController :
} }
} }
fun updateLibrary() { fun updateLibrary() = presenter.updateLibrary()
presenter.libraryMangas = presenter.getLibrary()
}
/** Set the toolbar to fully transparent or colored and translucent */ /** Set the toolbar to fully transparent or colored and translucent */
private fun colorToolbar(isColor: Boolean) { private fun colorToolbar(isColor: Boolean) {
@ -757,7 +755,7 @@ class StatsDetailsController :
override fun onBarValueChanged(highlight: Highlight?, e: Entry?) { override fun onBarValueChanged(highlight: Highlight?, e: Entry?) {
highlightedBar = highlight?.let { Triple(it.x, it.y, it.dataSetIndex) } highlightedBar = highlight?.let { Triple(it.x, it.y, it.dataSetIndex) }
highlightedDay = e?.let { presenter.historyByDayAndManga.keys.toTypedArray()[e.x.toInt()] } highlightedDay = e?.let { presenter.historyByDayAndManga.keys.toTypedArray()[e.x.toInt()] }
presenter.setupReadDuration(highlightedDay) presenter.doSetupReadDuration(highlightedDay)
updateStatsAdapter(true) updateStatsAdapter(true)
} }

View file

@ -33,6 +33,7 @@ import java.util.Calendar
import java.util.Locale import java.util.Locale
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.math.roundToInt import kotlin.math.roundToInt
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
@ -59,7 +60,7 @@ class StatsDetailsPresenter(
private val context private val context
get() = view?.view?.context ?: prefs.context get() = view?.view?.context ?: prefs.context
var libraryMangas = getLibrary() var libraryMangas = runBlocking { getLibrary() }
set(value) { set(value) {
field = value field = value
mangasDistinct = field.distinct() mangasDistinct = field.distinct()
@ -150,7 +151,7 @@ class StatsDetailsPresenter(
} }
} }
private fun setupSeriesType() { private suspend fun setupSeriesType() {
currentStats = ArrayList() currentStats = ArrayList()
val libraryFormat = mangasDistinct.filterByChip().groupBy { it.seriesType() } val libraryFormat = mangasDistinct.filterByChip().groupBy { it.seriesType() }
@ -170,7 +171,7 @@ class StatsDetailsPresenter(
sortCurrentStats() sortCurrentStats()
} }
private fun setupStatus() { private suspend fun setupStatus() {
currentStats = ArrayList() currentStats = ArrayList()
val libraryFormat = mangasDistinct.filterByChip().groupBy { it.status } val libraryFormat = mangasDistinct.filterByChip().groupBy { it.status }
@ -190,7 +191,7 @@ class StatsDetailsPresenter(
sortCurrentStats() sortCurrentStats()
} }
private fun setupScores() { private suspend fun setupScores() {
currentStats = ArrayList() currentStats = ArrayList()
val libraryFormat = mangasDistinct.filterByChip().groupBy { it.getMeanScoreToInt() } val libraryFormat = mangasDistinct.filterByChip().groupBy { it.getMeanScoreToInt() }
val scoreMap = StatsHelper.SCORE_COLOR_MAP.plus(null to pieColorList[1]) val scoreMap = StatsHelper.SCORE_COLOR_MAP.plus(null to pieColorList[1])
@ -212,7 +213,7 @@ class StatsDetailsPresenter(
} }
} }
private fun setupLanguages() { private suspend fun setupLanguages() {
currentStats = ArrayList() currentStats = ArrayList()
val libraryFormat = mangasDistinct.filterByChip().groupBy { it.getLanguage() } val libraryFormat = mangasDistinct.filterByChip().groupBy { it.getLanguage() }
@ -232,7 +233,7 @@ class StatsDetailsPresenter(
sortCurrentStats() sortCurrentStats()
} }
private fun setupLength() { private suspend fun setupLength() {
currentStats = ArrayList() currentStats = ArrayList()
var mangaFiltered = mangasDistinct.filterByChip() var mangaFiltered = mangasDistinct.filterByChip()
StatsHelper.STATS_LENGTH.forEach { range -> StatsHelper.STATS_LENGTH.forEach { range ->
@ -259,7 +260,7 @@ class StatsDetailsPresenter(
} }
} }
private fun setupTrackers() { private suspend fun setupTrackers() {
currentStats = ArrayList() currentStats = ArrayList()
val libraryFormat = mangasDistinct.filterByChip() val libraryFormat = mangasDistinct.filterByChip()
.map { it to getTracks(it).ifEmpty { listOf(null) } } .map { it to getTracks(it).ifEmpty { listOf(null) } }
@ -289,7 +290,7 @@ class StatsDetailsPresenter(
sortCurrentStats() sortCurrentStats()
} }
private fun setupSources() { private suspend fun setupSources() {
currentStats = ArrayList() currentStats = ArrayList()
val libraryFormat = mangasDistinct.filterByChip().groupBy { it.source } val libraryFormat = mangasDistinct.filterByChip().groupBy { it.source }
@ -312,7 +313,7 @@ class StatsDetailsPresenter(
sortCurrentStats() sortCurrentStats()
} }
private fun setupCategories() { private suspend fun setupCategories() {
currentStats = ArrayList() currentStats = ArrayList()
val libraryFormat = libraryMangas.filterByChip().groupBy { it.category } val libraryFormat = libraryMangas.filterByChip().groupBy { it.category }
val categories = getCategories() val categories = getCategories()
@ -335,7 +336,7 @@ class StatsDetailsPresenter(
sortCurrentStats() sortCurrentStats()
} }
private fun setupTags() { private suspend fun setupTags() {
currentStats = ArrayList() currentStats = ArrayList()
val mangaFiltered = mangasDistinct.filterByChip() val mangaFiltered = mangasDistinct.filterByChip()
val tags = mangaFiltered.flatMap { it.getTags() }.distinctBy { it.uppercase() } val tags = mangaFiltered.flatMap { it.getTags() }.distinctBy { it.uppercase() }
@ -382,7 +383,7 @@ class StatsDetailsPresenter(
} }
} }
fun setupReadDuration(day: Calendar? = null) { private suspend fun setupReadDuration(day: Calendar? = null) {
currentStats = ArrayList() currentStats = ArrayList()
historyByDayAndManga = history.mapValues { h -> historyByDayAndManga = history.mapValues { h ->
@ -410,6 +411,12 @@ class StatsDetailsPresenter(
currentStats?.sortByDescending { it.readDuration } currentStats?.sortByDescending { it.readDuration }
} }
fun doSetupReadDuration(day: Calendar? = null) {
presenterScope.launchIO {
setupReadDuration(day)
}
}
/** /**
* Filter the stat data according to the chips selected * Filter the stat data according to the chips selected
*/ */
@ -508,7 +515,7 @@ class StatsDetailsPresenter(
/** /**
* Get mean score rounded to two decimal of a list of manga * Get mean score rounded to two decimal of a list of manga
*/ */
private fun List<LibraryManga>.getMeanScoreRounded(): Double? { private suspend fun List<LibraryManga>.getMeanScoreRounded(): Double? {
val mangaTracks = this.map { it to getTracks(it) } val mangaTracks = this.map { it to getTracks(it) }
val scoresList = mangaTracks.filter { it.second.isNotEmpty() } val scoresList = mangaTracks.filter { it.second.isNotEmpty() }
.mapNotNull { it.second.getMeanScoreByTracker() } .mapNotNull { it.second.getMeanScoreByTracker() }
@ -518,7 +525,7 @@ class StatsDetailsPresenter(
/** /**
* Get mean score rounded to int of a single manga * Get mean score rounded to int of a single manga
*/ */
private fun LibraryManga.getMeanScoreToInt(): Int? { private suspend fun LibraryManga.getMeanScoreToInt(): Int? {
val mangaTracks = getTracks(this) val mangaTracks = getTracks(this)
val scoresList = mangaTracks.filter { it.score > 0 } val scoresList = mangaTracks.filter { it.score > 0 }
.mapNotNull { it.get10PointScore() } .mapNotNull { it.get10PointScore() }
@ -565,12 +572,16 @@ class StatsDetailsPresenter(
return StatsSort.entries.sorted().map { context.getString(it.resourceId) }.toTypedArray() return StatsSort.entries.sorted().map { context.getString(it.resourceId) }.toTypedArray()
} }
fun getTracks(manga: Manga): MutableList<Track> { suspend fun getTracks(manga: Manga): MutableList<Track> {
return runBlocking { getTrack.awaitAllByMangaId(manga.id) }.toMutableList() return getTrack.awaitAllByMangaId(manga.id).toMutableList()
} }
fun getLibrary(): MutableList<LibraryManga> { fun updateLibrary() {
return runBlocking { getLibraryManga.await() }.toMutableList() presenterScope.launch { libraryMangas = getLibrary() }
}
private suspend fun getLibrary(): MutableList<LibraryManga> {
return getLibraryManga.await().toMutableList()
} }
private fun getCategories(): MutableList<Category> { private fun getCategories(): MutableList<Category> {