Package com.pspdfkit.utils
Class Response
-
- All Implemented Interfaces:
public class Response<T extends Object>
A generic class that holds a value with its loading status. Can be used to represent any operation that can be in one of the following states: Loading, Success, Error.
fun example() { // With data val responseWithData: Response<String> = Response.Success("Hello")
// Empty success val emptyResponse: Response<String> = Response.SuccessEmpty // Or using convenience constructor val anotherEmptyResponse: Response<String> = Response.success() // Handling all cases when (response) { is Response.Success -> println("Success with data: ${response.data}") is Response.SuccessEmpty -> println("Success with no data") is Response.Error -> println("Error: ${response.exception.message}") is Response.Loading -> println("Loading...") } // Using handlers response .onSuccess { data -> println("Success: $data") } .onSuccessEmpty { println("Success with no data") } .onError { error -> println("Error: ${error.message}") } .onLoading { println("Loading...") }
}
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final class
Response.Success
public class
Response.SuccessEmpty
public final class
Response.Error
public class
Response.Loading
-
Method Summary
Modifier and Type Method Description final Boolean
isSuccess()
final Boolean
isError()
final Boolean
isLoading()
final T
getOrNull()
final T
getOrDefault(T defaultValue)
final Response<T>
onSuccess(SuspendFunction1<T, Unit> action)
final Response<T>
onSuccessEmpty(SuspendFunction0<Unit> action)
final Response<T>
onError(Function1<Throwable, Unit> action)
final Response<T>
onLoading(Function0<Unit> action)
-
-
Method Detail
-
getOrDefault
final T getOrDefault(T defaultValue)
-
onSuccessEmpty
final Response<T> onSuccessEmpty(SuspendFunction0<Unit> action)
-
-
-
-