Parcourir la source

1.修改iOS Apex Drivers文件上传,没有文件也可以上传参数。
2.修改iOS Apex Drivers,将上传列表抽取为公用组件。

Pen Li il y a 8 ans
Parent
commit
73c76b2ee1

+ 25 - 14
common/NetworkUtils.m

@@ -311,20 +311,31 @@ repeat:
         DebugLog(@"parameter: key=%@   value=%@",key,[params objectForKey:key]);
     }
     DebugLog(@"================parms==================");
-    [stringM appendString:[NSString stringWithFormat:@"--%@\r\n",BOUNDARY]];
-    
-    [stringM appendFormat:@"Content-Disposition: form-data; name=\"upfile\"; filename=%@\r\n",filename];
-    [stringM appendString:@"Content-Type: application/mac-binary\r\n"];
-    [stringM appendString:@"\r\n"];
-    
-    NSData *stringM_data = [stringM dataUsingEncoding:NSUTF8StringEncoding];
-    [dataM appendData:stringM_data];
-    
-//    NSData *file_data = [NSData dataWithContentsOfFile:filePath];
-    [dataM appendData:filedata];
-    
-    NSString *end = [NSString stringWithFormat:@"\r\n--%@--",BOUNDARY];
-    [dataM appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
+    // Apex Drivers 2018.5.5
+    if (filedata) {
+        
+        [stringM appendString:[NSString stringWithFormat:@"--%@\r\n",BOUNDARY]];
+        
+        [stringM appendFormat:@"Content-Disposition: form-data; name=\"upfile\"; filename=%@\r\n",filename];
+        [stringM appendString:@"Content-Type: application/mac-binary\r\n"];
+        [stringM appendString:@"\r\n"];
+        
+        NSData *stringM_data = [stringM dataUsingEncoding:NSUTF8StringEncoding];
+        [dataM appendData:stringM_data];
+        
+        //    NSData *file_data = [NSData dataWithContentsOfFile:filePath];
+        [dataM appendData:filedata];
+        
+        NSString *end = [NSString stringWithFormat:@"\r\n--%@--",BOUNDARY];
+        [dataM appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
+        
+    } else {
+        
+        NSData *stringM_data = [stringM dataUsingEncoding:NSUTF8StringEncoding];
+        [dataM appendData:stringM_data];
+        NSString *end = [NSString stringWithFormat:@"\r\n--%@--",BOUNDARY];
+        [dataM appendData:[end dataUsingEncoding:NSUTF8StringEncoding]];
+    }
     
     // Data
     NSData *data = dataM;

+ 18 - 0
common/upload/RAUploadListViewController.h

@@ -0,0 +1,18 @@
+//
+//  RAUploadListViewController.h
+//  Apex And Drivers
+//
+//  Created by Jack on 2018/6/6.
+//  Copyright © 2018年 USAI. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface RAUploadListViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
+
+@property (weak, nonatomic,readonly) UITableView *uploadTableView;
+
+@property (nonatomic,copy) UITableViewCell * (^dequeueCell)(UITableView *tableView,NSIndexPath *indexPath);
+@property (nonatomic,copy) void (^endDisplayCell)(UITableView *tableView,UITableViewCell *cell);
+
+@end

+ 206 - 0
common/upload/RAUploadListViewController.m

@@ -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

+ 1 - 0
common/upload/RAUploadManager.h

@@ -26,6 +26,7 @@ typedef NS_ENUM(NSInteger, QueueStatus) {
     QueueStatusFinish = 4
 };
 
+extern NSString *const kUploadSetting;
 
 @interface RAUploadManager : NSObject
 @property (assign) int maxThread;

+ 10 - 9
common/upload/RAUploadManager.m

@@ -9,7 +9,8 @@
 #import "RAUploadManager.h"
 #import "NetworkUtils.h"
 #import "RAUtils.h"
-#import "UploadSettingController.h"
+
+NSString *const kUploadSetting = @"UploadSettingKey";
 
 @interface RAUploadManager ()
 
@@ -344,14 +345,14 @@
 }
 
 
--(NSString*)filePath:(NSMutableDictionary*)task
-{
-    
-    NSString *path = [RAUtils appCacheDirectory];
-    path= [path stringByAppendingPathComponent:task[@"path"]];
-    path= [path stringByAppendingPathComponent:task[@"file"]];
-    return path;
-}
+//-(NSString*)filePath:(NSMutableDictionary*)task
+//{
+//    
+//    NSString *path = [RAUtils appCacheDirectory];
+//    path= [path stringByAppendingPathComponent:task[@"path"]];
+//    path= [path stringByAppendingPathComponent:task[@"file"]];
+//    return path;
+//}
 -(void) saveTasks
 {
     NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];

+ 5 - 1
common/upload/RAUploadOperation.m

@@ -95,7 +95,8 @@ static const NSTimeInterval waitTimeIntervalDelta = 60 * 3;
 //    NSString *md5 = [RAUtils md5WithFile:[self filePath:_taskinfo ]];
     DebugLog(@"task %@",_taskinfo);
     NSString* file_path = [self filePath:_taskinfo];
-    if(file_path==nil)
+    BOOL noFileTag = [[_taskinfo objectForKey:@"noFile"] boolValue]; // Apex Drivers 2018.6.6
+    if(file_path==nil && !noFileTag)
     {
         DebugLog(@"error no file");
         block_task[@"status"]=[NSNumber numberWithInt:TaskStatusError];
@@ -213,6 +214,9 @@ static const NSTimeInterval waitTimeIntervalDelta = 60 * 3;
     
     // V1.07 之后图片不再保存到Temp
     NSString *file = [task objectForKey:@"file"];
+    if (file.length == 0) { // Apex Drivers 2018.6.6
+        return nil;
+    }
     NSString *dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
     NSString *path = [dir stringByAppendingPathComponent:file];