Last active
July 6, 2017 12:36
-
-
Save whalemare/53752af91c0aa9403fe37d6097f236e3 to your computer and use it in GitHub Desktop.
MVP implementation
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
/** | |
* @since 2017 | |
* @author Anton Vlasov - whalemare | |
*/ | |
interface BasePresenter<in V : BaseView, R> { | |
fun attachView(view: V) | |
fun detachView() | |
fun attachRouter(router: R?) | |
fun detachRouter() | |
} |
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
/** | |
* @since 2017 | |
* @author Anton Vlasov - whalemare | |
*/ | |
interface BaseView { | |
fun showError(text: String) | |
} |
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
/** | |
* @since 2017 | |
* @author Anton Vlasov - whalemare | |
*/ | |
abstract class MvpController< | |
V : BaseView, | |
P : BasePresenter<V, R>, | |
R : BaseRouter>(bundle: Bundle) | |
: BaseController(bundle), BaseView { | |
@field:javax.inject.Inject | |
protected lateinit var presenter: P | |
protected abstract fun getMvpRouter(): R? | |
protected abstract fun getMvpView(): V | |
override fun onAttach(view: android.view.View) { | |
super.onAttach(view) | |
presenter.attachView(getMvpView()) | |
presenter.attachRouter(getMvpRouter()) | |
} | |
override fun onDetach(view: android.view.View) { | |
super.onDetach(view) | |
presenter.detachView() | |
presenter.detachRouter() | |
} | |
override fun showError(text: String) { | |
activity?.message(text) | |
} | |
} |
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
/** | |
* @since 2017 | |
* @author Anton Vlasov - whalemare | |
*/ | |
abstract class MvpPresenter<V : BaseView, R : BaseRouter> : BasePresenter<V, R> { | |
private var viewRef: WeakReference<V>? = null | |
private var routerRef: WeakReference<R>? = null | |
abstract fun onViewAttached() | |
open fun onRouterAttached() {} | |
override fun attachView(view: V) { | |
viewRef = WeakReference(view) | |
onViewAttached() | |
} | |
override fun attachRouter(router: R?) { | |
if(router != null) { | |
routerRef = WeakReference(router) | |
onRouterAttached() | |
} | |
} | |
override fun detachRouter() { | |
routerRef?.clear() | |
routerRef = null | |
} | |
override fun detachView() { | |
viewRef?.clear() | |
viewRef = null | |
} | |
protected fun getView(): V? { | |
return viewRef?.get() | |
} | |
protected fun isViewAttached(): Boolean { | |
return viewRef?.get() != null | |
} | |
protected fun isActive(body: (v: V) -> Unit) { | |
if (isViewAttached()) { | |
tryOnUiThread({ | |
body.invoke(getView()!!) | |
}) | |
} | |
} | |
protected fun showError(error: Throwable) { | |
Timber.e(error, "error: \n") | |
isActive { it.showError(error.localizedMessage) } | |
} | |
fun tryOnUiThread(body: () -> Unit) { | |
if (Looper.myLooper() == Looper.getMainLooper()) { | |
body.invoke() | |
} else { | |
throw IllegalThreadStateException( | |
"You can`t access to view from not UI thread. Current thread = ${Thread.currentThread()}" | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment