Last active
September 28, 2022 13:06
-
-
Save y2k/e68cc22fb230bcbc32c0aef5e6c1a685 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
/* | |
* Репозиторий с примером | |
* https://github.com/y2k/LiteMoxy/tree/master/app/src/main/java/im/y2k/litemoxy | |
*/ | |
import android.support.v4.app.Fragment | |
abstract class MvpPresenter<T : Any> { | |
val view: MvpView<T> = BufferView() | |
fun attach(view: MvpView<T>) = (this.view as BufferView).attach(view) | |
fun detach() = (view as BufferView).detach() | |
private class BufferView<T : Any> : MvpView<T> { | |
private val buffer = ArrayList<T>() | |
private var view: MvpView<T>? = null | |
override fun update(event: T) { | |
buffer.removeAll { it::class == event::class } | |
buffer.add(event) | |
view?.update(event) | |
} | |
fun attach(view: MvpView<T>) { | |
buffer.forEach(view::update) | |
this.view = view | |
} | |
fun detach() { | |
view = null | |
} | |
} | |
} | |
interface MvpView<T> { | |
fun update(event: T) | |
} | |
abstract class MvpFragment<T : Any> : Fragment(), MvpView<T> { | |
abstract val presenter: MvpPresenter<T> | |
override fun onResume() { | |
super.onResume() | |
retainInstance = true | |
presenter.attach(this) | |
} | |
override fun onPause() { | |
super.onPause() | |
presenter.detach() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment