From c778162345afb2d103dee595d564f93809e846ee Mon Sep 17 00:00:00 2001 From: DatLag Date: Fri, 17 May 2024 18:00:17 +0200 Subject: [PATCH] filter nsfw content if not allowed --- .../datlag/aniflow/module/NetworkModule.kt | 3 +++ .../datlag/aniflow/trace/TraceRepository.kt | 24 ++++++++++++------- .../aniflow/trace/model/SearchResponse.kt | 13 +++++++++- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/module/NetworkModule.kt b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/module/NetworkModule.kt index 4d06cda..724f753 100644 --- a/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/module/NetworkModule.kt +++ b/composeApp/src/commonMain/kotlin/dev/datlag/aniflow/module/NetworkModule.kt @@ -177,8 +177,11 @@ data object NetworkModule { ) } bindSingleton { + val appSettings = instance() + TraceRepository( trace = instance(), + nsfw = appSettings.adultContent ) } bindSingleton { diff --git a/trace/src/commonMain/kotlin/dev/datlag/aniflow/trace/TraceRepository.kt b/trace/src/commonMain/kotlin/dev/datlag/aniflow/trace/TraceRepository.kt index 81244a9..16c187c 100644 --- a/trace/src/commonMain/kotlin/dev/datlag/aniflow/trace/TraceRepository.kt +++ b/trace/src/commonMain/kotlin/dev/datlag/aniflow/trace/TraceRepository.kt @@ -7,21 +7,25 @@ import kotlinx.coroutines.flow.* import kotlinx.serialization.Serializable class TraceRepository( - private val trace: Trace + private val trace: Trace, + private val nsfw: Flow = flowOf(false), ) { private val byteArray = MutableStateFlow(null) @OptIn(ExperimentalCoroutinesApi::class) - val response: Flow = byteArray.transformLatest { - return@transformLatest if (it == null || it.isEmpty()) { + val response: Flow = combine(byteArray, nsfw.distinctUntilChanged()) { t1, t2 -> + t1 to t2 + }.transformLatest { (t1, t2) -> + return@transformLatest if (t1 == null || t1.isEmpty()) { emit(State.None) } else { emit( State.fromResponse( - CatchResult.repeat(2) { - trace.search(it) - }.asNullableSuccess() + response = CatchResult.repeat(2) { + trace.search(t1) + }.asNullableSuccess(), + nsfw = t2 ) ) } @@ -44,11 +48,13 @@ class TraceRepository( data object Error : State companion object { - fun fromResponse(response: SearchResponse?): State { - return if (response == null || response.isError) { + fun fromResponse(response: SearchResponse?, nsfw: Boolean): State { + val nsfwAware = response?.nsfwAware(nsfw) + + return if (nsfwAware == null || nsfwAware.isError) { Error } else { - Success(response) + Success(nsfwAware) } } } diff --git a/trace/src/commonMain/kotlin/dev/datlag/aniflow/trace/model/SearchResponse.kt b/trace/src/commonMain/kotlin/dev/datlag/aniflow/trace/model/SearchResponse.kt index 6d73ec2..5645386 100644 --- a/trace/src/commonMain/kotlin/dev/datlag/aniflow/trace/model/SearchResponse.kt +++ b/trace/src/commonMain/kotlin/dev/datlag/aniflow/trace/model/SearchResponse.kt @@ -9,7 +9,7 @@ data class SearchResponse( ) { @Transient - val isError = !error.isNullOrBlank() + val isError = !error.isNullOrBlank() || result.isEmpty() @Transient val combinedResults: Set = result.groupBy { it.aniList.id }.mapValues { entry -> @@ -25,6 +25,17 @@ data class SearchResponse( ) }.values.toSet() + fun nsfwAware(allowed: Boolean): SearchResponse { + return if (allowed) { + this + } else { + SearchResponse( + error = error, + result = result.filterNot { it.aniList.isAdult } + ) + } + } + @Serializable data class Result( @SerialName("anilist") val aniList: AniList,