Skip to content

Commit

Permalink
scan selection dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Apr 29, 2024
1 parent 4456fc1 commit b6e0032
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,30 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.arkivanov.decompose.extensions.compose.subscribeAsState
import com.maxkeppeker.sheets.core.models.base.rememberUseCaseState
import com.maxkeppeler.sheets.option.OptionDialog
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.OptionDetails
import com.maxkeppeler.sheets.option.models.OptionSelection
import dev.chrisbanes.haze.haze
import dev.datlag.aniflow.LocalHaze
import dev.datlag.aniflow.LocalPaddingValues
import dev.datlag.aniflow.SharedRes
import dev.datlag.aniflow.common.asMedium
import dev.datlag.aniflow.common.isScrollingUp
import dev.datlag.aniflow.common.plus
import dev.datlag.aniflow.common.preferred
import dev.datlag.aniflow.other.StateSaver
import dev.datlag.aniflow.other.rememberImagePickerState
import dev.datlag.aniflow.trace.TraceStateMachine
import dev.datlag.aniflow.trace.model.SearchResponse
import dev.datlag.aniflow.ui.navigation.screen.initial.home.component.AiringOverview
import dev.datlag.aniflow.ui.navigation.screen.initial.home.component.PopularSeasonOverview
import dev.datlag.aniflow.ui.navigation.screen.initial.home.component.TrendingOverview
import dev.datlag.aniflow.ui.navigation.screen.initial.model.FABConfig
import dev.datlag.tooling.decompose.lifecycle.collectAsStateWithLifecycle
import io.github.aakira.napier.Napier
import dev.icerock.moko.resources.compose.stringResource

@Composable
fun HomeScreen(component: HomeComponent) {
Expand Down Expand Up @@ -66,27 +74,41 @@ private fun MainView(component: HomeComponent, modifier: Modifier = Modifier) {

when (val current = traceState) {
is TraceStateMachine.State.Success -> {
val results = remember(traceState) { current.response.result.sortedByDescending { it.similarity } }

LaunchedEffect(current) {
results.maxByOrNull { it.similarity }?.aniList?.asMedium()?.let(component::details)
val results = remember(traceState) {
current.response.combinedResults.sortedWith(
compareByDescending<SearchResponse.CombinedResult> {
it.maxSimilarity
}.thenByDescending {
it.avgSimilarity
}
)
}
/*OptionDialog(
state = rememberUseCaseState(visible = true),
config = OptionConfig(mode = DisplayMode.LIST),
val useCase = rememberUseCaseState(visible = results.isNotEmpty())

OptionDialog(
state = useCase,
selection = OptionSelection.Single(
options = results.mapIndexedNotNull { index, result ->
result.aniList.title?.preferred()?.let { title ->
Option(
titleText = title,
selected = index == 0
options = results.map {
Option(
titleText = it.aniList.asMedium().title.preferred(),
details = OptionDetails(
title = stringResource(SharedRes.strings.similarity_title),
body = if (it.isSingle) {
stringResource(SharedRes.strings.similarity_text_single, it.avgPercentage)
} else {
stringResource(SharedRes.strings.similarity_text_max_avg, it.maxPercentage, it.avgPercentage)
}
)
}
)
},
onSelectOption = { index, _ ->
component.details(results[index].aniList.asMedium())
}
) { index, _ ->
results.getOrNull(index)?.aniList?.asMedium()?.let(component::details)
}
)*/
),
config = OptionConfig(
mode = DisplayMode.LIST
)
)
}
else -> { }
}
Expand Down
3 changes: 3 additions & 0 deletions composeApp/src/commonMain/moko-resources/base/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@
<string name="color_green">Green</string>
<string name="color_gray">Gray</string>
<string name="color_custom">Custom</string>
<string name="similarity_title">Similarity</string>
<string name="similarity_text_max_avg">Maximum: %s\nAverage: %s</string>
<string name="similarity_text_single">Average: %s</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ data class SearchResponse(
val isError = !error.isNullOrBlank()

@Transient
val bestResult: Result? = result.maxByOrNull { it.similarity }
val combinedResults: Set<CombinedResult> = result.groupBy { it.aniList.id }.mapValues { entry ->
CombinedResult(
aniList = Result.AniList(
id = entry.key,
idMal = entry.value.firstNotNullOfOrNull { it.aniList.idMal },
isAdult = entry.value.any { it.aniList.isAdult },
title = entry.value.firstNotNullOfOrNull { it.aniList.title }
),
maxSimilarity = entry.value.maxOf { it.similarity },
avgSimilarity = entry.value.map { it.similarity }.average().toFloat()
)
}.values.toSet()

@Serializable
data class Result(
Expand All @@ -36,4 +47,14 @@ data class SearchResponse(
)
}
}

data class CombinedResult(
val aniList: Result.AniList,
val maxSimilarity: Float,
val avgSimilarity: Float = maxSimilarity,
) {
val isSingle: Boolean = maxSimilarity == avgSimilarity
val maxPercentage: String = "${(maxSimilarity * 100F).toInt()}%"
val avgPercentage: String = "${(avgSimilarity * 100F).toInt()}%"
}
}

0 comments on commit b6e0032

Please sign in to comment.