From 32c67c31049f891605e7e5e4dcfabc9a8735f4d4 Mon Sep 17 00:00:00 2001 From: alenafedyakshina Date: Tue, 10 Oct 2023 11:16:58 +0100 Subject: [PATCH] Provide ViewModelNodeActivity.kt --- .../navigation/node/viewModel/MyViewModel.kt | 2 +- .../ActivityIntegrationPointWithViewModel.kt | 2 +- .../IntegrationPointViewModel.kt | 2 +- .../integration/ViewModelNodeActivity.kt | 56 +++++++++++++++++++ .../utils/viewmodel/node/ViewModelNode.kt | 4 +- 5 files changed, 61 insertions(+), 5 deletions(-) rename utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/{ => integration}/ActivityIntegrationPointWithViewModel.kt (88%) rename utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/{ => integration}/IntegrationPointViewModel.kt (94%) create mode 100644 utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/ViewModelNodeActivity.kt diff --git a/demos/appyx-navigation/android/src/main/kotlin/com/bumble/appyx/navigation/node/viewModel/MyViewModel.kt b/demos/appyx-navigation/android/src/main/kotlin/com/bumble/appyx/navigation/node/viewModel/MyViewModel.kt index 76db1ad3a..3270dcd3a 100644 --- a/demos/appyx-navigation/android/src/main/kotlin/com/bumble/appyx/navigation/node/viewModel/MyViewModel.kt +++ b/demos/appyx-navigation/android/src/main/kotlin/com/bumble/appyx/navigation/node/viewModel/MyViewModel.kt @@ -1,6 +1,6 @@ package com.bumble.appyx.navigation.node.viewModel -import com.bumble.appyx.utils.viewmodel.IntegrationPointViewModel +import com.bumble.appyx.utils.viewmodel.integration.IntegrationPointViewModel import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow diff --git a/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/ActivityIntegrationPointWithViewModel.kt b/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/ActivityIntegrationPointWithViewModel.kt similarity index 88% rename from utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/ActivityIntegrationPointWithViewModel.kt rename to utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/ActivityIntegrationPointWithViewModel.kt index ecd1bc96a..3fd1a0ac7 100644 --- a/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/ActivityIntegrationPointWithViewModel.kt +++ b/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/ActivityIntegrationPointWithViewModel.kt @@ -1,4 +1,4 @@ -package com.bumble.appyx.utils.viewmodel +package com.bumble.appyx.utils.viewmodel.integration import android.os.Bundle import androidx.activity.ComponentActivity diff --git a/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/IntegrationPointViewModel.kt b/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/IntegrationPointViewModel.kt similarity index 94% rename from utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/IntegrationPointViewModel.kt rename to utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/IntegrationPointViewModel.kt index fad5babe4..c75e25a0f 100644 --- a/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/IntegrationPointViewModel.kt +++ b/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/IntegrationPointViewModel.kt @@ -1,4 +1,4 @@ -package com.bumble.appyx.utils.viewmodel +package com.bumble.appyx.utils.viewmodel.integration import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider diff --git a/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/ViewModelNodeActivity.kt b/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/ViewModelNodeActivity.kt new file mode 100644 index 000000000..83e6cba79 --- /dev/null +++ b/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/integration/ViewModelNodeActivity.kt @@ -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: ActivityIntegrationPointWithViewModel + protected set + + protected open fun createIntegrationPoint(savedInstanceState: Bundle?) = + ActivityIntegrationPointWithViewModel( + 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, + grantResults: IntArray + ) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + appyxV2IntegrationPoint.onRequestPermissionsResult(requestCode, permissions, grantResults) + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + appyxV2IntegrationPoint.onSaveInstanceState(outState) + } + +} diff --git a/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/node/ViewModelNode.kt b/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/node/ViewModelNode.kt index 762f3532a..44f7e6994 100644 --- a/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/node/ViewModelNode.kt +++ b/utils/viewmodel-android/src/main/kotlin/com/bumble/appyx/utils/viewmodel/node/ViewModelNode.kt @@ -5,7 +5,7 @@ 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.ActivityIntegrationPointWithViewModel +import com.bumble.appyx.utils.viewmodel.integration.ActivityIntegrationPointWithViewModel open class ViewModelNode( buildContext: BuildContext, @@ -20,7 +20,7 @@ open class ViewModelNode( init { lifecycle.addObserver(object : DefaultPlatformLifecycleObserver { override fun onDestroy() { - if (!(integrationPoint as ActivityIntegrationPointWithViewModel).isChangingConfigurations()) { + if (!(integrationPoint as ActivityIntegrationPointWithViewModel).isChangingConfigurations) { (integrationPoint as ActivityIntegrationPointWithViewModel).viewModel.clear(id) } }