forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 105
Refactor compose container lifecycle delegate #2580
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c11e413
Refactor compose container lifecycle delegate
ASalavei 16a0018
Remove duplicated code
ASalavei d59b0ee
Fix tests compilation
ASalavei 6627626
Fix tests compilation
ASalavei 886781a
Add userInterfaceStyle check
ASalavei f5a3393
PR Comments
ASalavei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
25 changes: 25 additions & 0 deletions
25
...kit/src/uikitMain/objc/CMPUIKitUtils/CMPUIKitUtils/CMPComposeContainerLifecycleDelegate.h
This file contains hidden or 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,25 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| */ | ||
|
|
||
| #import <UIKit/UIKit.h> | ||
|
|
||
| @protocol CMPComposeContainerLifecycleDelegate | ||
|
|
||
| - (void)composeContainerWillAppear; | ||
| - (void)composeContainerDidDisappear; | ||
| - (void)composeContainerWillDealloc; | ||
|
|
||
| @end |
23 changes: 23 additions & 0 deletions
23
...-uikit/src/uikitMain/objc/CMPUIKitUtils/CMPUIKitUtils/CMPComposeContainerLifecycleState.h
This file contains hidden or 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,23 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| */ | ||
|
|
||
| #import <UIKit/UIKit.h> | ||
|
|
||
| typedef NS_ENUM(NSInteger, CMPComposeContainerLifecycleState) { | ||
| CMPComposeContainerLifecycleStateInitialized, | ||
| CMPComposeContainerLifecycleStateStarted, | ||
| CMPComposeContainerLifecycleStateStopped | ||
| }; |
This file contains hidden or 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
43 changes: 43 additions & 0 deletions
43
compose/ui/ui-uikit/src/uikitMain/objc/CMPUIKitUtils/CMPUIKitUtils/CMPView.h
This file contains hidden or 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,43 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| */ | ||
|
|
||
| #import <UIKit/UIKit.h> | ||
| #import "CMPComposeContainerLifecycleDelegate.h" | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| @interface CMPView : UIView | ||
|
|
||
| - (id)initWithLifecycleDelegate:(id<CMPComposeContainerLifecycleDelegate> _Nullable)delegate; | ||
|
|
||
| /// Notifies the view is added to a view hierarchy | ||
| - (void)viewDidAppear; | ||
|
|
||
| /// Notifies the view was removed from a view hierarchy | ||
| - (void)viewDidDisappear; | ||
|
|
||
| /// Indicates that view is considered alive in terms of structural containment | ||
| - (void)viewDidEnterWindowHierarchy; | ||
|
|
||
| /// Indicates that view is considered as closed in terms of structural containment | ||
| - (void)viewDidLeaveWindowHierarchy; | ||
|
|
||
| /// Indicates that trait interface style trait changed | ||
| - (void)userInterfaceStyleDidChange; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
143 changes: 143 additions & 0 deletions
143
compose/ui/ui-uikit/src/uikitMain/objc/CMPUIKitUtils/CMPUIKitUtils/CMPView.m
This file contains hidden or 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,143 @@ | ||
| /* | ||
| * Copyright 2025 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. | ||
| */ | ||
|
|
||
| #import "CMPView.h" | ||
| #import "CMPComposeContainerLifecycleState.h" | ||
|
|
||
| #pragma mark - CMPViewController | ||
|
|
||
| @implementation CMPView { | ||
| CMPComposeContainerLifecycleState _lifecycleState; | ||
| id<CMPComposeContainerLifecycleDelegate> _lifecycleDelegate; | ||
| BOOL _isViewInWindowHierarchy; | ||
| } | ||
|
|
||
| - (id)initWithLifecycleDelegate:(id<CMPComposeContainerLifecycleDelegate>)delegate { | ||
| self = [super initWithFrame:CGRectZero]; | ||
|
|
||
| if (self) { | ||
| _lifecycleDelegate = delegate; | ||
| _lifecycleState = CMPComposeContainerLifecycleStateInitialized; | ||
| _isViewInWindowHierarchy = NO; | ||
|
|
||
| __weak typeof(self) weakSelf = self; | ||
| if (@available(iOS 17, *)) { | ||
| [self registerForTraitChanges:@[[UITraitUserInterfaceStyle class]] | ||
| withHandler:^(__kindof id<UITraitEnvironment> _Nonnull traitEnvironment, | ||
| UITraitCollection * _Nonnull previousCollection) { | ||
| [weakSelf userInterfaceStyleDidChange]; | ||
| }]; | ||
| } | ||
| } | ||
|
|
||
| return self; | ||
| } | ||
|
|
||
| - (void)didMoveToWindow { | ||
| [super didMoveToWindow]; | ||
|
|
||
| [self updateViewAppearanceState]; | ||
| } | ||
|
|
||
| - (void)didMoveToSuperview { | ||
| [super didMoveToSuperview]; | ||
|
|
||
| [self updateViewAppearanceState]; | ||
| } | ||
|
|
||
| - (void)updateViewAppearanceState { | ||
| BOOL isViewInWindowHierarchy = self.superview != nil && self.window != nil; | ||
| if (_isViewInWindowHierarchy != isViewInWindowHierarchy) { | ||
| _isViewInWindowHierarchy = isViewInWindowHierarchy; | ||
| if (isViewInWindowHierarchy) { | ||
| [_lifecycleDelegate composeContainerWillAppear]; | ||
| [self transitViewLifecycleToStarted]; | ||
| [self viewDidAppear]; | ||
| } else { | ||
| [self viewDidDisappear]; | ||
| [self scheduleViewHierarchyContainmentCheck]; | ||
| [_lifecycleDelegate composeContainerDidDisappear]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| - (void)transitViewLifecycleToStarted { | ||
| switch (_lifecycleState) { | ||
| case CMPComposeContainerLifecycleStateInitialized: | ||
| case CMPComposeContainerLifecycleStateStopped: | ||
| _lifecycleState = CMPComposeContainerLifecycleStateStarted; | ||
| [self viewDidEnterWindowHierarchy]; | ||
| break; | ||
| case CMPComposeContainerLifecycleStateStarted: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| - (void)scheduleViewHierarchyContainmentCheck { | ||
| double delayInSeconds = 0.5; | ||
|
|
||
| dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ | ||
| switch (self->_lifecycleState) { | ||
| case CMPComposeContainerLifecycleStateInitialized: | ||
| NSAssert(false, @"Attempt to schedule hierarchy check without starting the container"); | ||
| break; | ||
| case CMPComposeContainerLifecycleStateStopped: | ||
| break; | ||
| case CMPComposeContainerLifecycleStateStarted: | ||
| // perform check | ||
| if (!self->_isViewInWindowHierarchy) { | ||
| self->_lifecycleState = CMPComposeContainerLifecycleStateStopped; | ||
| [self viewDidLeaveWindowHierarchy]; | ||
| } | ||
| break; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection { | ||
| [super traitCollectionDidChange:previousTraitCollection]; | ||
|
|
||
| if (@available(iOS 17, *)) { | ||
| // Do nothing | ||
| } else if (self.traitCollection.userInterfaceStyle != previousTraitCollection.userInterfaceStyle) { | ||
| [self userInterfaceStyleDidChange]; | ||
| } | ||
| } | ||
|
|
||
| - (void)viewDidAppear { | ||
| } | ||
|
|
||
| - (void)viewDidDisappear { | ||
| } | ||
|
|
||
| - (void)viewDidEnterWindowHierarchy { | ||
| } | ||
|
|
||
| - (void)viewDidLeaveWindowHierarchy { | ||
| } | ||
|
|
||
| - (void)userInterfaceStyleDidChange { | ||
| } | ||
|
|
||
| - (void)dealloc { | ||
| if (_lifecycleState == CMPComposeContainerLifecycleStateStarted) { | ||
| [self viewDidLeaveWindowHierarchy]; | ||
| } | ||
|
|
||
| [_lifecycleDelegate composeContainerWillDealloc]; | ||
| } | ||
|
|
||
| @end | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.