A lightweight state management library for Compose Multiplatform.

Overview

Staccato

A lightweight state management library for Compose Multiplatform.

The term staccato (pronounced "stuh-caw-toe") means detached, or separated, notes . Staccato notes have space, or silence, between them. There are different degrees of staccato notes.

This project is currently experimental and the API subject to breaking changes without notice.

Download

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

dependencies {
    implementation 'com.github.marcellogalhardo:staccato:Tag'
}

(Please replace {Tag} with the latest version numbers)

Basic Usage

class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            StaccatoHost(
                // Optional parameter, only required when running on Android.
                isChangingConfigurations = { this@MainActivity.isChangingConfigurations }
            ) {
                // Creates a single instance shared by all scopes of StaccatoHost.
                val httpClient = singleton { HttpClient() }

                // Creates a new scope.
                StaccatoScope {
                    // Creates a ViewModel inside the `StaccatoScope`
                    // if the scope is disposed, so is the ViewModel.
                    val viewModel = scoped { MyViewModel1(httpClient) }
                    Component1(viewModel) // renders the UI.
                }
            }
        }
    }
}

Note that configuration changes will not affect the httpClient nor viewModel instances.

Supported Retentions

  • singleton: retains it with the StaccatoHost (single instance).
  • scoped: retains it with the parent StaccatoScope.
  • reusable (TODO): similar to singleton but can be removed from memory if not in use.

Advanced Usage: Providers (TODO)

val HttpClientProvider = Provider {
    singleton { HttpClient() }
}

val MyViewModelProvider = Provider {
    val httpClient = HttpClientProvider.get()
    scoped { MyViewModel(httpClient) }
}

val MyViewModelProvider = {
    val httpClient = HttpClientProvider.get()
    scoped { MyViewModel(httpClient) }
}

val MyViewModelProvider = { id: Int ->
    val httpClient = HttpClientProvider.get()
    scoped(key1 = id) { MyViewModel(id, httpClient) }
}

// Previous example
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val isChangingConfigurations = { isChangingConfigurations }
        setContent {
            StaccatoHost(isChangingConfigurations) {
                StaccatoScope {
                    val viewModel = MyViewModelProvider.get()
                    Component1(viewModel) // renders the UI.
                }
            }
        }
    }
}

Advanced Usage: Navigation Integration (TODO)

// Now, the StaccatoScope will connect to the `navBackStackEntry` and only // remove from the memory if the `navBackStackEntry` is also removed. StaccatoScope(navBackStackEntry) { // Same API. val viewModel = MyViewModelProvider.get() Component1(viewModel) // renders the UI. } } } } ">
@Composable
private fun NavRouting() {
    val navController = rememberNavController()
    NavHost(navController, startDestination = "launch") {
        composable("launch") { navBackStackEntry ->
            // Now, the StaccatoScope will connect to the `navBackStackEntry` and only
            // remove from the memory if the `navBackStackEntry` is also removed.
            StaccatoScope(navBackStackEntry) {
                // Same API.
                val viewModel = MyViewModelProvider.get()
                Component1(viewModel) // renders the UI.
            }
        }
    }
}

Advanced Usage: Process Death

val MyViewModelProvider = Provider {
    val httpClient = HttpClientProvider.get()
    val saver = MyViewModelSaver()
    scoped(saver) { MyViewModel(httpClient) }
}

Advanced Usage: Closeable (TODO)

val MyViewModelProvider = Provider {
    val httpClient = HttpClientProvider.get()
    scoped(onClose = { vm -> vm.close() }) { MyViewModel(httpClient) }
}

Note that if MyViewModel implements Closeable, it will be automatically invoked.

Testing with Providers (TODO)

@Before
fun setUp() {
    HttpClientProvider.replace {
        // Creates fake version of HttpClient or whatever.
    }
    // Now `HttpClientProvider` will always return the return you choose.
}

License

Copyright 2021 Marcello Galhardo

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.
You might also like...
A lightweight library for using Material Colors in Android Jetpack Compose Project 🚀🚀🚀
A lightweight library for using Material Colors in Android Jetpack Compose Project 🚀🚀🚀

How To Use Step 1: Add the JitPack repository to your build file. Add it in your root build.gradle / build.gradle.kts at the end of repositories: Groo

TaskScheduler - A concise practical task management tool for scheduling tasks for Android

TaskScheduler A concise practical task management tool for scheduling tasks for

laboratory is the next generation Minecraft server management tool fully written in Kotlin
laboratory is the next generation Minecraft server management tool fully written in Kotlin

laboratory laboratory is the next generation Minecraft server management tool fully written in Kotlin Installation Linux: Clone this repository using

🧱 Brick - Multiplatform navigation library for Compose.
🧱 Brick - Multiplatform navigation library for Compose.

Brick Take control of your apps Brick is a lightweight library to make navigation. Features Framework free (Router can be injected in any layer of pro

A lightweight particle system for Jetpack Compose - Quarks
A lightweight particle system for Jetpack Compose - Quarks

compose-particle-system Quarks is a lightweight particle system for Jetpack Compose. There are endless possibilities for creating generative art with

Koin-compose - A pragmatic lightweight dependency injection framework for Kotlin developers
Koin-compose - A pragmatic lightweight dependency injection framework for Kotlin developers

What is KOIN? - https://insert-koin.io A pragmatic lightweight dependency inject

A Sudoku game for Android & Desktop written with Jetpack Compose Multiplatform
A Sudoku game for Android & Desktop written with Jetpack Compose Multiplatform

🚀 Compose Arcade A sample Kotlin Multiplatform Compose Sudoku app for Android & Desktop. Most code is shared between Android & Desktop using Kotlin M

Here you can try out Kotlin Multiplatform and Jetpack Compose with some other cutting-edge technologies.
Here you can try out Kotlin Multiplatform and Jetpack Compose with some other cutting-edge technologies.

wire The Wire is a Kotlin Multiplatform sample project, currently supporting Android and Windows. Tools And Technolagies Architecture: MVVM MultiThrea

Simple application made using Compose Multiplatform, SQLDelight, Decompose.
Simple application made using Compose Multiplatform, SQLDelight, Decompose.

Notes Simple application made using Compose Multiplatform, SQLDelight and Decompose. Screenshots: Video: TODO There is plans for adding next stuff: Te

Releases(0.0.1-alpha03)
Owner
Marcello Galhardo
Marcello Galhardo
Odyssey it's a declarative multiplatform navigation library for Multiplatform Compose

Odyssey Odyssey it's a declarative multiplatform navigation library for Multiplatform Compose ?? WARNING! It's an early preview, so you use it with yo

Alex 168 Jan 5, 2023
Using State in Jetpack Compose Codelab

Using State in Jetpack Compose Codelab This folder contains the source code for the Using State in Jetpack Compose codelab. In this codelab, you will

Jose Javier 0 Nov 17, 2021
Android Clean Architecture That Screams (MVVM + JetPack Compose UI + Flow + State)

Android Clean Architecture That Screams (MVVM + JetPack Compose UI + Flow + State)

Praveen Sharma 9 May 11, 2022
Compose-buttons - A set of Loading animations used in Buttons to convey a "loading" state after the button is clicked.

Loading Buttons A set of Loading animations used in Buttons to convey a "loading" state after the button is clicked. A simple demo application that sh

Brad Ball 16 Jul 5, 2022
Undo snapshot state changes in Compose.

compose-undo Track changes to any snapshot state object and restore state from any point in the past. Usage implementation 'com.zachklipp.compose-undo

Zach Klippenstein 135 Jan 1, 2023
Android.compose.squircle - Android LightWeight Squircle Library for JetPack Compose

Android LightWeight Squircle Library for JetPack Compose Usage Based on Compose

Quang Nguyen 9 Jul 5, 2022
Lightweight library to tweak the fling behaviour in Android. This library is only compatible with Jetpack-Compose.

Flinger (Only compatible with compose) What is Flinger? Flinger is a plugin that is made on top of jetpack compose that will help the developer to twe

Joseph James 73 Dec 24, 2022
Field state manager and basic set of validation, fields

Compose Forms Field state manager and basic set of validation, fields

Vitaliy Zarubin 9 Jan 4, 2022
Holi is a lightweight Jetpack Compose library of colors, gradients and cool utility functions for all your palette needs!

Holi A library of colors, gradients and utils built using Jetpack Compose for Android Features A wide collection of colors from different palettes for

Siddhesh Patil 167 Dec 5, 2022
Holi is a lightweight Jetpack Compose library of colors, gradients and cool utility functions for all your palette needs!

Holi is a lightweight Jetpack Compose library of colors, gradients and cool utility functions for all your palette needs!

Sid Patil 167 Dec 5, 2022