-
Notifications
You must be signed in to change notification settings - Fork 498
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes a bug that cannot use custom scope viewModel in mvrx-compose. (#…
…712) * Create a failure test for custom compose fragment lifecycle scope. - Even if create and pass a custom lifecycle scope in Fragment, `mavericksViewModel` function uses the fragment's lifecycleScope. * Change to create a viewModelContext using the received viewModelStoreOwner and savedStateRegistry. - Even if create and pass a custom lifecycle scope in Fragment, `mavericksViewModel` function uses the fragment's lifecycleScope. * Add newline at EOF to fix detekt warning
- Loading branch information
Showing
3 changed files
with
151 additions
and
1 deletion.
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
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
143 changes: 143 additions & 0 deletions
143
mvrx-compose/src/test/kotlin/com/airbnb/mvrx/compose/CustomLifecycleOwnerScopeTest.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,143 @@ | ||
package com.airbnb.mvrx.compose | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.LinearLayout | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.compose.ui.test.junit4.createAndroidComposeRule | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentContainerView | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.LifecycleRegistry | ||
import androidx.lifecycle.ViewModelStore | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import androidx.savedstate.SavedStateRegistry | ||
import androidx.savedstate.SavedStateRegistryOwner | ||
import com.airbnb.mvrx.Mavericks | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Before | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class CustomLifecycleOwnerScopeTest { | ||
@get:Rule | ||
val composeTestRule = createAndroidComposeRule<CustomLifecycleOwnerScopeTestActivity>() | ||
|
||
@Before | ||
fun setUp() { | ||
Mavericks.initialize(composeTestRule.activity) | ||
} | ||
|
||
@Test | ||
fun `activity_customScope_viewModel1 and activity_customScope_viewModel2 are different`() { | ||
assertNotNull(composeTestRule.activity.fragment.viewModel1) | ||
assertNotNull(composeTestRule.activity.fragment.viewModel2) | ||
assert(composeTestRule.activity.fragment.viewModel1 !== composeTestRule.activity.fragment.viewModel2) | ||
} | ||
|
||
@Test | ||
fun `fragment_customScope_viewModel1 and fragment_customScope_viewModel2 are different`() { | ||
assertNotNull(composeTestRule.activity.viewModel1) | ||
assertNotNull(composeTestRule.activity.viewModel2) | ||
assert(composeTestRule.activity.viewModel1 !== composeTestRule.activity.viewModel2) | ||
} | ||
} | ||
|
||
@Composable | ||
private fun CustomViewModelScope(content: @Composable (LifecycleOwner) -> Unit) { | ||
val originLifecycleOwner = LocalLifecycleOwner.current | ||
val customLifecycleRegistry = remember { LifecycleRegistry(originLifecycleOwner) } | ||
val customScope = remember { | ||
CustomLifecycleOwner( | ||
customLifecycleRegistry, | ||
ViewModelStore(), | ||
(originLifecycleOwner as SavedStateRegistryOwner).savedStateRegistry | ||
) | ||
} | ||
|
||
customLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) | ||
|
||
content(customScope) | ||
|
||
DisposableEffect(Unit) { | ||
onDispose { | ||
customLifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) | ||
} | ||
} | ||
} | ||
|
||
class CustomLifecycleOwner( | ||
override val lifecycle: Lifecycle, | ||
override val viewModelStore: ViewModelStore, | ||
override val savedStateRegistry: SavedStateRegistry, | ||
) : LifecycleOwner, ViewModelStoreOwner, SavedStateRegistryOwner | ||
|
||
class CustomLifecycleOwnerScopeTestActivity : AppCompatActivity() { | ||
lateinit var fragment: CustomLifecycleOwnerScopeTestFragment | ||
lateinit var viewModel1: CounterViewModel | ||
lateinit var viewModel2: CounterViewModel | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val fragmentId = 123 | ||
val fragmentContainerView = FragmentContainerView(this).apply { | ||
id = fragmentId | ||
} | ||
val composeView = ComposeView(this).apply { | ||
setContent { | ||
CustomViewModelScope { scope -> | ||
this@CustomLifecycleOwnerScopeTestActivity.viewModel1 = mavericksViewModel<CounterViewModel, CounterState>(scope = scope) | ||
} | ||
CustomViewModelScope { scope -> | ||
this@CustomLifecycleOwnerScopeTestActivity.viewModel2 = mavericksViewModel<CounterViewModel, CounterState>(scope = scope) | ||
} | ||
} | ||
} | ||
|
||
setContentView( | ||
LinearLayout(this).apply { | ||
addView(fragmentContainerView) | ||
addView(composeView) | ||
} | ||
) | ||
|
||
fragment = CustomLifecycleOwnerScopeTestFragment() | ||
|
||
supportFragmentManager.beginTransaction() | ||
.add( | ||
fragmentId, | ||
fragment | ||
) | ||
.commit() | ||
} | ||
} | ||
|
||
class CustomLifecycleOwnerScopeTestFragment : Fragment() { | ||
lateinit var viewModel1: CounterViewModel | ||
lateinit var viewModel2: CounterViewModel | ||
|
||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { | ||
return ComposeView(requireContext()).apply { | ||
setContent { | ||
CustomViewModelScope { scope -> | ||
this@CustomLifecycleOwnerScopeTestFragment.viewModel1 = mavericksViewModel<CounterViewModel, CounterState>(scope = scope) | ||
} | ||
CustomViewModelScope { scope -> | ||
this@CustomLifecycleOwnerScopeTestFragment.viewModel2 = mavericksViewModel<CounterViewModel, CounterState>(scope = scope) | ||
} | ||
} | ||
} | ||
} | ||
} |