Skip to content

Conversation

rocka
Copy link
Member

@rocka rocka commented Jul 19, 2025

Change summary

Replaced res/xml/settings_nav.xml with SettingsRoute to simplify the process of navigation between fragments, as well as adding new fragments in the future, since there is no need to define an "action" between all adjacent destinations.

see also: https://developer.android.com/guide/navigation/design/kotlin-dsl

According to the docs above, the route object should be serializable. While data class with only primitive members become serializable automatically, ListFragment could have ConfigDescriptor and RawConfig as params. To workaround this, RawConfig was marked @Serializable since it contains primitives only, and ConfigDescriptor was modified to store its raw RawConfig value before parsing. Such that SettingsRoute could carry those params while being Serializable:

@Serializable
data class ListConfig(val params: Params) : SettingsRoute() {
    // ... ...

    val desc: ConfigDescriptor<*, *>
        get() = ConfigDescriptor.parse(params.desc).getOrElse { throw it }

    val cfg: RawConfig
        get() = params.cfg
}

Inside the fragment, there is a new lazyRoute function to obtain params in a more type-safe way:

inline fun <reified T : Any> Fragment.lazyRoute() = lazy {
    findNavController().getBackStackEntry<T>().toRoute<T>()
}

Navigation animation was also moved to a centralized position:

fun <T : Any> NavController.navigateWithAnim(route: T) {
    navigate(route, navOptions {
        anim {
            enter = R.animator.nav_default_enter_anim
            exit = R.animator.nav_default_exit_anim
            popEnter = R.animator.nav_default_pop_enter_anim
            popExit = R.animator.nav_default_pop_exit_anim
        }
    })
}

Things could be improved

  • uri param in SettingsRoute.PinyinDict got serialized and deserialized multiple times when handling Intent
  • Investigate how to simplify the initialization of SettingsRoute.QuickPhraseEdit, eg. make CustomQuickPhrase/BuiltinQuickPhrase Serializable
  • Apk size got a little bit larger ¯\(ツ)

@berberman berberman merged commit b5d12c8 into master Jul 21, 2025
5 checks passed
@berberman berberman deleted the refactor/settings-navigation branch July 21, 2025 12:28
mokapsing pushed a commit to mokapsing/fcitx5-android that referenced this pull request Jul 23, 2025
* Migrate navigation graph xml to kotlin DSL

see also: https://developer.android.com/guide/navigation/design/kotlin-dsl

* Make ConfigDescriptor serializable

* Make QuickPhrase serializable

---------

Co-authored-by: Potato Hatsue <1793913507@qq.com>
mokapsing pushed a commit to mokapsing/fcitx5-android that referenced this pull request Jul 23, 2025
* Migrate navigation graph xml to kotlin DSL

see also: https://developer.android.com/guide/navigation/design/kotlin-dsl

* Make ConfigDescriptor serializable

* Make QuickPhrase serializable

---------

Co-authored-by: Potato Hatsue <1793913507@qq.com>
mokapsing pushed a commit to mokapsing/fcitx5-android that referenced this pull request Aug 7, 2025
* Migrate navigation graph xml to kotlin DSL

see also: https://developer.android.com/guide/navigation/design/kotlin-dsl

* Make ConfigDescriptor serializable

* Make QuickPhrase serializable

---------

Co-authored-by: Potato Hatsue <1793913507@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants