'Specific Module routes in Jetbrains Compose with Decompose on kotlin desktop application

I'm using JetBrains Compose framework on a desktop application project, and for routing they suggest in the official documentation arkivanov-Decompose library for routing between views (Composables).

Works like a charm, but the more views you have, the longer your routing file gets. I was wondering if I could make it look a little bit better.

I'm only familiar with web routing like in Angular, when we can define the routes inside the modules. There, every module can have a module-routes.ts file with something like:

const routes: Routes = [
  { path: 'first-component', component: FirstComponent },
  { path: 'second-component', component: SecondComponent },
];

This way I can manage all elements that concern the module inside the module, and the routes are imported into a global Router module.

In Decompose I'm trying to do something along those lines, so I can encapsulate certain views in their respective modules (some views only interact with views of the same module, but I'm having a hard time having my router to be distributed between the modules. Does anyone have an idea on how to do it?

I have my router and my child set:

private val router =
    router<Configuration, Content>(
        initialConfiguration = Configuration.Auth, // Starting with Login
        childFactory = ::createChild // The Router calls this function, providing the child Configuration and ComponentContext
    )

and my child factory:

private fun createChild(configuration: Configuration, context: ComponentContext): Content =
    when (configuration) {
        is Configuration.Auth -> auth(configuration)
        is Configuration.UserList -> userList()
        is Configuration.NewUser-> newUser()

can I get those configs from the modules to make it cleaner? Can I have different routes for different types of user (admin, normalUser, etc...)?



Solution 1:[1]

Can I get those configs from the modules to make it cleaner?

Yes, you can move the Configuration sealed class and all its subclasses into a separate file, and make it internal. You can also move the createChild function.

Can I have different routes for different types of user (admin, normalUser, etc...)?

You can define separate configuration classes for each route.

PS: also please note that you can organise your components into a tree. So on each component you will have just a few routes and limited responsibility.

Something like this:

              Root
          /          \
     Auth              Main
    /    \            /    \
SignIn  SignUp  UserList  UserProfile

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Phil Dukhov