|
|
|
|
@ -3,8 +3,17 @@ package org.koitharu.kotatsu.stats.data
|
|
|
|
|
import androidx.collection.LongIntMap
|
|
|
|
|
import androidx.collection.MutableLongIntMap
|
|
|
|
|
import androidx.room.withTransaction
|
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
|
|
|
import kotlinx.coroutines.flow.combine
|
|
|
|
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
|
|
|
|
import kotlinx.coroutines.flow.flatMapLatest
|
|
|
|
|
import kotlinx.coroutines.flow.flowOf
|
|
|
|
|
import kotlinx.coroutines.flow.map
|
|
|
|
|
import org.koitharu.kotatsu.core.db.MangaDatabase
|
|
|
|
|
import org.koitharu.kotatsu.core.db.entity.toManga
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
|
|
|
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.combine
|
|
|
|
|
import org.koitharu.kotatsu.stats.domain.StatsPeriod
|
|
|
|
|
import org.koitharu.kotatsu.stats.domain.StatsRecord
|
|
|
|
|
import java.util.NavigableMap
|
|
|
|
|
@ -13,6 +22,7 @@ import java.util.concurrent.TimeUnit
|
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
|
|
class StatsRepository @Inject constructor(
|
|
|
|
|
private val settings: AppSettings,
|
|
|
|
|
private val db: MangaDatabase,
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
|
|
@ -71,4 +81,14 @@ class StatsRepository @Inject constructor(
|
|
|
|
|
suspend fun clearStats() {
|
|
|
|
|
db.getStatsDao().clear()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun observeHasStats(mangaId: Long): Flow<Boolean> = settings.observeAsFlow(AppSettings.KEY_STATS_ENABLED) {
|
|
|
|
|
isStatsEnabled
|
|
|
|
|
}.flatMapLatest { isEnabled ->
|
|
|
|
|
if (isEnabled) {
|
|
|
|
|
db.getStatsDao().observeRowCount(mangaId).map { it > 0 }
|
|
|
|
|
} else {
|
|
|
|
|
flowOf(false)
|
|
|
|
|
}
|
|
|
|
|
}.distinctUntilChanged()
|
|
|
|
|
}
|
|
|
|
|
|