|
|
@@ -0,0 +1,206 @@
|
|
|
+//
|
|
|
+// RAUploadListViewController.m
|
|
|
+// Apex And Drivers
|
|
|
+//
|
|
|
+// Created by Jack on 2018/6/6.
|
|
|
+// Copyright © 2018年 USAI. All rights reserved.
|
|
|
+//
|
|
|
+
|
|
|
+#import "RAUploadListViewController.h"
|
|
|
+#import "UIView+Toast.h"
|
|
|
+#import "AppDelegate.h"
|
|
|
+
|
|
|
+@interface RAUploadListViewController ()
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation RAUploadListViewController
|
|
|
+
|
|
|
+- (void)viewDidLoad {
|
|
|
+ [super viewDidLoad];
|
|
|
+ [ [ UIApplication sharedApplication] setIdleTimerDisabled:YES ] ;
|
|
|
+ // Do any additional setup after loading the view.
|
|
|
+ [self.view insertSubview:[UIView new] atIndex:0];
|
|
|
+
|
|
|
+ AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
|
|
|
+
|
|
|
+
|
|
|
+ [appdelegate.uploadManager addObserver:self
|
|
|
+ forKeyPath:@"arr_queue"
|
|
|
+ options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
|
|
|
+ context:@"arr_queue changed"];
|
|
|
+
|
|
|
+ self.title = @"Upload List";
|
|
|
+ UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStylePlain target:self action:@selector(clearList)];
|
|
|
+ self.navigationItem.rightBarButtonItem = uploadListItem;
|
|
|
+
|
|
|
+
|
|
|
+ self.uploadTableView.tableFooterView = [UIView new];
|
|
|
+}
|
|
|
+- (void)clearList {
|
|
|
+
|
|
|
+ AppDelegate* appdelegate = (AppDelegate* )[UIApplication sharedApplication].delegate;
|
|
|
+ if(appdelegate.uploadManager.arr_queue.count==0)
|
|
|
+ {
|
|
|
+ [RAUtils alert_view:nil title:@"Upload list is empty."];
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ UIAlertController *alert = [UIAlertController alertControllerWithTitle:
|
|
|
+ @"Clear upload list" message:@"Are you sure remove all error/finish task?" preferredStyle:UIAlertControllerStyleAlert];
|
|
|
+
|
|
|
+
|
|
|
+ UIAlertAction *OK = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+
|
|
|
+ NSMutableArray* arr_tasks =[NSMutableArray new];
|
|
|
+ for(NSMutableDictionary* task in appdelegate.uploadManager.arr_queue)
|
|
|
+ {
|
|
|
+ if([task[@"status"] intValue]==TaskStatusFinish||[task[@"status"] intValue]==TaskStatusError)
|
|
|
+ [arr_tasks addObject:task];
|
|
|
+ }
|
|
|
+ [appdelegate.uploadManager removeTasks:arr_tasks];
|
|
|
+
|
|
|
+ }];
|
|
|
+
|
|
|
+ [alert addAction:OK];
|
|
|
+
|
|
|
+ UIAlertAction *CANCEL = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+
|
|
|
+ }];
|
|
|
+ [alert addAction:CANCEL];
|
|
|
+
|
|
|
+ [self presentViewController:alert animated:YES completion:nil];
|
|
|
+
|
|
|
+}
|
|
|
+-(void) dealloc
|
|
|
+{
|
|
|
+ [ [ UIApplication sharedApplication] setIdleTimerDisabled:NO ] ;
|
|
|
+ AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
|
|
|
+ [appdelegate.uploadManager removeObserver:self forKeyPath:@"arr_queue"];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
|
|
|
+{
|
|
|
+
|
|
|
+ if([keyPath isEqualToString:@"arr_queue"])
|
|
|
+ {
|
|
|
+ __weak typeof(self) weakself = self;
|
|
|
+ dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
+ blockDebugLog(@"arr_queue changed reload tableview");
|
|
|
+ [self.uploadTableView reloadData];
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+#pragma mark - TableView DataSource
|
|
|
+
|
|
|
+- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
|
+
|
|
|
+ AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
|
|
|
+ return Appdelegate.uploadManager.arr_queue.count;
|
|
|
+}
|
|
|
+
|
|
|
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
+{
|
|
|
+ return self.dequeueCell(tableView,indexPath);
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - TableView Delegate
|
|
|
+
|
|
|
+- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
|
|
|
+{
|
|
|
+ AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
|
|
|
+ NSMutableDictionary * task = Appdelegate.uploadManager.arr_queue[indexPath.row];
|
|
|
+
|
|
|
+
|
|
|
+ __weak typeof(self) weakself = self;
|
|
|
+ UITableViewRowAction *startAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Restart"handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
|
|
|
+
|
|
|
+ if (Appdelegate.uploadManager.onlyWiFi && Appdelegate.uploadManager.reach.currentReachabilityStatus != ReachableViaWiFi) {
|
|
|
+ [weakself.view makeToast:@"Current Network is not WiFi" duration:3.0 position:CSToastPositionCenter];
|
|
|
+ [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
|
|
|
+ return ;
|
|
|
+ }
|
|
|
+ DebugLog(@"Start click");
|
|
|
+ task[@"retry"]=[NSNumber numberWithInt:0];
|
|
|
+ [Appdelegate.uploadManager startTask:task];
|
|
|
+
|
|
|
+ [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
|
|
|
+ }];
|
|
|
+
|
|
|
+
|
|
|
+ startAction.backgroundColor = UIColorFromRGB(0xff9933);
|
|
|
+
|
|
|
+
|
|
|
+ UITableViewRowAction *removeAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"Remove" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
|
|
|
+
|
|
|
+
|
|
|
+ [Appdelegate.uploadManager removeTask:task];
|
|
|
+
|
|
|
+ // [tableView reloadRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationMiddle];
|
|
|
+
|
|
|
+ }];
|
|
|
+
|
|
|
+ removeAction.backgroundColor = UIColorFromRGB(0x9BBF5A);
|
|
|
+
|
|
|
+ switch ([task[@"status"] intValue]) {
|
|
|
+
|
|
|
+ case TaskStatusFinish:
|
|
|
+ return @[removeAction];
|
|
|
+
|
|
|
+ case TaskStatusError:
|
|
|
+
|
|
|
+ return @[startAction,removeAction];
|
|
|
+
|
|
|
+ case TaskStatusStop:
|
|
|
+ return @[startAction];
|
|
|
+ default:
|
|
|
+ return nil;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ AppDelegate* Appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
|
|
|
+ NSDictionary * item_json = Appdelegate.uploadManager.arr_queue[indexPath.row];
|
|
|
+
|
|
|
+ bool ret=false;
|
|
|
+ switch ([item_json[@"status"] intValue]) {
|
|
|
+ case TaskStatusStart:
|
|
|
+ case TaskStatusWait:
|
|
|
+
|
|
|
+ ret=false;
|
|
|
+ break;
|
|
|
+ case TaskStatusFinish:
|
|
|
+ case TaskStatusStop:
|
|
|
+ case TaskStatusError:
|
|
|
+
|
|
|
+ ret = true;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ ret=false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ return ret;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+- (UITableViewCellEditingStyle)tableView:(UITableView *)tv editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+
|
|
|
+ return UITableViewCellEditingStyleDelete;
|
|
|
+}
|
|
|
+
|
|
|
+// reloadRow 该IndexPath Cell会改变
|
|
|
+- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+
|
|
|
+ if (self.endDisplayCell) {
|
|
|
+ self.endDisplayCell(tableView, cell);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+@end
|