RAUploadListViewController.m 6.8 KB

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