-
Notifications
You must be signed in to change notification settings - Fork 13
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 #71 from isaac-udy/viewmodel-results-lifecycle-bug
Revert to using `addObserver` instead of `launchWhenCreated` in `LazyEnroResultChannel`
- Loading branch information
Showing
4 changed files
with
144 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package dev.enro.core | ||
|
||
abstract class EnroException(message: String) : IllegalStateException(message) | ||
|
||
class EnroLifecycleException(message: String) : EnroException(message) |
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
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
120 changes: 120 additions & 0 deletions
120
enro/src/androidTest/java/dev/enro/result/ViewModelResultTests.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,120 @@ | ||
package dev.enro.result | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.lifecycle.ViewModel | ||
import androidx.test.core.app.ActivityScenario | ||
import dev.enro.* | ||
import dev.enro.annotations.NavigationDestination | ||
import dev.enro.core.NavigationKey | ||
import dev.enro.core.forward | ||
import dev.enro.core.result.closeWithResult | ||
import dev.enro.core.result.registerForNavigationResult | ||
import dev.enro.expectFragment | ||
import dev.enro.viewmodel.enroViewModels | ||
import dev.enro.viewmodel.navigationHandle | ||
import kotlinx.parcelize.Parcelize | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
|
||
class ViewModelResultTests { | ||
@Test | ||
fun givenOrchestratedResultFlowManagedByViewModels_whenOrchestratedResultFlowExecutes_thenResultsAreReceivedCorrectly() { | ||
ActivityScenario.launch(DefaultActivity::class.java) | ||
.getNavigationHandle<NavigationKey>() | ||
.forward(OrchestratorKey()) | ||
|
||
expectFragment<OrchestratorFragment>() | ||
.viewModel | ||
.let { | ||
assertEquals("FirstStep -> SecondStep(SecondStepNested)", it.currentResult) | ||
} | ||
} | ||
} | ||
|
||
|
||
@Parcelize | ||
class OrchestratorKey : NavigationKey | ||
|
||
class OrchestratorViewModel : ViewModel() { | ||
var currentResult = "" | ||
|
||
val navigation by navigationHandle<NavigationKey>() | ||
val resultOne by registerForNavigationResult<String>(navigation) { | ||
currentResult = it | ||
resultTwo.open(SecondStepKey()) | ||
} | ||
val resultTwo by registerForNavigationResult<String>(navigation) { | ||
currentResult = "$currentResult -> $it" | ||
} | ||
|
||
init { | ||
resultOne.open(FirstStepKey()) | ||
} | ||
} | ||
|
||
@NavigationDestination(OrchestratorKey::class) | ||
class OrchestratorFragment : TestFragment() { | ||
val viewModel by enroViewModels<OrchestratorViewModel>() | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
viewModel.hashCode() | ||
} | ||
} | ||
|
||
@Parcelize | ||
class FirstStepKey : NavigationKey.WithResult<String> | ||
|
||
class FirstStepViewModel : ViewModel() { | ||
private val navigation by navigationHandle<FirstStepKey>() | ||
init { | ||
navigation.closeWithResult("FirstStep") | ||
} | ||
} | ||
|
||
@NavigationDestination(FirstStepKey::class) | ||
class FirstStepFragment : TestFragment() { | ||
private val viewModel by enroViewModels<FirstStepViewModel>() | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
viewModel.hashCode() | ||
} | ||
} | ||
|
||
@Parcelize | ||
class SecondStepKey : NavigationKey.WithResult<String> | ||
|
||
class SecondStepViewModel : ViewModel() { | ||
private val navigation by navigationHandle<SecondStepKey>() | ||
private val nested by registerForNavigationResult<String>(navigation) { | ||
navigation.closeWithResult("SecondStep($it)") | ||
} | ||
init { | ||
nested.open(SecondStepNestedKey()) | ||
} | ||
} | ||
|
||
@NavigationDestination(SecondStepKey::class) | ||
class SecondStepFragment : TestFragment() { | ||
private val viewModel by enroViewModels<SecondStepViewModel>() | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
viewModel.hashCode() | ||
} | ||
} | ||
|
||
|
||
@Parcelize | ||
class SecondStepNestedKey : NavigationKey.WithResult<String> | ||
|
||
class SecondStepNestedViewModel : ViewModel() { | ||
private val navigation by navigationHandle<SecondStepNestedKey>() | ||
init { | ||
navigation.closeWithResult("SecondStepNested") | ||
} | ||
} | ||
|
||
@NavigationDestination(SecondStepNestedKey::class) | ||
class SecondStepNestedFragment : TestFragment() { | ||
private val viewModel by enroViewModels<SecondStepNestedViewModel>() | ||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
viewModel.hashCode() | ||
} | ||
} |