Skip to content

Instantly share code, notes, and snippets.

@y2k
Created February 15, 2019 13:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y2k/0a985b8e82f828536a8df5b14bd52aaf to your computer and use it in GitHub Desktop.
Save y2k/0a985b8e82f828536a8df5b14bd52aaf to your computer and use it in GitHub Desktop.
lite moxy
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