mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-05-04 15:07:43 +02:00
Add the upload statistic
This commit is contained in:
@@ -143,28 +143,8 @@ NSString *const kMediaLoaderProgressDownloadRateKey = @"kMediaLoaderProgressDown
|
||||
|
||||
NSString* progressString = [NSString stringWithFormat:@"%@ / %@", [NSByteCountFormatter stringFromByteCount:downloadData.length countStyle:NSByteCountFormatterCountStyleFile], [NSByteCountFormatter stringFromByteCount:expectedSize countStyle:NSByteCountFormatterCountStyleFile]];
|
||||
[dict setValue:progressString forKey:kMediaLoaderProgressStringKey];
|
||||
|
||||
NSMutableString* remaingTimeStr = [[NSMutableString alloc] init];
|
||||
|
||||
if (dataRemainingTime < 1) {
|
||||
[remaingTimeStr appendString:@"< 1s"];
|
||||
} else if (dataRemainingTime < 60)
|
||||
{
|
||||
[remaingTimeStr appendFormat:@"%ds", (int)dataRemainingTime];
|
||||
}
|
||||
else if (dataRemainingTime < 3600)
|
||||
{
|
||||
[remaingTimeStr appendFormat:@"%dm %2ds", (int)(dataRemainingTime/60), ((int)dataRemainingTime) % 60];
|
||||
}
|
||||
else if (dataRemainingTime >= 3600)
|
||||
{
|
||||
[remaingTimeStr appendFormat:@"%dh %dm %ds", (int)(dataRemainingTime / 3600),
|
||||
((int)(dataRemainingTime) % 3600) / 60,
|
||||
(int)(dataRemainingTime) % 60];
|
||||
}
|
||||
[remaingTimeStr appendString:@" left"];
|
||||
|
||||
[dict setValue:remaingTimeStr forKey:kMediaLoaderProgressRemaingTimeKey];
|
||||
|
||||
[dict setValue:[MediaManager formatSecondsInterval:dataRemainingTime] forKey:kMediaLoaderProgressRemaingTimeKey];
|
||||
|
||||
NSString* downloadRateStr = [NSString stringWithFormat:@"%@/s", [NSByteCountFormatter stringFromByteCount:meanRate * 1024 countStyle:NSByteCountFormatterCountStyleFile]];
|
||||
[dict setValue:downloadRateStr forKey:kMediaLoaderProgressDownloadRateKey];
|
||||
|
||||
@@ -28,6 +28,8 @@ extern NSString *const kMediaDownloadDidFailNotification;
|
||||
|
||||
+ (id)sharedInstance;
|
||||
|
||||
+ (NSString*)formatSecondsInterval:(CGFloat)secondsInterval;
|
||||
|
||||
+ (UIImage *)resize:(UIImage *)image toFitInSize:(CGSize)size;
|
||||
|
||||
// Load a picture from the local cache (Do not start any remote requests)
|
||||
|
||||
@@ -39,6 +39,30 @@ static NSMutableDictionary* pendingMediaLoadersByURL = nil;
|
||||
return sharedMediaManager;
|
||||
}
|
||||
|
||||
+ (NSString*)formatSecondsInterval:(CGFloat)secondsInterval {
|
||||
NSMutableString* formattedString = [[NSMutableString alloc] init];
|
||||
|
||||
if (secondsInterval < 1) {
|
||||
[formattedString appendString:@"< 1s"];
|
||||
} else if (secondsInterval < 60)
|
||||
{
|
||||
[formattedString appendFormat:@"%ds", (int)secondsInterval];
|
||||
}
|
||||
else if (secondsInterval < 3600)
|
||||
{
|
||||
[formattedString appendFormat:@"%dm %2ds", (int)(secondsInterval/60), ((int)secondsInterval) % 60];
|
||||
}
|
||||
else if (secondsInterval >= 3600)
|
||||
{
|
||||
[formattedString appendFormat:@"%dh %dm %ds", (int)(secondsInterval / 3600),
|
||||
((int)(secondsInterval) % 3600) / 60,
|
||||
(int)(secondsInterval) % 60];
|
||||
}
|
||||
[formattedString appendString:@" left"];
|
||||
|
||||
return formattedString;
|
||||
}
|
||||
|
||||
+ (UIImage *)resize:(UIImage *)image toFitInSize:(CGSize)size {
|
||||
UIImage *resizedImage = image;
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
Copyright 2014 OpenMarket Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface UploadManager : NSObject
|
||||
|
||||
// trigger the kMediaUploadProgressNotification notification from the upload parameters
|
||||
// URL : the uploading media URL
|
||||
// bytesWritten / totalBytesWritten / totalBytesExpectedToWrite : theses parameters are provided by NSURLConnectionDelegate
|
||||
// initialRange / range: the current upload info could be a subpart of uploads. initialRange defines the global upload progress already did done before this current upload.
|
||||
// range is the range value of this upload in the global scope.
|
||||
// e.g. : Upload a media can be split in two parts :
|
||||
// 1 - upload the thumbnail -> initialRange = 0, range = 0.1 : assume that the thumbnail upload is 10% of the upload process
|
||||
// 2 - upload the media -> initialRange = 0,1, range = 0.9 : the media upload is 90% of the global upload
|
||||
+ (void) onUploadProgress:(NSString*)URL bytesWritten:(NSUInteger)bytesWritten totalBytesWritten:(long long)totalBytesWritten totalBytesExpectedToWrite:(long long)totalBytesExpectedToWrite initialRange:(CGFloat)initialRange range:(CGFloat)range;
|
||||
|
||||
// returns the stats info with kMediaLoaderProgress... key
|
||||
+ (NSDictionary*)statsInfoForURL:(NSString*)URL;
|
||||
|
||||
// the upload
|
||||
+ (void)removeURL:(NSString*)URL;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
Copyright 2014 OpenMarket Ltd
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
#import "UploadManager.h"
|
||||
#import "MediaManager.h"
|
||||
|
||||
NSString *const kUploadManagerUploadStartTimeKey = @"kUploadManagerUploadStartTimeKey";
|
||||
NSString *const kUploadManagerStatsStartTimeKey = @"kUploadManagerUploadStartTimeKey";
|
||||
|
||||
@implementation UploadManager
|
||||
|
||||
// Table of stats dictionry by media URL
|
||||
static NSMutableDictionary* statsByURL = nil;
|
||||
|
||||
// trigger the kMediaUploadProgressNotification notification from the upload parameters
|
||||
// URL : the uploading media URL
|
||||
// bytesWritten / totalBytesWritten / totalBytesExpectedToWrite : theses parameters are provided by NSURLConnectionDelegate
|
||||
// initialRange / range: the current upload info could be a subpart of uploads. initialRange defines the global upload progress already did done before this current upload.
|
||||
// range is the range value of this upload in the global scope.
|
||||
// e.g. : Upload a media can be split in two parts :
|
||||
// 1 - upload the thumbnail -> initialRange = 0, range = 0.1 : assume that the thumbnail upload is 10% of the upload process
|
||||
// 2 - upload the media -> initialRange = 0,1, range = 0.9 : the media upload is 90% of the global upload
|
||||
+ (void) onUploadProgress:(NSString*)URL bytesWritten:(NSUInteger)bytesWritten totalBytesWritten:(long long)totalBytesWritten totalBytesExpectedToWrite:(long long)totalBytesExpectedToWrite initialRange:(CGFloat)initialRange range:(CGFloat)range {
|
||||
|
||||
// sanity check
|
||||
if (!URL) {
|
||||
// should never happen
|
||||
return;
|
||||
}
|
||||
|
||||
if (!statsByURL) {
|
||||
statsByURL = [[NSMutableDictionary alloc] init];
|
||||
}
|
||||
|
||||
CFAbsoluteTime currentTime = CFAbsoluteTimeGetCurrent();
|
||||
|
||||
NSMutableDictionary* dict = [statsByURL valueForKey:URL];
|
||||
|
||||
if (!dict) {
|
||||
dict = [[NSMutableDictionary alloc] init];
|
||||
|
||||
// init the start times
|
||||
[dict setValue:[NSNumber numberWithDouble:currentTime] forKey:kUploadManagerUploadStartTimeKey];
|
||||
[dict setValue:[NSNumber numberWithDouble:currentTime] forKey:kUploadManagerStatsStartTimeKey];
|
||||
|
||||
[statsByURL setValue:dict forKey:URL];
|
||||
}
|
||||
|
||||
CGFloat progressRate = initialRange + (((float)totalBytesWritten) / ((float)totalBytesExpectedToWrite) * range);
|
||||
|
||||
[dict setValue:[NSNumber numberWithFloat:progressRate] forKey:kMediaLoaderProgressRateKey];
|
||||
|
||||
CGFloat dataRate = 0;
|
||||
CFAbsoluteTime statsStartTime = ((NSNumber*)[dict valueForKey:kUploadManagerStatsStartTimeKey]).doubleValue;
|
||||
|
||||
if (currentTime != statsStartTime)
|
||||
{
|
||||
dataRate = bytesWritten / 1024.0 / (currentTime - statsStartTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
dataRate = bytesWritten / 1024.0 / 0.001;
|
||||
}
|
||||
|
||||
CGFloat dataRemainingTime = 0;
|
||||
|
||||
if (0 != dataRate)
|
||||
{
|
||||
dataRemainingTime = (totalBytesExpectedToWrite - totalBytesWritten)/ 1024.0 / dataRate;
|
||||
}
|
||||
|
||||
NSString* progressString = [NSString stringWithFormat:@"%@ / %@", [NSByteCountFormatter stringFromByteCount:totalBytesWritten countStyle:NSByteCountFormatterCountStyleFile], [NSByteCountFormatter stringFromByteCount:totalBytesExpectedToWrite countStyle:NSByteCountFormatterCountStyleFile]];
|
||||
[dict setValue:progressString forKey:kMediaLoaderProgressStringKey];
|
||||
|
||||
[dict setValue:[MediaManager formatSecondsInterval:dataRemainingTime] forKey:kMediaLoaderProgressRemaingTimeKey];
|
||||
|
||||
NSString* downloadRateStr = [NSString stringWithFormat:@"%@/s", [NSByteCountFormatter stringFromByteCount:dataRate * 1024 countStyle:NSByteCountFormatterCountStyleFile]];
|
||||
[dict setValue:downloadRateStr forKey:kMediaLoaderProgressDownloadRateKey];
|
||||
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:kMediaUploadProgressNotification object:URL userInfo:dict];
|
||||
}
|
||||
|
||||
// returns the stats info with kMediaLoaderProgress... key
|
||||
+ (NSDictionary*)statsInfoForURL:(NSString*)URL {
|
||||
// sanity check
|
||||
if (URL) {
|
||||
return [statsByURL valueForKey:URL];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
// the upload
|
||||
+ (void)removeURL:(NSString*)URL {
|
||||
if (URL) {
|
||||
[statsByURL removeObjectForKey:URL];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user