Skip to content

Instantly share code, notes, and snippets.

@motorro
Created August 14, 2022 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motorro/6d2b86f2d59583167ebf91bc75c7d697 to your computer and use it in GitHub Desktop.
Save motorro/6d2b86f2d59583167ebf91bc75c7d697 to your computer and use it in GitHub Desktop.
Proxy machine state
abstract class ProxyMachineState<PG: Any, PU: Any, CG: Any, CU: Any> : CommonMachineState<PG, PU>() {
/**
* Proxy state machine
*/
private val machine = object : CommonStateMachine.Base<CG, CU>(::init) {
fun doStart() {
start()
}
override fun setUiState(uiState: CU) {
this@ProxyMachineState.setUiState(mapUiState(uiState))
}
}
override fun doStart() {
machine.doStart()
}
override fun doProcess(gesture: PG) {
mapGesture(gesture)?.let(machine::process)
}
override fun doClear() {
machine.clear()
}
/**
* Creates initial child state
*/
protected abstract fun init(): CommonMachineState<CG, CU>
/**
* Maps child UI state to parent if relevant
* @param parent Parent gesture
* @return Mapped gesture or null if not applicable
*/
protected abstract fun mapGesture(parent: PG): CG?
/**
* Maps child UI state to parent
* @param child Child UI state
*/
protected abstract fun mapUiState(child: CU): PU
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment