-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ✨ handle error state handy (#23)
Create a new property on QueryViewModel error to describe an error on UI Create a new QueryViewState error to model the state To not duplicate code on data sources, errors are handled on ComicRepository Model error using Either<ComicError, List<T>> Empty results on ComicLocalDataSource produce EmptyResultsError Any network error produce NetworkError Suggestions will display error as a single suggestion A search will display error as text on the screen, hiding result list Error suggestions do not propagate search results closes #16
- Loading branch information
1 parent
e8d9ad8
commit e2f99ba
Showing
17 changed files
with
198 additions
and
40 deletions.
There are no files selected for viewing
47 changes: 41 additions & 6 deletions
47
app/src/main/java/es/ffgiraldez/comicsearch/comics/data/ComicRepository.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,55 @@ | ||
package es.ffgiraldez.comicsearch.comics.data | ||
|
||
import arrow.core.Either | ||
import arrow.core.None | ||
import arrow.core.Option | ||
import arrow.core.Some | ||
import es.ffgiraldez.comicsearch.comics.domain.ComicError | ||
import es.ffgiraldez.comicsearch.comics.domain.ComicError.EmptyResultsError | ||
import es.ffgiraldez.comicsearch.comics.domain.ComicError.NetworkError | ||
import es.ffgiraldez.comicsearch.comics.domain.Query | ||
import es.ffgiraldez.comicsearch.platform.left | ||
import es.ffgiraldez.comicsearch.platform.right | ||
import io.reactivex.Flowable | ||
|
||
abstract class ComicRepository<T>( | ||
private val local: ComicLocalDataSource<T>, | ||
private val remote: ComicRemoteDataSource<T> | ||
) { | ||
fun findByTerm(term: String): Flowable<List<T>> = | ||
fun findByTerm(term: String): Flowable<Either<ComicError, List<T>>> = | ||
local.findQueryByTerm(term) | ||
.flatMap { | ||
when (it) { | ||
is None -> remote.findByTerm(term) | ||
.flatMapPublisher { local.insert(term, it).toFlowable<List<T>>() } | ||
is Some -> local.findByQuery(it.t) | ||
.flatMap { findSuggestions(it, term) } | ||
|
||
private fun findSuggestions( | ||
query: Option<Query>, | ||
term: String | ||
): Flowable<out Either<ComicError, List<T>>> = when (query) { | ||
is None -> searchSuggestions(term) | ||
is Some -> fetchSuggestions(query) | ||
} | ||
|
||
private fun searchSuggestions(term: String): Flowable<Either<ComicError, List<T>>> = | ||
remote.findByTerm(term) | ||
.map { right<ComicError, List<T>>(it) } | ||
.onErrorReturn { left<ComicError, List<T>>(NetworkError) } | ||
.flatMapPublisher { saveSuggestions(it, term) } | ||
|
||
private fun saveSuggestions( | ||
results: Either<ComicError, List<T>>, | ||
term: String | ||
): Flowable<Either<ComicError, List<T>>> = | ||
results.fold({ _ -> | ||
Flowable.just(results) | ||
}, { | ||
local.insert(term, it).toFlowable<Either<ComicError, List<T>>>() | ||
}) | ||
|
||
private fun fetchSuggestions(it: Some<Query>): Flowable<Either<EmptyResultsError, List<T>>> = | ||
local.findByQuery(it.t) | ||
.map { | ||
when (it.isEmpty()) { | ||
true -> Either.left(EmptyResultsError) | ||
false -> Either.right(it) | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
16 changes: 16 additions & 0 deletions
16
app/src/main/java/es/ffgiraldez/comicsearch/platform/Utilities.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package es.ffgiraldez.comicsearch.platform | ||
|
||
import arrow.core.Either | ||
import arrow.core.left | ||
import arrow.core.right | ||
|
||
fun <A, B, C> safe(first: A?, second: B?, block: (A, B) -> C): C? { | ||
return if (first != null && second != null) { | ||
block(first, second) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
fun <A, B> left(a: A): Either<A, B> = a.left() | ||
fun <A, B> right(b: B): Either<A, B> = b.right() |
This file contains 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
4 changes: 4 additions & 0 deletions
4
app/src/main/java/es/ffgiraldez/comicsearch/query/base/presentation/QueryViewState.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,19 @@ | ||
package es.ffgiraldez.comicsearch.query.base.presentation | ||
|
||
import es.ffgiraldez.comicsearch.comics.domain.ComicError | ||
|
||
sealed class QueryViewState<out T> { | ||
|
||
companion object { | ||
fun <T> result(volumeList: List<T>): QueryViewState<T> = Result(volumeList) | ||
fun <T> idle(): QueryViewState<T> = Idle | ||
fun <T> loading(): QueryViewState<T> = Loading | ||
fun <T> error(error: ComicError): QueryViewState<T> = Error(error) | ||
} | ||
|
||
object Idle : QueryViewState<Nothing>() | ||
object Loading : QueryViewState<Nothing>() | ||
data class Result<out T>(val results: List<T>) : QueryViewState<T>() | ||
data class Error(val error: ComicError) : QueryViewState<Nothing>() | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
app/src/main/java/es/ffgiraldez/comicsearch/query/base/ui/ComicErrorBindingAdapter.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package es.ffgiraldez.comicsearch.query.base.ui | ||
|
||
import es.ffgiraldez.comicsearch.comics.domain.ComicError | ||
|
||
fun ComicError.toHumanResponse(): String = when (this) { | ||
ComicError.NetworkError -> "no internet connection" | ||
ComicError.EmptyResultsError -> "search without suggestion" | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.