Coder Social home page Coder Social logo

yuelvic / kaptain Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adrielcafe/kaptain

0.0 1.0 0.0 177 KB

๐Ÿ‘จโ€โœˆ๏ธโ›ต multi-module navigation on Android has never been so easier!

License: MIT License

Kotlin 100.00%

kaptain's Introduction

JitPack Github Actions Android API Kotlin ktlint License MIT

Kaptain

Kaptain is a small, dependencyless and easy to use Android library that helps you to navigate between activities spread over several modules.

Usage

Given the following project structure:

/app
  MyApplication.kt
/feature-a
  FeatureAActivity.kt
/feature-b
  FeatureBActivity.kt
/feature-shared
  Destination.kt
  • app module imports all modules below
  • feature-a and feature-b imports only feature-shared
  • feature-shared imports nothing

1. Define destinations

First, you must list all possible destinations (Activities) of your app. Create a sealed class that implements the KaptainDestination interface.

Optionally, you can add arguments to your destination using a data class.

sealed class Destination : KaptainDestination {

    object FeatureA : Destination()
    data class FeatureB(val message: String) : Destination()
}

2. Create a Kaptain instance

Next, create a new Kaptain instance and associate your destinations with the corresponding Activities.

class MyApplication : Application() {
    
    val kaptain = Kaptain {
        add<Destination.FeatureA, FeatureAActivity>()
        add<Destination.FeatureB, FeatureBActivity>()
    }
}

Ideally, you should inject this instance as a singleton using a DI library. Check the sample app for an example using Koin.

3. Navigate between activities

Now you can navigate to any Activity, from any module:

class FeatureAActivity : AppCompatActivity() {

    fun goToFeatureB() {
        kaptain.navigate(
            activity = this,
            destination = Destination.FeatureB(message = "Ahoy!"),
            requestCode = 0x123 // Optional
        )
    }
}

4. Retrieve a destination content

After arrive at your destination, you can retrieve it's content:

class FeatureBActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_feature_b)

        val importantMessage = kaptain.fromIntent<Destination.FeatureB>(this)?.message
    }
}

Dynamic feature modules

Kaptain works great with dynamic features!

1. Add destinations on demand

You can add/remove destinations at any time:

kaptain.add<Destination.DynamicFeatureA, DynamicFeatureAActivity>()
kaptain.remove<Destination.DynamicFeatureB>()

2. Make sure the destination exists before navigating

if (kaptain.has<Destination.DynamicFeatureA>) {
    kaptain.navigate(this, Destination.DynamicFeatureA)
}

Import to your project

  1. Add the JitPack repository in your root build.gradle at the end of repositories:
allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}
  1. Next, add the library to your module:
dependencies {
    implementation "com.github.adrielcafe.kaptain:kaptain:$currentVersion"
}

Current version: JitPack

kaptain's People

Contributors

adrielcafe avatar yuelvic avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.