-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Either class for livedata handling (#14)
* 🎉 Initial structure for the Either refactor * ♻️ Adds Either class for livedata handling
- Loading branch information
1 parent
38e3980
commit c232199
Showing
8 changed files
with
101 additions
and
96 deletions.
There are no files selected for viewing
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
45 changes: 45 additions & 0 deletions
45
presentation/src/main/kotlin/com/hernandazevedo/moviedb/view/base/Either.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,45 @@ | ||
package com.hernandazevedo.moviedb.view.base | ||
|
||
/** | ||
* Based on https://medium.com/@lupajz/you-either-love-it-or-you-havent-used-it-yet-a55f9b866dbe | ||
*/ | ||
sealed class Either<out E, out V> { | ||
data class Left<out E>(val error: E) : Either<E, Nothing>() | ||
data class Right<out V>(val value: V) : Either<Nothing, V>() | ||
|
||
fun <V> right(value: V): Either<Nothing, V> = Either.Right(value) | ||
fun <E> left(value: E): Either<E, Nothing> = Either.Left(value) | ||
|
||
fun <V> either(action: () -> V): Either<Exception, V> = | ||
try { right(action()) } catch (e: Exception) { left(e) } | ||
} | ||
|
||
inline infix fun <E, V, V2> Either<E, V> | ||
.map(f: (V) -> V2): Either<E, V2> = when (this) { | ||
is Either.Left -> this | ||
is Either.Right -> Either.Right(f(this.value)) | ||
} | ||
|
||
infix fun <E, V, V2> Either<E, (V) -> V2> | ||
.apply(f: Either<E, V>): Either<E, V2> = when (this) { | ||
is Either.Left -> this | ||
is Either.Right -> f.map(this.value) | ||
} | ||
|
||
inline infix fun <E, V, V2> Either<E, V> | ||
.flatMap(f: (V) -> Either<E, V2>): Either<E, V2> = when (this) { | ||
is Either.Left -> this | ||
is Either.Right -> f(value) | ||
} | ||
|
||
inline infix fun <E, E2, V> Either<E, V> | ||
.mapError(f: (E) -> E2): Either<E2, V> = when (this) { | ||
is Either.Left -> Either.Left(f.invoke(error)) | ||
is Either.Right -> this | ||
} | ||
|
||
inline fun <E, V, A> Either<E, V> | ||
.fold(e: (E) -> A, v: (V) -> A): A = when (this) { | ||
is Either.Left -> e(this.error) | ||
is Either.Right -> v(this.value) | ||
} |
44 changes: 0 additions & 44 deletions
44
presentation/src/main/kotlin/com/hernandazevedo/moviedb/view/base/Resource.kt
This file was deleted.
Oops, something went wrong.
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