Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dmdevgo committed Aug 22, 2019
1 parent fac47a9 commit fd4da6e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 28 deletions.
10 changes: 3 additions & 7 deletions rxpm/src/main/kotlin/me/dmdev/rxpm/PresentationModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class PresentationModel {
/**
* The [lifecycle][Lifecycle] of this presentation model.
*/
val lifecycleObservable = lifecycle.distinctUntilChanged()
val lifecycleObservable: Observable<Lifecycle> = lifecycle.distinctUntilChanged()
internal val lifecycleConsumer = lifecycle.asConsumer()

/**
Expand Down Expand Up @@ -116,13 +116,9 @@ abstract class PresentationModel {
*/
fun attachToParent(parent: PresentationModel) {

if (parent == this) {
throw IllegalArgumentException("Presentation model can't be attached to itself.")
}
require(parent != this) { "Presentation model can't be attached to itself." }

if (lifecycle.hasValue()) {
throw IllegalStateException("Presentation model can't be a child more than once. It must not be reused.")
}
check(!lifecycle.hasValue()) { "Presentation model can't be a child more than once. It must not be reused." }

when (parent.lifecycle.value) {

Expand Down
2 changes: 1 addition & 1 deletion rxpm/src/main/kotlin/me/dmdev/rxpm/State.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ infix fun <T> State<T>.bindTo(consumer: (T) -> Unit) {
interface DiffStrategy<T> {

/**
* Сompares the old and the new values.
* Compares the old and the new values.
* @return [true] if both values ​​are identical or [false] if they are different.
*/
fun areTheSame(new: T, old: T): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ class PmActivityDelegate<PM, A>(
commonDelegate.onUnbind()

when (retainMode) {
RetainMode.IS_FINISHING -> {
IS_FINISHING -> {
if (pmActivity.isFinishing) {
commonDelegate.onDestroy()
}
}

RetainMode.CONFIGURATION_CHANGES -> {
CONFIGURATION_CHANGES -> {
if (!pmActivity.isChangingConfigurations) {
commonDelegate.onDestroy()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ class PmFragmentDelegate<PM, F>(
*/
fun onDestroy() {
when (retainMode) {
RetainMode.SAVED_STATE -> {
SAVED_STATE -> {
if (pmFragment.activity?.isFinishing == true
|| (pmFragment.fragmentManager?.isStateSaved?.not() == true)
) {
commonDelegate.onDestroy()
}
}

RetainMode.CONFIGURATION_CHANGES -> {
CONFIGURATION_CHANGES -> {
if (pmFragment.activity?.isChangingConfigurations?.not() == true) {
commonDelegate.onDestroy()
}
Expand Down
14 changes: 4 additions & 10 deletions rxpm/src/main/kotlin/me/dmdev/rxpm/test/PmTestHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ class PmTestHelper(val pm: PresentationModel) {

enum class LifecycleSteps { ALL, BYPASS_BINDING, BYPASS_RESUMING }

private val lifecycleStates = PresentationModel.Lifecycle.values()

/**
* Sets the lifecycle of the [presentation model][pm] under test to the specified [state][lifecycleState].
* This will also create natural sequence of states before the requested one.
Expand Down Expand Up @@ -79,14 +77,10 @@ class PmTestHelper(val pm: PresentationModel) {

private fun checkStateAllowed(lifecycleState: PresentationModel.Lifecycle) {
pm.currentLifecycleState?.let { currentState ->
if (lifecycleState <= currentState
&& !isBindedAgain(lifecycleState)
&& !isResumedAgain((lifecycleState))
) {
throw IllegalStateException(
"You can't set lifecycle state as $lifecycleState when it already is $pm.currentLifecycleState."
)
}
check(!(lifecycleState <= currentState
&& !isBindedAgain(lifecycleState)
&& !isResumedAgain((lifecycleState)))
) { "You can't set lifecycle state as $lifecycleState when it already is $pm.currentLifecycleState." }
}
}

Expand Down
11 changes: 5 additions & 6 deletions rxpm/src/test/kotlin/me/dmdev/rxpm/util/SchedulersRule.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package me.dmdev.rxpm.util

import io.reactivex.android.plugins.RxAndroidPlugins
import io.reactivex.plugins.RxJavaPlugins
import io.reactivex.schedulers.Schedulers
import io.reactivex.schedulers.TestScheduler
import org.junit.rules.ExternalResource
import io.reactivex.android.plugins.*
import io.reactivex.plugins.*
import io.reactivex.schedulers.*
import org.junit.rules.*

class SchedulersRule(private val useTestScheduler: Boolean = false) : ExternalResource() {

private lateinit var _testScheduler: TestScheduler

val testScheduler: TestScheduler
get() {
if (!useTestScheduler) throw IllegalStateException("TestScheduler is switched off.")
check(useTestScheduler) { "TestScheduler is switched off." }
return _testScheduler
}

Expand Down

0 comments on commit fd4da6e

Please sign in to comment.