forked from TelegramMessenger/Telegram-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTGLocationController.m
186 lines (149 loc) · 5.77 KB
/
TGLocationController.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
177
178
179
180
181
182
183
184
185
186
#import "TGLocationController.h"
#import <WatchCommonWatch/WatchCommonWatch.h>
#import "TGWatchCommon.h"
#import "TGBridgeLocationSignals.h"
#import "TGBridgeLocationVenue+TGTableItem.h"
#import "WKInterfaceTable+TGDataDrivenTable.h"
#import "TGTableDeltaUpdater.h"
#import "TGLocationMapHeaderController.h"
#import "TGLocationVenueRowController.h"
NSString *const TGLocationControllerIdentifier = @"TGLocationController";
const NSUInteger TGLocationControllerBatchLimit = 14;
@implementation TGLocationControllerContext
@end
@interface TGLocationController () <TGTableDataSource>
{
TGLocationControllerContext *_context;
SMetaDisposable *_locationDisposable;
NSArray *_venueModels;
NSArray *_currentVenueModels;
CLLocation *_currentLocation;
}
@end
@implementation TGLocationController
- (instancetype)init
{
self = [super init];
if (self != nil)
{
_locationDisposable = [[SMetaDisposable alloc] init];
[self.alertGroup _setInitialHidden:true];
[self.table _setInitialHidden:true];
self.table.tableDataSource = self;
}
return self;
}
- (void)dealloc
{
[_locationDisposable dispose];
}
- (void)configureWithContext:(TGLocationControllerContext *)context
{
_context = context;
__weak TGLocationController *weakSelf = self;
[_locationDisposable setDisposable:[[[TGBridgeLocationSignals nearbyVenuesWithLimit:TGLocationControllerBatchLimit] deliverOn:[SQueue mainQueue]] startWithNext:^(id next)
{
__strong TGLocationController *strongSelf = weakSelf;
if (strongSelf == nil)
return;
if ([next isKindOfClass:[NSString class]])
{
[strongSelf performInterfaceUpdate:^(bool animated)
{
__strong TGLocationController *strongSelf = weakSelf;
if (strongSelf == nil)
return;
if ([next isEqualToString:TGBridgeLocationAccessRequiredKey])
{
strongSelf.alertGroup.hidden = false;
strongSelf.alertLabel.text = TGLocalized(@"Watch.Location.Access");
strongSelf.activityGroup.hidden = true;
}
else if ([next isEqualToString:TGBridgeLocationLoadingKey])
{
strongSelf.alertGroup.hidden = true;
strongSelf.activityGroup.hidden = false;
}
}];
}
else if ([next isKindOfClass:[CLLocation class]])
{
strongSelf->_currentLocation = (CLLocation *)next;
[strongSelf performInterfaceUpdate:^(bool animated)
{
__strong TGLocationController *strongSelf = weakSelf;
if (strongSelf == nil)
return;
strongSelf.table.hidden = false;
[strongSelf.table reloadData];
}];
}
else if ([next isKindOfClass:[NSArray class]])
{
strongSelf->_venueModels = (NSArray *)next;
[strongSelf performInterfaceUpdate:^(bool animated)
{
__strong TGLocationController *strongSelf = weakSelf;
if (strongSelf == nil)
return;
NSArray *currentVenueModels = strongSelf->_currentVenueModels;
strongSelf->_currentVenueModels = strongSelf->_venueModels;
strongSelf.alertGroup.hidden = true;
strongSelf.table.hidden = false;
strongSelf.activityGroup.hidden = true;
[TGTableDeltaUpdater updateTable:strongSelf.table oldData:currentVenueModels newData:strongSelf->_currentVenueModels controllerClassForIndexPath:^Class(TGIndexPath *indexPath)
{
return [strongSelf table:strongSelf.table rowControllerClassAtIndexPath:indexPath];
}];
}];
}
} error:^(id error)
{
} completed:^
{
}]];
}
- (Class)headerControllerClassForTable:(WKInterfaceTable *)table
{
return [TGLocationMapHeaderController class];
}
- (void)table:(WKInterfaceTable *)table updateHeaderController:(TGLocationMapHeaderController *)controller
{
__weak TGLocationController *weakSelf = self;
controller.currentLocationPressed = ^
{
__strong TGLocationController *strongSelf = weakSelf;
if (strongSelf == nil)
return;
TGBridgeLocationMediaAttachment *location = [[TGBridgeLocationMediaAttachment alloc] init];
location.latitude = strongSelf->_currentLocation.coordinate.latitude;
location.longitude = strongSelf->_currentLocation.coordinate.longitude;
[strongSelf dismissController];
if (strongSelf->_context.completionBlock != nil)
strongSelf->_context.completionBlock(location);
};
[controller updateWithLocation:_currentLocation];
}
- (NSUInteger)numberOfRowsInTable:(WKInterfaceTable *)table section:(NSUInteger)section
{
return _venueModels.count;
}
- (Class)table:(WKInterfaceTable *)table rowControllerClassAtIndexPath:(NSIndexPath *)indexPath
{
return [TGLocationVenueRowController class];
}
- (void)table:(WKInterfaceTable *)table updateRowController:(TGLocationVenueRowController *)controller forIndexPath:(TGIndexPath *)indexPath
{
[controller updateWithLocationVenue:_venueModels[indexPath.row]];
}
- (void)table:(WKInterfaceTable *)table didSelectRowAtIndexPath:(TGIndexPath *)indexPath
{
[self dismissController];
if (_context.completionBlock != nil)
_context.completionBlock([_venueModels[indexPath.row] locationAttachment]);
}
+ (NSString *)identifier
{
return TGLocationControllerIdentifier;
}
@end