Skip to content

Commit

Permalink
Added incoming percent complete
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchiles committed Apr 27, 2015
1 parent 7dbc2c9 commit 1804e90
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 55 deletions.
Binary file modified ChatSecure/Base.lproj/Localizable.strings
Binary file not shown.
119 changes: 71 additions & 48 deletions ChatSecure/Classes/Controllers/OTREncryptionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,42 @@ - (void)dataHandler:(OTRDataHandler*)dataHandler

// for now, just accept all incoming files
[dataHandler startIncomingTransfer:transfer];
//Create placeholder for updating progress

OTRMessage *newMessage = [((OTRMessage *)transfer.tag) copy];
newMessage.transportedSecurely = YES;
newMessage.text = nil;

NSRange imageRange = [transfer.mimeType rangeOfString:@"image"];
NSRange audioRange = [transfer.mimeType rangeOfString:@"audio"];
NSRange videoRange = [transfer.mimeType rangeOfString:@"video"];

OTRMediaItem *mediaItem = nil;
if(audioRange.location == 0) {
mediaItem = [[OTRAudioItem alloc] init];

} else if (imageRange.location == 0) {
mediaItem = [[OTRImageItem alloc] init];

} else if (videoRange.location == 0) {
mediaItem = [[OTRVideoItem alloc] init];
}

mediaItem.filename = transfer.fileName;
mediaItem.isIncoming = YES;
newMessage.mediaItemUniqueId = mediaItem.uniqueId;

if ([[OTRAppDelegate appDelegate].messagesViewController otr_isVisible] && [[OTRAppDelegate appDelegate].messagesViewController.buddy.uniqueId isEqualToString:newMessage.buddyUniqueId])
{
newMessage.read = YES;
}

[[OTRDatabaseManager sharedInstance].readWriteDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[newMessage saveWithTransaction:transaction];
[mediaItem saveWithTransaction:transaction];
}];


}

- (void)dataHandler:(OTRDataHandler*)dataHandler
Expand All @@ -434,7 +470,8 @@ - (void)dataHandler:(OTRDataHandler*)dataHandler

[[OTRDatabaseManager sharedInstance].readWriteDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
OTRMessage *tagMessage = transfer.tag;
OTRMediaItem *mediaItem = [OTRMediaItem fetchObjectWithUniqueID:tagMessage.mediaItemUniqueId transaction:transaction];
OTRMessage *databaseMessage = [OTRMessage fetchObjectWithUniqueID:tagMessage.uniqueId transaction:transaction];
OTRMediaItem *mediaItem = [OTRMediaItem fetchObjectWithUniqueID:databaseMessage.mediaItemUniqueId transaction:transaction];
mediaItem.transferProgress = progress;
[mediaItem saveWithTransaction:transaction];
[mediaItem touchParentMessageWithTransaction:transaction];
Expand All @@ -454,74 +491,60 @@ - (void)dataHandler:(OTRDataHandler*)dataHandler
}];
}
else if ([transfer isKindOfClass:[OTRDataIncomingTransfer class]]) {
NSRange imageRange = [transfer.mimeType rangeOfString:@"image"];
NSRange audioRange = [transfer.mimeType rangeOfString:@"audio"];
NSRange videoRange = [transfer.mimeType rangeOfString:@"video"];

OTRMessage *parentMessage = transfer.tag;
__block OTRMessage *tagMessage = transfer.tag;

OTRMessage *message = [[OTRMessage alloc] init];
message.incoming = YES;
message.buddyUniqueId = parentMessage.buddyUniqueId;
message.transportedSecurely = YES;
__block OTRMessage *message = nil;
__block OTRMediaItem *mediaItem = nil;

if ([[OTRAppDelegate appDelegate].messagesViewController otr_isVisible] && [[OTRAppDelegate appDelegate].messagesViewController.buddy.uniqueId isEqualToString:message.buddyUniqueId])
{
message.read = YES;
}
[[OTRDatabaseManager sharedInstance].readWriteDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
message = [OTRMessage fetchObjectWithUniqueID:tagMessage.uniqueId transaction:transaction];
mediaItem = [OTRMediaItem fetchObjectWithUniqueID:message.mediaItemUniqueId transaction:transaction];
}];

