https://github.com/seanghay/result-of
A Kotlin utility sealed class for handling failure & success.
https://github.com/seanghay/result-of
android kotlin sealed
Last synced: 3 months ago
JSON representation
A Kotlin utility sealed class for handling failure & success.
- Host: GitHub
- URL: https://github.com/seanghay/result-of
- Owner: seanghay
- License: apache-2.0
- Created: 2020-08-27T03:27:36.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-04-23T09:28:07.000Z (over 3 years ago)
- Last Synced: 2023-03-03T22:04:56.388Z (over 2 years ago)
- Topics: android, kotlin, sealed
- Language: Kotlin
- Homepage:
- Size: 86.9 KB
- Stars: 14
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
## A utility sealed class for handling failure & success.
[](https://travis-ci.org/seanghay/result-of)
[](https://opensource.org/licenses/Apache-2.0)This is not the same as standard Kotlin `Result` because it is a **sealed class not a `value class` (previously called `inline class`)**.
### Installation
```groovy
dependencies {
implementation("com.seanghay:resultof:1.0.0")
}
```> ⚠️ The library is still in `jcenter()`, I plan to move it to `mavenCentral()` soon. However, if you removed `jcenter()` from your repositories, it will not be resolved. I suggest use jitpack for the time being.
#### JitPack
```groovy
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}dependencies {
implementation 'com.github.seanghay:result-of:1.0.0'
}
```### Basic Usage
```kotlin
val result = resultOf {
fetchUsers()
}result.onSuccess { users -> println(users) }
result.onFailure { throwable -> println(throwable) }
```### Usage with LiveData
```kotlin
class MyViewModel : ViewModel() {
val profile = liveData {
val result = resultOf { userRepository.getMyProfile() }
emit(result)
}
}
``````kotlin
class MyFragment: Fragment() {
private val viewModel: MyViewModel by viewModels()
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// observe user profile result
viewModel.profile.observe(viewLifecycleOwner) { result ->
// we only need `profileUrl` so we map it.
result.map { it.profileUrl }.onSuccess { url ->
Glide.with(this)
.load(url)
.into(binding.imageView)
}result.onFailure { throwable ->
Toast.makeText(
requireContext(),
throwable?.localizedMessage ?: "Oops!",
Toast.LENGTH_SHORT
).show()
}
}
}
}
```### Transformations
- `map` Map successful value into something else
- `flatMap` Map another `ResultOf` into anther `ResultOf`
- `failureMap` Map throwable of the Failure into another throwable. For example, map `IllegalStateException` to `MyException`
- `failureFlatMap` same as `flatMap` but for Failure### Retrieving Value & Throwable
- `result.valueOrNull`
- `result.valueOrThrow`
- `result.valueOrDefault { "my-default-value" }`
- `result.throwableOrNull`### Conversion
We can convert from standard Kotlin Result into ResultOf by calling `asResultOf()` on Result object.
```kotlin
val result = runCatching { 12345 }.asResultOf()
```### Nullable ResultOf to Non-null ResultOf Conversion
Most of the time, we don't want `null` value to be the successful value, so we want it to be a Failure instead.
We can do that by calling `failureOnNull()````kotlin
val nullableResult: ResultOf = resultOf { null }
val nonNullResult: ResultOf = nullableResult.failureOnNull()println(nullableResult.isFailure)
// falseprintln(nonNullResult.isFailure)
// true
```