Giter Site home page Giter Site logo

chuross / mori-router Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 0.0 391 KB

Annotation based Android router library. (with Shared Element support)

Home Page: https://qiita.com/chuross/items/3fc5ad7f72e219df5181

License: Apache License 2.0

Java 5.22% Kotlin 94.78%
android-router-library android-library shared-element annotation-processor

mori-router's Introduction

MoriRouter

Annotation based Android router library.

This library for single activity application.

And This library provide easy implementation for SharedElement.

Futures

  • Auto generate routing codes
  • Auto generate Fragment builder codes
  • DeepLink support
  • Shared element support

Download

Gradle

  1. add JitPack repository to your project root build.gradle.
repositories {
    maven { url "https://jitpack.io" }
}
  1. add the dependency
dependencies {
    implementation 'com.github.chuross.mori-router:annotation:x.x.x'
    annotationProcessor 'com.github.chuross.mori-router:compiler:x.x.x' // or kpt
}

Usage

Basic

  1. Add annotations in your screen fragment.
@RouterPath(name = "main")
class MainScreenFragment : Fragment() {

    @Argument
    lateinit var param1: String

    @Argument(name = "ieei")
    lateinit var param2: Int

    @Argument(required = false)
    var param3: ArrayList<String> = arrayListOf()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MoriBinder.bind(this) // MoriBinder is auto generated class.
    }

    ....
}
  1. Execute build command, Then MoriRouter class is auto generated.
val transitionFactory = DefaultTransitionFactory { /* return `android.support.transition` or `android.transition` */ }

val options = MoriRouterOptions.Builder(R.id.container)
                .setEnterTransitionFactory(transitionFactory)
                .setExitTransitionFactory(transitionFactory)
                .build()

val router = MoriRouter(supportFragmentManager, options) // MoriRouter is auto generated class.

router.main("required1", 1000) // main(String param1, Integer ieei)
    .param3(arrayListOf("fuga")) // optional value
    .launch() // launch main screen


// pop screen
router.pop()

Fragment builder support

Also can use @WithArguments annotation. This library generate {class_name}Builder code.

@WithArguments
class HogeFragment : Fragment() {

    @Argument
    lateinit var hogeName: String

    ....
}
val fragment: Fragment = HogeFragmentBuilder(hogeName).build() // HogeScreenFragmentBuilder is auto generated class

override enter / exit transition

@RouterPath(
    name = "main",
    overrideEnterTransitionFactory = MainScreenTransitionFactory::class,
    overrideExitTransitionFactory = MainScreenTransitionFactory::class
)

DeepLink support

  1. If use deepLink support, uri parameter add to @RouterPath, and add @UriArgument parameters in your screen fragment.
@RouterPath(
  name = "second",
  uris = [
    "example://hoge/{hoge_id}/{fuga}",
    "https://example.com/hoge/{hoge_id}/{fuga}" //also can use multiple uri
  ]
)
class SecondScreenFragment : Fragment() {

    @UriArgument(name = "hoge_id")
    var hogeId: Int

    @UriArgument
    var fuga: String

    // If use `@UriArgument`, Don't use `required = true`.
    @Argument(required = false)
    var piyo: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        MoriBinder.bind(this)
    }
}
  1. MoriRouter has dispatch method. Then call dispatch with Uri.
router.dispatch(Uri.parse("example://hoge/123/test")) // launch SecondScreenFragment (hogeId = 123, fuga=test)
router.dispatch(Uri.parse("https://example.com/hoge/123/test")) // launch SecondScreenFragment (hogeId = 123, fuga=test)

SharedElement support

Basic

  1. set transition name in your XML layout or in your code.

XML

<YourLayout
    ....
    android:id="@+id/your_id" <!-- must have view id -->
    android:transitionName="your_transition_name" />

Code

// yourView must has view id
// ex) yourView.setId(R.id.your_id)
ViewCompat.setTransitionName(yourView, "your_transition_name");
  1. add sharedEnterTransitionFactory and sharedExitTransitionFactory to @RouterPath.
@RouterPath(
    name = "third",
    sharedEnterTransitionFactory = ThirdScreenSharedTransitionFactory::class,
    sharedExitTransitionFactory = ThirdScreenSharedTransitionFactory::class
)
class ThirdScreenFragment : Fragment() {
   ....

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // ThirdScreenFragment must has `R.id.your_id` view
        // this id is same before screen's shared element id
        MoriBinder.bindElement(this, R.id.your_id)
   }
}
  1. add SharedElements when before transition third screen.
// yourView has `R.id.your_id`
router.third().addSharedElement(yourView).launch()

Dynamic Shared Element transition

if you need dynammic sharedElement transition, you should use this option.

  1. set transition name in your code.
ViewCompat.setTransitionName(yourView, "your_transition_name");
  1. add manualSharedViewNames to @RouterPath
@RouterPath(
    name = "third",
    manualSharedViewNames = ["shared_view_image"],
    sharedEnterTransitionFactory = ThirdScreenSharedTransitionFactory::class,
    sharedExitTransitionFactory = ThirdScreenSharedTransitionFactory::class
)
class ThirdScreenFragment : Fragment() {
   ....

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)

       val callback = ThirdSharedElementCallBack() // auto generated class
                        .sharedViewImage({ /* get shared element view from ViewPager */ })

       setEnterSharedElementCallback(callback)
   }
}
  1. setExitSharedElementCallback when before transition third screen.
@RouterPath(
    name = "second"
)
class SecondScreenFragment : Fragment() {
   ....

   override fun onCreate(savedInstanceState: Bundle?) {
       super.onCreate(savedInstanceState)

       val callback = ThirdSharedElementCallBack() // auto generated class
                        .sharedViewImage({ /* get shared element view from RecyclerView */ })

       setExitSharedElementCallback(callback)
   }

   ....

   override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        ....

        // call manualSharedMapping
        router.third().manualSharedMapping(context).launch()
   }
}

License

Copyright 2018 chuross

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

mori-router's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

mori-router's Issues

Auto generation don't work

for reproduce just replace in your sample project app:
// project dependency implementation project(':annotation') kapt project(':compiler')
to
implementation 'com.github.chuross.mori-router:annotation:1.0.1' kapt 'com.github.chuross.mori-router:compiler:1.0.1'
and rebuild

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.