val decorView = this.window.decorView
ViewStubTaskManager.instance(decorView)
.addTask(ViewStubTaskContent(decorView))
.addTask(ViewStubTaskTitle(decorView))
.addTask(ViewStubTaskBottom(decorView))
.start()
class ViewStubTaskManager private constructor(val decorView: View) : Runnable {
private var iViewStubTask: IViewStubTask? = null
companion object {
const val TAG = “ViewStubTaskManager”
@JvmStatic
fun instance(decorView: View): ViewStubTaskManager {
return ViewStubTaskManager(decorView)
}
}
private val queue: MutableList = CopyOnWriteArrayList()
private val list: MutableList = CopyOnWriteArrayList()
fun setCallBack(iViewStubTask: IViewStubTask?): ViewStubTaskManager {
this.iViewStubTask = iViewStubTask
return this
}
fun addTask(viewStubTasks: List): ViewStubTaskManager {
queue.addAll(viewStubTasks)
list.addAll(viewStubTasks)
return this
}
fun addTask(viewStubTask: ViewStubTask): ViewStubTaskManager {
queue.add(viewStubTask)
list.add(viewStubTask)
return this
}
fun start() {
if (isEmpty()) {
return
}
iViewStubTask?.beforeTaskExecute()
// 指定 decorView 绘制下一帧的时候会回调里面的 runnable
ViewCompat.postOnAnimation(decorView, this)
}
fun stop() {
queue.clear()
list.clear()
decorView.removeCallbacks(null)
}
private fun isEmpty() = queue.isEmpty() || queue.size == 0
override fun run() {
if (!isEmpty()) {
// 当队列不为空的时候,先加载当前 viewStubTask
val viewStubTask = queue.removeAt(0)
viewStubTask.inflate()
iViewStubTask?.onTaskExecute(viewStubTask)
// 加载完成之后,再 postOn