RAUploadListViewController.m 7.3 KB

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