Skip to content

Instantly share code, notes, and snippets.

@psteiger
Created June 29, 2019 21:06
Show Gist options
  • Select an option

  • Save psteiger/6af771faed37f39baee52cd2ff835909 to your computer and use it in GitHub Desktop.

Select an option

Save psteiger/6af771faed37f39baee52cd2ff835909 to your computer and use it in GitHub Desktop.
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