🎹 Jetpack Compose Framework for simple state and event management.
Bored of writing boilerplate code in every new Android project to handle states, events, effects, etc?
Just use Composer!
You're probably familiar with declaring Ui states for every screen in your app. Most of the time it was just a sealed class, for example:
sealed class HomeState {
object Initial : HomeState()
object Loading : HomeState()
data class Success(...) : HomeState()
data class Error(val throwable: Throwable) : HomeState()
...
}
Most of the screens you create will look like this. Initial, Loading, Success, Error like states. So is it worth writing it every time? Nope.
Using composer's SimpleUiState and/or ComplexUiState gives you access to these predefined states for every screen:
- Loading
- Retrying
- SwipeRefreshing
- Success
- Failure
- SwipeRefreshingFailure
This should cover most of the cases for you. If not, you can create custom states for a specific screen/case/etc.
Imagine you want to create a new feature (let's say it's one screen for now).
class HomeViewModel : ViewModel {
// ViewModel logic
}
You've created a new ViewModel and you want to hold the screen's state inside. With composer it is really easy.
class HomeViewModel : ViewModel(),
SimpleUiStateManager by SimpleUiStateManagerImpl(
initialState = SimpleUiState.Loading
) {
// ViewModel logic
}
Next, create a composable function that will present the state, based on the ComposerViewModel:
@Composable
fun HomeScreen(
homeViewModel: HomeViewModel = viewModel() // use your DI to get the ViewModel
) {
SimpleUiStateView(homeViewModel) {
HomeScreenSuccessStateView()
}
}
dependencies {
implementation "com.patrykkosieradzki:composer:1.0.0"
}
See how to import the snapshot
Snapshots of the current development version of Composer are available, which track the latest versions.
To import snapshot versions on your project, add the code snippet below on your gradle file.
repositories {
maven { url 'https://s01.oss.sonatype.org/content/repositories/snapshots/' }
}
Next, add the below dependency to your module's build.gradle
file.
dependencies {
implementation "com.patrykkosieradzki:composer:1.0.0-SNAPSHOT"
}
Copyright 2021 Patryk Kosieradzki.
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.