forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTGInterfaceController.m
176 lines (128 loc) · 4.58 KB
/
TGInterfaceController.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#import "TGInterfaceController.h"
#import "WKInterfaceTable+TGDataDrivenTable.h"
@interface TGInterfaceControllerContext : NSObject
@property (nonatomic, weak) TGInterfaceController *presentingController;
@property (nonatomic, strong) id context;
@end
@implementation TGInterfaceControllerContext
+ (TGInterfaceControllerContext *)contextWithPresentingController:(TGInterfaceController *)presentingController
context:(id<TGInterfaceContext>)context
{
NSParameterAssert(presentingController);
TGInterfaceControllerContext *controllerContext = [[TGInterfaceControllerContext alloc] init];
controllerContext.presentingController = presentingController;
controllerContext.context = context;
return controllerContext;
}
@end
@interface TGInterfaceController ()
{
NSString *_title;
void (^_pendingInterfaceUpdate)(bool animated);
}
@end
@implementation TGInterfaceController
@dynamic title;
- (void)configureWithContext:(id<TGInterfaceContext>)context
{
}
- (void)awakeWithContext:(id)context
{
[super awakeWithContext:context];
_visible = true;
id<TGInterfaceContext> unwrappedContext = nil;
if ([context isKindOfClass:[TGInterfaceControllerContext class]])
{
TGInterfaceControllerContext *controllerContext = (TGInterfaceControllerContext *)context;
_presentingController = controllerContext.presentingController;
unwrappedContext = controllerContext.context;
}
[self configureWithContext:unwrappedContext];
}
- (void)pushControllerWithClass:(Class)controllerClass context:(id<TGInterfaceContext>)context
{
NSParameterAssert([controllerClass isSubclassOfClass:[TGInterfaceController class]]);
TGInterfaceControllerContext *controllerContext = [TGInterfaceControllerContext contextWithPresentingController:self context:context];
[self pushControllerWithName:[controllerClass identifier] context:controllerContext];
}
- (void)presentControllerWithClass:(Class)controllerClass context:(id<TGInterfaceContext>)context
{
NSParameterAssert([controllerClass isSubclassOfClass:[TGInterfaceController class]]);
TGInterfaceControllerContext *controllerContext = [TGInterfaceControllerContext contextWithPresentingController:self context:context];
[self presentControllerWithName:[controllerClass identifier] context:controllerContext];
}
- (void)willActivate
{
[super willActivate];
_visible = true;
if (_pendingInterfaceUpdate != nil)
{
_pendingInterfaceUpdate(false);
_pendingInterfaceUpdate = nil;
}
}
- (void)didDeactivate
{
[super didDeactivate];
_visible = false;
}
- (bool)isPresenting
{
return !_visible;
}
- (void)_willPresentController
{
_visible = false;
}
- (void)presentControllerWithName:(NSString *)name context:(id)context
{
[self _willPresentController];
[super presentControllerWithName:name context:context];
}
- (void)presentControllerWithNames:(NSArray *)names contexts:(NSArray *)contexts
{
[self _willPresentController];
[super presentControllerWithNames:names contexts:contexts];
}
- (void)presentTextInputControllerWithSuggestions:(NSArray *)suggestions allowedInputMode:(WKTextInputMode)inputMode completion:(void (^)(NSArray *))completion
{
[self _willPresentController];
[super presentTextInputControllerWithSuggestions:suggestions allowedInputMode:inputMode completion:completion];
}
- (void)performInterfaceUpdate:(void (^)(bool))updates
{
if (updates == nil)
return;
if (self.isVisible)
updates(true);
else
_pendingInterfaceUpdate = [updates copy];
}
- (TGInterfaceControllerContext *)contextForSegueWithIdentifier:(NSString *)segueIdentifier inTable:(WKInterfaceTable *)table rowIndex:(NSInteger)rowIndex
{
TGIndexPath *indexPath = [table indexPathForRowIndex:rowIndex];
return [TGInterfaceControllerContext contextWithPresentingController:self context:[self contextForSegueWithIdentifer:segueIdentifier table:table indexPath:indexPath]];
}
- (id<TGInterfaceContext>)contextForSegueWithIdentifer:(NSString *)segueIdentifier table:(WKInterfaceTable *)table indexPath:(TGIndexPath *)indexPath
{
return nil;
}
#pragma mark - Properties
- (NSString *)title
{
return _title;
}
- (void)setTitle:(NSString *)title
{
if ([title isEqualToString:_title])
return;
_title = title;
[super setTitle:title];
}
#pragma mark -
+ (NSString *)identifier
{
NSAssert(true, @"Do not use TGInterfaceController directly");
return nil;
}
@end