Skip to content

Commit

Permalink
refactor: bypass navigation enum not found
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Nov 11, 2024
1 parent ea06f86 commit e62a41c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ fun HomeView(context: ViewContext) {
Icon(Icons.Filled.Search, null)
},
onClick = {
context.navController.navigate(SearchViewRoute(currentTab.kind))
context.navController.navigate(SearchViewRoute(currentTab.kind?.name))
}
)
},
Expand Down
34 changes: 8 additions & 26 deletions app/src/main/java/io/github/zyrouge/symphony/ui/view/Search.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

private data class SearchResult(
val songIds: List<String>,
Expand All @@ -86,25 +81,7 @@ private data class SearchResult(
)

@Serializable
data class SearchViewRoute(
@Serializable(with = GrooveKindsSerializer::class) val initialChip: Groove.Kinds?,
) {
// NOTE: seems like r8 obfuscates the class, which makes compose navigator to find this class, this is a temporary fix
companion object {
object GrooveKindsSerializer : KSerializer<Groove.Kinds> {
override val descriptor = PrimitiveSerialDescriptor(
"Groove.Kinds",
PrimitiveKind.STRING,
)

override fun serialize(encoder: Encoder, value: Groove.Kinds) =
encoder.encodeString(value.name)

override fun deserialize(decoder: Decoder) =
enumValueOf<Groove.Kinds>(decoder.decodeString())
}
}
}
data class SearchViewRoute(val initialChip: String?)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
Expand All @@ -113,8 +90,13 @@ fun SearchView(context: ViewContext, route: SearchViewRoute) {
var terms by rememberSaveable { mutableStateOf("") }
var isSearching by remember { mutableStateOf(false) }
var results by remember { mutableStateOf<SearchResult?>(null) }
val initialChip = remember {
route.initialChip?.let { enumValueOf<Groove.Kinds>(it) }
}
var selectedChip by rememberSaveable {
mutableStateOf(initialChip)
}

var selectedChip by rememberSaveable { mutableStateOf(route.initialChip) }
fun isChipSelected(kind: Groove.Kinds) = selectedChip == null || selectedChip == kind

var currentTermsRoutine: Job? = null
Expand Down Expand Up @@ -269,7 +251,7 @@ fun SearchView(context: ViewContext, route: SearchViewRoute) {
Text(it.label(context))
},
modifier = Modifier.onGloballyPositioned { coordinates ->
if (!initialScroll && route.initialChip == it) {
if (!initialScroll && initialChip == it) {
val windowWidth = with(density) {
configuration.screenWidthDp.dp.toPx()
}
Expand Down

0 comments on commit e62a41c

Please sign in to comment.