-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue #553: Add AndroidX ViewModel integration as separate module #598
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8b27516
Add new module for android view model
5c833c1
Addressing PR comments
32c67c3
Provide ViewModelNodeActivity.kt
395441b
Update dependencies
fac0d30
Update deps
bf0b527
Add parent node
dddbad1
Make InteractionTarget abstract
634cbdc
update deps
0b4f184
Don't define children
bc837bf
Create an example ViewModelActivity
1573c1a
Extract CounterScreen
bd70fbe
EOF
66bb13f
Use ViewModelNode in example
593ac05
Use ViewModelNode in example
09903be
Use ViewModelNode in example
7206271
Rename classes + create VM factory
KovalevAndrey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 36 additions & 0 deletions
36
...on/android/src/main/kotlin/com/bumble/appyx/navigation/node/viewModel/ViewModelExample.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.bumble.appyx.navigation.node.viewModel | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewmodel.CreationExtras | ||
import androidx.lifecycle.viewmodel.initializer | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.getAndUpdate | ||
|
||
class ViewModelExample(startCounterValue: Int = 0) : ViewModel() { | ||
private val _uiState = MutableStateFlow(UiState(startCounterValue)) | ||
val uiState: StateFlow<UiState> = _uiState | ||
|
||
fun incrementCounter() { | ||
_uiState.getAndUpdate { value -> | ||
UiState(value.counter + 1) | ||
} | ||
} | ||
|
||
companion object { | ||
object StartCounterKey : CreationExtras.Key<Int> | ||
|
||
val Factory: ViewModelProvider.Factory = viewModelFactory { | ||
initializer { | ||
val startCounterValue = this[StartCounterKey] ?: 0 | ||
ViewModelExample( | ||
startCounterValue | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
data class UiState(val counter: Int) |
39 changes: 39 additions & 0 deletions
39
...id/src/main/kotlin/com/bumble/appyx/navigation/node/viewModel/ViewModelExampleActivity.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,39 @@ | ||
package com.bumble.appyx.navigation.node.viewModel | ||
|
||
import android.os.Bundle | ||
import androidx.activity.compose.setContent | ||
import androidx.compose.animation.ExperimentalAnimationApi | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.platform.LocalLifecycleOwner | ||
import androidx.compose.ui.unit.ExperimentalUnitApi | ||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen | ||
import com.bumble.appyx.navigation.integration.NodeHost | ||
import com.bumble.appyx.navigation.platform.AndroidLifecycle | ||
import com.bumble.appyx.navigation.ui.AppyxSampleAppTheme | ||
import com.bumble.appyx.utils.viewmodel.integration.ViewModelNodeActivity | ||
|
||
@ExperimentalUnitApi | ||
@ExperimentalAnimationApi | ||
@ExperimentalComposeUiApi | ||
class ViewModelExampleActivity : ViewModelNodeActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
installSplashScreen() | ||
super.onCreate(savedInstanceState) | ||
setContent { | ||
AppyxSampleAppTheme { | ||
Surface(color = MaterialTheme.colorScheme.background) { | ||
NodeHost( | ||
lifecycle = AndroidLifecycle(LocalLifecycleOwner.current.lifecycle), | ||
integrationPoint = appyxV2IntegrationPoint, | ||
) { | ||
ViewModelExampleNode(it) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
68 changes: 68 additions & 0 deletions
68
...ndroid/src/main/kotlin/com/bumble/appyx/navigation/node/viewModel/ViewModelExampleNode.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,68 @@ | ||
package com.bumble.appyx.navigation.node.viewModel | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.lifecycle.viewmodel.initializer | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
import com.bumble.appyx.interactions.core.state.MutableSavedStateMap | ||
import com.bumble.appyx.navigation.modality.BuildContext | ||
import com.bumble.appyx.utils.viewmodel.node.ViewModelNode | ||
import com.bumble.appyx.utils.viewmodel.node.viewModels | ||
|
||
class ViewModelExampleNode( | ||
buildContext: BuildContext, | ||
private val startCounterValue: Int = (buildContext.savedStateMap?.getValue("startCounterValue") as? Int) ?: 10 | ||
) : ViewModelNode(buildContext) { | ||
|
||
private val viewModel: ViewModelExample by viewModels( | ||
factoryProducer = { | ||
viewModelFactory { | ||
initializer { | ||
ViewModelExample( | ||
startCounterValue | ||
) | ||
} | ||
} | ||
} | ||
) | ||
|
||
override fun onSaveInstanceState(state: MutableSavedStateMap) { | ||
state["startCounterValue"] = viewModel.uiState.value.counter | ||
super.onSaveInstanceState(state) | ||
} | ||
|
||
|
||
@Composable | ||
@Override | ||
override fun View(modifier: Modifier) { | ||
val uiState by viewModel.uiState.collectAsState(initial = UiState(0)) | ||
|
||
Column( | ||
modifier = modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.spacedBy(24.dp) | ||
) { | ||
Text( | ||
modifier = Modifier.align(Alignment.CenterHorizontally), | ||
text = "Counter: ${uiState.counter}", | ||
fontSize = 45.sp | ||
) | ||
|
||
Button( | ||
modifier = Modifier.align(Alignment.CenterHorizontally), | ||
onClick = { viewModel.incrementCounter() } | ||
) { | ||
Text("Increment") | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
plugins { | ||
id("com.bumble.appyx.android.library") | ||
id("appyx-publish-android") | ||
} | ||
|
||
publishingPlugin { | ||
artifactId = "utils-node-viewmodel" | ||
} | ||
|
||
appyx { | ||
namespace.set("com.bumble.appyx.utils.viewmodel") | ||
|
||
buildFeatures { | ||
compose.set(true) | ||
} | ||
} | ||
|
||
dependencies { | ||
api(project(":appyx-navigation:appyx-navigation")) | ||
api(project(":appyx-interactions:appyx-interactions")) | ||
api(libs.androidx.activity) | ||
api(libs.androidx.appcompat) | ||
api(libs.androidx.lifecycle.viewmodel) | ||
implementation(libs.androidx.lifecycle.viewmodel.compose) | ||
debugApi(libs.compose.runtime) | ||
implementation(libs.androidx.core.core) | ||
implementation(libs.androidx.lifecycle.common) | ||
releaseImplementation(libs.compose.runtime) | ||
} |
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 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<issues format="6" by="lint 7.1.3" type="baseline" client="gradle" dependencies="false" | ||
name="AGP (7.1.3)" variant="all" version="7.1.3"> | ||
|
||
</issues> |
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,2 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest /> |
13 changes: 13 additions & 0 deletions
13
...e/appyx/utils/viewmodel/integration/ActivityIntegrationPointWithViewModelStoreProvider.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,13 @@ | ||
package com.bumble.appyx.utils.viewmodel.integration | ||
|
||
import android.os.Bundle | ||
import androidx.activity.ComponentActivity | ||
import com.bumble.appyx.navigation.integration.ActivityIntegrationPoint | ||
|
||
open class ActivityIntegrationPointWithViewModelStoreProvider( | ||
activity: ComponentActivity, | ||
savedInstanceState: Bundle?, | ||
) : ActivityIntegrationPoint(activity, savedInstanceState) { | ||
|
||
open val viewModelStoreProvider = ViewModelStoreProvider.getInstance(activity) | ||
} |
56 changes: 56 additions & 0 deletions
56
...oid/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/ViewModelNodeActivity.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,56 @@ | ||
package com.bumble.appyx.utils.viewmodel.integration | ||
|
||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.appcompat.app.AppCompatActivity | ||
import com.bumble.appyx.navigation.integrationpoint.IntegrationPointProvider | ||
|
||
/** | ||
* Helper class for root [Node] integration into projects using [AppCompatActivity]. | ||
* | ||
* See [NodeComponentActivity] for building upon [ComponentActivity]. | ||
* | ||
* Also offers base functionality to satisfy dependencies of Android-related functionality | ||
* down the tree via [appyxV2IntegrationPoint]: | ||
* - [ActivityStarter] | ||
* - [PermissionRequester] | ||
* | ||
* Feel free to not extend this and use your own integration point - in this case, | ||
* don't forget to take a look here what methods needs to be forwarded to the root Node. | ||
*/ | ||
open class ViewModelNodeActivity : AppCompatActivity(), IntegrationPointProvider { | ||
|
||
override lateinit var appyxV2IntegrationPoint: ActivityIntegrationPointWithViewModelStoreProvider | ||
protected set | ||
|
||
protected open fun createIntegrationPoint(savedInstanceState: Bundle?) = | ||
ActivityIntegrationPointWithViewModelStoreProvider( | ||
activity = this, | ||
savedInstanceState = savedInstanceState | ||
) | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
appyxV2IntegrationPoint = createIntegrationPoint(savedInstanceState) | ||
} | ||
|
||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | ||
super.onActivityResult(requestCode, resultCode, data) | ||
appyxV2IntegrationPoint.onActivityResult(requestCode, resultCode, data) | ||
} | ||
|
||
override fun onRequestPermissionsResult( | ||
requestCode: Int, | ||
permissions: Array<out String>, | ||
grantResults: IntArray | ||
) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults) | ||
appyxV2IntegrationPoint.onRequestPermissionsResult(requestCode, permissions, grantResults) | ||
} | ||
|
||
override fun onSaveInstanceState(outState: Bundle) { | ||
super.onSaveInstanceState(outState) | ||
appyxV2IntegrationPoint.onSaveInstanceState(outState) | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
...id/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/ViewModelStoreProvider.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,30 @@ | ||
package com.bumble.appyx.utils.viewmodel.integration | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.ViewModelStore | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import androidx.lifecycle.get | ||
|
||
open class ViewModelStoreProvider : ViewModel() { | ||
private val viewModelStores = mutableMapOf<String, ViewModelStore>() | ||
fun clear(nodeId: String) { | ||
val viewModelStore = viewModelStores.remove(nodeId) | ||
viewModelStore?.clear() | ||
} | ||
|
||
override fun onCleared() { | ||
super.onCleared() | ||
viewModelStores.values.forEach { it.clear() } | ||
viewModelStores.clear() | ||
} | ||
|
||
fun getViewModelStoreForNode(nodeId: String): ViewModelStore = | ||
viewModelStores.getOrPut(nodeId) { ViewModelStore() } | ||
|
||
companion object { | ||
fun getInstance(viewModelStoreOwner: ViewModelStoreOwner): ViewModelStoreProvider { | ||
return ViewModelProvider(viewModelStoreOwner).get() | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
.../viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/node/ViewModelNode.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,32 @@ | ||
package com.bumble.appyx.utils.viewmodel.node | ||
|
||
import androidx.lifecycle.ViewModelStore | ||
import androidx.lifecycle.ViewModelStoreOwner | ||
import com.bumble.appyx.navigation.lifecycle.DefaultPlatformLifecycleObserver | ||
import com.bumble.appyx.navigation.modality.BuildContext | ||
import com.bumble.appyx.navigation.node.Node | ||
import com.bumble.appyx.utils.viewmodel.integration.ActivityIntegrationPointWithViewModelStoreProvider | ||
|
||
open class ViewModelNode( | ||
buildContext: BuildContext, | ||
) : Node(buildContext), ViewModelStoreOwner { | ||
|
||
private val nodeViewModelStore by lazy { | ||
(integrationPoint as ActivityIntegrationPointWithViewModelStoreProvider).viewModelStoreProvider.getViewModelStoreForNode( | ||
id | ||
) | ||
} | ||
|
||
init { | ||
lifecycle.addObserver(object : DefaultPlatformLifecycleObserver { | ||
override fun onDestroy() { | ||
if (!(integrationPoint as ActivityIntegrationPointWithViewModelStoreProvider).isChangingConfigurations) { | ||
(integrationPoint as ActivityIntegrationPointWithViewModelStoreProvider).viewModelStoreProvider.clear(id) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
override val viewModelStore: ViewModelStore | ||
get() = nodeViewModelStore | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should revert this