Skip to content
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

Fix iOS lifecycle #670

Merged
merged 9 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Pending changes

- [#670](https://github.com/bumble-tech/appyx/pull/670) - Fixes ios lifecycle

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.bumble.appyx.navigation.node.Node
import com.bumble.appyx.navigation.platform.LifecycleListener
import com.bumble.appyx.navigation.platform.LocalOnBackPressedDispatcherOwner
import com.bumble.appyx.navigation.platform.OnBackPressedDispatcher
import com.bumble.appyx.navigation.platform.OnBackPressedDispatcherOwner
import com.bumble.appyx.navigation.platform.PlatformLifecycleRegistry
import com.bumble.appyx.utils.customisations.NodeCustomisationDirectory
import com.bumble.appyx.utils.customisations.NodeCustomisationDirectoryImpl
import kotlinx.cinterop.ExperimentalForeignApi
Expand All @@ -30,9 +30,8 @@ fun <N : Node<*>> IosNodeHost(
customisations: NodeCustomisationDirectory = remember { NodeCustomisationDirectoryImpl(null) },
factory: NodeFactory<N>,
) {
val platformLifecycleRegistry = remember {
PlatformLifecycleRegistry()
}
val lifecycleListener = remember { LifecycleListener() }

val mainScreen = UIScreen.mainScreen
val screenBounds = mainScreen.bounds

Expand All @@ -58,7 +57,7 @@ fun <N : Node<*>> IosNodeHost(

CompositionLocalProvider(LocalOnBackPressedDispatcherOwner provides onBackPressedDispatcherOwner) {
NodeHost(
lifecycle = platformLifecycleRegistry,
lifecycle = lifecycleListener.lifecycle,
integrationPoint = integrationPoint,
modifier = modifier,
customisations = customisations,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.bumble.appyx.navigation.platform

import com.bumble.appyx.navigation.lifecycle.Lifecycle
import platform.Foundation.NSNotificationCenter
import platform.Foundation.NSOperationQueue
import platform.UIKit.UIApplicationDidBecomeActiveNotification
import platform.UIKit.UIApplicationDidEnterBackgroundNotification
import platform.UIKit.UIApplicationWillResignActiveNotification
import platform.UIKit.UIApplicationWillTerminateNotification
import platform.darwin.NSObjectProtocol

class LifecycleListener {
val lifecycle: PlatformLifecycleRegistry = PlatformLifecycleRegistry()

private lateinit var didEnterBackgroundNotificationObserver: NSObjectProtocol
private lateinit var willResignActiveNotificationObserver: NSObjectProtocol
private lateinit var didBecomeActiveNotificationObserver: NSObjectProtocol
private lateinit var willTerminateNotificationObserver: NSObjectProtocol

init {
created()
startObserving()
}

private fun startObserving() {
// Goes to background
didEnterBackgroundNotificationObserver = addObserverFor(UIApplicationDidEnterBackgroundNotification) {
created()
}
// Becomes inactive
willResignActiveNotificationObserver = addObserverFor(UIApplicationWillResignActiveNotification) {
started()
}
// Gets focus back / Becomes active
didBecomeActiveNotificationObserver = addObserverFor(UIApplicationDidBecomeActiveNotification) {
resumed()
}
// Is about to be terminated
willTerminateNotificationObserver = addObserverFor(UIApplicationWillTerminateNotification) {
destroyed()
}
}

private fun addObserverFor(
notification: platform.Foundation.NSNotificationName,
block: () -> Unit
) : NSObjectProtocol {
return NSNotificationCenter.defaultCenter.addObserverForName(
name = notification,
`object` = null,
queue = NSOperationQueue.mainQueue,
usingBlock = {
block()
}
)
}

private fun created() {
lifecycle.setCurrentState(Lifecycle.State.CREATED)
}

private fun resumed() {
lifecycle.setCurrentState(Lifecycle.State.RESUMED)
}

private fun started() {
lifecycle.setCurrentState(Lifecycle.State.STARTED)
}

private fun destroyed() {
lifecycle.setCurrentState(Lifecycle.State.DESTROYED)
}
}
Loading