RAUploadListViewController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // RAUploadListViewController.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/6/6.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAUploadListViewController.h"
  9. #import "UIView+Toast.h"
  10. #import "AppDelegate.h"
  11. @interface RAUploadListViewController ()
  12. @end
  13. @implementation RAUploadListViewController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. [ [ UIApplication sharedApplication] setIdleTimerDisabled:YES ] ;
  17. // Do any additional setup after loading the view.
  18. [self.view insertSubview:[UIView new] atIndex:0];
  19. AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  20. // 如果ViewController创建在UploadManager初始化之前,那么KVO失效,界面刷新不正确而导致数组越界
  21. if (appdelegate.uploadManager != nil) {
  22. [appdelegate.uploadManager addObserver:self
  23. forKeyPath:@"arr_queue"
  24. options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
  25. context:@"arr_queue changed"];
  26. } else {
  27. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uploadRefreshNotification:) name:UPLOAD_COUNT_CHANGE_NOTIFICATION object:nil];
  28. }
  29. self.title = NSLocalizedString(@"Upload List", nil);
  30. UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Clear", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clearList)];
  31. self.navigationItem.rightBarButtonItem = uploadListItem;
  32. self.uploadTableView.tableFooterView = [UIView new];
  33. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uploadRefreshNotification:) name:UPLOAD_REFRESH_UI_NOTIFICATION object:nil];
  34. }
  35. - (void)clearList {
  36. AppDelegate* appdelegate = (AppDelegate* )[UIApplication sharedApplication].delegate;
  37. if(appdelegate.uploadManager.arr_queue.count==0)
  38. {
  39. [RAUtils alert_view:nil title:NSLocalizedString(@"Upload list is empty.", nil)];
  40. return;
  41. }
  42. UIAlertController *alert = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Clear upload list", nil) message:NSLocalizedString(@"Are you sure remove all error/finish task?", nil) preferredStyle:UIAlertControllerStyleAlert];
  43. UIAlertAction *OK = [UIAlertAction actionWithTitle:NSLocalizedString(@"YES", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  44. NSMutableArray* arr_tasks =[NSMutableArray new];
  45. for(NSMutableDictionary* task in appdelegate.uploadManager.arr_queue)
  46. {
  47. if([task[@"status"] intValue]==TaskStatusFinish||[task[@"status"] intValue]==TaskStatusError)
  48. [arr_tasks addObject:task];
  49. }
  50. [appdelegate.uploadManager removeTasks:arr_tasks];
  51. }];
  52. [alert addAction:OK];
  53. UIAlertAction *CANCEL = [UIAlertAction actionWithTitle:NSLocalizedString(@"NO", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  54. }];
  55. [alert addAction:CANCEL];
  56. [self presentViewController:alert animated:YES completion:nil];
  57. }
  58. -(void) dealloc
  59. {
  60. [ [ UIApplication sharedApplication] setIdleTimerDisabled:NO ] ;
  61. AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  62. [appdelegate.uploadManager removeObserver:self forKeyPath:@"arr_queue"];
  63. [[NSNotificationCenter defaultCenter] removeObserver:self];
  64. }
  65. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  66. {
  67. if([keyPath isEqualToString:@"arr_queue"])
  68. {
  69. __weak typeof(self) weakself = self;
  70. dispatch_async(dispatch_get_main_queue(), ^{
  71. blockDebugLog(@"arr_queue changed reload tableview");
  72. [self.uploadTableView reloadData];
  73. });
  74. }
  75. }
  76. - (void)uploadRefreshNotification:(NSNotification *)notification {
  77. dispatch_async(dispatch_get_main_queue(), ^{
  78. [self.uploadTableView reloadData];
  79. });
  80. }
  81. #pragma mark - TableView DataSource
  82. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  83. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  84. return Appdelegate.uploadManager.arr_queue.count;
  85. }
  86. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  87. {
  88. return self.dequeueCell(tableView,indexPath);
  89. }
  90. #pragma mark - TableView Delegate
  91. - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
  92. {
  93. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  94. NSMutableDictionary * task = Appdelegate.uploadManager.arr_queue[indexPath.row];
  95. __weak typeof(self) weakself = self;
  96. UITableViewRowAction *startAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:NSLocalizedString(@"Restart", nil) handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  97. if (Appdelegate.uploadManager.onlyWiFi && Appdelegate.uploadManager.reach.currentReachabilityStatus != ReachableViaWiFi) {
  98. [weakself.view makeToast:NSLocalizedString(@"Current Network is not WiFi", nil) duration:3.0 position:CSToastPositionCenter];
  99. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  100. return ;
  101. }
  102. DebugLog(@"Start click");
  103. task[@"retry"]=[NSNumber numberWithInt:0];
  104. [Appdelegate.uploadManager startTask:task];
  105. [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  106. }];
  107. startAction.backgroundColor = UIColorFromRGB(0xff9933);
  108. UITableViewRowAction *removeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:NSLocalizedString(@"Remove", nil) handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
  109. [Appdelegate.uploadManager removeTask:task];
  110. // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
  111. }];
  112. removeAction.backgroundColor = UIColorFromRGB(0x9BBF5A);
  113. switch ([task[@"status"] intValue]) {
  114. case TaskStatusFinish:
  115. return @[removeAction];
  116. case TaskStatusError:
  117. return @[startAction,removeAction];
  118. case TaskStatusStop:
  119. return @[startAction];
  120. default:
  121. return nil;
  122. break;
  123. }
  124. }
  125. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
  126. AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  127. NSDictionary * item_json = Appdelegate.uploadManager.arr_queue[indexPath.row];
  128. bool ret=false;
  129. switch ([item_json[@"status"] intValue]) {
  130. case TaskStatusStart:
  131. case TaskStatusWait:
  132. ret=false;
  133. break;
  134. case TaskStatusFinish:
  135. case TaskStatusStop:
  136. case TaskStatusError:
  137. ret = true;
  138. break;
  139. default:
  140. ret=false;
  141. break;
  142. }
  143. return ret;
  144. }
  145. - (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
  146. return UITableViewCellEditingStyleDelete;
  147. }
  148. // reloadRow 该IndexPath Cell会改变
  149. - (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  150. if (self.endDisplayCell) {
  151. self.endDisplayCell(tableView, cell);
  152. }
  153. }
  154. @end