forked from overtake/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TGSModalSenderView.m
363 lines (216 loc) · 11 KB
/
TGSModalSenderView.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//
// TGSModalSenderView.m
// Telegram
//
// Created by keepcoder on 07.05.15.
// Copyright (c) 2015 keepcoder. All rights reserved.
//
#import "TGSModalSenderView.h"
#import "UploadOperation.h"
#import "ImageUtils.h"
#import "NSViewCategory.h"
#import "TGLinearProgressView.h"
#import "BTRButton.h"
#import "ShareViewController.h"
@interface TGSModalSenderView ()
{
int _ts_count;
int _cs_count;
int _sent_count;
NSMutableArray *_operationsAndRequests;
NSMutableDictionary *_uploadedFileHash;
}
@property (nonatomic,strong) TMView *backgroundView;
@property (nonatomic,strong) TMView *containerView;
@property (nonatomic,copy) dispatch_block_t completionHandler;
@property (nonatomic,copy) dispatch_block_t errorHandler;
@property (nonatomic,strong) NSArray *rowItems;
@property (nonatomic,strong) TGLinearProgressView *progressView;
@property (nonatomic,strong) NSTextField *sharingTextField;
@property (nonatomic,strong) BTRButton *cancelButton;
@end
@implementation TGSModalSenderView
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
// Drawing code here.
}
-(instancetype)initWithFrame:(NSRect)frameRect {
if(self = [super initWithFrame:frameRect]) {
_backgroundView = [[TMView alloc] initWithFrame:frameRect];
_backgroundView.wantsLayer = YES;
_backgroundView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
_backgroundView.layer.backgroundColor = NSColorFromRGBWithAlpha(0x000000, 0.2).CGColor;
_containerView = [[TMView alloc] initWithFrame:NSMakeRect(0, 0, 260, 100)];
_containerView.wantsLayer = YES;
_containerView.layer.backgroundColor = [NSColor whiteColor].CGColor;
_containerView.layer.cornerRadius = 4;
_containerView.layer.borderColor = DIALOG_BORDER_COLOR.CGColor;
[_containerView setCenterByView:self];
[self addSubview:_backgroundView];
[self addSubview:_containerView];
_cancelButton = [[BTRButton alloc] initWithFrame:NSMakeRect(0, 0, NSWidth(_containerView.frame), 50)];
_cancelButton.layer.backgroundColor = [NSColor whiteColor].CGColor;
[_cancelButton setTitleColor:LINK_COLOR forControlState:BTRControlStateNormal];
[_cancelButton setTitle:NSLocalizedString(@"Cancel", nil) forControlState:BTRControlStateNormal];
weak();
[_cancelButton addBlock:^(BTRControlEvents events) {
[weakSelf cleanup];
[ShareViewController close];
} forControlEvents:BTRControlEventClick];
TMView *topSeparator = [[TMView alloc] initWithFrame:NSMakeRect(0, 49, NSWidth(_containerView.frame), DIALOG_BORDER_WIDTH)];
topSeparator.backgroundColor = DIALOG_BORDER_COLOR;
[self.containerView addSubview:_cancelButton];
[self.containerView addSubview:topSeparator];
_sharingTextField = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, NSWidth(_containerView.frame) - 20, 20)];
[_sharingTextField setCenterByView:_containerView];
[_sharingTextField setEditable:NO];
[_sharingTextField setSelectable:NO];
[_sharingTextField setDrawsBackground:NO];
[_sharingTextField setBordered:NO];
[_sharingTextField setAlignment:NSCenterTextAlignment];
[_sharingTextField setFont:TGSystemFont(13)];
[_sharingTextField setStringValue:@"Sharing..."];
[_sharingTextField setFrameOrigin:NSMakePoint(NSMinX(_sharingTextField.frame), NSHeight(_containerView.frame) - 30)];
[_containerView addSubview:_sharingTextField];
_progressView = [[TGLinearProgressView alloc] initWithFrame:NSMakeRect(0, 30, NSWidth(_containerView.frame), 3)];
[_progressView setCenterByView:_containerView];
[_containerView addSubview:_progressView];
}
return self;
}
-(void)sendItems:(NSArray *)shareItems rowItems:(NSArray *)rowItems completionHandler:(dispatch_block_t)completionHandler errorHandler:(dispatch_block_t)errorHandler {
_completionHandler = completionHandler;
_errorHandler = errorHandler;
_rowItems = rowItems;
_uploadedFileHash = [[NSMutableDictionary alloc] init];
_operationsAndRequests = [[NSMutableArray alloc] init];
NSExtensionItem *obj = shareItems.firstObject;
_ts_count = (int) obj.attachments.count * (int) rowItems.count;
if(obj.attributedContentText.length > 0) {
_ts_count+= 1 * (int) rowItems.count;
[_rowItems enumerateObjectsUsingBlock:^(id rowItem, NSUInteger idx, BOOL *stop) {
[self sendAsMessage:obj.attributedContentText.string rowItem:rowItem];
}];
}
[obj.attachments enumerateObjectsUsingBlock:^(NSItemProvider *itemProvider, NSUInteger idx, BOOL *stop) {
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeURL]) {
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error) {
[_rowItems enumerateObjectsUsingBlock:^(id rowItem, NSUInteger idx, BOOL *stop) {
if(![url isFileURL]) {
[self sendAsMessage:url.absoluteString rowItem:rowItem];
} else {
[self sendAsMedia:url.path rowItem:rowItem];
}
}];
}];
}
}];
}
-(void)update {
if(_ts_count == _sent_count) {
[self.progressView setProgress:100 animated:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
_completionHandler();
});
}
}
-(void)error {
[self cleanup];
_errorHandler();
}
-(void)cleanup {
[_operationsAndRequests enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if([obj isKindOfClass:[TGS_RPCRequest class]]) {
[obj cancelRequest];
} else if([obj isKindOfClass:[UploadOperation class]]) {
[obj cancel];
}
}];
[_operationsAndRequests removeAllObjects];
}
-(void)cancel {
[self cleanup];
_completionHandler();
}
-(void)updateProgress:(int)progress {
assert([NSThread isMainThread]);
int part_progress = ceil(100/_ts_count);
int current_progress = (part_progress * _cs_count)+ (progress / 100 * part_progress);
[_progressView setProgress:current_progress animated:YES];
}
-(void)performMediaRequest:(id)media rowItem:(TGS_ConversationRowItem *)rowItem path:(NSString *)path {
_cs_count++;
id request = [TGS_RPCRequest sendRequest:[TLAPI_messages_sendMedia createWithFlags:0 peer:[self inputPeer:rowItem] reply_to_msg_id:0 media:media random_id:rand_long() reply_markup:nil] successHandler:^(id request, TLUpdates *response) {
_sent_count++;
[self update];
} errorHandler:^(id request, RpcError *error) {
[self error];
}];
[_operationsAndRequests addObject:request];
}
-(void)sendAsMedia:(NSString *)path rowItem:(TGS_ConversationRowItem *)rowItem {
[ASQueue dispatchOnStageQueue:^{
UploadOperation *operation = [[UploadOperation alloc] init];
[operation setUploadComplete:^(UploadOperation *operation, id file) {
[self updateProgress:100];
id media;
if(operation.uploadType == UploadImageType)
media = [TL_inputMediaUploadedPhoto createWithFile:file caption:@""];
else
media = [TL_inputMediaUploadedDocument createWithFile:file mime_type:mimetypefromExtension([path pathExtension]) attributes:[@[[TL_documentAttributeFilename createWithFile_name:[path lastPathComponent]]] mutableCopy]];
[self performMediaRequest:media rowItem:rowItem path:path];
}];
[operation setUploaderRequestFileHash:^id(UploadOperation *o) {
return _uploadedFileHash[fileMD5(o.filePath)];
}];
[operation setUploaderNeedSaveFileHash:^(UploadOperation *o, id file) {
_uploadedFileHash[fileMD5(o.filePath)] = file;
}];
[operation setUploadProgress:^(UploadOperation *operation, NSUInteger current, NSUInteger total) {
[self updateProgress:(int)current/(int)total*100];
}];
if([imageTypes() indexOfObject:[path pathExtension]] != NSNotFound && fileSize(path) <= 10*1014*1024) {
NSImage *originImage = imageFromFile(path);
originImage = prettysize(originImage);
if(originImage.size.width / 10 > originImage.size.height) {
[operation setFilePath:path];
[operation ready:UploadDocumentType];
return;
}
originImage = strongResize(originImage, 1280);
NSData *imageData = jpegNormalizedData(originImage);
[operation setFileData:imageData];
[operation ready:UploadImageType];
} else {
[operation setFilePath:path];
[operation ready:UploadDocumentType];
}
[_operationsAndRequests addObject:operation];
}];
}
-(void)sendAsMessage:(NSString *)message rowItem:(TGS_ConversationRowItem *)rowItem {
[ASQueue dispatchOnMainQueue:^{
[self updateProgress:30];
}];
id request = [TGS_RPCRequest sendRequest:[TLAPI_messages_sendMessage createWithFlags:0 peer:[self inputPeer:rowItem] reply_to_msg_id:0 message:message random_id:rand_long() reply_markup:nil entities:nil] successHandler:^(TGS_RPCRequest *request, id response) {
_cs_count++;
_sent_count++;
[self updateProgress:100];
[self update];
} errorHandler:^(TGS_RPCRequest *request, RpcError *error) {
[self error];
}];
[_operationsAndRequests addObject:request];
}
-(id)inputPeer:(TGS_ConversationRowItem *)rowItem {
id input;
if(rowItem.chat.n_id != 0) {
input = [TL_inputPeerChat createWithChat_id:rowItem.chat.n_id];
} else {
return [TL_inputPeerUser createWithUser_id:rowItem.user.n_id access_hash:rowItem.user.access_hash];
}
if(!input)
return [TL_inputPeerEmpty create];
return input;
}
@end