// // RAUploadListViewController.m // Apex And Drivers // // Created by Jack on 2018/6/6. // Copyright © 2018年 USAI. All rights reserved. // #import "RAUploadListViewController.h" #import "UIView+Toast.h" #import "AppDelegate.h" @interface RAUploadListViewController () @end @implementation RAUploadListViewController - (void)viewDidLoad { [super viewDidLoad]; [ [ UIApplication sharedApplication] setIdleTimerDisabled:YES ] ; // Do any additional setup after loading the view. [self.view insertSubview:[UIView new] atIndex:0]; AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate; // 如果ViewController创建在UploadManager初始化之前,那么KVO失效,界面刷新不正确而导致数组越界 if (appdelegate.uploadManager != nil) { [appdelegate.uploadManager addObserver:self forKeyPath:@"arr_queue" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:@"arr_queue changed"]; } else { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uploadRefreshNotification:) name:UPLOAD_COUNT_CHANGE_NOTIFICATION object:nil]; } self.title = NSLocalizedString(@"Upload List", nil); UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Clear", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clearList)]; self.navigationItem.rightBarButtonItem = uploadListItem; self.uploadTableView.tableFooterView = [UIView new]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uploadRefreshNotification:) name:UPLOAD_REFRESH_UI_NOTIFICATION object:nil]; } - (void)clearList { AppDelegate* appdelegate = (AppDelegate* )[UIApplication sharedApplication].delegate; if(appdelegate.uploadManager.arr_queue.count==0) { [RAUtils message_alert:nil title:NSLocalizedString(@"Upload list is empty.", nil) controller:self]; // [RAUtils alert_view:nil title:NSLocalizedString(@"Upload list is empty.", nil)]; return; } UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Clear upload list", nil) message:NSLocalizedString(@"Are you sure remove all error/finish task?", nil) preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *OK = [UIAlertAction actionWithTitle:NSLocalizedString(@"YES", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { NSMutableArray* arr_tasks =[NSMutableArray new]; for(NSMutableDictionary* task in appdelegate.uploadManager.arr_queue) { if([task[@"status"] intValue]==TaskStatusFinish||[task[@"status"] intValue]==TaskStatusError) [arr_tasks addObject:task]; } [appdelegate.uploadManager removeTasks:arr_tasks]; }]; [alert addAction:OK]; UIAlertAction *CANCEL = [UIAlertAction actionWithTitle:NSLocalizedString(@"NO", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]; [alert addAction:CANCEL]; [self presentViewController:alert animated:YES completion:nil]; } -(void) dealloc { [ [ UIApplication sharedApplication] setIdleTimerDisabled:NO ] ; AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate; [appdelegate.uploadManager removeObserver:self forKeyPath:@"arr_queue"]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if([keyPath isEqualToString:@"arr_queue"]) { __weak typeof(self) weakself = self; dispatch_async(dispatch_get_main_queue(), ^{ blockDebugLog(@"arr_queue changed reload tableview"); [self.uploadTableView reloadData]; }); } } - (void)uploadRefreshNotification:(NSNotification *)notification { dispatch_async(dispatch_get_main_queue(), ^{ [self.uploadTableView reloadData]; }); } #pragma mark - TableView DataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate; return Appdelegate.uploadManager.arr_queue.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return self.dequeueCell(tableView,indexPath); } #pragma mark - TableView Delegate - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath { AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate; NSMutableDictionary * task = Appdelegate.uploadManager.arr_queue[indexPath.row]; __weak typeof(self) weakself = self; UITableViewRowAction *startAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"Restart", nil) handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { if (Appdelegate.uploadManager.onlyWiFi && Appdelegate.uploadManager.reach.currentReachabilityStatus != ReachableViaWiFi) { [weakself.view makeToast:NSLocalizedString(@"Current Network is not WiFi", nil) duration:3.0 position:CSToastPositionCenter]; [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle]; return ; } DebugLog(@"Start click"); task[@"retry"]=[NSNumber numberWithInt:0]; [Appdelegate.uploadManager startTask:task]; [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle]; }]; startAction.backgroundColor = UIColorFromRGB(0xff9933); UITableViewRowAction *removeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:NSLocalizedString(@"Remove", nil) handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) { [Appdelegate.uploadManager removeTask:task]; // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle]; }]; removeAction.backgroundColor = UIColorFromRGB(0x9BBF5A); switch ([task[@"status"] intValue]) { case TaskStatusFinish: return @[removeAction]; case TaskStatusError: return @[startAction,removeAction]; case TaskStatusStop: return @[startAction]; default: return nil; break; } } - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate; NSDictionary * item_json = Appdelegate.uploadManager.arr_queue[indexPath.row]; bool ret=false; switch ([item_json[@"status"] intValue]) { case TaskStatusStart: case TaskStatusWait: ret=false; break; case TaskStatusFinish: case TaskStatusStop: case TaskStatusError: ret = true; break; default: ret=false; break; } return ret; } - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } // reloadRow 该IndexPath Cell会改变 - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { if (self.endDisplayCell) { self.endDisplayCell(tableView, cell); } } @end