在开始之前就是这张图,我们先创建一个 sealed class 来罗列出用户的所有行为。通常以 intent 结尾。当用户启动应用最开始看到就是 InitialIntent 。这里理解可能有一些困难,这是用户并没有进行任何操作,我们可以从两个角度来理解,可以理解初始化,也可以理解为用户打开应用就是一个行为。因为这里没有任何的数据所以定义为 object 。应该是一个单例吧。当用户刷新界面时候,定义 RefreshIntent。然后我们通过勾选或取消勾选任务来表示任务是否已完成,在 TastIntent 为这两个动作创建对应的 Intent
sealed class TaskIntent{
}
sealed class TaskIntent{
object InitialIntent: TaskIntent()
}
sealed class TaskIntent{
object InitialIntent: TaskIntent()
object RefreshIntent: TaskIntent()
}
sealed class TaskIntent{
object InitialIntent: TaskIntent()
object RefreshIntent: TaskIntent()
data class ActivativeTaskIntent(val task:Task) : TaskIntent()
data class CompleteTaskIntent(val task):TaskIntent()
}
sealed class TaskIntent{
object InitialIntent: TaskIntent()
object RefreshIntent: TaskIntent()
data class ActivativeTaskIntent(val task:Task) : TaskIntent()
data class CompleteTaskIntent(val task:Task):TaskIntent()
object clearCompletedToTaskIntent:TaskIntent()
}
sealed class TaskIntent{
object InitialIntent: TaskIntent()
object RefreshIntent: TaskIntent()
data class ActivativeTaskIntent(val task:Task) : TaskIntent()
data class CompleteTaskIntent(val task:Task):TaskIntent()
object clearCompletedToTaskIntent:TaskIntent()
data class ChangeFilterIntent(val filterType:TaskFilterType):TaskIntent()
}
class TaskFragment{
fun intents() : Observable<TasksIntent>{
}
}
class TaskFragment{
fun intents() : Observable<TasksIntent>{
return initialIntent()
}
private fun initialIntent():Observable<InitialIntent>{
return Observable.just(InitiailIntent)
}
}
class TaskFragment{
fun intents() : Observable<TasksIntent>{
return Observable.merge(initialIntent(),refreshIntent())
}
private fun initialIntent():Observable<InitialIntent>{
return Observable.just(InitiailIntent)
}
private fun refreshIntent() :Observable<RefreshIntent>{
return RxSwipeRefreshLayout.refreshes(swipeRefreshLayout)
.map{ RefreshIntent }
}
}
fun actionFromIntent(Intent: TaskIntent):TaskAction = when(intent){
is InitialIntent ->
is RefreshIntent ->
...
}
fun actionFromIntent(Intent: TaskIntent):TaskAction = when(intent){
is InitialIntent -> LoadAndFilterTaskAction(TaskFilteType.ALL_TASK)
is RefreshIntent ->
...
}
fun actionFromIntent(Intent: TaskIntent):TaskAction = when(intent){
is InitialIntent -> LoadAndFilterTaskAction(TaskFilteType.ALL_TASK)
is RefreshIntent -> LoadTaksAction
...
}
sealed class TaskAction {
data class LoadAndFilterTasksAction(val filterType:TaskFilterType):TaskAction()
object LoadTaskAction:TaskAction()
...
}