Skip to content

Instantly share code, notes, and snippets.

@rommansabbir
Created December 9, 2020 18:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rommansabbir/3961954d25db8c1b8fc6d08d4fe5813c to your computer and use it in GitHub Desktop.
Save rommansabbir/3961954d25db8c1b8fc6d08d4fe5813c to your computer and use it in GitHub Desktop.
How to use Kotlin's Sealed Class in Android Development for better & clean code
sealed class AppFailure {
/**
* Global Failure classes
* These failures will be used across all over the app including Data Layer, Domain Layer, Framework Layer
*/
class GeneralFailure(var message: String, var errorCode: Int? = null) : AppFailure()
class UnauthorizedFailure(var message: String = "Unauthorized", errorCode: Int? = null) : AppFailure()
class LoginFailure(var message: String = "Unable to login", errorCode: Int? = null) : AppFailure()
class ServerFailure(var message: String = "Unable to connect to the server side", errorCode: Int? = null) : AppFailure()
class NoInternetFailure(var message: String = "Device is not connected to the internet", errorCode: Int? = null) : AppFailure()
/**
* Feature based failures
*/
abstract class FeatureFailure : AppFailure() {
class AFailure(var message: String, var errorCode: Int? = null) : FeatureFailure()
class BFailure(var message: String, var errorCode: Int? = null) : FeatureFailure()
class AnotherFailure(var message: String, var errorCode: Int? = null) : FeatureFailure()
}
}
/**
* This sealed class represent the any kind of API result across the application
*/
sealed class APIResult {
object Loading : APIResult()
class Success(var data: Any) : APIResult()
class Error(var failure: AppFailure) : APIResult()
}
/**
* Handle all failures here. Place this code into your Base Activity class
*
* @param failure, [AppFailure]
*/
@SuppressLint("LogNotTimber")
fun handleFailure(failure: AppFailure) {
when (failure) {
is AppFailure.GeneralFailure -> {
Log.d("TAG", "handleFailure: ${failure.message}")
}
is AppFailure.LoginFailure -> {
Log.d("TAG", "handleFailure: ${failure.message}")
}
is AppFailure.UnauthorizedFailure -> {
Log.d("TAG", "handleFailure: ${failure.message}")
}
is AppFailure.NoInternetFailure -> {
Log.d("TAG", "handleFailure: ${failure.message}")
}
is AppFailure.ServerFailure -> {
Log.d("TAG", "handleFailure: ${failure.message}")
}
is AppFailure.FeatureFailure.AFailure -> {
Log.d("TAG", "handleFailure: ${failure.message}")
}
is AppFailure.FeatureFailure.BFailure -> {
Log.d("TAG", "handleFailure: ${failure.message}")
}
is AppFailure.FeatureFailure.AnotherFailure -> {
Log.d("TAG", "handleFailure: ${failure.message}")
}
else -> {
Log.d("TAG", "handleFailure: Something went wrong}")
}
}
}
/**
* Handle your API calling result here.
* Place this code inside your view model
*
* [Make sure to execute your API calls either using Kotlin's Coroutine or RxJava]
*/
fun handleAPIResult(result: APIResult) {
when (result) {
is APIResult.Loading -> {
// Show loading
}
is APIResult.Success -> {
// Hide Loading
// Write your logic here
}
is APIResult.Error -> {
// Hide loading
/**
* Hand over the [APIResult.Error] value which is [AppFailure] to the [handleFailure]
* function to handle the failure state
*/
handleFailure(result.failure)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment