Skip to content

Instantly share code, notes, and snippets.

View mig35's full-sized avatar

Mikhail Gurevich mig35

View GitHub Profile
internal class ChangeFavoriteStatusCommand(
private val mainItemUid: String,
private val changeToFavorite: Boolean,
private val changeFavoriteAction: suspend () -> Unit,
private val onFavoriteChangeFailed: (Throwable) -> Unit
) : ActionCommandWithStrategy<Unit, ListScreenState>(ConcurrentStrategyWithTag(mainItemUid)) {
override fun onCommandWasAdded(dataState: ListScreenState): ListScreenState =
// we update item state only if the list contains this item
if (dataState.pageData?.itemsList?.any { it.key == mainItemUid } == true) {
class SimpleListViewModel(application: Application) : ListViewModel(application) {
private val mutableScreenState = MutableLiveData(
ListScreenState(null, null, null)
)
override val screenState: LiveData<ListScreenState> = mutableScreenState
override val commandManager: CommandManager<ListScreenState> by lazy {
CommandManagerImpl(mutableScreenState, viewModelScope)
}
open class SingleWithTagStrategy(private val tag: Any) : SingleStrategy() {
override fun shouldAddToPendingActions(
pendingActionCommands: RemoveOnlyList<ActionCommand<*, *>>,
runningActionCommands: List<ActionCommand<*, *>>
): Boolean =
!pendingActionCommands.any { command ->
command.strategy.let { it is SingleWithTagStrategy && it.tag == tag }
}
&&
open class SingleStrategy : ExecutionStrategy {
override fun shouldAddToPendingActions(
pendingActionCommands: RemoveOnlyList<ActionCommand<*, *>>,
runningActionCommands: List<ActionCommand<*, *>>
): Boolean =
!pendingActionCommands.any { it.strategy is SingleStrategy }
&& !runningActionCommands.any { it.strategy is SingleStrategy }
override fun shouldBlockOtherTask(pendingActionCommand: ActionCommand<*, *>): Boolean =
open class ConcurrentStrategyWithTag(private val tag: Any) : ConcurrentStrategy() {
override fun shouldAddToPendingActions(
pendingActionCommands: RemoveOnlyList<ActionCommand<*, *>>,
runningActionCommands: List<ActionCommand<*, *>>
): Boolean {
pendingActionCommands.removeAll { command ->
command.strategy.let { it is ConcurrentStrategyWithTag && it.tag == tag }
}
return true
open class ConcurrentStrategy : ExecutionStrategy {
override fun shouldAddToPendingActions(
pendingActionCommands: RemoveOnlyList<ActionCommand<*, *>>,
runningActionCommands: List<ActionCommand<*, *>>
): Boolean =
true
override fun shouldBlockOtherTask(pendingActionCommand: ActionCommand<*, *>): Boolean =
false
abstract fun shouldAddToPendingActions(
dataState: DataState,
pendingActionCommands: RemoveOnlyList<ActionCommand<*, *>>,
runningActionCommands: List<ActionCommand<*, *>>
): Boolean
abstract fun shouldBlockOtherTask(pendingActionCommand: ActionCommand<*, *>): Boolean
abstract fun shouldExecuteAction(
open fun onCommandWasAdded(dataState: DataState): DataState = dataState
open fun onExecuteStarting(dataState: DataState): DataState = dataState
abstract suspend fun executeCommand(dataState: DataState): CommandResult
open fun onExecuteSuccess(dataState: DataState, result: CommandResult): DataState = dataState
open fun onExecuteFail(dataState: DataState, error: Throwable): DataState = dataState
open fun onExecuteFinished(dataState: DataState): DataState = dataState
bazel build //app/src/main:bazel
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
kotlin_version = "1.3.61"
kotlin_release_sha = "3901151ad5d94798a268d1771c6c0b7e305a608c2889fc98a674802500597b1c"
rules_kotlin_compiler_release = {
"urls": [
"https://github.com/JetBrains/kotlin/releases/download/v{v}/kotlin-compiler-{v}.zip".format(v = kotlin_version),
],
"sha256": kotlin_release_sha,
}