Created
February 15, 2019 13:13
-
-
Save y2k/0a985b8e82f828536a8df5b14bd52aaf to your computer and use it in GitHub Desktop.
lite moxy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface LitePresenter<T : Any> { | |
fun attachView(view: LiteView<T>) | |
fun detachView() | |
} | |
open class BaseLitePresenter<T : Any> : LitePresenter<T> { | |
private val buffer = ArrayList<T>() | |
private var view: LiteView<T>? = null | |
private var firstAttached = false | |
fun dispatch(msg: T) { | |
if (msg !is SkipStrategyMsg) { | |
buffer.removeAll { msg::class == it::class } | |
buffer.add(msg) | |
} | |
view?.update(msg) | |
} | |
override fun attachView(view: LiteView<T>) { | |
this.view = view | |
if (!firstAttached) { | |
onFirstViewAttach() | |
firstAttached = true | |
} | |
buffer.forEach(view::update) | |
} | |
override fun detachView() { | |
this.view = null | |
} | |
open fun onFirstViewAttach() = Unit | |
} | |
interface SkipStrategyMsg | |
interface LiteView<T> { | |
fun update(msg: T) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment