forked from androidx/androidx
/
CMPViewController.m
145 lines (119 loc) · 4.79 KB
/
CMPViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2023 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 "CMPViewController.h"
#import <objc/runtime.h>
#pragma mark - UIViewController + CMPUIKitUtilsPrivate
@interface UIViewController(CMPUIKitUtilsPrivate)
@end
@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 [self cmp_isRootViewController];
}
}
@end
#pragma mark - CMPViewControllerLifecycleState
typedef NS_ENUM(NSInteger, CMPViewControllerLifecycleState) {
CMPViewControllerLifecycleStateInitalized,
CMPViewControllerLifecycleStateStarted,
CMPViewControllerLifecycleStateDestroyed
};
#pragma mark - CMPViewController
@implementation CMPViewController {
CMPViewControllerLifecycleState _lifecycleState;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
_lifecycleState = CMPViewControllerLifecycleStateInitalized;
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self viewControllerDidEnterWindowHierarchy];
}
- (void)transitLifecycleToStarted {
switch (_lifecycleState) {
case CMPViewControllerLifecycleStateDestroyed:
@throw [NSException exceptionWithName:@"CMPViewControllerMisuse"
reason:@"CMPViewController shouldn't be reused after completely removed from hierarchy, because it's logically marked as Destroyed. You must create a new CMPViewController and use it instead."
userInfo:nil];
case CMPViewControllerLifecycleStateInitalized:
_lifecycleState = CMPViewControllerLifecycleStateStarted;
[self viewControllerDidEnterWindowHierarchy];
[self scheduleHierarchyContainmentCheck];
break;
case CMPViewControllerLifecycleStateStarted:
break;
}
}
- (void)scheduleHierarchyContainmentCheck {
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 CMPViewControllerLifecycleStateInitalized:
case CMPViewControllerLifecycleStateDestroyed:
assert(false);
break;
case CMPViewControllerLifecycleStateStarted:
// perform check
if ([self cmp_isInWindowHierarchy]) {
// everything is fine, schedule next one
[self scheduleHierarchyContainmentCheck];
} else {
self->_lifecycleState = CMPViewControllerLifecycleStateDestroyed;
[self viewControllerDidLeaveWindowHierarchy];
}
break;
}
});
}
- (void)viewControllerDidEnterWindowHierarchy {
[self transitLifecycleToStarted];
}
- (void)viewControllerDidLeaveWindowHierarchy {
}
@end