'Best way to handle network response in jetpack compose when using SRP

When using the Single Responsibility pattern, I wonder what would be the best approach to show network response (Success, error, progress). storing state for every request would create so many states in viewModel and will have to pass so many states to a component. Any other way, worth a try?



Solution 1:[1]

I use this in my main Activity's onCreate function to capture all unhandled exceptions but only when the app is released for production. During debug, I want the app to crash so that the exception stack is printed to LogCat:

if (!BuildConfig.DEBUG) {
    Thread.setDefaultUncaughtExceptionHandler { paramThread, paramThrowable ->
         val message: String

         if (paramThrowable is HttpException) {
              message = R.string.problem_processing_request.stringRes()
         } else {
              message = R.string.unknown_problem.stringRes()
         }

         App.mainViewModel.displaySnackbar(sbInfo = SnackbarInfo(message = message, isCritical = true))
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Johann