Skip to content

Commit

Permalink
Implemented multi presenter
Browse files Browse the repository at this point in the history
  • Loading branch information
apikyanrobert committed Feb 12, 2021
1 parent 089358f commit d314f6a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
2 changes: 1 addition & 1 deletion lifecyclemvp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
api 'com.github.RobertApikyan:AbstractMvp:1.0.6'
api 'com.github.RobertApikyan:AbstractMvp:1.0.9-experimental'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@ class LifecycleMvp {
* 'this' is activity or fragment with implement's Login interface
*/
fun <V : LifecycleView, P : LifecyclePresenter<V>> from(view: V,
presenterKey: String,
presenterFactory: () -> P) =
from(view, Presenter.Factory.fromLambda(presenterFactory))
from(view, presenterKey, Presenter.Factory.fromLambda(presenterFactory))

inline fun <V : LifecycleView, reified P : LifecyclePresenter<V>> from(view: V,
noinline presenterFactory: () -> P) =
from(view, P::class.java.canonicalName
?: P::class.java.name, Presenter.Factory.fromLambda(presenterFactory))

fun <V : LifecycleView, P : LifecyclePresenter<V>> from(view: V,
presenterKey: String,
presenterFactory: Presenter.Factory<V, P>) =
from(LifecycleMvpFactory(view, presenterFactory))
from(LifecycleMvpFactory(view, presenterKey, presenterFactory))

inline fun <V : LifecycleView, reified P : LifecyclePresenter<V>> from(view: V,
presenterFactory: Presenter.Factory<V, P>) =
from(LifecycleMvpFactory(view, P::class.java.canonicalName
?: P::class.java.name, presenterFactory))

/**
* Use this method and provide custom LifecycleMvpFactory class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import robertapikyan.com.abstractmvp.presentation.presenter.Presenter
*/
open class LifecycleMvpFactory<V : LifecycleView, P : LifecyclePresenter<V>>(
private val view: V,
private val presenterKey:String,
private val presenterFactory: Presenter.Factory<V, P>
) : Mvp.Factory<V, P> {

Expand Down Expand Up @@ -46,6 +47,10 @@ open class LifecycleMvpFactory<V : LifecycleView, P : LifecyclePresenter<V>>(
return presenterFactory.createPresenter()
}

override fun getPresenterKey(): Any {
return presenterKey
}

/**
* Method returns IPresenterHolder implementation with ViewModel from android arc. components
* @return ViewModelPresenterHolder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,24 @@ import robertapikyan.com.abstractmvp.presentation.presenter.IPresenterHolder
*/
class ViewModelPresenterHolder<V : LifecycleView, P : LifecyclePresenter<V>> : ViewModel(), IPresenterHolder<V, P> {

private lateinit var presenter: P
private val presenters = LinkedHashMap<Any,P>()

override fun put(presenter: P) {
this.presenter = presenter
override fun put(presenterKey: Any, presenter: P) {
this.presenters[presenterKey] = presenter
}

override fun get(): P {
assertPresenterNotNull()
return presenter
override fun get(presenterKey: Any): P {
return presenters[presenterKey]
?: throw IllegalStateException("ViewModelPresenterHolder.get($presenterKey) method is called when, " +
"ViewModelPresenterHolder.hasPresenter($presenterKey) = false")
}

override fun hasPresenter() = ::presenter.isInitialized
override fun hasPresenter(presenterKey: Any) = presenters.containsKey(presenterKey)

override fun onCleared() {
if (this::presenter.isInitialized){
for (presenter in presenters.values) {
presenter.onDestroy()
}
super.onCleared()
}

private fun assertPresenterNotNull() {
// actually this will never happen
if (!hasPresenter())
throw IllegalStateException("ViewModelPresenterHolder.get() method is called when, " +
"ViewModelPresenterHolder.hasPresenter() = false")
}
}

0 comments on commit d314f6a

Please sign in to comment.