Skip to content
This repository
tree: 4430b9c381
Fetching contributors…

Cannot retrieve contributors at this time

file 140 lines (126 sloc) 4.741 kb
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
//
// ReportDownloadCoordinator.m
// AppSales
//
// Created by Ole Zorn on 01.07.11.
// Copyright 2011 omz:software. All rights reserved.
//

#import "ReportDownloadCoordinator.h"
#import "ReportDownloadOperation.h"
#import "ReportImportOperation.h"
#import "ASAccount.h"
#import "Product.h"
#import "PromoCodeOperation.h"

@implementation ReportDownloadCoordinator

@synthesize isBusy;

- (id)init
{
    self = [super init];
    if (self) {
reportDownloadQueue = [[NSOperationQueue alloc] init];
reportDownloadQueue.maxConcurrentOperationCount = 1;
[reportDownloadQueue addObserver:self forKeyPath:@"operationCount" options:NSKeyValueObservingOptionNew context:nil];
    }
return self;
}

+ (id)sharedReportDownloadCoordinator
{
static id sharedCoordinator = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedCoordinator = [[self alloc] init];
});
return sharedCoordinator;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"operationCount"]) {
self.isBusy = (reportDownloadQueue.operationCount > 0);
}
}

- (void)downloadReportsForAccount:(ASAccount *)account
{
if (account.isDownloadingReports) {
return;
}
ReportDownloadOperation *operation = [[[ReportDownloadOperation alloc] initWithAccount:account] autorelease];
account.isDownloadingReports = YES;
account.downloadStatus = NSLocalizedString(@"Waiting...", nil);
account.downloadProgress = 0.0;
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
UIBackgroundTaskIdentifier backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
NSLog(@"Background task for downloading reports has expired!");
}];
[operation setCompletionBlock:^ {
dispatch_async(dispatch_get_main_queue(), ^ {
account.isDownloadingReports = NO;
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground) {
NSInteger numberOfReportsDownloaded = operation.downloadCount;
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfReportsDownloaded];
}
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
if (backgroundTaskID != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
}
});
}];
[reportDownloadQueue addOperation:operation];
}

- (void)cancelDownloadForAccount:(ASAccount *)account
{
if (!account.isDownloadingReports) return;
account.downloadStatus = NSLocalizedString(@"Cancelling...", nil);
for (NSOperation *operation in [reportDownloadQueue operations]) {
if ([operation isKindOfClass:[ReportDownloadOperation class]] && [[(ReportDownloadOperation *)operation accountObjectID] isEqual:account.objectID]) {
[operation cancel];
}
}
}

- (void)downloadPromoCodesForProduct:(Product *)product numberOfCodes:(NSInteger)numberOfCodes
{
    if (product.isDownloadingPromoCodes) return;
    product.isDownloadingPromoCodes = YES;
    PromoCodeOperation *operation = [[[PromoCodeOperation alloc] initWithProduct:product numberOfCodes:numberOfCodes] autorelease];
    operation.completionBlock = ^ {
        dispatch_async(dispatch_get_main_queue(), ^{
            product.isDownloadingPromoCodes = NO;
        });
    };
    [reportDownloadQueue addOperation:operation];
}

- (void)cancelAllDownloads
{
[reportDownloadQueue cancelAllOperations];
}

- (void)importReportsIntoAccount:(ASAccount *)account
{
[self importReportsIntoAccount:account fromDirectory:nil deleteAfterImport:NO];
}

- (void)importReportsIntoAccount:(ASAccount *)account fromDirectory:(NSString *)path deleteAfterImport:(BOOL)deleteFlag
{
ReportImportOperation *operation = [[[ReportImportOperation alloc] initWithAccount:account] autorelease];
operation.importDirectory = path;
operation.deleteOriginalFilesAfterImport = deleteFlag;
account.isDownloadingReports = YES;
[[UIApplication sharedApplication] setIdleTimerDisabled:YES];
UIBackgroundTaskIdentifier backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
NSLog(@"Background task for importing has expired!");
}];
[operation setCompletionBlock:^ {
dispatch_async(dispatch_get_main_queue(), ^ {
account.isDownloadingReports = NO;
[[UIApplication sharedApplication] setIdleTimerDisabled:NO];
if (backgroundTaskID != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskID];
}
});
}];
[reportDownloadQueue addOperation:operation];
}

- (void)dealloc
{
[reportDownloadQueue removeObserver:self forKeyPath:@"operationCount"];
[reportDownloadQueue release];
[super dealloc];
}

@end
Something went wrong with that request. Please try again.