Skip to content

Commit

Permalink
Add allTerminal, onAllTerminal and firstExceptionOrNull to lists of R…
Browse files Browse the repository at this point in the history
…esource
  • Loading branch information
Adrián García committed May 27, 2020
1 parent f760f44 commit 7ece2ab
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- No new features, yet!

## [1.3.2] - 2020-05-27
### Added
- Added `allTerminal`, `onAllTerminal` and `firstExceptionOrNull` functions to lists of `Resource`s

## [1.3.1] - 2020-04-22
### Added
- Upgrade Kotlin to 1.3.72 and Kodein to 6.5.5, apart from other Android dependencies.
Expand Down Expand Up @@ -86,7 +90,8 @@ state (`success` or `failure`)
### Added
- Initial architecture release.

[Unreleased]: https://github.com/bq/mini-kotlin/compare/1.3.1...HEAD
[Unreleased]: https://github.com/bq/mini-kotlin/compare/1.3.2...HEAD
[1.3.2]: https://github.com/bq/mini-kotlin/compare/1.3.1...1.3.2
[1.3.1]: https://github.com/bq/mini-kotlin/compare/1.3.0...1.3.1
[1.3.0]: https://github.com/bq/mini-kotlin/compare/1.2.0...1.3.0
[1.2.0]: https://github.com/bq/mini-kotlin/compare/1.1.2...1.2.0
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ Add the following dependencies to your app's `build.gradle`:

```groovy
dependencies {
def mini_version = "1.3.1"
def mini_version = "1.3.2"
// Minimum working dependencies
implementation "com.github.bq.mini-kotlin:mini-android:$mini_version"
kapt "com.github.bq.mini-kotlin:mini-processor:$mini_version"
Expand Down
16 changes: 15 additions & 1 deletion mini-common/src/main/java/mini/Resource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ inline fun <T, R> Resource<T>.map(crossinline transform: (data: T) -> R): Resour
return Resource(value)
}

/** All resources completed, whether they're in success or failure state. */
fun <T> Iterable<Resource<T>>.allTerminal(): Boolean {
return this.all { it.isTerminal }
}

/** All resources succeeded. */
fun <T> Iterable<Resource<T>>.allSuccesful(): Boolean {
return this.all { it.isSuccess }
Expand All @@ -157,6 +162,11 @@ fun <T> Iterable<Resource<T>>.anyLoading(): Boolean {
/** Any resource empty */
fun <T> Iterable<Resource<T>>.anyEmpty(): Boolean = this.any { it.isEmpty }

fun <T> Iterable<Resource<T>>.onAllTerminal(fn: () -> Unit): Iterable<Resource<T>> {
if (this.allTerminal()) fn()
return this
}

fun <T> Iterable<Resource<T>>.onAllSuccessful(fn: () -> Unit): Iterable<Resource<T>> {
if (this.allSuccesful()) fn()
return this
Expand All @@ -177,4 +187,8 @@ fun <T> Iterable<Resource<T>>.onAnyEmpty(fn: () -> Unit): Iterable<Resource<T>>
return this
}

fun Iterable<Task>.onAnyIdle(fn: () -> Unit): Iterable<Task> = onAnyEmpty(fn).map { it as Task }
fun Iterable<Task>.onAnyIdle(fn: () -> Unit): Iterable<Task> = onAnyEmpty(fn).map { it as Task }

/** Returns the first exception that can be found in a list of resources, null if it can't find any */
fun <T> Iterable<Resource<T>>.firstExceptionOrNull() : Throwable? =
this.firstOrNull { it.isFailure && it.exceptionOrNull() != null }?.exceptionOrNull()

0 comments on commit 7ece2ab

Please sign in to comment.