@@ -26,6 +26,7 @@ @interface ShareViewController ()
2626@property NSURL * payloadFolderURL;
2727@property UIAlertController* alert;
2828@property NSString * attributedContentText;
29+ @property NSUInteger unprocessed;
2930@end
3031
3132@implementation ShareViewController
@@ -68,7 +69,7 @@ - (NSArray*)getSendableAttachments {
6869 return [self isWebURL: a];
6970 }];
7071 if (item) {
71- [res addObject: item];
72+ [res addObject: item];
7273 }
7374 if ([res count ] == 0 ) {
7475 item = [self firstSatisfiesTypeIdentifierCond: attachments cond: ^(NSItemProvider * a) {
@@ -90,7 +91,7 @@ - (NSArray*)getSendableAttachments {
9091}
9192
9293- (void )didReceiveMemoryWarning {
93- [super didReceiveMemoryWarning ];
94+ [super didReceiveMemoryWarning ];
9495}
9596
9697- (void ) openApp {
@@ -105,16 +106,22 @@ - (void) openApp {
105106 }
106107}
107108
108- - (void ) maybeCompleteRequest : (BOOL )lastItem {
109- if (!lastItem) { return ; }
109+ - (void ) startProcessingAlreadyInMainThread : (NSInteger )items {
110+ self.unprocessed = items;
111+ }
112+
113+ - (void ) completeRequestAlreadyInMainThread {
110114 [self writeManifest ];
111- dispatch_async (dispatch_get_main_queue (), ^{
112- [self .alert dismissViewControllerAnimated: true completion: ^{
113- [self .extensionContext completeRequestReturningItems: nil completionHandler: ^(BOOL expired) {
114- [self openApp ];
115- }];
115+ [self .alert dismissViewControllerAnimated: true completion: ^{
116+ [self .extensionContext completeRequestReturningItems: nil completionHandler: ^(BOOL expired) {
117+ [self openApp ];
116118 }];
117- });
119+ }];
120+ }
121+
122+ - (void ) completeProcessingItemAlreadyInMainThread {
123+ if (--self.unprocessed > 0 ) { return ; }
124+ [self completeRequestAlreadyInMainThread ];
118125}
119126
120127- (NSURL *)getIncomingShareFolder {
@@ -149,34 +156,46 @@ - (NSURL*)getManifestFileURL {
149156 return [incomingShareFolderURL URLByAppendingPathComponent: @" manifest.json" ];
150157}
151158
152- - (void )appendManifestType : (NSString *)type originalFileURL : (NSURL *) originalFileURL {
153- [self .manifest addObject: @{
154- @" type" : type,
155- @" originalPath" :[originalFileURL absoluteURL ].path ,
156- }];
159+ - (void )completeItemAndAppendManifestType : (NSString *)type originalFileURL : (NSURL *) originalFileURL {
160+ dispatch_async (dispatch_get_main_queue (), ^{
161+ [self .manifest addObject: @{
162+ @" type" : type,
163+ @" originalPath" :[originalFileURL absoluteURL ].path ,
164+ }];
165+ [self completeProcessingItemAlreadyInMainThread ];
166+ });
157167}
158168
159- - (void )appendManifestType : (NSString *)type originalFileURL : (NSURL *) originalFileURL content : (NSString *)content {
160- [self .manifest addObject: @{
161- @" type" : type,
162- @" originalPath" :[originalFileURL absoluteURL ].path ,
163- @" content" : content,
164- }];
169+ - (void )completeItemAndAppendManifestType : (NSString *)type originalFileURL : (NSURL *) originalFileURL content : (NSString *)content {
170+ dispatch_async (dispatch_get_main_queue (), ^{
171+ [self .manifest addObject: @{
172+ @" type" : type,
173+ @" originalPath" :[originalFileURL absoluteURL ].path ,
174+ @" content" : content,
175+ }];
176+ [self completeProcessingItemAlreadyInMainThread ];
177+ });
165178}
166179
167- - (void )appendManifestType : (NSString *)type originalFileURL : (NSURL *) originalFileURL scaledFileURL : (NSURL *)scaledFileURL thumbnailFileURL : (NSURL *)thumbnailFileURL {
168- [self .manifest addObject: @{
169- @" type" : type,
170- @" originalPath" :[originalFileURL absoluteURL ].path ,
171- @" scaledPath" :[scaledFileURL absoluteURL ].path ,
172- @" thumbnailPath" :[thumbnailFileURL absoluteURL ].path ,
173- }];
180+ - (void )completeItemAndAppendManifestType : (NSString *)type originalFileURL : (NSURL *) originalFileURL scaledFileURL : (NSURL *)scaledFileURL thumbnailFileURL : (NSURL *)thumbnailFileURL {
181+ dispatch_async (dispatch_get_main_queue (), ^{
182+ [self .manifest addObject: @{
183+ @" type" : type,
184+ @" originalPath" :[originalFileURL absoluteURL ].path ,
185+ @" scaledPath" :[scaledFileURL absoluteURL ].path ,
186+ @" thumbnailPath" :[thumbnailFileURL absoluteURL ].path ,
187+ }];
188+ [self completeProcessingItemAlreadyInMainThread ];
189+ });
174190}
175191
176- - (void )appendManifestAndLogErrorWithText : (NSString *)text error : (NSError *)error {
177- [self .manifest addObject: @{
178- @" error" : [NSString stringWithFormat: @" %@ : %@ " , text, error != nil ? error : @" <empty>" ],
179- }];
192+ - (void )completeItemAndAppendManifestAndLogErrorWithText : (NSString *)text error : (NSError *)error {
193+ dispatch_async (dispatch_get_main_queue (), ^{
194+ [self .manifest addObject: @{
195+ @" error" : [NSString stringWithFormat: @" %@ : %@ " , text, error != nil ? error : @" <empty>" ],
196+ }];
197+ [self completeProcessingItemAlreadyInMainThread ];
198+ });
180199}
181200
182201- (NSError *)writeManifest {
@@ -195,41 +214,29 @@ - (void) handleText:(NSString *)text loadError:(NSError *)error {
195214 // But if the text is short enough, we also include it in the manifest so
196215 // GUI can easily pre-fill it into the chat compose box.
197216 if (error != nil ) {
198- [self appendManifestAndLogErrorWithText :@" handleText: load error" error: error];
217+ [self completeItemAndAppendManifestAndLogErrorWithText :@" handleText: load error" error: error];
199218 return ;
200219 }
201220 NSURL * originalFileURL = [self getPayloadURLFromExt: @" txt" ];
202221 [text writeToURL: originalFileURL atomically: true encoding: NSUTF8StringEncoding error: &error];
203222 if (error != nil ){
204- [self appendManifestAndLogErrorWithText :@" handleText: unable to write payload file" error: error];
223+ [self completeItemAndAppendManifestAndLogErrorWithText :@" handleText: unable to write payload file" error: error];
205224 return ;
206225 }
207226 if (text.length < TEXT_LENGTH_THRESHOLD ) {
208- [self appendManifestType :@" text" originalFileURL: originalFileURL content: text];
227+ [self completeItemAndAppendManifestType :@" text" originalFileURL: originalFileURL content: text];
209228 } else {
210- [self appendManifestType :@" text" originalFileURL: originalFileURL];
229+ [self completeItemAndAppendManifestType :@" text" originalFileURL: originalFileURL];
211230 }
212231}
213232
214- - (void ) handleData : (NSData *)data type : (NSString *)type ext : (NSString *)ext {
215- NSURL * originalFileURL = [self getPayloadURLFromExt: ext];
216- BOOL OK = [data writeToURL: originalFileURL atomically: true ];
217- if (!OK ){
218- [self appendManifestAndLogErrorWithText: @" handleData: unable to write payload file" error: nil ];
219- return ;
220- }
221- [self appendManifestType: type originalFileURL: originalFileURL];
222- }
223-
224- - (void ) handleAndMaybeCompleteMediaFile : (NSURL *)url isVideo : (BOOL )isVideo lastItem : (BOOL )lastItem {
233+ - (void ) handleAndCompleteMediaFile : (NSURL *)url isVideo : (BOOL )isVideo {
225234 ProcessMediaCompletion completion = ^(NSError * error, NSURL * scaled, NSURL * thumbnail) {
226235 if (error != nil ) {
227- [self appendManifestAndLogErrorWithText: @" handleAndMaybeCompleteMediaFile" error: error];
228- [self maybeCompleteRequest: lastItem];
236+ [self completeItemAndAppendManifestAndLogErrorWithText: @" handleAndCompleteMediaFile" error: error];
229237 return ;
230238 }
231- [self appendManifestType: isVideo ? @" video" : @" image" originalFileURL: url scaledFileURL: scaled thumbnailFileURL: thumbnail];
232- [self maybeCompleteRequest: lastItem];
239+ [self completeItemAndAppendManifestType: isVideo ? @" video" : @" image" originalFileURL: url scaledFileURL: scaled thumbnailFileURL: thumbnail];
233240 };
234241 if (isVideo) {
235242 [MediaUtils processVideoFromOriginal: url completion: completion];
@@ -239,76 +246,72 @@ - (void) handleAndMaybeCompleteMediaFile:(NSURL *)url isVideo:(BOOL)isVideo last
239246}
240247
241248// processItem will invokve the correct function on the Go side for the given attachment type.
242- - (void )processItem : (NSItemProvider *)item lastItem : ( BOOL ) lastItem {
249+ - (void )processItem : (NSItemProvider *)item {
243250
244251 NSItemProviderCompletionHandler urlHandler = ^(NSURL * url, NSError * error) {
245252 if (self.attributedContentText != nil ){
246253 [self handleText: [NSString stringWithFormat: @" %@ %@ " , self .attributedContentText, url.absoluteString] loadError: error];
247254 }else {
248255 [self handleText: url.absoluteString loadError: error];
249256 }
250- [self maybeCompleteRequest: lastItem];
251257 };
252258
253259 NSItemProviderCompletionHandler textHandler = ^(NSString * text, NSError * error) {
254260 [self handleText: text loadError: error];
255- [self maybeCompleteRequest: lastItem];
256261 };
257262
258263 NSItemProviderCompletionHandler imageHandler = ^(UIImage* image, NSError * error) {
259264 if (error != nil ) {
260- [self appendManifestAndLogErrorWithText: @" imageHandler: load error" error: error];
261- [self maybeCompleteRequest: lastItem];
265+ [self completeItemAndAppendManifestAndLogErrorWithText: @" imageHandler: load error" error: error];
262266 return ;
263267 }
264268 NSData * imageData = UIImageJPEGRepresentation (image, .85 );
265269 NSURL * originalFileURL = [self getPayloadURLFromExt: @" jpg" ];
266270 BOOL OK = [imageData writeToURL: originalFileURL atomically: true ];
267271 if (!OK ){
268- [self appendManifestAndLogErrorWithText :@" handleData: unable to write payload file" error: nil ];
272+ [self completeItemAndAppendManifestAndLogErrorWithText :@" handleData: unable to write payload file" error: nil ];
269273 return ;
270274 }
271- [self handleAndMaybeCompleteMediaFile : originalFileURL isVideo: false lastItem: lastItem ];
275+ [self handleAndCompleteMediaFile : originalFileURL isVideo: false ];
272276 };
273277
274278 // The NSItemProviderCompletionHandler interface is a little tricky. The caller of our handler
275279 // will inspect the arguments that we have given, and will attempt to give us the attachment
276280 // in this form. For files, we always want a file URL, and so that is what we pass in.
277281 NSItemProviderCompletionHandler fileHandler = ^(NSURL * url, NSError * error) {
282+ if (error != nil ) {
283+ [self completeItemAndAppendManifestAndLogErrorWithText: @" fileHandler: load error" error: error];
284+ return ;
285+ }
286+
278287 BOOL hasImage = [item hasItemConformingToTypeIdentifier: @" public.image" ];
279288 BOOL hasVideo = [item hasItemConformingToTypeIdentifier: @" public.movie" ];
280-
289+
281290 // Check for no URL (it might have not been possible for the OS to give us one)
282291 if (url == nil ) {
283292 if (hasImage) {
284293 // Try to handle with our imageHandler function
285294 [item loadItemForTypeIdentifier: @" public.image" options: nil completionHandler: imageHandler];
286295 } else {
287- [self maybeCompleteRequest: lastItem ];
296+ [self completeItemAndAppendManifestAndLogErrorWithText: @" fileHandler: no url or image " error: nil ];
288297 }
289298 return ;
290299 }
291- if (error != nil ) {
292- [self appendManifestAndLogErrorWithText: @" fileHandler: load error" error: error];
293- [self maybeCompleteRequest: lastItem];
294- return ;
295- }
300+
296301 NSURL * filePayloadURL = [self getPayloadURLFromURL: url];
297302 [[NSFileManager defaultManager ] copyItemAtURL: url toURL: filePayloadURL error: &error];
298303 if (error != nil ) {
299- [self appendManifestAndLogErrorWithText: @" fileHandler: copy error" error: error];
300- [self maybeCompleteRequest: lastItem];
304+ [self completeItemAndAppendManifestAndLogErrorWithText: @" fileHandler: copy error" error: error];
301305 return ;
302306 }
303307
304308
305309 if (hasVideo) {
306- [self handleAndMaybeCompleteMediaFile : filePayloadURL isVideo: true lastItem: lastItem ];
310+ [self handleAndCompleteMediaFile : filePayloadURL isVideo: true ];
307311 } else if (hasImage) {
308- [self handleAndMaybeCompleteMediaFile : filePayloadURL isVideo: false lastItem: lastItem ];
312+ [self handleAndCompleteMediaFile : filePayloadURL isVideo: false ];
309313 } else {
310- [self appendManifestType: @" file" originalFileURL: filePayloadURL];
311- [self maybeCompleteRequest: lastItem];
314+ [self completeItemAndAppendManifestType: @" file" originalFileURL: filePayloadURL];
312315 }
313316 };
314317
@@ -328,37 +331,37 @@ - (void)processItem:(NSItemProvider*)item lastItem:(BOOL)lastItem {
328331 } else {
329332 [[[PushNotifier alloc ] init ] localNotification: @" extension" msg: @" We failed to send your message. Please try from the Keybase app."
330333 badgeCount: -1 soundName: @" default" convID: @" " typ: @" chat.extension" ];
331- [self maybeCompleteRequest: lastItem ];
334+ [self completeItemAndAppendManifestAndLogErrorWithText: @" unknown type " error: nil ];
332335 }
333336}
334337
335338- (void )showProgressView {
336339 self.alert = [UIAlertController
337- alertControllerWithTitle: @" Working on it"
338- message: @" Preparing content for sharing into Keybase."
339- preferredStyle: UIAlertControllerStyleAlert];
340+ alertControllerWithTitle: @" Working on it"
341+ message: @" Preparing content for sharing into Keybase."
342+ preferredStyle: UIAlertControllerStyleAlert];
340343 UIActivityIndicatorView* spinner = [[UIActivityIndicatorView alloc ] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleGray];
341344 [spinner setTranslatesAutoresizingMaskIntoConstraints: NO ];
342345 [self .alert.view addConstraints: @[
343- [NSLayoutConstraint constraintWithItem: spinner
344- attribute: NSLayoutAttributeCenterX
345- relatedBy: NSLayoutRelationEqual
346- toItem: self .alert.view
347- attribute: NSLayoutAttributeCenterX
348- multiplier: 1 constant: 0 ],
349- [NSLayoutConstraint constraintWithItem: spinner
350- attribute: NSLayoutAttributeCenterY
351- relatedBy: NSLayoutRelationEqual
352- toItem: self .alert.view
353- attribute: NSLayoutAttributeCenterY
354- multiplier: 1 constant: 40 ],
355- [NSLayoutConstraint constraintWithItem: self .alert.view
356- attribute: NSLayoutAttributeBottom
357- relatedBy: NSLayoutRelationEqual
358- toItem: spinner
359- attribute: NSLayoutAttributeBottom
360- multiplier: 1 constant: 10 ]
361- ]
346+ [NSLayoutConstraint constraintWithItem: spinner
347+ attribute: NSLayoutAttributeCenterX
348+ relatedBy: NSLayoutRelationEqual
349+ toItem: self .alert.view
350+ attribute: NSLayoutAttributeCenterX
351+ multiplier: 1 constant: 0 ],
352+ [NSLayoutConstraint constraintWithItem: spinner
353+ attribute: NSLayoutAttributeCenterY
354+ relatedBy: NSLayoutRelationEqual
355+ toItem: self .alert.view
356+ attribute: NSLayoutAttributeCenterY
357+ multiplier: 1 constant: 40 ],
358+ [NSLayoutConstraint constraintWithItem: self .alert.view
359+ attribute: NSLayoutAttributeBottom
360+ relatedBy: NSLayoutRelationEqual
361+ toItem: spinner
362+ attribute: NSLayoutAttributeBottom
363+ multiplier: 1 constant: 10 ]
364+ ]
362365 ];
363366
364367 [self .alert.view addSubview: spinner];
@@ -381,14 +384,14 @@ - (void)ensureManifestAndPayloadFolder {
381384- (void )viewDidLoad {
382385 NSArray * items = [self getSendableAttachments ];
383386 if ([items count ] == 0 ) {
384- [self maybeCompleteRequest: YES ];
387+ [self completeRequestAlreadyInMainThread ];
385388 return ;
386389 }
390+ [self startProcessingAlreadyInMainThread: [items count ]];
387391 [self showProgressView ];
388392 [self ensureManifestAndPayloadFolder ];
389393 for (int i = 0 ; i < [items count ]; i++) {
390- BOOL lastItem = (BOOL )(i == [items count ]-1 );
391- [self processItem: items[i] lastItem: lastItem];
394+ [self processItem: items[i]];
392395 }
393396}
394397
0 commit comments