Skip to content

ikarenkov/Modo

Repository files navigation

Modo

Maven Central License: MIT

Modo is a simple and convenient state-base navigation library for Jetpack Compose.

Basic navigation MultiScreen navigation Nested navigation

The Main idea

  • NavigationState defines UI
    • Initial state is defined in constructor of ContainerScreen by navModel: NavModel<State, Action>
    • To update state, use dispatch(action: Action) on NavigationContainer, or build-in extension functions for StackScreen and MultiScreen
  • There are Screen and ContainerScreen
    • ContainerScreen can contain and render child screens
    • There are some build-in implementation of ContainerScreen like StackScreen and MultiScreen
  • You can easily create custom Action by extending Action or ReducerAction,

Quick start

The best way to lear the library is discovering sample application. You can add Modo into your project doing following steps

  1. Add gradle dependency. In your build.gradle.kts
    plugins {
        //...
        //for serialization screens
        id("kotlin-parcelize")
    }
    
    dependencies {
        implementation("com.github.terrakok:modo-compose:${latest_version}")
    }
  2. Create your first screen. Generally it's convenient to use StackScreen as the first screen in the hierarchy:
    @Parcelize
    class SampleStack(
        private val stackNavModel: StackNavModel
    ) : StackScreen(stackNavModel) {
    
        constructor(rootScreen: Screen) : this(StackNavModel(rootScreen))
    
        @Composable
        override fun Content(modifier: Modifier) {
            // Render the last screen from the stack
            TopScreenContent(modifier) { modifier ->
                // Define the animation for changing screens
                SlideTransition(modifier)
            }
        }
    }
  3. Create simple screen
    @Parcelize
    class SampleScreen(
        override val screenKey: ScreenKey = generateScreenKey()
    ) : Screen {
    
        @Composable
        override fun Content(modifier: Modifier) {
            Text(text = "Hello world", modifier = modifier.fillMaxSize())
        }
    }
  4. Integrate screen with to your application. You can use rememberRootScreen() inside your Fragment or Activity:
    1. For Activity
      class MyActivity : AppCompatActivity() {
         override fun onCreate(savedInstanceState: Bundle?) {
             super.onCreate(savedInstanceState)
             setContent {
                 // Remember root screen using rememberSeaveable under the hood.
                 val rootScreen = rememberRootScreen {
                     SampleStack(MainScreen(1))
                 }
                 rootScreen.Content(modifier = Modifier.fillMaxSize())
             }
         }
      }
    2. For Fragment
      class MyFragment : Fragment() {
         override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View =
             ComposeView(inflater.context).apply {
                 setContent {
                     val rootScreen = rememberRootScreen {
                         SampleStack(MainScreen(screenIndex = 1, canOpenFragment = true))
                     }
                     rootScreen.Content(modifier = Modifier.fillMaxSize())
                 }
             }
      }

List of features

Feature Status Source/Docs Sample
Stack navigation StackScreen, build-in commands SampleStack, sample of navigation actions
Multi screen navigation MultiScreen, build-in commands SampleMultiscreen
Dialogs DialogScreen Dialogs playground
Arguments Arguments can be passed as constructor parameter of Screen. MainScreen
Animation ScreenTransition, helpers for stack screen transition SampleStack
Android lifecycle support ModoScreenAndroidAdapter connect Modo with android-related features. You can safely use LocalLifecycleOwner and other fun inside Screen's. To correctly observe screen lifecycle it's recommender to use LifecycleScreenEffect MainScreen
Android ViewModel support ModoScreenAndroidAdapter connect Modo with android-related features. You can safely use viewModel and other fun inside Screen's. AndroidViewModelSampleScreen.kt
Activity and fragment integration Use rememberRootScreen inside Fragment or Activity. These functions are declared in Modo and automatically handle saving and restoring during Activity lifecycle and process death. ModoSampleActivity, ModoFragment.kt, ModoLegacyIntegrationActivity.kt
Activity and process death handling Automatically provided by Fragment and Activity Modo integration. For more details take a look to ModoSampleActivity, ModoFragment.kt
Screen effects You can use LaunchedScreenEffect and DisposableScreenEffect analogies for Screen, it's linked to life time of a Screen. There is also LifecycleScreenEffect for easy observing lifecycle. See ScreenEffects for details. ScreenEffectsSampleScreen.kt
Pager and LazyList integration You can create custom ContainerScreen and use internal Screen inside Vertical/HorizontalPager and LazyRow/Column. It's vital to define key = {} lambda with InternalContent for correct intagration. For more details take a look to the samples. StackInLazyColumnScreen.kt, HorizontalPagerScreen.kt

BuildIn Container Screens

You can implement container screen by your own, but Modo provides several default containers

  1. StackScreen - stack navigation implementation. Sample of navigation

  2. Multiscreen - multiscreen navigation. Sample of navigation

Check out sample for more.

License

MIT License

Copyright (c) Konstantin Tskhovrebov (@terrakok)
          and Karenkov Igor (@KarenkovID)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.