From 68476a51dc4dfbb76b74d27ed13033699d61d6c2 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Tue, 18 Apr 2017 13:50:01 -0400 Subject: [PATCH] Download service emits progress notifications // FREEBIE --- .../Attachments/OWSAttachmentsProcessor.h | 8 +++++- .../Attachments/OWSAttachmentsProcessor.m | 26 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Messages/Attachments/OWSAttachmentsProcessor.h b/src/Messages/Attachments/OWSAttachmentsProcessor.h index 9c88e4e04..1fb3ef4f2 100644 --- a/src/Messages/Attachments/OWSAttachmentsProcessor.h +++ b/src/Messages/Attachments/OWSAttachmentsProcessor.h @@ -1,7 +1,13 @@ -// Copyright © 2016 Open Whisper Systems. All rights reserved. +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// NS_ASSUME_NONNULL_BEGIN +extern NSString *const kAttachmentDownloadProgressNotification; +extern NSString *const kAttachmentDownloadProgressKey; +extern NSString *const kAttachmentDownloadAttachmentIDKey; + @class TSMessage; @class TSThread; @class TSNetworkManager; diff --git a/src/Messages/Attachments/OWSAttachmentsProcessor.m b/src/Messages/Attachments/OWSAttachmentsProcessor.m index 87a869a42..05a526951 100644 --- a/src/Messages/Attachments/OWSAttachmentsProcessor.m +++ b/src/Messages/Attachments/OWSAttachmentsProcessor.m @@ -20,6 +20,14 @@ NS_ASSUME_NONNULL_BEGIN +NSString *const kAttachmentDownloadProgressNotification = @"kAttachmentDownloadProgressNotification"; +NSString *const kAttachmentDownloadProgressKey = @"kAttachmentDownloadProgressKey"; +NSString *const kAttachmentDownloadAttachmentIDKey = @"kAttachmentDownloadAttachmentIDKey"; + +// Use a slightly non-zero value to ensure that the progress +// indicator shows up as quickly as possible. +static const CGFloat kAttachmentDownloadProgressTheta = 0.001f; + @interface OWSAttachmentsProcessor () @property (nonatomic, readonly) TSNetworkManager *networkManager; @@ -259,7 +267,10 @@ NS_ASSUME_NONNULL_BEGIN abortDownload(); return; } - + + [self fireProgressNotification:MAX(kAttachmentDownloadProgressTheta, progress.fractionCompleted) + attachmentId:pointer.uniqueId]; + // We only need to check the content length header once. if (hasCheckedContentLength) { return; @@ -322,6 +333,19 @@ NS_ASSUME_NONNULL_BEGIN }]; } +- (void)fireProgressNotification:(CGFloat)progress attachmentId:(NSString *)attachmentId +{ + dispatch_async(dispatch_get_main_queue(), ^{ + NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter]; + [notificationCenter postNotificationName:kAttachmentDownloadProgressNotification + object:nil + userInfo:@{ + kAttachmentDownloadProgressKey : @(progress), + kAttachmentDownloadAttachmentIDKey : attachmentId + }]; + }); +} + - (void)setAttachment:(TSAttachmentPointer *)pointer isDownloadingInMessage:(nullable TSMessage *)message { pointer.state = TSAttachmentPointerStateDownloading;