if (imageRange.location != NSNotFound) {
mediaItem.transferProgress = 1;

if ([mediaItem isKindOfClass:[OTRAudioItem class]]) {
OTRAudioItem *audioItem = (OTRAudioItem *)mediaItem;

[[OTRMediaFileManager sharedInstance] setData:transfer.fileData forItem:audioItem buddyUniqueId:message.buddyUniqueId completion:^(NSInteger bytesWritten, NSError *error) {

NSURL *url = [[OTRMediaServer sharedInstance] urlForMediaItem:audioItem buddyUniqueId:message.buddyUniqueId];
AVURLAsset *audioAsset = [AVURLAsset assetWithURL:url];
audioItem.timeLength = CMTimeGetSeconds(audioAsset.duration);

[[OTRDatabaseManager sharedInstance].readWriteDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[audioItem saveWithTransaction:transaction];
[message saveWithTransaction:transaction];
}];


} completionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];

} else if ([mediaItem isKindOfClass:[OTRImageItem class]]) {
OTRImageItem *imageItem = (OTRImageItem *)mediaItem;

UIImage *tempImage = [UIImage imageWithData:transfer.fileData];
OTRImageItem *imageItem = [[OTRImageItem alloc] init];
imageItem.width = tempImage.size.width;
imageItem.height = tempImage.size.height;
imageItem.isIncoming = YES;
imageItem.filename = transfer.fileName;

message.mediaItemUniqueId = imageItem.uniqueId;

[[OTRDatabaseManager sharedInstance].readWriteDatabaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[message saveWithTransaction:transaction];
[imageItem saveWithTransaction:transaction];
} completionBlock:^{
[[OTRMediaFileManager sharedInstance] setData:transfer.fileData forItem:imageItem buddyUniqueId:parentMessage.buddyUniqueId completion:^(NSInteger bytesWritten, NSError *error) {
[[OTRMediaFileManager sharedInstance] setData:transfer.fileData forItem:imageItem buddyUniqueId:message.buddyUniqueId completion:^(NSInteger bytesWritten, NSError *error) {
[imageItem touchParentMessage];
} completionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
}];
}
else if (audioRange.location != NSNotFound) {


OTRAudioItem *audioItem = [[OTRAudioItem alloc] init];
audioItem.filename = transfer.fileName;
audioItem.isIncoming = YES;
} else if ([mediaItem isKindOfClass:[OTRVideoItem class]]) {
OTRVideoItem *videoItem = (OTRVideoItem *)mediaItem;

message.mediaItemUniqueId = audioItem.uniqueId;

[[OTRMediaFileManager sharedInstance] setData:transfer.fileData forItem:audioItem buddyUniqueId:parentMessage.buddyUniqueId completion:^(NSInteger bytesWritten, NSError *error) {

NSURL *url = [[OTRMediaServer sharedInstance] urlForMediaItem:audioItem buddyUniqueId:parentMessage.buddyUniqueId];
AVURLAsset *audioAsset = [AVURLAsset assetWithURL:url];
audioItem.timeLength = CMTimeGetSeconds(audioAsset.duration);

[[OTRDatabaseManager sharedInstance].readWriteDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[audioItem saveWithTransaction:transaction];
[message saveWithTransaction:transaction];
}];
[[OTRMediaFileManager sharedInstance] setData:transfer.fileData forItem:videoItem buddyUniqueId:message.buddyUniqueId completion:^(NSInteger bytesWritten, NSError *error) {


} completionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];

}
else if (videoRange.location != NSNotFound) {
OTRVideoItem *videoItem =[[OTRVideoItem alloc] init];
videoItem.filename = transfer.fileName;
videoItem.isIncoming = YES;

message.mediaItemUniqueId = videoItem.uniqueId;

[[OTRMediaFileManager sharedInstance] setData:transfer.fileData forItem:videoItem buddyUniqueId:parentMessage.buddyUniqueId completion:^(NSInteger bytesWritten, NSError *error) {

[[OTRDatabaseManager sharedInstance].readWriteDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[videoItem saveWithTransaction:transaction];
[message saveWithTransaction:transaction];
Expand Down
26 changes: 19 additions & 7 deletions ChatSecure/Classes/View Controllers/OTRMessagesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ - (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionVi

[attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:iconString attributes:iconAttributes]];
}
else if([message isMediaMessage] && ![message isIncoming]) {
else if([message isMediaMessage]) {

__block OTRMediaItem *mediaItem = nil;
//Get the media item
Expand All @@ -1148,16 +1148,28 @@ - (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionVi
float percentProgress = mediaItem.transferProgress * 100;

NSString *progressString = nil;
NSUInteger insertIndex = 0;

if(percentProgress > 0) {
progressString = [NSString stringWithFormat:@"%@ %.0f%% ",SENDING_STRING,percentProgress];
} else {
progressString = [NSString stringWithFormat:@"%@ ",WAITING_STRING];
if (mediaItem.isIncoming && mediaItem.transferProgress < 1) {
progressString = [NSString stringWithFormat:@" %@ %.0f%%",INCOMING_STRING,percentProgress];
insertIndex = [attributedString length];
} else if (!mediaItem.isIncoming) {
if(percentProgress > 0) {
progressString = [NSString stringWithFormat:@"%@ %.0f%% ",SENDING_STRING,percentProgress];
} else {
progressString = [NSString stringWithFormat:@"%@ ",WAITING_STRING];
}
}

if ([progressString length]) {
UIFont *font = [UIFont systemFontOfSize:12];
[attributedString insertAttributedString:[[NSAttributedString alloc] initWithString:progressString attributes:@{NSFontAttributeName: font}] atIndex:insertIndex];
}
UIFont *font = [UIFont systemFontOfSize:12];
[attributedString insertAttributedString:[[NSAttributedString alloc] initWithString:progressString attributes:@{NSFontAttributeName: font}] atIndex:0];


}


return attributedString;
}

Expand Down
1 change: 1 addition & 0 deletions ChatSecure/Strings/Strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
#define RECENT_STRING [OTRLanguageManager translatedString: @"Recent"]
#define errSSLBufferOverflowString [OTRLanguageManager translatedString: @"Insufficient buffer provided"]
#define COPY_STRING [OTRLanguageManager translatedString: @"Copy"]
#define INCOMING_STRING [OTRLanguageManager translatedString: @"Incoming"]
#define CONNECTING_STRING [OTRLanguageManager translatedString: @"Connecting"]
#define LOCKED_WARN_STRING [OTRLanguageManager translatedString: @"The fingerprint has not been verified"]
#define UNLOCK_STRING [OTRLanguageManager translatedString: @"Unlock"]
Expand Down
4 changes: 4 additions & 0 deletions ChatSecure/Strings/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -970,5 +970,9 @@
"SENDING_STRING" : {
"comment": "Label text for when a transfer is in progress (normally followed by a percent value 34%)",
"string": "Sending"
},
"INCOMING_STRING" : {
"comment": "Label for incoming data transfers",
"string": "Incoming"
}
}

0 comments on commit 1804e90

Please sign in to comment.