UploadViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // UploadViewController.m
  3. // RA Image
  4. //
  5. // Created by Jack on 2017/5/5.
  6. // Copyright © 2017年 USAI. All rights reserved.
  7. //
  8. #import "UploadViewController.h"
  9. #import "UploadCell.h"
  10. #import "UIView+Toast.h"
  11. @interface UploadViewController ()<UITableViewDelegate,UITableViewDataSource>
  12. @property (strong, nonatomic) IBOutlet UITableView *uploadTable;
  13. @end
  14. @implementation UploadViewController
  15. - (void)viewDidLoad {
  16. [super viewDidLoad];
  17. [ [ UIApplication sharedApplication] setIdleTimerDisabled:YES ] ;
  18. // Do any additional setup after loading the view.
  19. [self.view insertSubview:[UIView new] atIndex:0];
  20. AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  21. [appdelegate.uploadManager addObserver:self
  22. forKeyPath:@"arr_queue"
  23. options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
  24. context:@"arr_queue changed"];
  25. self.title = @"Upload List";
  26. UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStylePlain target:self action:@selector(clearList)];
  27. self.navigationItem.rightBarButtonItem = uploadListItem;
  28. self.uploadTable.tableFooterView = [UIView new];
  29. // appdelegate.uploadManager.delegate = self;
  30. }
  31. - (void)clearList {
  32. AppDelegate* appdelegate = (AppDelegate* )[UIApplication sharedApplication].delegate;
  33. if(appdelegate.uploadManager.arr_queue.count==0)
  34. {
  35. [RAUtils message_alert:nil title:@"Upload list is empty." controller:self];
  36. // [RAUtils alert_view:nil title:@"Upload list is empty."];
  37. return;
  38. }
  39. UIAlertController *alert = [UIAlertController alertControllerWithTitle:
  40. @"Clear upload list" message:@"Are you sure remove all error/finish task?" preferredStyle:UIAlertControllerStyleAlert];
  41. UIAlertAction *OK = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  42. NSMutableArray* arr_tasks =[NSMutableArray new];
  43. for(NSMutableDictionary* task in appdelegate.uploadManager.arr_queue)
  44. {
  45. if([task[@"status"] intValue]==TaskStatusFinish||[task[@"status"] intValue]==TaskStatusError)
  46. [arr_tasks addObject:task];
  47. }
  48. [appdelegate.uploadManager removeTasks:arr_tasks];
  49. }];
  50. [alert addAction:OK];
  51. UIAlertAction *CANCEL = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  52. }];
  53. [alert addAction:CANCEL];
  54. [self presentViewController:alert animated:YES completion:nil];
  55. }
  56. -(void) dealloc
  57. {
  58. [ [ UIApplication sharedApplication] setIdleTimerDisabled:NO ] ;
  59. AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  60. [appdelegate.uploadManager removeObserver:self forKeyPath:@"arr_queue"];
  61. }
  62. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  63. {
  64. if([keyPath isEqualToString:@"arr_queue"])
  65. {
  66. // NSArray* arr1 = [change objectForKey:NSKeyValueChangeOldKey];
  67. // NSArray* arr2 = [change objectForKey:NSKeyValueChangeNewKey];
  68. // if(arr1.count!=arr2.count)
  69. __weak typeof(self) weakself = self;
  70. dispatch_async(dispatch_get_main_queue(), ^{
  71. blockDebugLog(@"arr_queue changed reload tableview");
  72. [self.tableView reloadData];
  73. });
  74. }
  75. // if(context)
  76. // NSLog(@"old: %@", [change objectForKey:NSKeyValueChangeOldKey]);
  77. // NSLog(@"old: %@", [change objectForKey:NSKeyValueChangeNewKey]);
  78. // NSLog(@"context: %@", context);
  79. }
  80. - (void)didReceiveMemoryWarning {
  81. [super didReceiveMemoryWarning];
  82. // Dispose of any resources that can be recreated.
  83. }
  84. #pragma mark - TableView DataSource
  85. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  86. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  87. return Appdelegate.uploadManager.arr_queue.count;
  88. }
  89. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  90. {
  91. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  92. {
  93. NSString *CellIdentifier = @"UploadCell";
  94. UploadCell * cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
  95. NSDictionary * item_json = Appdelegate.uploadManager.arr_queue[indexPath.row];
  96. //
  97. // if (cell.operation) {
  98. // cell.operation.updateUI = nil;
  99. // }
  100. RAUploadOperation* op = item_json[@"operation"];
  101. NSString* status =nil;
  102. switch ([item_json[@"status"] intValue]) {
  103. case TaskStatusStart:
  104. status=@"uploading";
  105. break;
  106. case TaskStatusStop:
  107. status=@"stop";
  108. break;
  109. case TaskStatusError:
  110. status=@"warning";
  111. break;
  112. case TaskStatusWait:
  113. status=@"waiting";
  114. break;
  115. case TaskStatusFinish:
  116. status=@"finish";
  117. break;
  118. default:
  119. status=@"warning";
  120. break;
  121. }
  122. double progress =[item_json[@"progress"] doubleValue];
  123. cell.progress=progress;
  124. cell.name=item_json[@"file"];
  125. cell.state= status;
  126. cell.errorMsg = item_json[@"msg"];
  127. // __block RAUploadOperation* block_op = op;
  128. // __block UploadCell* block_cell = cell;
  129. // __block NSIndexPath* block_index = indexPath;
  130. // long idx = indexPath.row;
  131. // __weak typeof(self) weakself = self;
  132. op.updateUI=^(){
  133. // blockDebugLog(@"updateUI @ row%ld %p \n operation %p",idx,block_cell,block_op);
  134. NSString* status =nil;
  135. switch ([item_json[@"status"] intValue]) {
  136. case TaskStatusStart:
  137. status=@"uploading";
  138. break;
  139. case TaskStatusStop:
  140. status=@"stop";
  141. break;
  142. case TaskStatusError:
  143. status=@"warning";
  144. break;
  145. case TaskStatusWait:
  146. status=@"waiting";
  147. break;
  148. case TaskStatusFinish:
  149. status=@"finish";
  150. break;
  151. default:
  152. status=@"warning";
  153. break;
  154. }
  155. double progress =[item_json[@"progress"] doubleValue];
  156. cell.progress=progress;
  157. cell.name=item_json[@"file"];
  158. cell.state= status;
  159. cell.errorMsg = item_json[@"msg"];
  160. };
  161. DebugLog(@"%@ set update ui",item_json[@"file"]);
  162. // cell.operation = op;
  163. //
  164. return cell;
  165. }
  166. }
  167. #pragma mark - TableView Delegate
  168. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  169. {
  170. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  171. NSMutableDictionary * task = Appdelegate.uploadManager.arr_queue[indexPath.row];
  172. __weak typeof(self) weakself = self;
  173. UITableViewRowAction *startAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Restart"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  174. if (Appdelegate.uploadManager.onlyWiFi && Appdelegate.uploadManager.reach.currentReachabilityStatus != ReachableViaWiFi) {
  175. [weakself.view makeToast:@"Current Network is not WiFi" duration:3.0 position:CSToastPositionCenter];
  176. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  177. return ;
  178. }
  179. DebugLog(@"Start click");
  180. task[@"retry"]=[NSNumber numberWithInt:0];
  181. [Appdelegate.uploadManager startTask:task];
  182. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  183. }];
  184. startAction.backgroundColor = UIColorFromRGB(0xff9933);
  185. UITableViewRowAction *removeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Remove" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  186. [Appdelegate.uploadManager removeTask:task];
  187. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  188. }];
  189. removeAction.backgroundColor = UIColorFromRGB(0x9BBF5A);
  190. // UIColorFromRGB(0x336699);
  191. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  192. switch ([task[@"status"] intValue]) {
  193. case TaskStatusFinish:
  194. return @[removeAction];
  195. case TaskStatusError:
  196. return @[startAction,removeAction];
  197. case TaskStatusStop:
  198. return @[startAction];
  199. default:
  200. return nil;
  201. break;
  202. }
  203. }
  204. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  205. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  206. NSDictionary * item_json = Appdelegate.uploadManager.arr_queue[indexPath.row];
  207. bool ret=false;
  208. switch ([item_json[@"status"] intValue]) {
  209. case TaskStatusStart:
  210. case TaskStatusWait:
  211. ret=false;
  212. break;
  213. case TaskStatusFinish:
  214. case TaskStatusStop:
  215. case TaskStatusError:
  216. ret = true;
  217. break;
  218. default:
  219. ret=false;
  220. break;
  221. }
  222. return ret;
  223. }
  224. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  225. return UITableViewCellEditingStyleDelete;
  226. }
  227. //- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  228. // UploadCell *upCell = (UploadCell *)cell;
  229. //
  230. //}
  231. // reloadRow 该IndexPath Cell会改变
  232. - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  233. UploadCell *upCell = (UploadCell *)cell;
  234. // if (upCell.operation) {
  235. // upCell.operation.updateUI = nil;
  236. // }
  237. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  238. NSUInteger row = indexPath.row;
  239. NSUInteger count = Appdelegate.uploadManager.arr_queue.count;
  240. if (count > row) {
  241. NSDictionary * item_json = Appdelegate.uploadManager.arr_queue[indexPath.row];
  242. RAUploadOperation* operation = item_json[@"operation"];
  243. UploadCell *newCell = (UploadCell *)[tableView cellForRowAtIndexPath:indexPath];
  244. if (newCell != upCell) {
  245. // reload row
  246. } else {
  247. operation.updateUI = nil; // restart 会调用就会导致cell不会刷新
  248. }
  249. }
  250. upCell.name = nil;
  251. upCell.progress = 0;
  252. upCell.state = nil;
  253. upCell.errorMsg = nil;
  254. }
  255. /*
  256. #pragma mark - UploadManager Delegate
  257. - (void)uploadManager:(RAUploadManager *)manager didRemoveTasks:(NSArray *)tasks {
  258. // 修复finish后滑动Table导致数组越界崩溃。
  259. [self.uploadTable reloadData];
  260. }
  261. */
  262. @end