-
Notifications
You must be signed in to change notification settings - Fork 0
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 #55 from huanshankeji/save-viewmodel-in-navigation…
…-on-js-dom Save the ViewModel states on JS DOM, especially during navigation
- Loading branch information
Showing
19 changed files
with
344 additions
and
42 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
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
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
23 changes: 23 additions & 0 deletions
23
...n/src/jsMain/kotlin/com/huanshankeji/compose/ui/platform/DefaultViewModelOwnerStore.js.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,23 @@ | ||
package com.huanshankeji.compose.ui.platform | ||
|
||
// copied and adapted from "DefaultViewModelOwnerStore.skiko.kt" in `androidx.compose.ui.platform` | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.InternalComposeApi | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
|
||
/** | ||
* Internal helper to provide [ViewModelStoreOwner] from Compose UI module. | ||
* In applications please use [androidx.lifecycle.viewmodel.compose.LocalViewModelStoreOwner]. | ||
* | ||
* @hide | ||
*/ | ||
internal val LocalInternalViewModelStoreOwner = staticCompositionLocalOf<ViewModelStoreOwner?> { | ||
null | ||
} | ||
|
||
@InternalComposeApi | ||
@Composable | ||
fun findComposeDefaultViewModelStoreOwner(): ViewModelStoreOwner? = | ||
LocalInternalViewModelStoreOwner.current |
36 changes: 36 additions & 0 deletions
36
common/src/jsMain/kotlin/com/huanshankeji/compose/ui/window/ComposeWindow.js.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,36 @@ | ||
package com.huanshankeji.compose.ui.window | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Composition | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.lifecycle.ViewModelStore | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import com.huanshankeji.compose.ExperimentalApi | ||
import com.huanshankeji.compose.ui.platform.LocalInternalViewModelStoreOwner | ||
import org.jetbrains.compose.web.dom.DOMScope | ||
import org.jetbrains.compose.web.renderComposableInBody | ||
import org.w3c.dom.HTMLBodyElement | ||
|
||
@ExperimentalApi | ||
class SimpleViewModelStoreOwner : ViewModelStoreOwner { | ||
override val viewModelStore: ViewModelStore = ViewModelStore() | ||
} | ||
|
||
fun renderComposableInBodyWithViewModelStoreOwner( | ||
content: @Composable DOMScope<HTMLBodyElement>.() -> Unit | ||
): Composition = | ||
renderComposableInBody { | ||
// copied and adapted from `ComposeWindow` in "ComposeWindow.web.kt" in `androidx.compose.ui.window` | ||
// also see `ComposeViewport` on Wasm JS | ||
@OptIn(ExperimentalApi::class) | ||
CompositionLocalProvider( | ||
/* TODO add back these 2 lines below if needed one day | ||
in a function possibly named `renderComposableInBodyWithLifecycle` */ | ||
//LocalSystemTheme provides systemThemeObserver.currentSystemTheme.value, | ||
//LocalLifecycleOwner provides this, | ||
LocalInternalViewModelStoreOwner provides SimpleViewModelStoreOwner(), | ||
content = { | ||
content() | ||
} | ||
) | ||
} |
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
9 changes: 9 additions & 0 deletions
9
demo/src/commonMain/kotlin/com/huanshankeji/compose/material/demo/Material3ViewModel.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,9 @@ | ||
package com.huanshankeji.compose.material.demo | ||
|
||
import androidx.lifecycle.ViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
|
||
class Material3ViewModel : ViewModel() { | ||
val countState = MutableStateFlow(0) | ||
val checkedState = MutableStateFlow(false) | ||
} |
5 changes: 3 additions & 2 deletions
5
demo/src/jsMain/kotlin/com/huanshankeji/compose/material/demo/Main.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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package com.huanshankeji.compose.material.demo | ||
|
||
import com.huanshankeji.compose.html.material3.require | ||
import org.jetbrains.compose.web.renderComposableInBody | ||
import com.huanshankeji.compose.ui.window.renderComposableInBodyWithViewModelStoreOwner | ||
|
||
fun main() { | ||
require("material-symbols/outlined.css") | ||
renderComposableInBody { App() } | ||
//renderComposableInBody { App() } // "No ViewModelStoreOwner was provided via LocalViewModelStoreOwner" | ||
renderComposableInBodyWithViewModelStoreOwner { App() } | ||
} |
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,9 @@ | ||
public final class com/huanshankeji/androidx/lifecycle/viewmodel/compose/ViewModelKt { | ||
public static final fun defaultCreationExtras (Landroidx/lifecycle/ViewModelStoreOwner;)Landroidx/lifecycle/viewmodel/CreationExtras; | ||
} | ||
|
||
public final class com/huanshankeji/androidx/lifecycle/viewmodel/compose/ViewModel_composeUiKt { | ||
public static final fun defaultViewModelStoreOwner (Landroidx/compose/runtime/Composer;I)Landroidx/lifecycle/ViewModelStoreOwner; | ||
public static final fun viewModel (Lkotlin/reflect/KClass;Landroidx/lifecycle/ViewModelStoreOwner;Ljava/lang/String;Landroidx/lifecycle/ViewModelProvider$Factory;Landroidx/lifecycle/viewmodel/CreationExtras;Landroidx/compose/runtime/Composer;II)Landroidx/lifecycle/ViewModel; | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public final class com/huanshankeji/androidx/lifecycle/viewmodel/compose/ViewModelKt { | ||
public static final fun defaultCreationExtras (Landroidx/lifecycle/ViewModelStoreOwner;)Landroidx/lifecycle/viewmodel/CreationExtras; | ||
} | ||
|
||
public final class com/huanshankeji/androidx/lifecycle/viewmodel/compose/ViewModel_composeUiKt { | ||
public static final fun defaultViewModelStoreOwner (Landroidx/compose/runtime/Composer;I)Landroidx/lifecycle/ViewModelStoreOwner; | ||
public static final fun viewModel (Lkotlin/reflect/KClass;Landroidx/lifecycle/ViewModelStoreOwner;Ljava/lang/String;Landroidx/lifecycle/ViewModelProvider$Factory;Landroidx/lifecycle/viewmodel/CreationExtras;Landroidx/compose/runtime/Composer;II)Landroidx/lifecycle/ViewModel; | ||
} | ||
|
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
Oops, something went wrong.