| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- //
- // UploadViewController.m
- // RA Image
- //
- // Created by Jack on 2017/5/5.
- // Copyright © 2017年 USAI. All rights reserved.
- //
- #import "UploadViewController.h"
- #import "UploadCell.h"
- #import "UIView+Toast.h"
- @interface UploadViewController ()<UITableViewDelegate,UITableViewDataSource>
- @property (strong, nonatomic) IBOutlet UITableView *uploadTable;
- @end
- @implementation UploadViewController
- - (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;
-
-
- [appdelegate.uploadManager addObserver:self
- forKeyPath:@"arr_queue"
- options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
- context:@"arr_queue changed"];
-
- self.title = @"Upload List";
- UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStylePlain target:self action:@selector(clearList)];
- self.navigationItem.rightBarButtonItem = uploadListItem;
-
-
- self.uploadTable.tableFooterView = [UIView new];
- // appdelegate.uploadManager.delegate = self;
- }
- - (void)clearList {
-
- AppDelegate* appdelegate = (AppDelegate* )[UIApplication sharedApplication].delegate;
- if(appdelegate.uploadManager.arr_queue.count==0)
- {
- [RAUtils message_alert:nil title:@"Upload list is empty." controller:self];
- // [RAUtils alert_view:nil title:@"Upload list is empty."];
- return;
- }
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:
- @"Clear upload list" message:@"Are you sure remove all error/finish task?" preferredStyle:UIAlertControllerStyleAlert];
-
-
- UIAlertAction *OK = [UIAlertAction actionWithTitle:@"YES" 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:@"NO" 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"];
- }
- - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
- {
-
- if([keyPath isEqualToString:@"arr_queue"])
- {
- // NSArray* arr1 = [change objectForKey:NSKeyValueChangeOldKey];
- // NSArray* arr2 = [change objectForKey:NSKeyValueChangeNewKey];
- // if(arr1.count!=arr2.count)
-
- __weak typeof(self) weakself = self;
- dispatch_async(dispatch_get_main_queue(), ^{
- blockDebugLog(@"arr_queue changed reload tableview");
- [self.tableView reloadData];
- });
-
- }
- // if(context)
- // NSLog(@"old: %@", [change objectForKey:NSKeyValueChangeOldKey]);
- // NSLog(@"old: %@", [change objectForKey:NSKeyValueChangeNewKey]);
- // NSLog(@"context: %@", context);
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #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
- {
- AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
-
-
-
- {
- NSString *CellIdentifier = @"UploadCell";
- UploadCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
-
-
- NSDictionary * item_json = Appdelegate.uploadManager.arr_queue[indexPath.row];
- //
- // if (cell.operation) {
- // cell.operation.updateUI = nil;
- // }
- RAUploadOperation* op = item_json[@"operation"];
-
- NSString* status =nil;
- switch ([item_json[@"status"] intValue]) {
- case TaskStatusStart:
- status=@"uploading";
- break;
- case TaskStatusStop:
- status=@"stop";
- break;
- case TaskStatusError:
- status=@"warning";
- break;
- case TaskStatusWait:
- status=@"waiting";
- break;
- case TaskStatusFinish:
- status=@"finish";
- break;
-
- default:
- status=@"warning";
- break;
- }
-
- double progress =[item_json[@"progress"] doubleValue];
- cell.progress=progress;
- cell.name=item_json[@"file"];
- cell.state= status;
- cell.errorMsg = item_json[@"msg"];
-
-
-
-
- // __block RAUploadOperation* block_op = op;
- // __block UploadCell* block_cell = cell;
- // __block NSIndexPath* block_index = indexPath;
- // long idx = indexPath.row;
- // __weak typeof(self) weakself = self;
-
- op.updateUI=^(){
-
-
- // blockDebugLog(@"updateUI @ row%ld %p \n operation %p",idx,block_cell,block_op);
-
- NSString* status =nil;
- switch ([item_json[@"status"] intValue]) {
- case TaskStatusStart:
- status=@"uploading";
- break;
- case TaskStatusStop:
- status=@"stop";
- break;
- case TaskStatusError:
- status=@"warning";
- break;
- case TaskStatusWait:
- status=@"waiting";
- break;
- case TaskStatusFinish:
- status=@"finish";
- break;
-
- default:
- status=@"warning";
- break;
- }
- double progress =[item_json[@"progress"] doubleValue];
- cell.progress=progress;
- cell.name=item_json[@"file"];
- cell.state= status;
- cell.errorMsg = item_json[@"msg"];
-
- };
- DebugLog(@"%@ set update ui",item_json[@"file"]);
- // cell.operation = op;
- //
- return cell;
- }
-
- }
- #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:@"Restart"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
-
- if (Appdelegate.uploadManager.onlyWiFi && Appdelegate.uploadManager.reach.currentReachabilityStatus != ReachableViaWiFi) {
- [weakself.view makeToast:@"Current Network is not WiFi" 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:@"Remove" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
-
-
- [Appdelegate.uploadManager removeTask:task];
-
- // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
-
- }];
-
- removeAction.backgroundColor = UIColorFromRGB(0x9BBF5A);
-
-
-
-
- // UIColorFromRGB(0x336699);
-
-
- // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
-
- 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;
-
-
- }
- //- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
- // UploadCell *upCell = (UploadCell *)cell;
- //
- //}
- // reloadRow 该IndexPath Cell会改变
- - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
- UploadCell *upCell = (UploadCell *)cell;
- // if (upCell.operation) {
- // upCell.operation.updateUI = nil;
- // }
- AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
- NSUInteger row = indexPath.row;
- NSUInteger count = Appdelegate.uploadManager.arr_queue.count;
- if (count > row) {
- NSDictionary * item_json = Appdelegate.uploadManager.arr_queue[indexPath.row];
- RAUploadOperation* operation = item_json[@"operation"];
- UploadCell *newCell = (UploadCell *)[tableView cellForRowAtIndexPath:indexPath];
- if (newCell != upCell) {
- // reload row
- } else {
- operation.updateUI = nil; // restart 会调用就会导致cell不会刷新
- }
- }
- upCell.name = nil;
- upCell.progress = 0;
- upCell.state = nil;
- upCell.errorMsg = nil;
- }
- /*
- #pragma mark - UploadManager Delegate
- - (void)uploadManager:(RAUploadManager *)manager didRemoveTasks:(NSArray *)tasks {
- // 修复finish后滑动Table导致数组越界崩溃。
- [self.uploadTable reloadData];
- }
- */
- @end
|