From 4eb4ec7de06f8dd2db39bfa53084bec0f1de4779 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Tue, 12 Sep 2023 19:32:36 +0300 Subject: [PATCH] Fix nsfw sources filtering --- .../explore/data/MangaSourcesRepository.kt | 63 +++++++------------ 1 file changed, 21 insertions(+), 42 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt b/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt index 23dbfd026..711cad065 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/explore/data/MangaSourcesRepository.kt @@ -3,6 +3,7 @@ package org.koitharu.kotatsu.explore.data import androidx.room.withTransaction import dagger.Reusable import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.map @@ -11,12 +12,12 @@ import org.koitharu.kotatsu.core.db.MangaDatabase import org.koitharu.kotatsu.core.db.dao.MangaSourcesDao import org.koitharu.kotatsu.core.db.entity.MangaSourceEntity import org.koitharu.kotatsu.core.model.MangaSource +import org.koitharu.kotatsu.core.model.isNsfw import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.observeAsFlow import org.koitharu.kotatsu.core.ui.util.ReversibleHandle import org.koitharu.kotatsu.parsers.model.ContentType import org.koitharu.kotatsu.parsers.model.MangaSource -import org.koitharu.kotatsu.parsers.util.move import java.util.Collections import java.util.EnumSet import javax.inject.Inject @@ -76,14 +77,6 @@ class MangaSourcesRepository @Inject constructor( } } - suspend fun setSourcesEnabled(sources: Iterable, isEnabled: Boolean) { - db.withTransaction { - for (s in sources) { - dao.setEnabled(s.name, isEnabled) - } - } - } - suspend fun disableAllSources() { db.withTransaction { assimilateNewSources() @@ -99,46 +92,20 @@ class MangaSourcesRepository @Inject constructor( } } - suspend fun setPosition(source: MangaSource, index: Int) { - db.withTransaction { - val all = dao.findAll().toMutableList() - val sourceIndex = all.indexOfFirst { x -> x.source == source.name } - if (sourceIndex !in all.indices) { - val entity = MangaSourceEntity( - source = source.name, - isEnabled = false, - sortKey = index, - ) - all.add(index, entity) - dao.upsert(entity) - } else { - all.move(sourceIndex, index) - } - for ((i, e) in all.withIndex()) { - if (e.sortKey != i) { - dao.setSortKey(e.source, i) - } - } - } - } - - fun observeNewSources(): Flow> = dao.observeAll().map { entities -> + fun observeNewSources(): Flow> = combine( + dao.observeAll(), + observeIsNsfwDisabled(), + ) { entities, skipNsfw -> val result = EnumSet.copyOf(remoteSources) for (e in entities) { result.remove(MangaSource(e.source)) } + if (skipNsfw) { + result.removeAll { x -> x.isNsfw() } + } result }.distinctUntilChanged() - suspend fun getNewSources(): Set { - val entities = dao.findAll() - val result = EnumSet.copyOf(remoteSources) - for (e in entities) { - result.remove(MangaSource(e.source)) - } - return result - } - suspend fun assimilateNewSources(): Set { val new = getNewSources() if (new.isEmpty()) { @@ -153,6 +120,9 @@ class MangaSourcesRepository @Inject constructor( ) } dao.insertIfAbsent(entities) + if (settings.isNsfwContentDisabled) { + new.removeAll { x -> x.isNsfw() } + } return new } @@ -160,6 +130,15 @@ class MangaSourcesRepository @Inject constructor( return dao.findAll().isEmpty() } + private suspend fun getNewSources(): MutableSet { + val entities = dao.findAll() + val result = EnumSet.copyOf(remoteSources) + for (e in entities) { + result.remove(MangaSource(e.source)) + } + return result + } + private fun List.toSources(skipNsfwSources: Boolean): List { val result = ArrayList(size) for (entity in this) {