-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from eadm/presentation-1.1
presentation/base/1.1.1
- Loading branch information
Showing
8 changed files
with
101 additions
and
31 deletions.
There are no files selected for viewing
This file contains 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
27 changes: 27 additions & 0 deletions
27
...n-base/src/main/java/ru/nobird/android/presentation/base/DefaultPresenterViewContainer.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ru.nobird.android.presentation.base | ||
|
||
import androidx.annotation.CallSuper | ||
|
||
class DefaultPresenterViewContainer<V> : PresenterViewContainer<V> { | ||
@Volatile | ||
override var view: V? = null | ||
private set | ||
|
||
@CallSuper | ||
override fun attachView(view: V) { | ||
val previousView = this.view | ||
|
||
check(previousView == null) { "Previous view is not detached! previousView = $previousView" } | ||
|
||
this.view = view | ||
} | ||
|
||
@CallSuper | ||
override fun detachView(view: V) { | ||
val previousView = this.view | ||
|
||
check(previousView === view) { "Unexpected view! previousView = $previousView, getView to unbind = $view" } | ||
|
||
this.view = null | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
presentation-base/src/main/java/ru/nobird/android/presentation/base/DisposableViewModel.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ru.nobird.android.presentation.base | ||
|
||
import android.os.Bundle | ||
import androidx.annotation.CallSuper | ||
import androidx.lifecycle.ViewModel | ||
import io.reactivex.disposables.CompositeDisposable | ||
|
||
abstract class DisposableViewModel : ViewModel() { | ||
protected val compositeDisposable = CompositeDisposable() | ||
|
||
protected open val nestedDisposables: List<DisposableViewModel> = emptyList() | ||
|
||
@CallSuper | ||
override fun onCleared() { | ||
nestedDisposables.forEach { it.onCleared() } | ||
compositeDisposable.dispose() | ||
} | ||
|
||
@CallSuper | ||
open fun onSaveInstanceState(outState: Bundle) { | ||
nestedDisposables.forEachIndexed { index, nestedPresenter -> | ||
val bundle = Bundle() | ||
nestedPresenter.onSaveInstanceState(bundle) | ||
|
||
outState.putBundle("${nestedPresenter::class.java.canonicalName}:$index", bundle) | ||
} | ||
} | ||
|
||
@CallSuper | ||
open fun onRestoreInstanceState(savedInstanceState: Bundle) { | ||
nestedDisposables.forEachIndexed { index, nestedPresenter -> | ||
savedInstanceState | ||
.getBundle("${nestedPresenter::class.java.canonicalName}:$index") | ||
?.let(nestedPresenter::onRestoreInstanceState) | ||
} | ||
} | ||
} |
43 changes: 13 additions & 30 deletions
43
presentation-base/src/main/java/ru/nobird/android/presentation/base/PresenterBase.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,25 @@ | ||
package ru.nobird.android.presentation.base | ||
|
||
import android.os.Bundle | ||
import androidx.annotation.CallSuper | ||
import androidx.lifecycle.ViewModel | ||
import io.reactivex.disposables.CompositeDisposable | ||
import ru.nobird.android.presentation.base.delegate.PresenterDelegate | ||
|
||
abstract class PresenterBase<V> : ViewModel() { | ||
protected val compositeDisposable = CompositeDisposable() | ||
abstract class PresenterBase<V>( | ||
private val presenterViewContainer: PresenterViewContainer<V> = DefaultPresenterViewContainer() | ||
) : DisposableViewModel(), PresenterViewContainer<V> by presenterViewContainer { | ||
protected open val delegates: List<PresenterDelegate<in V>> = emptyList() | ||
|
||
@Volatile | ||
var view: V? = null | ||
private set | ||
override val nestedDisposables: List<DisposableViewModel> | ||
get() = delegates | ||
|
||
@CallSuper | ||
open fun attachView(view: V) { | ||
val previousView = this.view | ||
|
||
check(previousView == null) { "Previous view is not detached! previousView = $previousView" } | ||
|
||
this.view = view | ||
override fun attachView(view: V) { | ||
presenterViewContainer.attachView(view) | ||
delegates.forEach { it.attachView(view) } | ||
} | ||
|
||
@CallSuper | ||
open fun detachView(view: V) { | ||
val previousView = this.view | ||
|
||
if (previousView === view) { | ||
this.view = null | ||
} else { | ||
throw IllegalStateException("Unexpected view! previousView = $previousView, getView to unbind = $view") | ||
} | ||
override fun detachView(view: V) { | ||
delegates.forEach { it.detachView(view) } | ||
presenterViewContainer.detachView(view) | ||
} | ||
|
||
@CallSuper | ||
override fun onCleared() { | ||
compositeDisposable.dispose() | ||
} | ||
|
||
open fun onSaveInstanceState(outState: Bundle) {} | ||
open fun onRestoreInstanceState(savedInstanceState: Bundle) {} | ||
} |
6 changes: 6 additions & 0 deletions
6
presentation-base/src/main/java/ru/nobird/android/presentation/base/PresenterContract.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package ru.nobird.android.presentation.base | ||
|
||
interface PresenterContract<V> { | ||
fun attachView(view: V) | ||
fun detachView(view: V) | ||
} |
3 changes: 3 additions & 0 deletions
3
...entation-base/src/main/java/ru/nobird/android/presentation/base/PresenterViewContainer.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package ru.nobird.android.presentation.base | ||
|
||
interface PresenterViewContainer<V> : ViewContainer<V>, PresenterContract<V> |
5 changes: 5 additions & 0 deletions
5
presentation-base/src/main/java/ru/nobird/android/presentation/base/ViewContainer.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package ru.nobird.android.presentation.base | ||
|
||
interface ViewContainer<V> { | ||
val view: V? | ||
} |
9 changes: 9 additions & 0 deletions
9
...tion-base/src/main/java/ru/nobird/android/presentation/base/delegate/PresenterDelegate.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ru.nobird.android.presentation.base.delegate | ||
|
||
import ru.nobird.android.presentation.base.DisposableViewModel | ||
import ru.nobird.android.presentation.base.PresenterContract | ||
|
||
abstract class PresenterDelegate<V> : PresenterContract<V>, DisposableViewModel() { | ||
override fun attachView(view: V) {} | ||
override fun detachView(view: V) {} | ||
} |