123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- //
- // KMCloudUploadOperationQueue.m
- // PDF Reader Pro Edition
- //
- // Created by 万军 on 2020/2/21.
- // Copyright © 2020 WanJun. All rights reserved.
- //
- #import "KMCloudUploadOperationQueue.h"
- #import "KMCloudOperation.h"
- @interface KMCloudUploadOperationQueue ()
- @end
- @implementation KMCloudUploadOperationQueue
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self name:KMServerCloudFileManagerUploadSuccessfulNotification object:nil];
- [[NSNotificationCenter defaultCenter] removeObserver:self name:KMServerCloudFileManagerUploadFailureNotification object:nil];
- }
- + (instancetype)sharedInstance {
- static KMCloudUploadOperationQueue *sharedInstance;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedInstance = [KMCloudUploadOperationQueue new];
- sharedInstance.uploadingOperations = [NSMutableDictionary dictionary];
- });
- return sharedInstance;
- }
- #pragma mark - download
- - (void)addUploadOperation:(NSOperation *)op
- {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serverCloudFileManagerUploadSuccessfulNotification:) name:KMServerCloudFileManagerUploadSuccessfulNotification object:nil];
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serverCloudFileManagerUploadFailureNotification:) name:KMServerCloudFileManagerUploadFailureNotification object:nil];
-
- if ([op isKindOfClass:[KMCloudOperation class]]) {
- KMCloudOperation *operation = (KMCloudOperation *)op;
- if (operation.serverType == KMDropbox) {
- [self addDropboxUploadOperation:(KMCloudOperation *)op];
- } else if (operation.serverType == KMGoogleDrive) {
- [self addGoogleDriveUploadOperation:(KMCloudOperation *)op];
- }
- }
- }
- - (void)addDropboxUploadOperation:(KMCloudOperation *)op {
- if ([self isUploadOperationExisting:op.filePath]) {
- NSLog(@"KMDropboxOperation exists %@", op);
- return;
- }
- if (![self.uploadingOperations objectForKey:op.filePath]) {
- @synchronized (self) {
- [self.uploadingOperations setObject:op forKey:op.filePath];
- }
- [self addOperation:op];
- }
- }
- - (void)addGoogleDriveUploadOperation:(KMCloudOperation *)op {
- if ([self isUploadOperationExisting:op.filePath]) {
- NSLog(@"KMGoogleDriveOperation exists %@", op);
- return;
- }
- if (![self.uploadingOperations objectForKey:op.filePath]) {
- @synchronized (self) {
- [self.uploadingOperations setObject:op forKey:op.filePath];
- }
- [self addOperation:op];
- }
- }
- - (NSOperation *)isUploadOperationExisting:(NSString *)urlString {
- NSLog(@"self.uploadingOperations.allKeys == %@",self.uploadingOperations.allKeys);
- for (NSString *url in self.uploadingOperations.allKeys) {
- if ([url isEqualToString:urlString]) {
- return [self.uploadingOperations objectForKey:url];
- }
- }
- return nil;
- }
- #pragma mark - cancel
- - (void)cancel:(NSString *)urlString {
- NSOperation *op = [self.uploadingOperations objectForKey:urlString];
- if (op) {
- @synchronized (self) {
- [self.uploadingOperations removeObjectForKey:urlString];
- }
- }
- }
- - (void)cancelAll {
- @synchronized (self) {
- [self.uploadingOperations removeAllObjects];
- }
- [self cancelAllOperations];
- }
- #pragma mark - NSNotificationCenter
- - (void)serverCloudFileManagerUploadSuccessfulNotification:(NSNotification *)notification {
- if (notification.object) {
- KMCloudOperation *operation = (KMCloudOperation *)notification.object;
- if ([self.uploadingOperations objectForKey:operation.filePath]) {
- @synchronized (self) {
- [self.uploadingOperations removeObjectForKey:operation.filePath];
- }
- }
- }
- }
- - (void)serverCloudFileManagerUploadFailureNotification:(NSNotification *)notification {
- if (notification.object) {
- KMCloudOperation *operation = (KMCloudOperation *)notification.object;
- if ([self.uploadingOperations objectForKey:operation.filePath]) {
- @synchronized (self) {
- [self.uploadingOperations removeObjectForKey:operation.filePath];
- }
- }
- }
- }
- @end
|