forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of iOS bindings to Lifecycle (#1043)
Initial implementation of iOS bindings to Lifecycle. <img width="836" alt="ios_lifecycle" src="https://github.com/JetBrains/compose-multiplatform-core/assets/4167681/ec73ed26-5435-43e7-a4cf-5d3b9fe91693">
- Loading branch information
1 parent
58a4e0d
commit 4cc81ce
Showing
5 changed files
with
328 additions
and
49 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
76 changes: 76 additions & 0 deletions
76
...e/ui/ui/src/uikitMain/kotlin/androidx/compose/ui/window/ApplicationStateListener.uikit.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,76 @@ | ||
/* | ||
* Copyright 2024 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package androidx.compose.ui.window | ||
|
||
import kotlinx.cinterop.ObjCAction | ||
import platform.Foundation.NSNotificationCenter | ||
import platform.Foundation.NSSelectorFromString | ||
import platform.UIKit.UIApplication | ||
import platform.UIKit.UIApplicationDidEnterBackgroundNotification | ||
import platform.UIKit.UIApplicationState | ||
import platform.UIKit.UIApplicationWillEnterForegroundNotification | ||
import platform.darwin.NSObject | ||
|
||
internal class ApplicationStateListener( | ||
/** | ||
* Callback which will be called with `true` when the app becomes active, and `false` when the app goes background | ||
*/ | ||
private val callback: (Boolean) -> Unit | ||
) : NSObject() { | ||
init { | ||
val notificationCenter = NSNotificationCenter.defaultCenter | ||
|
||
notificationCenter.addObserver( | ||
this, | ||
NSSelectorFromString(::applicationWillEnterForeground.name), | ||
UIApplicationWillEnterForegroundNotification, | ||
null | ||
) | ||
|
||
notificationCenter.addObserver( | ||
this, | ||
NSSelectorFromString(::applicationDidEnterBackground.name), | ||
UIApplicationDidEnterBackgroundNotification, | ||
null | ||
) | ||
} | ||
|
||
@ObjCAction | ||
fun applicationWillEnterForeground() { | ||
callback(true) | ||
} | ||
|
||
@ObjCAction | ||
fun applicationDidEnterBackground() { | ||
callback(false) | ||
} | ||
|
||
/** | ||
* Deregister from [NSNotificationCenter] | ||
*/ | ||
fun dispose() { | ||
val notificationCenter = NSNotificationCenter.defaultCenter | ||
|
||
notificationCenter.removeObserver(this, UIApplicationWillEnterForegroundNotification, null) | ||
notificationCenter.removeObserver(this, UIApplicationDidEnterBackgroundNotification, null) | ||
} | ||
|
||
companion object { | ||
val isApplicationActive: Boolean | ||
get() = UIApplication.sharedApplication.applicationState != UIApplicationState.UIApplicationStateBackground | ||
} | ||
} |
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
Oops, something went wrong.