To complete a main operation, it is necessary to complete several sub operations. I can say that each operation has its own tasks such as starting, processing and stopping.
The structure for a single operation as below:
sealed class DenemeState {
object Operation1Start: DenemeState()
object Operation1WaitingToStart: DenemeState()
object GetDataFromOperation1: DenemeState()
object Operation1Stop: DenemeState()
object Operation1WaitingToStop: DenemeState()
}
Functions that start and stop operations take a callback function and forward the status in the future.
fun startOperation1(cb: ((status:String) -> Unit)) {
}
fun stopOperation1(cb: ((status:String) -> Unit)) {
}
My main target is, before starting the operation, setting the state from Operation1Start to Operation1WaitingToStart, then start the operation, then reading the operations from the callback in Operation1WaitingToStart inside the when.
Unfortunately, the code below did not work, do you think such a thing is possible or how can I achieve it in a similar way?
sealed class DenemeState {
var Operation1StartListener : ((status: String) -> Unit)? = null
object Operation1Start: DenemeState()
object Operation1WaitingToStart: DenemeState()
object GetDataFromOperation1: DenemeState()
object Operation1Stop: DenemeState()
object Operation1WaitingToStop: DenemeState()
}
fun startOperation1(cb: ((status:String) -> Unit)) {
}
fun stopOperation1(cb: ((status:String) -> Unit)) {
}
var denemeState: DenemeState = DenemeState.Operation1Start
fun run1() {
when (val value = denemeState) {
DenemeState.Operation1Start -> {
denemeState = DenemeState.Operation1WaitingToStart
startOperation1(denemeState.Operation1StartListener!!)
}
DenemeState.Operation1WaitingToStart -> {
denemeState.Operation1StartListener {
}
}
DenemeState.GetDataFromOperation1 -> {
}
DenemeState.Operation1Stop -> {
}
DenemeState.Operation1WaitingToStop -> {
}
}
}
Error
Reference has a nullable type '((String) -> Unit)?', use explicit '?.invoke()' to make a function-like call instead
Type mismatch: inferred type is () -> Unit but String was expected