• Stars
    star
    122
  • Rank 292,031 (Top 6 %)
  • Language
    Kotlin
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A lightweight Android activity router

Krouter

A lightweight Android activity router written in Kotlin.

crossroads

Basic usage

// anywhere in the app, preferably on application creation
val krouter = Krouter.from(context, hashMapOf(
    "user/:id/likes" to UserLikesActivity::class.java
))

// anywhere that can access the val above
krouter.start("user/42/likes") // this will start UserLikesActivity

// inside UserLikesActivity, possibly inside onCreate()
val id: Int = intent.getIntExtra("id", 0) // 42

Adding more extras

In order to add more extras to the intent, one can do the following:

krouter.getRouter("user/42/likes")!!
    .withIntent { it.putExtra("name", "John") }
    .start()

Starting for result

In order to set a request code:

krouter.getRouter("user/42/likes")!!
    .startForResult(activity, 123) // 123 is the request code

Advanced routing configuration

It is possible to instantiate Krouter establishing a regular expression that the parameter must specify:

val krouter = Krouter(context, hashMapOf(
    Route("user/:id/likes", hashMapOf(
        "id" to Schema("^[0-9]{2}$")
    )) to UserLikesActivity::class.java
)

There are default implementations for integer, float, double, long, char:

// the constants must be statically imported from Schema.Type
Route("user/:id/likes", hashMapOf("id" to Schema(INT)))
Route("my/balance/:value", hashMapOf("value" to Schema(FLOAT)))

If no schema is defined, Krouter will try to infer the type accordingly. If no schema is suitable, the param will be coerced to a String.

Interceptor

You can add some interceptors to Krouter:

// If you are not logged in, go to login
krouter.addInterceptor(object : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Router? {

        val route = chain.request()
        if(route?.url == "user/:id" && !login) {
            val target = chain.proceed(route)
            val router = krouter.getRouter("login")
            router?.intent?.putExtra("nav", target?.url)
            router?.update()
            return router
        }
        return chain.proceed(route)
    }

})

Installation

Add the dependency:

dependencies {
    compile 'com.github.denisidoro.github:krouter:0.0.2'
}

Best practices

  • Use dependency injection: Krouter was idealized to be used in conjunction with Dagger.
  • Compose routers: say you have an activity flow in your app that's only accessible to users who are admin, for instance. If you don't want to deal with a huge routing map that has routes all flows, then create multiple Krouter instances. val globalKrouter and @AdminScope val adminKrouter, for example.

More Repositories

1

navi

An interactive cheatsheet tool for the command-line
Rust
13,795
star
2

floki

A JSON/EDN browser for the terminal
Clojure
315
star
3

dotfiles

Awesome personal dotfiles
Shell
161
star
4

cheats

Community-sourced cheatsheets for navi
109
star
5

beavr

A command-line autocompleter with steroids 💪
Clojure
40
star
6

graffiti

An opinionated, declarative GraphQL implementation in Clojure
Clojure
35
star
7

abra

Easily share data between terminal windows!
Rust
24
star
8

docpars

An ultra-fast parser for declarative command-line options for your shell scripts.
Rust
22
star
9

korn

Kotlin for React Native
Kotlin
20
star
10

navi-tldr-pages

tldr-pages for navi, an interactive cheatsheet tool for the command-line
Shell
19
star
11

pipers

Use pipe expressions in your PromQL queries or code!
JavaScript
14
star
12

rosebud

A personal financial management system
Clojure
13
star
13

remacro

Plain-text macro expander with React-inspired widgets. Markdown with steroids. 💪
JavaScript
12
star
14

hello-kotlin-android

A functional reactive Android app written in Kotlin
Java
7
star
15

reseau

A highly scalable, reactive, MVVM-like library for Android
Kotlin
7
star
16

docoptsh

A command line argument parser written in pure Bash
Shell
6
star
17

xpt

An abstraction over whatever package managers you have installed in your system
Shell
5
star
18

homebrew-tools

Homebrew taps for tools I maintain
Ruby
5
star
19

louvre

A lightweight, cross-platform game scraper written in Rust
Rust
4
star
20

enzyme

Control Windows features with a keyboard, a mouse, a joystick or speech recognition
AutoHotkey
4
star
21

emojis

Emoji list
Shell
3
star
22

wacom2Tuio

Cross-platform TUIO output bridge for Wacom tablets
C++
3
star
23

leila

Hexapod robot
JavaScript
3
star
24

blog

Personal blog
HTML
1
star
25

katacoda-scenarios

Katacoda Scenarios
Shell
1
star
26

uri

HTML
1
star
27

channel-rs

Rust
1
star
28

misc

Shell
1
star
29

storm

Cloud utilities
Rust
1
star
30

rust-common

Toolbelt for Rust code
Rust
1
star
31

tuioGestures

Execute command-line code and hotkeys using TUIO-compatible devices
JavaScript
1
star
32

denisidoro.github.io

About me
HTML
1
star
33

git-monorepo

A proof-of-concept CLI for faster workflows using monorepos.
Shell
1
star
34

quark

Clojure(script) utility belt
Clojure
1
star
35

move-reminder

A mind map ecosystem which uses indented plain text files.
TypeScript
1
star
36

xWacom

Add custom multitouch gestures to your Wacom tablet
C++
1
star
37

bandex

API em PHP para obtenção dos cardápios dos bandejões da USP e da ECP
PHP
1
star