All Projects → Zhuinden → simple-stack-compose-integration

Zhuinden / simple-stack-compose-integration

Licence: Apache-2.0 license
[ACTIVE/BETA] Compose integration for Simple-Stack.

Programming Languages

kotlin
9241 projects
java
68154 projects - #9 most used programming language

Simple Stack Compose Integration

Default behavior for Jetpack Compose using Simple-Stack.

Using Simple Stack Compose Integration

In order to use Simple Stack Compose Integration, you need to add jitpack to your project root build.gradle.kts (or build.gradle):

// build.gradle.kts
allprojects {
    repositories {
        // ...
        maven { setUrl("https://jitpack.io") }
    }
    // ...
}

or

// build.gradle
allprojects {
    repositories {
        // ...
        maven { url "https://jitpack.io" }
    }
    // ...
}

In newer projects, you need to also update the settings.gradle file's dependencyResolutionManagement block:

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
        maven { url 'https://jitpack.io' }  // <--
        jcenter() // Warning: this repository is going to shut down soon
    }
}

and then, add the dependency to your module's build.gradle.kts (or build.gradle):

// build.gradle.kts
implementation("com.github.Zhuinden:simple-stack-compose-integration:0.9.5")

or

// build.gradle
implementation 'com.github.Zhuinden:simple-stack-compose-integration:0.9.5'

As Compose requires Java-8 bytecode, you need to also add this:

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
    jvmTarget = '1.8'
    useIR = true
}
buildFeatures {
    compose true
}
composeOptions {
    kotlinCompilerExtensionVersion '1.1.1'
}

What does it do?

Provides defaults for Composable-driven navigation and animation support.

class MainActivity : AppCompatActivity() {
    private val composeStateChanger = ComposeStateChanger() // <--

    private val backPressedCallback = object: OnBackPressedCallback(true) { // this is the only way to make Compose BackHandler work reliably
        override fun handleOnBackPressed() {
            if (!Navigator.onBackPressed(this@MainActivity)) {
                this.remove()
                onBackPressed()
                this@MainActivity.onBackPressedDispatcher.addCallback(this)
            }
        }
    }

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

        onBackPressedDispatcher.addCallback(backPressedCallback) // this is the only way to make Compose BackHandler work reliably

        val backstack = Navigator.configure()
            .setStateChanger(AsyncStateChanger(composeStateChanger))  // <--
            .install(this, androidContentFrame, History.of(FirstKey()))

        setContent {
            BackstackProvider(backstack) {  // <--
                MaterialTheme {
                    Box(Modifier.fillMaxSize()) {
                        composeStateChanger.RenderScreen()  // <--
                    }
                }
            }
        }
    }

    @Suppress("RedundantModalityModifier")
    override final fun onBackPressed() { // you cannot use `onBackPressed()` if you use `OnBackPressedDispatcher`
        super.onBackPressed() // therefore this is needed to use Compose's BackHandler
    }
}

and

abstract class ComposeKey: DefaultComposeKey(), Parcelable {
    override val saveableStateProviderKey: Any = this // data class + parcelable!
}

and

@Immutable
@Parcelize
data class SecondKey(private val noArgsPlaceholder: String = ""): ComposeKey() {
    @Composable
    override fun ScreenComposable(modifier: Modifier) {
        SecondScreen(modifier)
    }
}

What about ViewModels?

You can use ScopedServices for that.

abstract class ComposeKey : DefaultComposeKey(), Parcelable, DefaultServiceProvider.HasServices {
    override val saveableStateProviderKey: Any = this

    override fun getScopeTag(): String = javaClass.name

    override fun bindServices(serviceBinder: ServiceBinder) {
    }
}

and

val backstack = Navigator.configure()
                    .setScopedServices(DefaultServiceProvider())
                    // ...

and

@Immutable
@Parcelize
data class DogListKey(private val noArgsPlaceholder: String = "") : ComposeKey() {
    override fun bindServices(serviceBinder: ServiceBinder) {
        with(serviceBinder) {
            add(DogListViewModel(lookup<DogDataSource>(), backstack)) // <--
        }
    }

    @Composable
    override fun ScreenComposable(modifier: Modifier) {
        val viewModel = rememberService<DogListViewModel>() // <--

        val dogs by viewModel.dogList.observeAsState()

        DogListScreen(dogs.value)
    }
}

License

Copyright 2021 Gabor Varadi

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].