A collection of small handy, ready-made hooks for Jetpack Compose so that you don't have to think about the states, logic or anything. Just plug it in and use!
Inspired from react hooks
.
Add the following dependency in your build.gradle
implementation("me.nikhilchaudhari:compose-usefetch:{latest-version}")
When you want just to fetch some data and don't want to setup everything unnecessarily!
For example -
var resultState = useFetch(url = "https://yoururl.com/something")
when(val data = resultState.value) {
Result.Error -> { /* When there is some error, show Toast/snackbar/error message */ }
Result.Response -> { /* When response is received successfully, show the data on UI */ }
Result.Loading -> { /* When request is loading, show the loading progress on UI */ }
}
Add the following dependency in your build.gradle
implementation("me.nikhilchaudhari:compose-usenetworkstate:{latest-version}")
When you want to use the network connectivity as a state in Compose!
For example -
val networkState by useNetworkState()
if (networkState == NetworkState.Online) {
// Call the main UI
} else {
// Show the network is disconnected/offline UI
Text(text = "You are offline! Please connect to the network!")
}
Add the following dependency in your build.gradle
implementation("me.nikhilchaudhari:compose-usereducer:{latest-version}")
When you want to make your state updates in Redux style.
For example -
var count by remember { mutableStateOf(0) }
val dispatcher = useReducer {
"increment" does { count++ }
"decrement" does { count-- }
}
Button(onClick = { dispatcher.dispatch("increment") }) { / ** / }
Text(text = count.toString())
Button(onClick = { dispatcher.dispatch("decrement") }) {/**/}
Checkout more examples here in the sample app. You can go to individual hook repository to know more about those.
Please feel free to contribute. Create your own hooks. Checkout the contribution guide for more.