From b7e5dd1944806f8c39cf0a4eacc9259d3c403b1c Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Thu, 26 Nov 2026 00:15:26 +0200 Subject: [PATCH 1/2] fix: give ProposalListRoute's enum filter a NavType off Android Navigation finds an enum argument's NavType by reflection on Android only; on the desktop and iOS it returns UNKNOWN, so building GizaNavHost's graph threw on ProposalListRoute.filter right after unlocking. routeTypeMap now names ProposalFilter with an EnumNavType that writes it by name through NavType.StringType, and the route is registered with it. RouteNavTypesJvmTest finds every @Serializable route on the classpath and builds them into one graph, checks GizaNavHost passes the map to each route that cannot be built without it, and round-trips every filter through a real NavController. Co-Authored-By: Claude Opus 5.5 --- .../ui/composable/navigation/GizaNavHost.kt | 2 +- .../ui/composable/navigation/RouteNavTypes.kt | 52 +++++ .../routes/ProposalDestinationTest.kt | 12 +- .../navigation/RouteNavTypesJvmTest.kt | 207 ++++++++++++++++++ 4 files changed, 267 insertions(+), 6 deletions(-) create mode 100644 composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/RouteNavTypes.kt create mode 100644 composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/navigation/RouteNavTypesJvmTest.kt diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/GizaNavHost.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/GizaNavHost.kt index c3291e2f..29563ac1 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/GizaNavHost.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/GizaNavHost.kt @@ -1110,7 +1110,7 @@ fun GizaNavHost( } ) } - composable { backStackEntry -> + composable(typeMap = routeTypeMap) { backStackEntry -> val route = backStackEntry.toRoute() ProposalListScreen( diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/RouteNavTypes.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/RouteNavTypes.kt new file mode 100644 index 00000000..6215a32d --- /dev/null +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/RouteNavTypes.kt @@ -0,0 +1,52 @@ +package mobi.giza.compose.ui.composable.navigation + +import androidx.navigation.NavType +import androidx.savedstate.SavedState +import mobi.giza.compose.ui.view.state.ProposalFilter +import kotlin.enums.EnumEntries +import kotlin.reflect.KType +import kotlin.reflect.typeOf + +/** + * The `NavType`s a route's arguments need beyond the ones navigation knows. + * + * Navigation derives an argument's type from its serial descriptor, and on Android + * an enum is one of those it knows -- it finds the class by reflection. Off Android + * there is no reflection to find it with: on the desktop and on iOS the enum case + * returns `UNKNOWN`, and building the graph throws on the first route carrying one. + * The app compiled, ran on a phone, and crashed on the desktop as soon as it was + * unlocked. + * + * So each enum or custom-typed argument is named here, and the `composable<…>` for + * any route carrying one passes this map. `RouteNavTypesJvmTest` builds a destination + * for every route with it, and checks that `GizaNavHost` passes it wherever a route + * cannot be built without it. + */ +val routeTypeMap: Map> = mapOf( + typeOf() to EnumNavType(ProposalFilter.entries), +) + +/** + * An enum argument, written by its name. + * + * By name rather than ordinal so that a back stack saved before an entry was added or + * reordered still reads as the state it meant, and so a route string is legible. It + * is stored as a string, through [NavType.StringType], which every platform has. + */ +class EnumNavType>(private val entries: EnumEntries) : NavType(isNullableAllowed = false) { + + override val name: String = entries.first()::class.simpleName ?: "enum" + + override fun put(bundle: SavedState, key: String, value: E) { + StringType.put(bundle, key, value.name) + } + + override fun get(bundle: SavedState, key: String): E? = + StringType[bundle, key]?.let(::parseValue) + + override fun parseValue(value: String): E = + entries.firstOrNull { it.name == value } + ?: throw IllegalArgumentException("$value is not one of ${entries.map { it.name }}") + + override fun serializeAsValue(value: E): String = value.name +} diff --git a/composeApp/src/commonTest/kotlin/mobi/giza/compose/ui/composable/navigation/routes/ProposalDestinationTest.kt b/composeApp/src/commonTest/kotlin/mobi/giza/compose/ui/composable/navigation/routes/ProposalDestinationTest.kt index b55b2dc9..9102e8ec 100644 --- a/composeApp/src/commonTest/kotlin/mobi/giza/compose/ui/composable/navigation/routes/ProposalDestinationTest.kt +++ b/composeApp/src/commonTest/kotlin/mobi/giza/compose/ui/composable/navigation/routes/ProposalDestinationTest.kt @@ -133,11 +133,13 @@ class ProposalDestinationTest { * The filter rides on a type-safe navigation route, which means the route's * serializer has to describe it. * - * Navigation derives an argument's `NavType` from the serial descriptor, and - * takes an [SerialKind.ENUM] as a string. The round trip below is what that - * rests on; it fails if the enum is ever replaced by something navigation - * cannot place -- a sealed hierarchy, say -- which is the shape a filter - * carrying more than a state would want. + * Navigation derives an argument's `NavType` from the serial descriptor. On + * Android it finds an [SerialKind.ENUM]'s class by reflection; elsewhere it + * cannot, and the route is registered with `routeTypeMap`, which writes the + * enum by name (`RouteNavTypesJvmTest` builds it through a real controller). + * The round trip below fails if the enum is ever replaced by something else -- + * a sealed hierarchy, say -- which is the shape a filter carrying more than a + * state would want, and which the type map would then have to learn. */ @Test fun `the filter survives the route's serializer which is what navigation reads`() { diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/navigation/RouteNavTypesJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/navigation/RouteNavTypesJvmTest.kt new file mode 100644 index 00000000..03b90c6e --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/ui/composable/navigation/RouteNavTypesJvmTest.kt @@ -0,0 +1,207 @@ +package mobi.giza.compose.ui.composable.navigation + +import androidx.compose.material3.Text +import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.runDesktopComposeUiTest +import androidx.navigation.NavDestinationBuilder +import androidx.navigation.NavGraphBuilder +import androidx.navigation.NavGraphNavigator +import androidx.navigation.NavHostController +import androidx.navigation.NavType +import androidx.navigation.NavigatorProvider +import androidx.navigation.compose.ComposeNavigator +import androidx.navigation.compose.NavHost +import androidx.navigation.compose.composable +import androidx.navigation.compose.rememberNavController +import androidx.navigation.toRoute +import kotlinx.serialization.serializerOrNull +import mobi.giza.compose.ui.composable.navigation.routes.HomeRoute +import mobi.giza.compose.ui.composable.navigation.routes.LoadingRoute +import mobi.giza.compose.ui.composable.navigation.routes.ProposalListRoute +import mobi.giza.compose.ui.view.state.ProposalFilter +import java.io.File +import java.lang.reflect.Modifier +import java.net.JarURLConnection +import kotlin.reflect.KClass +import kotlin.reflect.KType +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFailsWith +import kotlin.test.assertTrue +import kotlin.test.fail + +/** + * Every route can be placed in a nav graph on the desktop. + * + * Navigation works out each argument's `NavType` from the route's serial descriptor + * when the graph is built, and off Android it knows only the primitives, strings and + * their lists. An enum argument compiled, ran on a phone, and threw on the desktop the + * moment `GizaNavHost` composed -- which is right after unlocking, so the desktop app + * could not be used at all. Nothing ran the graph on the JVM before this, so nothing + * said so. + * + * The routes are found on the classpath rather than listed, so a new route is checked + * without anyone remembering to add it here. + */ +@OptIn(ExperimentalTestApi::class) +class RouteNavTypesJvmTest { + + private val publicKey = "de1a1e64d1c4e0d6bd97b0e73d4dfd0e1ec1cdd1e6d8d0e2b9a7f3c5d0e1a2b3" + private val roomId = "d4c3b2a1".repeat(8) + + @Test + fun `every route builds into one graph with the app's type map`() { + val routes = serializableRoutes() + assertTrue( + routes.size >= 50, + "found only ${routes.size} routes on the classpath; the scan is probably broken: $routes" + ) + + val failures = routes.mapNotNull { route -> + runCatching { destination(route, routeTypeMap) } + .exceptionOrNull() + ?.let { "${route.simpleName}: ${it.message}" } + } + assertEquals(emptyList(), failures, "routes that cannot be put in a desktop nav graph") + + val navigator = ComposeNavigator() + val provider = NavigatorProvider().apply { + addNavigator(navigator) + addNavigator(NavGraphNavigator(this)) + } + val graph = NavGraphBuilder( + provider, + startDestination = LoadingRoute::class, + route = null, + typeMap = emptyMap(), + ).apply { + routes.forEach { addDestination(destination(it, routeTypeMap, navigator)) } + }.build() + + assertEquals(routes.size, graph.nodes.size()) + } + + /** + * The map only helps where it is passed. Each route that cannot be built without it + * has to be registered with it, and the registration is in `GizaNavHost`'s source. + * + * This is also the control: [ProposalListRoute] is the route that crashed, and if it + * ever builds without the map then the platform has learned enums and this can go. + */ + @Test + fun `GizaNavHost passes the type map to every route that needs one`() { + val needingMap = serializableRoutes().filter { route -> + runCatching { destination(route, emptyMap()) }.isFailure + } + assertTrue(ProposalListRoute::class in needingMap, "ProposalListRoute now builds without a type map") + + val navHost = sourceRoot() + .resolve("commonMain/kotlin/mobi/giza/compose/ui/composable/navigation/GizaNavHost.kt") + .readText() + + val unregistered = needingMap.filterNot { route -> + Regex("""composable<(?:[\w.]+\.)?${route.simpleName}>\(\s*typeMap\s*=\s*routeTypeMap""") + .containsMatchIn(navHost) + } + assertEquals( + emptyList(), + unregistered.map { it.simpleName }, + "these routes need `composable<…>(typeMap = routeTypeMap)` in GizaNavHost" + ) + } + + @Test + fun `without the type map the desktop cannot place a proposal filter`() { + val error = assertFailsWith { + destination(ProposalListRoute::class, emptyMap()) + } + assertTrue(error.message.orEmpty().contains("filter"), error.message) + } + + /** + * Through a real controller: the filter is written into the route string, read back + * out of the saved state, and decoded into the route the screen is given. + */ + @Test + fun `a proposal list opens on the filter it was sent with`() = runDesktopComposeUiTest(400, 400) { + lateinit var controller: NavHostController + setContent { + controller = rememberNavController() + NavHost(navController = controller, startDestination = HomeRoute(publicKey)) { + composable { Text("home") } + composable(typeMap = routeTypeMap) { Text("proposals") } + } + } + + for (filter in ProposalFilter.entries) { + runOnIdle { + controller.navigate( + ProposalListRoute(activeUserPublicKey = publicKey, chatRoomId = roomId, filter = filter) + ) + } + waitForIdle() + runOnIdle { + val route = controller.currentBackStackEntry!!.toRoute() + assertEquals(ProposalListRoute(publicKey, roomId, filter), route) + } + } + + // And with none named, which is how most callers arrive. + runOnIdle { controller.navigate(ProposalListRoute(activeUserPublicKey = publicKey, chatRoomId = roomId)) } + waitForIdle() + runOnIdle { + assertEquals( + ProposalFilter.ALL, + controller.currentBackStackEntry!!.toRoute().filter + ) + } + } + + @Test + fun `an enum is written by name and refuses a name it does not have`() { + val type = EnumNavType(ProposalFilter.entries) + + for (filter in ProposalFilter.entries) { + assertEquals(filter.name, type.serializeAsValue(filter)) + assertEquals(filter, type.parseValue(filter.name)) + } + assertFailsWith { type.parseValue("SOMETIMES") } + } + + private fun destination( + route: KClass<*>, + typeMap: Map>, + navigator: ComposeNavigator = ComposeNavigator(), + ) = NavDestinationBuilder(navigator, route, typeMap).build() + + /** Every `@Serializable` top-level class in the routes package. */ + private fun serializableRoutes(): List> { + val packageName = ProposalListRoute::class.java.packageName + val path = packageName.replace('.', '/') + val loader = javaClass.classLoader + + val classNames = loader.getResources(path).toList().flatMap { url -> + when (url.protocol) { + "file" -> File(url.toURI()).listFiles().orEmpty().map { it.name } + "jar" -> (url.openConnection() as JarURLConnection).jarFile.entries().toList() + .map { it.name } + .filter { it.startsWith("$path/") && it.count { c -> c == '/' } == path.count { c -> c == '/' } + 1 } + .map { it.substringAfterLast('/') } + else -> fail("cannot list classes from $url") + } + } + .filter { it.endsWith(".class") && '$' !in it } + .map { "$packageName.${it.removeSuffix(".class")}" } + .distinct() + + return classNames + .map { Class.forName(it, false, loader) } + .filter { !it.isInterface && !Modifier.isAbstract(it.modifiers) && !it.isEnum } + .filter { serializerOrNull(it) != null } + .map { it.kotlin } + .sortedBy { it.simpleName } + } + + private fun sourceRoot(): File = + File("src").takeIf { it.resolve("commonMain").isDirectory } ?: File("composeApp/src") +} From 9a1abc61397500072ab3c44ce06630e19f96f6ea Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Thu, 26 Nov 2026 00:15:26 +0200 Subject: [PATCH 2/2] fix: include jdk.unsupported and the other needed modules in the desktop runtime The jlinked runtime of createDistributable lacked jdk.unsupported, so protobuf-lite under DataStore could not reach sun.misc.Unsafe and every preference write failed, which made creating a profile show "Something went wrong". The modules are those suggestRuntimeModules reports. Checked in the packaged app, offline with a throwaway XDG_DATA_HOME: unlock, create a profile, reach home; the preference files are written. Co-Authored-By: Claude Opus 5.5 --- composeApp/build.gradle.kts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index 87cf83d6..39ffc007 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -527,6 +527,14 @@ compose.desktop { // name, `com.machankura.bennu.desktop`, until the Giza fork. packageName = "Giza" packageVersion = "1.0.0" + // The packaged app runs on a jlinked runtime holding only the modules named here + // and the plugin's defaults, and nothing fails at build time when one is missing. + // Without jdk.unsupported, protobuf-lite (under DataStore) cannot reach + // sun.misc.Unsafe and every preference write throws, so creating a profile fails. + // The rest are what `./gradlew :composeApp:suggestRuntimeModules` reports; rerun it + // after adding a JVM dependency, and check a profile can be created in + // `createDistributable`'s build, which is the only place a gap shows. + modules("java.instrument", "java.management", "java.naming", "java.sql", "jdk.unsupported") // Generated from docs/brand/giza-sphinx.svg by docs/scripts/app-icons.py // (docs/giza-app-icon.md, Phase 4). Edit the master and rerun, not these. macOS {