Skip to content

Commit

Permalink
Provide ViewModelNodeActivity.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
alenafedyakshina committed Oct 10, 2023
1 parent 5c833c1 commit 32c67c3
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
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: 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<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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}
Expand Down

0 comments on commit 32c67c3

Please sign in to comment.