-
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.
Migrate to new architecture and remove unused dependencies (#167)
- Loading branch information
Showing
35 changed files
with
455 additions
and
530 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
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
31 changes: 0 additions & 31 deletions
31
app/src/dev/java/com/xmartlabs/gong/device/logger/DebugNavigationLogger.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
69 changes: 67 additions & 2 deletions
69
app/src/main/java/com/xmartlabs/gong/device/common/ProcessState.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,80 @@ | ||
package com.xmartlabs.gong.device.common | ||
|
||
import kotlin.contracts.InvocationKind | ||
import kotlin.contracts.contract | ||
|
||
/** | ||
* A generic class that holds a value with its loading status. | ||
* @param <T> | ||
*/ | ||
sealed class ProcessState<out R> { | ||
data class Finish<out T>(val result: Result<T>) : ProcessState<T>() | ||
object Loading : ProcessState<Nothing>() | ||
data class Success<out T>(val data: T) : ProcessState<T>(), ProcessResult<T> | ||
data class Failure(val exception: Throwable) : ProcessState<Nothing>(), ProcessResult<Nothing> | ||
|
||
override fun toString(): String = when (this) { | ||
is Loading -> "Loading" | ||
is Finish<*> -> "Finish = Result[$result]" | ||
is Success<*> -> "Success[data=$data]" | ||
is Failure -> "Failure[exception=$exception]" | ||
} | ||
} | ||
|
||
sealed interface ProcessResult<out T> { | ||
companion object { | ||
inline fun <T> runCatching(block: () -> T): ProcessResult<T> = try { | ||
ProcessState.Success(block()) | ||
} catch (ex: Throwable) { | ||
ProcessState.Failure(ex) | ||
} | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T> ProcessState<T>.asResult(): ProcessResult<T>? = | ||
this as? ProcessResult<T> | ||
|
||
fun <T> ProcessResult<T>.getDataOrNull() = (this as? ProcessState.Success<T>)?.data | ||
|
||
fun <T> ProcessResult<T>.asProcessState(): ProcessState<T> = when (this) { | ||
is ProcessState.Failure -> this | ||
is ProcessState.Success -> this | ||
} | ||
|
||
fun <T> ProcessState<T>.getDataOrNull() = (this as? ProcessState.Success<T>)?.data | ||
|
||
inline fun <T> ProcessResult<T>.onFailure(action: (exception: Throwable) -> Unit): ProcessResult<T> { | ||
contract { | ||
callsInPlace(action, InvocationKind.AT_MOST_ONCE) | ||
} | ||
if (this is ProcessState.Failure) action(exception) | ||
return this | ||
} | ||
|
||
inline fun <T> ProcessResult<T>.onSuccess(action: (value: T) -> Unit): ProcessResult<T> { | ||
contract { | ||
callsInPlace(action, InvocationKind.AT_MOST_ONCE) | ||
} | ||
if (this is ProcessState.Success<T>) action(data) | ||
return this | ||
} | ||
|
||
fun ProcessState<*>.isLoading(): Boolean { | ||
contract { | ||
returns(true) implies (this@isLoading is ProcessState.Loading) | ||
} | ||
return this is ProcessState.Loading | ||
} | ||
|
||
fun <T> ProcessState<T>.isSuccess(): Boolean { | ||
contract { | ||
returns(true) implies (this@isSuccess is ProcessState.Success<T>) | ||
} | ||
return this is ProcessState.Success<T> | ||
} | ||
|
||
fun ProcessState<*>.isFailure(): Boolean { | ||
contract { | ||
returns(true) implies (this@isFailure is ProcessState.Failure) | ||
} | ||
return this is ProcessState.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
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
10 changes: 0 additions & 10 deletions
10
app/src/main/java/com/xmartlabs/gong/device/logger/NavigationLogger.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
19 changes: 8 additions & 11 deletions
19
app/src/main/java/com/xmartlabs/gong/domain/usecase/common/FlowCoroutineUseCase.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
Oops, something went wrong.