项目作者: chuross

项目描述 :
Annotation based Android router library. (with Shared Element support)
高级语言: Kotlin
项目地址: git://github.com/chuross/mori-router.git
创建时间: 2018-03-20T10:17:12Z
项目社区:https://github.com/chuross/mori-router

开源协议:Apache License 2.0

下载


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.

    1. repositories {
    2. maven { url "https://jitpack.io" }
    3. }
  2. add the dependency

  1. dependencies {
  2. implementation 'com.github.chuross.mori-router:annotation:x.x.x'
  3. annotationProcessor 'com.github.chuross.mori-router:compiler:x.x.x' // or kpt
  4. }

Usage

Basic

  1. Add annotations in your screen fragment.
  1. @RouterPath(name = "main")
  2. class MainScreenFragment : Fragment() {
  3. @Argument
  4. lateinit var param1: String
  5. @Argument(name = "ieei")
  6. lateinit var param2: Int
  7. @Argument(required = false)
  8. var param3: ArrayList<String> = arrayListOf()
  9. override fun onCreate(savedInstanceState: Bundle?) {
  10. super.onCreate(savedInstanceState)
  11. MoriBinder.bind(this) // MoriBinder is auto generated class.
  12. }
  13. ....
  14. }
  1. Execute build command, Then MoriRouter class is auto generated.
  1. val transitionFactory = DefaultTransitionFactory { /* return `android.support.transition` or `android.transition` */ }
  2. val options = MoriRouterOptions.Builder(R.id.container)
  3. .setEnterTransitionFactory(transitionFactory)
  4. .setExitTransitionFactory(transitionFactory)
  5. .build()
  6. val router = MoriRouter(supportFragmentManager, options) // MoriRouter is auto generated class.
  7. router.main("required1", 1000) // main(String param1, Integer ieei)
  8. .param3(arrayListOf("fuga")) // optional value
  9. .launch() // launch main screen
  10. // pop screen
  11. router.pop()

Fragment builder support

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

  1. @WithArguments
  2. class HogeFragment : Fragment() {
  3. @Argument
  4. lateinit var hogeName: String
  5. ....
  6. }
  1. val fragment: Fragment = HogeFragmentBuilder(hogeName).build() // HogeScreenFragmentBuilder is auto generated class

override enter / exit transition

  1. @RouterPath(
  2. name = "main",
  3. overrideEnterTransitionFactory = MainScreenTransitionFactory::class,
  4. overrideExitTransitionFactory = MainScreenTransitionFactory::class
  5. )
  1. If use deepLink support, uri parameter add to @RouterPath, and add @UriArgument parameters in your screen fragment.
  1. @RouterPath(
  2. name = "second",
  3. uris = [
  4. "example://hoge/{hoge_id}/{fuga}",
  5. "https://example.com/hoge/{hoge_id}/{fuga}" //also can use multiple uri
  6. ]
  7. )
  8. class SecondScreenFragment : Fragment() {
  9. @UriArgument(name = "hoge_id")
  10. var hogeId: Int
  11. @UriArgument
  12. var fuga: String
  13. // If use `@UriArgument`, Don't use `required = true`.
  14. @Argument(required = false)
  15. var piyo: String? = null
  16. override fun onCreate(savedInstanceState: Bundle?) {
  17. super.onCreate(savedInstanceState)
  18. MoriBinder.bind(this)
  19. }
  20. }
  1. MoriRouter has dispatch method. Then call dispatch with Uri.
  1. router.dispatch(Uri.parse("example://hoge/123/test")) // launch SecondScreenFragment (hogeId = 123, fuga=test)
  2. 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

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

Code

  1. // yourView must has view id
  2. // ex) yourView.setId(R.id.your_id)
  3. ViewCompat.setTransitionName(yourView, "your_transition_name");
  1. add sharedEnterTransitionFactory and sharedExitTransitionFactory to @RouterPath.
  1. @RouterPath(
  2. name = "third",
  3. sharedEnterTransitionFactory = ThirdScreenSharedTransitionFactory::class,
  4. sharedExitTransitionFactory = ThirdScreenSharedTransitionFactory::class
  5. )
  6. class ThirdScreenFragment : Fragment() {
  7. ....
  8. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  9. super.onViewCreated(view, savedInstanceState)
  10. // ThirdScreenFragment must has `R.id.your_id` view
  11. // this id is same before screen's shared element id
  12. MoriBinder.bindElement(this, R.id.your_id)
  13. }
  14. }
  1. add SharedElements when before transition third screen.
  1. // yourView has `R.id.your_id`
  2. 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.

    1. ViewCompat.setTransitionName(yourView, "your_transition_name");
  2. add manualSharedViewNames to @RouterPath

  1. @RouterPath(
  2. name = "third",
  3. manualSharedViewNames = ["shared_view_image"],
  4. sharedEnterTransitionFactory = ThirdScreenSharedTransitionFactory::class,
  5. sharedExitTransitionFactory = ThirdScreenSharedTransitionFactory::class
  6. )
  7. class ThirdScreenFragment : Fragment() {
  8. ....
  9. override fun onCreate(savedInstanceState: Bundle?) {
  10. super.onCreate(savedInstanceState)
  11. val callback = ThirdSharedElementCallBack() // auto generated class
  12. .sharedViewImage({ /* get shared element view from ViewPager */ })
  13. setEnterSharedElementCallback(callback)
  14. }
  15. }
  1. setExitSharedElementCallback when before transition third screen.

    1. @RouterPath(
    2. name = "second"
    3. )
    4. class SecondScreenFragment : Fragment() {
    5. ....
    6. override fun onCreate(savedInstanceState: Bundle?) {
    7. super.onCreate(savedInstanceState)
    8. val callback = ThirdSharedElementCallBack() // auto generated class
    9. .sharedViewImage({ /* get shared element view from RecyclerView */ })
    10. setExitSharedElementCallback(callback)
    11. }
    12. ....
    13. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    14. ....
    15. // call manualSharedMapping
    16. router.third().manualSharedMapping(context).launch()
    17. }
    18. }

License

  1. Copyright 2018 chuross
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.