Merge branch 'claude/nervous-sinoussi-247a5d' into giza
This commit is contained in:
commit
a5a802c5ff
5 files changed
+275
-6
No files matched your search
@@ -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 {
|
||||
|
||||
+1
-1
@@ -1128,7 +1128,7 @@ fun GizaNavHost(
|
||||
}
|
||||
)
|
||||
}
|
||||
composable<ProposalListRoute> { backStackEntry ->
|
||||
composable<ProposalListRoute>(typeMap = routeTypeMap) { backStackEntry ->
|
||||
val route = backStackEntry.toRoute<ProposalListRoute>()
|
||||
|
||||
ProposalListScreen(
|
||||
|
||||
+52
@@ -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<KType, NavType<*>> = mapOf(
|
||||
typeOf<ProposalFilter>() 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<E : Enum<E>>(private val entries: EnumEntries<E>) : NavType<E>(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
|
||||
}
|
||||
+7
-5
@@ -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`() {
|
||||
|
||||
+207
@@ -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<IllegalArgumentException> {
|
||||
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<HomeRoute> { Text("home") }
|
||||
composable<ProposalListRoute>(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<ProposalListRoute>()
|
||||
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<ProposalListRoute>().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<IllegalArgumentException> { type.parseValue("SOMETIMES") }
|
||||
}
|
||||
|
||||
private fun destination(
|
||||
route: KClass<*>,
|
||||
typeMap: Map<KType, NavType<*>>,
|
||||
navigator: ComposeNavigator = ComposeNavigator(),
|
||||
) = NavDestinationBuilder(navigator, route, typeMap).build()
|
||||
|
||||
/** Every `@Serializable` top-level class in the routes package. */
|
||||
private fun serializableRoutes(): List<KClass<*>> {
|
||||
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")
|
||||
}
|
||||
Reference in new issue
Block a user