Skip to content

Commit

Permalink
Merge pull request #2 from takahirom/takahirom/remove-unneeded-workar…
Browse files Browse the repository at this point in the history
…ound/2024-04-25

Remove unneeded workaround
  • Loading branch information
takahirom authored Apr 25, 2024
2 parents 4cbe70d + 05d2aec commit ee4fa50
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.ui.platform.testTag
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import androidx.lifecycle.ViewModelStore
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
Expand All @@ -24,6 +25,10 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain

internal fun ViewModelStore.rinViewModel(): RinViewModel {
return get("androidx.lifecycle.ViewModelProvider.DefaultKey:io.github.takahirom.rin.RinViewModel") as RinViewModel
}

class NavigationTest {
private val composeTestRule = createAndroidComposeRule<ComponentActivity>()

Expand Down Expand Up @@ -126,7 +131,7 @@ class NavigationTest {
}
}
startScreenRinViewModel =
LocalViewModelStoreOwner.current!!.viewModelStore.get("RinViewModel") as RinViewModel
LocalViewModelStoreOwner.current!!.viewModelStore.rinViewModel()
Text(state)
Button(onClick = {
repository.increment()
Expand Down Expand Up @@ -220,7 +225,7 @@ class NavigationTest {
}.mutableState.value

startScreenRinViewModel =
LocalViewModelStoreOwner.current!!.viewModelStore.get("RinViewModel") as RinViewModel
LocalViewModelStoreOwner.current!!.viewModelStore.rinViewModel()
Text(state)
Button(onClick = {
repository.increment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class RetainedTest {
}

// Hold on to our Continuity instance
val continuity = composeTestRule.activity.viewModelStore.get("RinViewModel") as RinViewModel
val continuity = composeTestRule.activity.viewModelStore.rinViewModel()

// We now have one list with three retained values
// - text2Enabled
Expand Down Expand Up @@ -151,7 +151,7 @@ class RetainedTest {
}

// Hold on to our Continuity instance
val continuity = composeTestRule.activity.viewModelStore.get("RinViewModel") as RinViewModel
val continuity = composeTestRule.activity.viewModelStore.rinViewModel()

// We now have one list with three retained values
// - text2Enabled
Expand Down
24 changes: 12 additions & 12 deletions rin/src/commonMain/kotlin/io/github/takahirom/rin/Rin.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.github.takahirom.rin

import androidx.compose.runtime.*
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlin.coroutines.CoroutineContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.lifecycle.viewmodel.viewModelFactory
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext

var RIN_DEBUG = false
Expand Down Expand Up @@ -87,13 +88,12 @@ fun <T : Any> rememberRetained(
): T {
// Caution: currentCompositeKeyHash is not unique so we need to store multiple values with the same key
val keyToUse: String = key ?: currentCompositeKeyHash.toString(36)
// Wait for https://github.com/JetBrains/compose-multiplatform-core/blob/jb-main/lifecycle/lifecycle-viewmodel-compose/src/commonMain/kotlin/androidx/lifecycle/viewmodel/compose/ViewModel.kt#L21C5-L22C1
val viewModelStoreOwner = LocalViewModelStoreOwner.current!!
val rinViewModel: RinViewModel = (viewModelStoreOwner.viewModelStore.get("RinViewModel") ?: run {
val viewModel = RinViewModel()
viewModelStoreOwner.viewModelStore.put("RinViewModel", viewModel)
viewModel
}) as RinViewModel
val viewModelFactory = remember {
viewModelFactory {
addInitializer(RinViewModel::class) { RinViewModel() }
}
}
val rinViewModel: RinViewModel = viewModel(modelClass = RinViewModel::class, factory = viewModelFactory)
val lifecycleOwner = LocalLifecycleOwner.current
val lifecycleOwnerHash = lifecycleOwner.hashCode().toString(36)
val removeRetainedWhenRemovingComposition = LocalShouldRemoveRetainedWhenRemovingComposition.current
Expand Down
18 changes: 14 additions & 4 deletions rin/src/iosTest/kotlin/IosNavigationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,19 @@ import kotlin.test.assertEquals
private const val TAG_REMEMBER = "remember"
private const val TAG_RETAINED_1 = "retained1"

internal fun ViewModelStore.rinViewModel(): RinViewModel {
return get("androidx.lifecycle.ViewModelProvider.DefaultKey:io.github.takahirom.rin.RinViewModel") as RinViewModel
}


class IosNavigationTest {
@OptIn(InternalComposeApi::class)
@Test
fun test() {
fun moleculeTest() {
val coroutineScope = CoroutineScope(Job())
assertEquals(
expected = "test",
actual = CoroutineScope(Job()).launchMolecule(RecompositionMode.Immediate) {
actual = coroutineScope.launchMolecule(RecompositionMode.Immediate) {
val nestedRegistry = remember {
object : ViewModelStoreOwner {
override val viewModelStore: ViewModelStore = ViewModelStore()
Expand All @@ -49,7 +55,9 @@ class IosNavigationTest {
}
}.value
)
coroutineScope.cancel()
}

@Composable
@OptIn(InternalComposeApi::class)
fun <T> CompositionLocalProviderWithReturnValue(
Expand All @@ -72,7 +80,9 @@ class IosNavigationTest {
TextField(
modifier = Modifier.testTag(TAG_REMEMBER),
value = text1,
onValueChange = { text1 = it },
onValueChange = {
text1 = it
},
label = {},
)
TextField(
Expand Down Expand Up @@ -197,7 +207,7 @@ class IosNavigationTest {
}.mutableState.value

startScreenRinViewModel =
LocalViewModelStoreOwner.current!!.viewModelStore.get("RinViewModel") as RinViewModel
LocalViewModelStoreOwner.current!!.viewModelStore.rinViewModel()
Text(state)
Button(onClick = {
repository.increment()
Expand Down

0 comments on commit ee4fa50

Please sign in to comment.