Hey Android developers! π§βπ»π©βπ» Have you been struggling with network state management in your Android projects? Good news β the Network State Utils library is here to simplify the process and offer seamless integration with Jetpack Compose to update your UI! πͺππ²
Step 1: Easily Install the Library π₯
Add the package to your project by first including jitpack in your root build.gradle:
allprojects {
repositories {
// ...
maven { url 'https://jitpack.io' }
}
}
Then, add the dependency to your module gradle:
dependencies {
implementation 'com.github.JimmyMcBride:NetworkStateUtilsLib:1.0.1'
}
Step 2: Leverage the NetworkState Sealed Class π
NetworkState
helps you manage your network requests' current status. States include Idle
, Loading
, Success
, and Error
. Take control of UI updates during specific network states!
sealed class NetworkState<out T> {
object Idle : NetworkState<Nothing>()
object Loading : NetworkState<Nothing>()
data class Success<T>(val data: T) : NetworkState<T>()
data class Error(val message: String) : NetworkState<Nothing>()
}
Step 3: duringState & DuringComposableState π
Using duringState
and DuringComposableState
, handle what happens at various network states. Achieve seamless integration with classic view models or Jetpack Compose!
networkEvent.duringState(
success = { data -> onSuccess(data) },
error = { message -> onError(message) },
loading = { onLoading() },
idle = { onIdle() }
)
networkEvent.DuringComposableState(
success = { data ->
Text(data.name)
},
error = { message ->
Text("N/A")
showSnackbar(message)
},
loading = { CircularProgressIndicator() },
idle = {}
)
Step 4: ConsumeNetworkEvent π
Use ConsumeNetworkEvent
to reset NetworkState
back to Idle after a specific event. It resets the state, ensuring your UI remains fresh for subsequent updates!
ConsumeNetworkEvent(
networkEvent = addCityEvent,
consumeEvent = citiesViewModel::consumeAddCityEvent,
onSuccess = {
isLoading = false
navController.popBackStack()
},
onError = {
isLoading = false
},
onLoading = { isLoading = true }
)
Step 5: Simplify with handleNetworkException & handleResponse π οΈ
Simplify your repository by using handleNetworkException
and handleResponse
for managing exceptions and responses during API calls.
fun <T> Response<T>.handleResponse(errorMessage: String = "Something went wrong.") =
if (this.isSuccessful && this.body() != null)
NetworkState.Success(data = this.body()!!)
else
NetworkState.Error(message = errorMessage)
suspend fun <T> handleNetworkException(apiCall: suspend () -> NetworkState<T>) = try {
apiCall()
} catch (e: Exception) {
NetworkState.Error(message = e.message.toString())
}
With Network State Utils, you have a powerful and accessible library to manage network events in your Android applications. Empower your code and simplify your network state management!
What are your thoughts on the Network State Utils library? Feel free to ask questions or leave comments below! And don't forget to hit that Like button and follow for more awesome content! π€©π