Skip to content

Commit

Permalink
Merge pull request #11 from mvarnagiris/behavior
Browse files Browse the repository at this point in the history
Behavior
  • Loading branch information
mvarnagiris authored Mar 9, 2018
2 parents f125317 + cac7211 commit 9b4b090
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/com/mvcoding/mvp/Behavior.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.mvcoding.mvp

abstract class Behavior<in VIEW : Presenter.View> : Presenter<VIEW>()
9 changes: 8 additions & 1 deletion src/main/kotlin/com/mvcoding/mvp/Presenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ import io.reactivex.Single
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable

abstract class Presenter<in VIEW : Presenter.View> {
abstract class Presenter<in VIEW : Presenter.View>(private vararg val behaviors: Behavior<VIEW>) {
private lateinit var compositeDisposable: CompositeDisposable
private var view: View? = null

infix fun attach(view: VIEW) {
ensureViewIsNotAttached(view)
this.view = view
this.compositeDisposable = CompositeDisposable()
behaviors.forEach { it attach view }
onViewAttached(view)
}

infix fun detach(view: VIEW) {
ensureGivenViewIsAttached(view)
this.view = null
this.compositeDisposable.dispose()
behaviors.forEach { it detach view }
onViewDetached(view)
}

Expand All @@ -47,17 +49,22 @@ abstract class Presenter<in VIEW : Presenter.View> {

protected fun <T> Observable<T>.subscribeUntilDetached(onNext: (T) -> Unit, onError: (Throwable) -> Unit): Disposable =
subscribe(onNext, onError).apply { disposeOnDetach(this) }

protected fun <T> Flowable<T>.subscribeUntilDetached(onNext: (T) -> Unit, onError: (Throwable) -> Unit): Disposable =
subscribe(onNext, onError).apply { disposeOnDetach(this) }

protected fun <T> Single<T>.subscribeUntilDetached(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit): Disposable =
subscribe(onSuccess, onError).apply { disposeOnDetach(this) }

protected fun <T> Maybe<T>.subscribeUntilDetached(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit): Disposable =
subscribe(onSuccess, onError).apply { disposeOnDetach(this) }

protected fun <T> Observable<T>.subscribeUntilDetached(onNext: (T) -> Unit, onError: (Throwable) -> Unit, onComplete: () -> Unit): Disposable =
subscribe(onNext, onError, onComplete).apply { disposeOnDetach(this) }

protected fun <T> Flowable<T>.subscribeUntilDetached(onNext: (T) -> Unit, onError: (Throwable) -> Unit, onComplete: () -> Unit): Disposable =
subscribe(onNext, onError, onComplete).apply { disposeOnDetach(this) }

protected fun <T> Maybe<T>.subscribeUntilDetached(onSuccess: (T) -> Unit, onError: (Throwable) -> Unit, onComplete: () -> Unit): Disposable =
subscribe(onSuccess, onError, onComplete).apply { disposeOnDetach(this) }

Expand Down
33 changes: 32 additions & 1 deletion src/test/kotlin/com/mvcoding/mvp/PresenterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import com.memoizr.assertk.expect
import com.memoizr.assertk.isInstance
import com.memoizr.assertk.of
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import io.reactivex.*
import org.junit.Before
import org.junit.Test
import javax.xml.stream.FactoryConfigurationError
import kotlin.test.assertFalse
import kotlin.test.assertTrue

Expand Down Expand Up @@ -190,6 +192,21 @@ class PresenterTest {
assertTrue { maybeOnNextOnErrorOnCompleteIsDisposed }
}

@Test
fun `when presenter is attached-detached it attaches-detaches all behaviors`() {
val behavior1 = BehaviorForTest()
val behavior2 = BehaviorForTest()
val presenter = PresenterForTest(behavior1, behavior2)

presenter attach viewForTest
behavior1.isAttached = true
behavior2.isAttached = true

presenter detach viewForTest
behavior1.isAttached = false
behavior2.isAttached = false
}

interface ViewForTest : Presenter.View {
fun observable(): Observable<Unit>
fun observableOnNext(): Observable<Unit>
Expand All @@ -208,7 +225,7 @@ class PresenterTest {
fun maybeOnSuccessOnErrorOnComplete(): Maybe<Unit>
}

class PresenterForTest : Presenter<ViewForTest>() {
class PresenterForTest(vararg behavior: Behavior<ViewForTest>) : Presenter<ViewForTest>(*behavior) {
override fun onViewAttached(view: ViewForTest) {
super.onViewAttached(view)
view.observable().subscribeUntilDetached()
Expand All @@ -228,4 +245,18 @@ class PresenterTest {
view.maybeOnSuccessOnErrorOnComplete().subscribeUntilDetached()
}
}

class BehaviorForTest : Behavior<ViewForTest>() {
var isAttached = false

override fun onViewAttached(view: ViewForTest) {
super.onViewAttached(view)
isAttached = true
}

override fun onViewDetached(view: ViewForTest) {
super.onViewDetached(view)
isAttached = false
}
}
}

0 comments on commit 9b4b090

Please sign in to comment.