Created
June 29, 2019 21:06
-
-
Save psteiger/6af771faed37f39baee52cd2ff835909 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| inline fun <T> Resource<List<T>>.filterResource(predicate: (T) -> Boolean): Resource<List<T>> = try { | |
| when (this) { | |
| is Resource.Success<List<T>> -> Resource.Success<List<T>>(data.filter(predicate)) | |
| is Resource.Loading<List<T>> -> Resource.Loading<List<T>>(partialData?.filter(predicate)) | |
| is Resource.Failure<List<T>> -> Resource.Failure<List<T>>(throwable) | |
| } | |
| } catch (e: Throwable) { | |
| Resource.Failure<List<T>>(e) | |
| } | |
| inline fun <T, U, V> Resource<V>.map(transform: (T) -> U): Resource<List<U>> where V : Iterable<T> = try { | |
| when (this) { | |
| is Resource.Success<V> -> Resource.Success<List<U>>(data.map(transform)) | |
| is Resource.Loading<V> -> Resource.Loading<List<U>>(partialData?.map(transform)) | |
| is Resource.Failure<V> -> Resource.Failure<List<U>>(throwable) | |
| } | |
| } catch (e: Throwable) { | |
| Resource.Failure<List<U>>(e) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment