Skip to content

Commit

Permalink
prepare bs dialog search support
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed May 14, 2024
1 parent 1d2ab6f commit ef2ab83
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ actual class BurningSeriesResolver(
}

actual fun resolveByName(english: String?, romaji: String?): Set<Series> {
val englishTrimmed = english?.trim()?.ifBlank { null }?.replace("'", "")
val romajiTrimmed = romaji?.trim()?.ifBlank { null }?.replace("'", "")
val englishTrimmed = english?.trim()?.ifBlank { null }?.replace("'", "")?.trim()
val romajiTrimmed = romaji?.trim()?.ifBlank { null }?.replace("'", "")?.trim()

if (seriesClient == null || (englishTrimmed == null && romajiTrimmed == null)) {
return emptySet()
Expand All @@ -100,6 +100,25 @@ actual class BurningSeriesResolver(
} else {
"title LIKE '%$romajiTrimmed%'"
}

return seriesBySelection(selection)
}

actual fun resolveByName(value: String): Set<Series> {
val trimmed = value.trim().replace("'", "").trim()

return if (trimmed.length >= 3) {
seriesBySelection("title LIKE '%$trimmed%'")
} else {
emptySet()
}
}

private fun seriesBySelection(selection: String): Set<Series> {
if (seriesClient == null) {
return emptySet()
}

val seriesCursor = seriesClient.query(
seriesContentUri,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ expect class BurningSeriesResolver {
val isAvailable: Boolean
fun resolveWatchedEpisodes(): Set<Episode>
fun resolveByName(english: String?, romaji: String?): Set<Series>
fun resolveByName(value: String): Set<Series>

fun close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ interface MediumComponent : ContentHolderComponent {
fun showCharacter(character: Character)
fun toggleFavorite()
fun edit()

suspend fun searchBS(value: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import dev.datlag.aniflow.common.*
import dev.datlag.aniflow.model.*
import dev.datlag.aniflow.other.BurningSeriesResolver
import dev.datlag.aniflow.other.Constants
import dev.datlag.aniflow.other.Series
import dev.datlag.aniflow.other.UserHelper
import dev.datlag.aniflow.settings.Settings
import dev.datlag.aniflow.settings.model.CharLanguage
Expand Down Expand Up @@ -198,14 +199,28 @@ class MediumScreenComponent(
get() = burningSeriesResolver.isAvailable

@OptIn(ExperimentalCoroutinesApi::class)
override val bsOptions = title.mapLatest {
private val bsDefaultOptions = title.mapLatest {
burningSeriesResolver.resolveByName(it.english, it.romaji)
}.flowOn(ioDispatcher()).stateIn(
scope = ioScope(),
started = SharingStarted.WhileSubscribed(),
initialValue = emptySet()
)

private val bsSearch = MutableStateFlow<String?>(null)

@OptIn(ExperimentalCoroutinesApi::class)
private val bsSearchOptions = bsSearch.mapLatest {
it?.ifBlank { null }?.let(burningSeriesResolver::resolveByName).orEmpty()
}.flowOn(ioDispatcher())

override val bsOptions: Flow<Collection<Series>> = combine(
bsSearchOptions,
bsDefaultOptions
) { search, default ->
search.ifEmpty { default }
}

private val dialogNavigation = SlotNavigation<DialogConfig>()
@OptIn(ExperimentalCoroutinesApi::class)
override val dialog: Value<ChildSlot<DialogConfig, DialogComponent>> = childSlot(
Expand Down Expand Up @@ -309,4 +324,8 @@ class MediumScreenComponent(
override fun edit() {
dialogNavigation.activate(DialogConfig.Edit)
}

override suspend fun searchBS(value: String) {
bsSearch.update { value }
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package dev.datlag.aniflow.ui.navigation.screen.medium.component

import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LocalContentColor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.maxkeppeker.sheets.core.models.base.Header
import com.maxkeppeker.sheets.core.models.base.IconSource
import com.maxkeppeker.sheets.core.models.base.UseCaseState
import com.maxkeppeker.sheets.core.models.base.rememberUseCaseState
import com.maxkeppeler.sheets.option.OptionDialog
import com.maxkeppeler.sheets.option.models.*
import com.maxkeppeler.sheets.option.models.DisplayMode
import com.maxkeppeler.sheets.option.models.Option
import com.maxkeppeler.sheets.option.models.OptionConfig
import com.maxkeppeler.sheets.option.models.OptionSelection
import dev.datlag.aniflow.SharedRes
import dev.datlag.aniflow.other.Series
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
Expand All @@ -25,7 +24,8 @@ import kotlinx.coroutines.flow.StateFlow
@Composable
fun BSDialog(
state: UseCaseState,
bsOptions: Collection<Series>
bsOptions: Collection<Series>,
onSearch: suspend (String) -> Unit
) {
OptionDialog(
state = state,
Expand All @@ -48,6 +48,23 @@ fun BSDialog(
tint = LocalContentColor.current
),
title = stringResource(SharedRes.strings.bs)
)
),
body = OptionBody.Custom {
var value by remember { mutableStateOf("") }

LaunchedEffect(value) {
onSearch(value)
}

OutlinedTextField(
modifier = Modifier.fillMaxWidth().padding(horizontal = 24.dp),
value = value,
onValueChange = { value = it },
placeholder = {
Text(text = "Search")
},
shape = MaterialTheme.shapes.medium
)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fun FABContent(
BSDialog(
state = bsState,
bsOptions = bsOptions,
onSearch = component::searchBS
)

RatingDialog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,9 @@ actual class BurningSeriesResolver {
return emptySet()
}

actual fun resolveByName(value: String): Set<Series> {
return emptySet()
}

actual fun close() { }
}

0 comments on commit ef2ab83

Please sign in to comment.