-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
98 changed files
with
3,259 additions
and
1,734 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="fr.geonature.commons" | ||
android:sharedUserId="@string/sharedUserId"> | ||
<manifest | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="fr.geonature.commons" | ||
android:sharedUserId="@string/sharedUserId"> | ||
|
||
<uses-permission android:name="fr.geonature.sync.permission.READ"/> | ||
<permission android:name="fr.geonature.sync.permission.READ"/> | ||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | ||
<uses-permission android:name="fr.geonature.sync.permission.READ" /> | ||
<uses-permission android:name="fr.geonature.sync.permission.WRITE" /> | ||
|
||
<permission | ||
android:name="fr.geonature.sync.permission.READ" | ||
android:protectionLevel="signature" /> | ||
<permission | ||
android:name="fr.geonature.sync.permission.WRITE" | ||
android:protectionLevel="signature" /> | ||
|
||
</manifest> |
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
120 changes: 120 additions & 0 deletions
120
commons/src/main/java/fr/geonature/commons/fp/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,120 @@ | ||
package fr.geonature.commons.fp | ||
|
||
/** | ||
* Represents a value of one of two possible types (a disjoint union). | ||
* Instances of [Either] are either an instance of [Left] or [Right]. | ||
* FP Convention dictates that [Left] is used for "failure" | ||
* and [Right] is used for "success". | ||
* | ||
* @see Left | ||
* @see Right | ||
*/ | ||
sealed class Either<out L, out R> { | ||
|
||
/** | ||
* Represents the left side of [Either] class which by convention is a "Failure". | ||
*/ | ||
data class Left<out L>(val a: L) : Either<L, Nothing>() | ||
|
||
/** | ||
* Represents the right side of [Either] class which by convention is a "Success". | ||
*/ | ||
data class Right<out R>(val b: R) : Either<Nothing, R>() | ||
|
||
/** | ||
* Returns true if this is a [Right], false otherwise. | ||
* @see Right | ||
*/ | ||
val isRight get() = this is Right<R> | ||
|
||
/** | ||
* Returns true if this is a [Left], false otherwise. | ||
* @see Left | ||
*/ | ||
val isLeft get() = this is Left<L> | ||
|
||
/** | ||
* Creates a [Left] type. | ||
* @see Left | ||
*/ | ||
fun <L> left(a: L) = | ||
Left(a) | ||
|
||
/** | ||
* Creates a [Right] type. | ||
* @see Right | ||
*/ | ||
fun <R> right(b: R) = | ||
Right(b) | ||
|
||
/** | ||
* Applies fnL if this is a [Left] or fnR if this is a [Right]. | ||
* @see Left | ||
* @see Right | ||
*/ | ||
fun fold( | ||
fnL: (L) -> Any, | ||
fnR: (R) -> Any | ||
): Any = | ||
when (this) { | ||
is Left -> fnL(a) | ||
is Right -> fnR(b) | ||
} | ||
} | ||
|
||
/** | ||
* Composes 2 functions. | ||
* | ||
* See [credits to Alex Hart.](https://proandroiddev.com/kotlins-nothing-type-946de7d464fb) | ||
*/ | ||
fun <A, B, C> ((A) -> B).c(f: (B) -> C): (A) -> C = | ||
{ | ||
f(this(it)) | ||
} | ||
|
||
/** | ||
* Right-biased flatMap() FP convention which means that [Either.Right] is assumed to be the default | ||
* case to operate on. | ||
* If it is [Either.Left], operations like map, flatMap, ... return the [Either.Left] value unchanged. | ||
*/ | ||
fun <T, L, R> Either<L, R>.flatMap(fn: (R) -> Either<L, T>): Either<L, T> = | ||
when (this) { | ||
is Either.Left -> Either.Left(a) | ||
is Either.Right -> fn(b) | ||
} | ||
|
||
/** | ||
* Right-biased map() FP convention which means that [Either.Right] is assumed to be the default case | ||
* to operate on. | ||
* If it is [Either.Left], operations like map, flatMap, ... return the [Either.Left] value unchanged. | ||
*/ | ||
fun <T, L, R> Either<L, R>.map(fn: (R) -> (T)): Either<L, T> = | ||
this.flatMap(fn.c(::right)) | ||
|
||
/** | ||
* Returns the value from this [Either.Right] or the given argument if this is a [Either.Left]. | ||
* Examples: | ||
* * `Right(12).getOrElse(17)` returns 12 | ||
* * `Left(12).getOrElse(17)` returns 17 | ||
*/ | ||
fun <L, R> Either<L, R>.getOrElse(value: R): R = | ||
when (this) { | ||
is Either.Left -> value | ||
is Either.Right -> b | ||
} | ||
|
||
/** | ||
* Left-biased onFailure() FP convention dictates that when this class is [Either.Left], it'll perform | ||
* the onFailure functionality passed as a parameter, but, overall will still return an either | ||
* object so you chain calls. | ||
*/ | ||
fun <L, R> Either<L, R>.onFailure(fn: (failure: L) -> Unit): Either<L, R> = | ||
this.apply { if (this is Either.Left) fn(a) } | ||
|
||
/** | ||
* Right-biased onSuccess() FP convention dictates that when this class is [Either.Right], it'll perform | ||
* the onSuccess functionality passed as a parameter, but, overall will still return an either | ||
* object so you chain calls. | ||
*/ | ||
fun <L, R> Either<L, R>.onSuccess(fn: (success: R) -> Unit): Either<L, R> = | ||
this.apply { if (this is Either.Right) fn(b) } |
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,12 @@ | ||
package fr.geonature.commons.fp | ||
|
||
/** | ||
* Base class for handling errors/failures/exceptions. | ||
* Every feature specific failure should extend [FeatureFailure] class. | ||
*/ | ||
sealed class Failure { | ||
object NetworkFailure : Failure() | ||
object ServerFailure : Failure() | ||
|
||
abstract class FeatureFailure : Failure() | ||
} |
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.