- Notifications
You must be signed in to change notification settings - Fork9
Android Multi-module navigator, trying to find a way to navigate into a modularized android project
License
florent37/Navigator
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Android Multi-module navigator, trying to find a way to navigate into a modularized android multi module project
implementation"com.github.florent37:navigator:1.0.0"
project | \--app | \--routing | \--splash | \--home | \--user
And we want the next screen flow
app --[directly starts]--> Splash --> Home --[clicks on an user]--> User(id)
splash
should only knowrouting
home
should only knowrouting
user
should only knowrouting
app
should only knowrouting
to present the splashrouting
should not know others modules
In a dedicated module, created shared routes into aRoute
object
in modulerouting
:
object Routes {object Splash : Route("/")object Home : Route("/home")object User : RouteWithParams<UserParams>("/user/{userId}") {classUserParams(valuserId:String) : Param }}
You need to associate an intent to eachRoute
in your feature's module
in modulesplash
:
Routes.Splash.register { context->Intent(context,SplashActivity::class.java)}
If you want an android module to register automatically its route,you can bind usingapplicationprovider's auto providers :
//automatically launched after application's onCreate()classRouteProvider :Provider() {overridefunprovide() {Routes.Splash.register { context->Intent(context,SplashActivity::class.java) } }}
then declare it in your module's AndroidManifest.xml
<providerandroid:authorities="${applicationId}.splash.RouteProvider"android:name=".RouteProvider"/>
You can push a route from an activity (or fragment) using
Navigator.of(this).push(Routes.Home)
or by its path
Navigator.of(this).push("/home/")
You can also change the route from anywhere (eg: an android ViewModel) usingNavigator.current()
Navigator.current()?.push(Routes.Home)
Navigating to a route with parameters forces to specify values
Navigator.of(this).push(Routes.User,UserParams(userId="3"))
You can retrieve them using kotlin's delegated properties
classUserActivity :Activity {privateval args by parameter<Routes.User.Params>()overridefunonCreate(savedInstanceState:Bundle?) {super.onCreate(savedInstanceState)val userId= args.userId
You can also call it by path's params
Navigator.of(this).push("/user/3")
It will usemoshi to create the parameter from path params
Navigator.of(this).pop()
If you want to replace the current screen
Navigator.current()?.pushReplacement(Routes.Splash)
A flavor is an endpoint of a route, you can use them to navigate to an Activity's BottomNavigation item
object Home : Route("/home/") {object UserTabs : Flavor<Home>(this,"home/tabUsers")object PostsTabs : FlavorWithParams<Home, PostsTabs.Params>(this,"home/tabPosts") {classParams(valuserId:Int?) : Parameter() }}
You can push this like a route
Navigator.current()?.push(Routes.Home.UserTabs)
And bind this into your Activity
classHomeActivity :AppCompatActivity(R.layout.activity_home) {overridefunonCreate(savedInstanceState:Bundle?) {super.onCreate(savedInstanceState) viewPager.adapter=ViewPagerAdapter( supportFragmentManager ) bottomNav.setupWithViewPager(viewPager) bindFlavor(Routes.Home.UserTabs, intent) .withBottomNav(bottomNav,R.id.tabUsers) bindFlavor(Routes.Home.PostsTabs, intent) .withBottomNav(bottomNav,R.id.tabPosts) }}
And to retrieve a flavor parameter
classPostsFragment :Fragment {privatevar args by optionalFlavorParameter<Routes.Home.PostsTabs.Params>()overridefunonViewCreated(view:View,savedInstanceState:Bundle?) {super.onViewCreated(view, savedInstanceState)val userId= args.userId
Author: Florent Champignyhttp://www.florentchampigny.com/
Copyright 2019 Florent37, Inc.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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.