Fix nsfw sources filtering
This commit is contained in:
@@ -3,6 +3,7 @@ package org.koitharu.kotatsu.explore.data
|
|||||||
import androidx.room.withTransaction
|
import androidx.room.withTransaction
|
||||||
import dagger.Reusable
|
import dagger.Reusable
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
|
import kotlinx.coroutines.flow.combine
|
||||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
import kotlinx.coroutines.flow.flatMapLatest
|
import kotlinx.coroutines.flow.flatMapLatest
|
||||||
import kotlinx.coroutines.flow.map
|
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.dao.MangaSourcesDao
|
||||||
import org.koitharu.kotatsu.core.db.entity.MangaSourceEntity
|
import org.koitharu.kotatsu.core.db.entity.MangaSourceEntity
|
||||||
import org.koitharu.kotatsu.core.model.MangaSource
|
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.AppSettings
|
||||||
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
import org.koitharu.kotatsu.core.prefs.observeAsFlow
|
||||||
import org.koitharu.kotatsu.core.ui.util.ReversibleHandle
|
import org.koitharu.kotatsu.core.ui.util.ReversibleHandle
|
||||||
import org.koitharu.kotatsu.parsers.model.ContentType
|
import org.koitharu.kotatsu.parsers.model.ContentType
|
||||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||||
import org.koitharu.kotatsu.parsers.util.move
|
|
||||||
import java.util.Collections
|
import java.util.Collections
|
||||||
import java.util.EnumSet
|
import java.util.EnumSet
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
@@ -76,14 +77,6 @@ class MangaSourcesRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun setSourcesEnabled(sources: Iterable<MangaSource>, isEnabled: Boolean) {
|
|
||||||
db.withTransaction {
|
|
||||||
for (s in sources) {
|
|
||||||
dao.setEnabled(s.name, isEnabled)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun disableAllSources() {
|
suspend fun disableAllSources() {
|
||||||
db.withTransaction {
|
db.withTransaction {
|
||||||
assimilateNewSources()
|
assimilateNewSources()
|
||||||
@@ -99,46 +92,20 @@ class MangaSourcesRepository @Inject constructor(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun setPosition(source: MangaSource, index: Int) {
|
fun observeNewSources(): Flow<Set<MangaSource>> = combine(
|
||||||
db.withTransaction {
|
dao.observeAll(),
|
||||||
val all = dao.findAll().toMutableList()
|
observeIsNsfwDisabled(),
|
||||||
val sourceIndex = all.indexOfFirst { x -> x.source == source.name }
|
) { entities, skipNsfw ->
|
||||||
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<Set<MangaSource>> = dao.observeAll().map { entities ->
|
|
||||||
val result = EnumSet.copyOf(remoteSources)
|
val result = EnumSet.copyOf(remoteSources)
|
||||||
for (e in entities) {
|
for (e in entities) {
|
||||||
result.remove(MangaSource(e.source))
|
result.remove(MangaSource(e.source))
|
||||||
}
|
}
|
||||||
|
if (skipNsfw) {
|
||||||
|
result.removeAll { x -> x.isNsfw() }
|
||||||
|
}
|
||||||
result
|
result
|
||||||
}.distinctUntilChanged()
|
}.distinctUntilChanged()
|
||||||
|
|
||||||
suspend fun getNewSources(): Set<MangaSource> {
|
|
||||||
val entities = dao.findAll()
|
|
||||||
val result = EnumSet.copyOf(remoteSources)
|
|
||||||
for (e in entities) {
|
|
||||||
result.remove(MangaSource(e.source))
|
|
||||||
}
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun assimilateNewSources(): Set<MangaSource> {
|
suspend fun assimilateNewSources(): Set<MangaSource> {
|
||||||
val new = getNewSources()
|
val new = getNewSources()
|
||||||
if (new.isEmpty()) {
|
if (new.isEmpty()) {
|
||||||
@@ -153,6 +120,9 @@ class MangaSourcesRepository @Inject constructor(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
dao.insertIfAbsent(entities)
|
dao.insertIfAbsent(entities)
|
||||||
|
if (settings.isNsfwContentDisabled) {
|
||||||
|
new.removeAll { x -> x.isNsfw() }
|
||||||
|
}
|
||||||
return new
|
return new
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,6 +130,15 @@ class MangaSourcesRepository @Inject constructor(
|
|||||||
return dao.findAll().isEmpty()
|
return dao.findAll().isEmpty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private suspend fun getNewSources(): MutableSet<MangaSource> {
|
||||||
|
val entities = dao.findAll()
|
||||||
|
val result = EnumSet.copyOf(remoteSources)
|
||||||
|
for (e in entities) {
|
||||||
|
result.remove(MangaSource(e.source))
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
private fun List<MangaSourceEntity>.toSources(skipNsfwSources: Boolean): List<MangaSource> {
|
private fun List<MangaSourceEntity>.toSources(skipNsfwSources: Boolean): List<MangaSource> {
|
||||||
val result = ArrayList<MangaSource>(size)
|
val result = ArrayList<MangaSource>(size)
|
||||||
for (entity in this) {
|
for (entity in this) {
|
||||||
|
|||||||
Reference in New Issue
Block a user