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

iOS modify lifecycle and containment #1027

Merged
merged 6 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.*
import androidx.compose.ui.main.defaultUIKitMain
import androidx.compose.ui.window.ComposeUIViewController
import bugs.IosBugs
import bugs.ProperContainmentDisposal
import bugs.StartRecompositionCheck
import platform.UIKit.UIViewController

Expand All @@ -27,6 +28,7 @@ fun IosDemo(arg: String, makeHostingController: ((Int) -> UIViewController)? = n
IosBugs,
NativeModalWithNaviationExample,
UIKitViewOrder,
ProperContainmentDisposal,
) + listOf(makeHostingController).mapNotNull {
it?.let {
SwiftUIInteropExample(it)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 bugs

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.mpp.demo.Screen
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.interop.LocalUIViewController
import platform.AVFoundation.AVPlayer
import platform.AVKit.AVPlayerViewController
import platform.Foundation.NSURL

val ProperContainmentDisposal = Screen.Example("Proper containment disposal") {
Box(Modifier.fillMaxSize().background(Color.DarkGray), contentAlignment = Alignment.Center) {
val viewController = LocalUIViewController.current
Button(onClick = {
val url = NSURL(string = "https://nonexisting")
val player = AVPlayer(uRL = url)
val playerController = AVPlayerViewController()
playerController.player = player
viewController.presentViewController(playerController, true, null)

}) {
Text("Present video player")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ NS_ASSUME_NONNULL_BEGIN

@interface CMPViewController : UIViewController

/// Indicates that view controller is considered alive in terms of structural containment.
/// Overriding classes should call super.
- (void)viewControllerDidEnterWindowHierarchy;

/// Indicates that view controller is considered alive in terms of structural containment
/// Overriding classes should call super.
- (void)viewControllerDidLeaveWindowHierarchy;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,41 @@ @interface UIViewController(CMPUIKitUtilsPrivate)

@implementation UIViewController(CMPUIKitUtilsPrivate)

- (BOOL)cmp_isRootViewController {
// Check that it's not rootViewController of one of windows of one of the connected scenes.
// In most apps it will be a single scene with a single connected window.
if (@available(iOS 13.0, *)) {
for (UIScene *scene in [UIApplication.sharedApplication connectedScenes]) {
if ([scene isKindOfClass:[UIWindowScene class]]) {
UIWindowScene *windowScene = (UIWindowScene *)scene;

for (UIWindow *window in windowScene.windows) {
if (window.rootViewController == self) {
return YES;
}
}
}
}
} else {
for (UIWindow* window in UIApplication.sharedApplication.windows) {
if (window.rootViewController == self) {
return YES;
}
}
}

return NO;
}

- (BOOL)cmp_isInWindowHierarchy {
if (self.view.window != nil) {
return YES;
} else if (self.parentViewController != nil) {
return [self.parentViewController cmp_isInWindowHierarchy];
} else if (self.presentingViewController != nil) {
return [self.presentingViewController cmp_isInWindowHierarchy];
} else {
return NO;
return [self cmp_isRootViewController];
}
}

Expand Down Expand Up @@ -63,19 +91,10 @@ - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibB

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[self transitLifecycleToStarted];
[self viewControllerDidEnterWindowHierarchy];
}

- (void)didMoveToParentViewController:(UIViewController *)parent {
[super didMoveToParentViewController:parent];

if (parent) {
[self transitLifecycleToStarted];
}
}

/// This method can be triggered by both `viewWillAppear` and `didMoveToParentViewController`. Mind that in second case the `view` could still not be constructed (adding `UIViewController` to inactive tab of `UITabBarController`, for example), so the `ComposeScene` is not neccessarily created at this point.
- (void)transitLifecycleToStarted {
switch (_lifecycleState) {
case CMPViewControllerLifecycleStateDestroyed:
Expand Down Expand Up @@ -117,6 +136,7 @@ - (void)scheduleHierarchyContainmentCheck {
}

- (void)viewControllerDidEnterWindowHierarchy {
[self transitLifecycleToStarted];
}

- (void)viewControllerDidLeaveWindowHierarchy {
Expand Down