RAUploadListViewController.m 7.0 KB

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