Skip to content

Commit

Permalink
fix and fasten search
Browse files Browse the repository at this point in the history
  • Loading branch information
DatL4g committed Nov 29, 2023
1 parent 1c8d337 commit fe5137d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package dev.datlag.burningseries.shared.ui.custom

import androidx.compose.foundation.text.InlineTextContent
import androidx.compose.foundation.text.appendInlineContent
import androidx.compose.material.LocalTextStyle
import androidx.compose.material.Text
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.Text
import androidx.compose.material3.LocalContentColor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand Down Expand Up @@ -70,7 +70,7 @@ fun ExpandableText(
modifier: Modifier = Modifier,
expandedMaxLines: Int = Int.MAX_VALUE,
toggle: @Composable (() -> Unit)? = null,
color: Color = LocalContentColor.current,
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
Expand Down Expand Up @@ -128,7 +128,7 @@ fun ExpandableText(
modifier: Modifier = Modifier,
expandedMaxLines: Int = Int.MAX_VALUE,
toggle: @Composable (() -> Unit)? = null,
color: Color = LocalContentColor.current,
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import dev.datlag.burningseries.model.algorithm.JaroWinkler
import dev.datlag.burningseries.model.common.safeSubList
import dev.datlag.burningseries.shared.ui.navigation.Component
import dev.datlag.burningseries.shared.ui.screen.initial.series.SeriesScreenComponent
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.*
import org.kodein.di.DI
import org.kodein.di.instance
Expand All @@ -40,16 +43,20 @@ class FavoriteScreenComponent(
if (t2.isBlank()) {
emptyList()
} else {
t1.map {
when {
it.title.equals(t2, true) -> it to 1.0
it.title.startsWith(t2, true) -> it to 0.95
it.title.contains(t2, true) -> it to 0.9
else -> it to JaroWinkler.distance(it.title, t2)
}
}.filter {
it.second > 0.85
}.sortedByDescending { it.second }.map { it.first }.safeSubList(0, 10)
coroutineScope {
t1.map {
async {
when {
it.title.trim().equals(t2.trim(), true) -> it to 1.0
it.title.trim().startsWith(t2.trim(), true) -> it to 0.95
it.title.trim().contains(t2.trim(), true) -> it to 0.9
else -> it to JaroWinkler.distance(it.title.trim(), t2.trim())
}
}
}.awaitAll().filter {
it.second > 0.85
}.sortedByDescending { it.second }.map { it.first }.safeSubList(0, 10)
}
}
}.stateIn(ioScope(), SharingStarted.Lazily, emptyList())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import dev.datlag.burningseries.model.state.SearchState
import dev.datlag.burningseries.network.state.SearchStateMachine
import dev.datlag.burningseries.shared.ui.navigation.Component
import dev.datlag.burningseries.shared.ui.screen.initial.series.SeriesScreenComponent
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.flow.*
import org.kodein.di.DI
import org.kodein.di.instance
Expand Down Expand Up @@ -50,16 +54,20 @@ class SearchScreenComponent(
if (t2.isBlank()) {
emptyList()
} else {
t1.map {
when {
it.title.equals(t2, true) -> it to 1.0
it.title.startsWith(t2, true) -> it to 0.95
it.title.contains(t2, true) -> it to 0.9
else -> it to JaroWinkler.distance(it.title, t2)
}
}.filter {
it.second > 0.85
}.sortedByDescending { it.second }.map { it.first }.safeSubList(0, 10)
coroutineScope {
t1.map {
async {
when {
it.title.trim().equals(t2.trim(), true) -> it to 1.0
it.title.trim().startsWith(t2.trim(), true) -> it to 0.95
it.title.trim().contains(t2.trim(), true) -> it to 0.9
else -> it to JaroWinkler.distance(it.title.trim(), t2.trim())
}
}
}.awaitAll().filter {
it.second > 0.85
}.sortedByDescending { it.second }.map { it.first }.safeSubList(0, 10)
}
}
}.stateIn(ioScope(), SharingStarted.Lazily, emptyList())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fun <T> List<T>.safeSubList(from: Int, to: Int): List<T> {
val safeFrom = max(min(from, lastIndex), 0)
return this.subList(
safeFrom,
max(safeFrom, min(to, lastIndex))
max(safeFrom, min(to, size))
)
}

Expand Down

0 comments on commit fe5137d

Please sign in to comment.