{"id":13537276,"url":"https://github.com/sellmair/kompass","last_synced_at":"2025-04-09T12:09:07.880Z","repository":{"id":83993081,"uuid":"113365232","full_name":"sellmair/kompass","owner":"sellmair","description":"Kotlin Multiplatform Router for Android and iOS","archived":false,"fork":false,"pushed_at":"2019-08-28T21:55:37.000Z","size":34188,"stargazers_count":346,"open_issues_count":9,"forks_count":13,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-02T11:04:09.338Z","etag":null,"topics":["android","android-library","animation","coordinator","coordinator-pattern","custom-transitions","fragments","kompass","kotlin","kotlin-android","kotlin-library","kotlin-mpp","kotlin-multi-platform","kotlin-multiplatform","multiplatform","mvvm","mvvm-architecture","mvvmc","router","transition"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sellmair.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2017-12-06T20:41:58.000Z","updated_at":"2024-12-15T19:23:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"1812906b-c2d6-4d7f-a851-1ace018471e5","html_url":"https://github.com/sellmair/kompass","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sellmair%2Fkompass","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sellmair%2Fkompass/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sellmair%2Fkompass/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sellmair%2Fkompass/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sellmair","download_url":"https://codeload.github.com/sellmair/kompass/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248036067,"owners_count":21037092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-library","animation","coordinator","coordinator-pattern","custom-transitions","fragments","kompass","kotlin","kotlin-android","kotlin-library","kotlin-mpp","kotlin-multi-platform","kotlin-multiplatform","multiplatform","mvvm","mvvm-architecture","mvvmc","router","transition"],"created_at":"2024-08-01T09:00:57.151Z","updated_at":"2025-04-09T12:09:07.856Z","avatar_url":"https://github.com/sellmair.png","language":"Kotlin","funding_links":[],"categories":["Routing","Libraries"],"sub_categories":["SQL","Architecture"],"readme":"![Kompass](https://github.com/sellmair/kompass/blob/develop/assets/Kompass_724.png?raw=true)\n\n## A powerful Kotlin Multiplatform Router for Android and iOS\n\u003cbr\u003e\n\n![GitHub top language](https://img.shields.io/github/languages/top/sellmair/kompass.svg)\n[![Build Status](https://travis-ci.org/sellmair/kompass.svg?branch=develop)](https://travis-ci.org/sellmair/kompass)\n![Bintray](https://img.shields.io/bintray/v/sellmair/sellmair/kompass-core.svg)\n![GitHub last commit](https://img.shields.io/github/last-commit/sellmair/kompass.svg)\n[![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/kompass-android/)\n\n\n#### Support\nI am happy to help you with any problem on [gitter](https://gitter.im/kompass-android/Help) \u003cbr\u003e\nFeel free to open any new issue! \n\n# What Kompass can do for you\n- Perfect fit for `MVVM`, `MVI`, `MVP`, `MVX` architectures\n- Powerful routing concept that targets multiple platforms like Android, JVM \u0026 iOS \n- Easy to use API's\n- Highly configurable implementations\n\n## Android\n- Flexible routing with fragments\n- Built in solution for passing arguments to fragments\n- Very easy support for transitions/animations \n- No XML configuration\n- Built in `DSL` to configure the `FragmentRouter`\n- Survives configuration changes\n- Can restore the \"routing stack\" after process death\n\n\n# What Kompass *can't* do for now\nWhile the `core` module is currently built and published for multiple platforms (JVM, iOS), there are no\ndefault `Router` implementations for any other platforms than `Android` yet. Those are currently \"work in progress\". \n`Kompass` can still be used as a common API for routing by providing custom implementations of a `Router` for your platform! \n\n\n# Setup\n\n## Step 1: Add the repository\nArtifacts are linked to jCenter. Add jCenter repository to your root build.gradle\n  \n`build.gradle` \n```groovy\n  allprojects {\n     repositories {\n        jcenter()\n     }\n  }\n\n```\n\n### Step 2: Add the dependency (Multiplatform)\n\n`build.gradle.kts`\n```kotlin\nkotlin {\n    sourceSets {\n        val commonMain by getting {\n            dependencies {\n                implementation(\"io.sellmair:kompass-core:0.2.0-alpha.5\")\n            }\n        }\n        \n        /* Optional Android module */\n        val androidMain by getting {\n            dependencies {\n                implementation(\"io.sellmair:kompass-android:0.2.0-alpha.5\")\n            }\n        }\n    }\n}\n```\n\n### Step 2: Add the dependency (Android Only)\n\n`build.gradle.kts`\n\n```kotlin\ndependencies {\n    implementation(\"io.sellmair:kompass-android:0.2.0-alpha.4\")\n}\n```\n\n\n### Optional Step 3: (Android: Highly encouraged) Enable Kotlin's Android extensions (with `@Parcelize`)\n\n`build.gradle.kts` \n\n```kotlin\nplugins {\n    // ...\n    id(\"org.jetbrains.kotlin.android.extensions\")\n}\n\n// ...\n\n// Currently still necessary for @Parcelize annotation\nandroidExtensions {\n    isExperimental = true\n}\n\n```\n\n\n\n# Usage\n## Example\nI recommend having a look at the [example](https://github.com/sellmair/kompass/tree/develop/android-example--fragment) app built with Kompass\n\n\u003cbr\u003e\u003cbr\u003e\n###### Gif\n\u003cimg src=\"https://github.com/sellmair/kompass/blob/develop/assets/example.gif?raw=true\" width=\"350\"\u003e\n\u003cbr\u003e\u003cbr\u003e\n\n#### Defining routes\nRoutes can easily be represented by data classes. Let's say your App has three routes that you might want to display:\nA `LoginRoute`, `ContactListRoute` and a `ChatRoute`: \n\n```kotlin\n\nsealed class AppRoute : Route, Parcelable\n\n@Parcelize\nclass LoginRoute: AppRoute()\n\n@Parcelize\ndata class ContactListRoute(val contacts: List\u003cContact\u003e): AppRoute()\n\n@Parcelize\ndata class ChatRoute(val contact: Contact): AppRoute() \n\n```\n\nAll the arguments necessary to display a certain route should be present in the route itself. \nThe example above uses the `@Parcelize` feature from the Kotlin's Android extensions\n\n\n#### Creating a router instance (Android)\n\nA `FragmentRouter` for Android can be configured quite easily by using the built in DSL for configuration. \n\n```kotlin\nrouter = FragmentRouter {\n            transitions {\n                register(LoginToContactListTransition())\n                register(ContactListToChatTransition())\n            }\n            routing {\n                route\u003cLoginRoute\u003e { LoginFragment::class }\n                route\u003cContactListRoute\u003e { ContactListFragment::class }\n                route\u003cChatRoute\u003e { ChatFragment::class }\n            }\n        }\n```\n\nThe above DSL shows two configurations: \n- `transitions`: configures animations/transitions that should be running when routing\n- `routing`: configures which `Fragment` should be displayed for a certain route\n\n\n#### Setting up a router instance (Android)\n\nA `FragmentRouter` needs a `ViewGroup` to place the fragments in. This can be setup like this:\n\n```kotlin\n\nclass MainActivity : AppCompatActivity(), KompassFragmentActivity {\n\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n        setContentView(R.layout.activity_main)\n        router.setup(savedInstanceState, R.id.container)\n    }\n\n\n    override fun onBackPressed() {\n        router.popRetainRootImmediateOrFinish()\n    }\n\n}\n\n```\n\nPlease note: In order to call the `setup` method, one needs to either implement `KomapssFragmentActivity` \nor `KompassFragment`! \n\n\n\n#### Routing (Simple push)\n\nLet's assume that the user taps on a certain contact in the contact list displayed by the `ContactListRoute`: \n\n```kotlin\nclass ContactListViewModel {\n \n    private val router = TODO(\"Maybe use DI?\")\n \n    fun onContactClicked(contact: Contact) {\n        router.push(ChatRoute(contact))\n    }\n\n}\n```\n\nThe code above will push the `ChatRoute` onto the \"routing stack\" and results in the `ChatFragment` being shown. \nPopping the \"routing stack\" will result in the `ContactListFragment` being displayed again. \n\n\n#### Routing (Replacing the current route)\n\n Let's assume the user successfully logged into your app. This should result in the current `LoginRoute`\nbeing replaced by the `ContactListRoute`\n\n\n```kotlin\nclass LoginViewModel {\n\n    private val router = TODO(\"What about Dagger?\")\n    \n    fun onLoginSuccessful(user: User) {\n        router.replaceTopWith(ContactListRoute(user.contacts))\n    }\n}\n```\n\nWrapping multiple instructions into one lambda block will bundle them to one single operation on the routing stack. \nSo you could alternatively write something like \n\n```kotlin\nfun onLoginSuccessful(user: User) {\n    router { pop().push(ContactListRoute(user.contacts)) }\n}\n```\n\n\n#### Routing (Arbitrary)\nKompass supports `arbitrary` routing: A instruction to the router is nothing more than a function from a list of \nroutes to a new list of routes. Let's say your app would like to remove all `ChatRoute` with a certain contact\n\n```kotlin\nfun removeContactFromStack(contact: Contact) {\n     router {\n        with(filter { it.route.contact == contact })\n     }\n     \n     //or\n     \n     router.plainStackInstruction { filter { it.route.contact == contact } }\n}\n``` \n\n\n#### Receiving the current route inside a `Fragment`\n\nAccessing the route from within the any `Fragment` implementation is easily done by conforming to the `KompassFragment`\ninterface:\n\n```kotlin\n\nclass ContactListFragment : Fragment(), KompassFragment {\n   \n    override val router: FragmentRouter\u003cAppRoute\u003e = TODO() \n   \n    private val route: ContactListRoute by route()\n    \n    override fun onCreate(savedInstanceState: Bundle?) {\n        val contacts = route.contacts \n        \n       //...\n    }\n\n}\n```\n\n\n#### Fragment Transitions\n\nIn order to support animations (fragment transitions) when routing you just need to implement a `FragmentTransition`. \nExample: Your chat app should show a `Slide` transition when going from the `ContactListFragment` to the \n`ChatFragment` and back. Simply implement the transition, check for your constraints and apply \nthe transitions to the fragment. It is also possible to apply generic constraints to your transition \nusing the `GenericFragmentTransition` API.\n\n```kotlin\n\nclass ContactListToChatTransition : FragmentTransition {\n    @SuppressLint(\"RtlHardcoded\")\n    override fun setup(\n        transaction: FragmentTransaction,\n        exitFragment: Fragment, exitRoute: Route,\n        enterFragment: Fragment, enterRoute: Route\n    ) {\n        if (exitFragment is ContactListFragment \u0026\u0026 enterFragment is ChatFragment) {\n            exitFragment.exitTransition = Slide(Gravity.LEFT)\n            enterFragment.enterTransition = Slide(Gravity.RIGHT)\n        }\n\n        if (exitFragment is ChatFragment \u0026\u0026 enterFragment is ContactListFragment) {\n            exitFragment.exitTransition = Slide(Gravity.RIGHT)\n            enterFragment.enterTransition = Slide(Gravity.LEFT)\n        }\n    }\n}\n\n```\n\nAfter the transition is implemented, just add it to the configuration of the `FragmentRouter` like seen above!\n\n```kotlin\n\nFragmentRouter { \n    transitions {\n        register(LoginToContactListTransition())\n    }\n}\n\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsellmair%2Fkompass","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsellmair%2Fkompass","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsellmair%2Fkompass/lists"}