forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTGContactsController.m
154 lines (120 loc) · 3.67 KB
/
TGContactsController.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
#import "TGContactsController.h"
#import "TGWatchCommon.h"
#import "TGBridgeContactsSignals.h"
#import "WKInterfaceTable+TGDataDrivenTable.h"
#import "TGInputController.h"
#import "TGUserRowController.h"
NSString *const TGContactsControllerIdentifier = @"TGContactsController";
const NSUInteger TGContactsControllerBatchCount = 15;
@implementation TGContactsControllerContext
- (instancetype)initWithQuery:(NSString *)query
{
self = [super init];
if (self != nil)
{
_query = query;
}
return self;
}
@end
@interface TGContactsController () <TGTableDataSource>
{
SMetaDisposable *_contactsDisposable;
TGContactsControllerContext *_context;
NSArray *_userModels;
}
@end
@implementation TGContactsController
- (instancetype)init
{
self = [super init];
if (self != nil)
{
_contactsDisposable = [[SMetaDisposable alloc] init];
[self.alertLabel _setInitialHidden:true];
[self.table _setInitialHidden:true];
self.table.tableDataSource = self;
}
return self;
}
- (void)dealloc
{
[_contactsDisposable dispose];
}
- (void)configureWithContext:(TGContactsControllerContext *)context
{
_context = context;
__weak TGContactsController *weakSelf = self;
[_contactsDisposable setDisposable:[[[TGBridgeContactsSignals searchContactsWithQuery:_context.query] deliverOn:[SQueue mainQueue]] startWithNext:^(NSArray *users)
{
__strong TGContactsController *strongSelf = weakSelf;
if (strongSelf == nil)
return;
if (users.count > TGContactsControllerBatchCount)
strongSelf->_userModels = [users subarrayWithRange:NSMakeRange(0, TGContactsControllerBatchCount)];
else
strongSelf->_userModels = users;
[strongSelf performInterfaceUpdate:^(bool animated)
{
__strong TGContactsController *strongSelf = weakSelf;
if (strongSelf == nil)
return;
strongSelf.activityIndicator.hidden = true;
if (strongSelf->_userModels.count > 0)
{
strongSelf.table.hidden = false;
[strongSelf.table reloadData];
}
else
{
strongSelf->_alertLabel.hidden = false;
strongSelf->_alertLabel.text = TGLocalized(@"Watch.Contacts.NoResults");
}
}];
} error:^(id error)
{
} completed:^
{
}]];
}
- (void)willActivate
{
[super willActivate];
[self.table notifyVisiblityChange];
}
- (void)didDeactivate
{
[super didDeactivate];
}
#pragma mark -
- (NSUInteger)numberOfRowsInTable:(WKInterfaceTable *)table section:(NSUInteger)section
{
return _userModels.count;
}
- (Class)table:(WKInterfaceTable *)table rowControllerClassAtIndexPath:(NSIndexPath *)indexPath
{
return [TGUserRowController class];
}
- (void)table:(WKInterfaceTable *)table updateRowController:(TGUserRowController *)controller forIndexPath:(TGIndexPath *)indexPath
{
__weak TGContactsController *weakSelf = self;
controller.isVisible = ^bool
{
__strong TGContactsController *strongSelf = weakSelf;
if (strongSelf == nil)
return false;
return strongSelf.isVisible;
};
[controller updateWithUser:_userModels[indexPath.row] context:_context.context];
}
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndexPath:(TGIndexPath *)indexPath
{
[self dismissController];
if (_context.completionBlock != nil)
_context.completionBlock(_userModels[indexPath.row]);
}
+ (NSString *)identifier
{
return TGContactsControllerIdentifier;
}
@end