RAUploadListViewController.m 7.9 KB

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