🎉 Introducing the Fantastic New Network State Utils Library for Android 🎉

Jimmy McBride - May 23 '23 - - Dev Community

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' }
    }
}
Enter fullscreen mode Exit fullscreen mode

Then, add the dependency to your module gradle:

dependencies {
    implementation 'com.github.JimmyMcBride:NetworkStateUtilsLib:1.0.1'
}
Enter fullscreen mode Exit fullscreen mode

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>()
}
Enter fullscreen mode Exit fullscreen mode

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() }
)
Enter fullscreen mode Exit fullscreen mode
networkEvent.DuringComposableState(
    success = { data ->
        Text(data.name)
    },
    error = { message ->
        Text("N/A")
        showSnackbar(message)
    },
    loading = { CircularProgressIndicator() },
    idle = {}
)
Enter fullscreen mode Exit fullscreen mode

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 }
)
Enter fullscreen mode Exit fullscreen mode

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())
}
Enter fullscreen mode Exit fullscreen mode

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! 🤩👍

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player