Skip to content

Commit 7ece2ab

Browse files
author
Adrián García
committed
Add allTerminal, onAllTerminal and firstExceptionOrNull to lists of Resource
1 parent f760f44 commit 7ece2ab

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
- No new features, yet!
99

10+
## [1.3.2] - 2020-05-27
11+
### Added
12+
- Added `allTerminal`, `onAllTerminal` and `firstExceptionOrNull` functions to lists of `Resource`s
13+
1014
## [1.3.1] - 2020-04-22
1115
### Added
1216
- Upgrade Kotlin to 1.3.72 and Kodein to 6.5.5, apart from other Android dependencies.
@@ -86,7 +90,8 @@ state (`success` or `failure`)
8690
### Added
8791
- Initial architecture release.
8892

89-
[Unreleased]: https://github.com/bq/mini-kotlin/compare/1.3.1...HEAD
93+
[Unreleased]: https://github.com/bq/mini-kotlin/compare/1.3.2...HEAD
94+
[1.3.2]: https://github.com/bq/mini-kotlin/compare/1.3.1...1.3.2
9095
[1.3.1]: https://github.com/bq/mini-kotlin/compare/1.3.0...1.3.1
9196
[1.3.0]: https://github.com/bq/mini-kotlin/compare/1.2.0...1.3.0
9297
[1.2.0]: https://github.com/bq/mini-kotlin/compare/1.1.2...1.2.0

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Add the following dependencies to your app's `build.gradle`:
184184

185185
```groovy
186186
dependencies {
187-
def mini_version = "1.3.1"
187+
def mini_version = "1.3.2"
188188
// Minimum working dependencies
189189
implementation "com.github.bq.mini-kotlin:mini-android:$mini_version"
190190
kapt "com.github.bq.mini-kotlin:mini-processor:$mini_version"

mini-common/src/main/java/mini/Resource.kt

+15-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ inline fun <T, R> Resource<T>.map(crossinline transform: (data: T) -> R): Resour
139139
return Resource(value)
140140
}
141141

142+
/** All resources completed, whether they're in success or failure state. */
143+
fun <T> Iterable<Resource<T>>.allTerminal(): Boolean {
144+
return this.all { it.isTerminal }
145+
}
146+
142147
/** All resources succeeded. */
143148
fun <T> Iterable<Resource<T>>.allSuccesful(): Boolean {
144149
return this.all { it.isSuccess }
@@ -157,6 +162,11 @@ fun <T> Iterable<Resource<T>>.anyLoading(): Boolean {
157162
/** Any resource empty */
158163
fun <T> Iterable<Resource<T>>.anyEmpty(): Boolean = this.any { it.isEmpty }
159164

165+
fun <T> Iterable<Resource<T>>.onAllTerminal(fn: () -> Unit): Iterable<Resource<T>> {
166+
if (this.allTerminal()) fn()
167+
return this
168+
}
169+
160170
fun <T> Iterable<Resource<T>>.onAllSuccessful(fn: () -> Unit): Iterable<Resource<T>> {
161171
if (this.allSuccesful()) fn()
162172
return this
@@ -177,4 +187,8 @@ fun <T> Iterable<Resource<T>>.onAnyEmpty(fn: () -> Unit): Iterable<Resource<T>>
177187
return this
178188
}
179189

180-
fun Iterable<Task>.onAnyIdle(fn: () -> Unit): Iterable<Task> = onAnyEmpty(fn).map { it as Task }
190+
fun Iterable<Task>.onAnyIdle(fn: () -> Unit): Iterable<Task> = onAnyEmpty(fn).map { it as Task }
191+
192+
/** Returns the first exception that can be found in a list of resources, null if it can't find any */
193+
fun <T> Iterable<Resource<T>>.firstExceptionOrNull() : Throwable? =
194+
this.firstOrNull { it.isFailure && it.exceptionOrNull() != null }?.exceptionOrNull()

0 commit comments

Comments
 (0)