Browse Source

1.完成iOS Apex Drivers离线判断没有数据。

Pen Li 7 năm trước cách đây
mục cha
commit
e42e28968b
1 tập tin đã thay đổi với 126 bổ sung107 xóa
  1. 126 107
      Redant Drivers/Apex And Drivers/Offline/RAOfflineHandler.m

+ 126 - 107
Redant Drivers/Apex And Drivers/Offline/RAOfflineHandler.m

@@ -424,6 +424,10 @@ static dispatch_semaphore_t _lock;
     NSString *path = [self.offlineDir stringByAppendingPathComponent:@"finish"];
     NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
     
+    if (dic == nil) {
+        dic = [NSDictionary dictionary];
+    }
+    
     return dic;
 }
 
@@ -587,88 +591,96 @@ static dispatch_semaphore_t _lock;
         // 加载Home
         NSMutableDictionary *home = [[self requestOfflineHome] mutableCopy];
         
-        // 遍历order,查找当前order,并修改状态/移除
-        // Accept 移动order到processing
-        // Reject 删除Order及对应的detail、edit
-        NSMutableArray<NSDictionary *> *sections = [[home objectForKey:@"sections"] mutableCopy];
-        __block NSDictionary *curOrder = nil; // 当前order
-        __block NSDictionary *rmSection = nil; // 若section下没有order,则移除section
-        // 查找New Order
-        for (int i = 0; i < sections.count; i++) {
-            NSDictionary *section = [sections objectAtIndex:i];
+        if (home != nil) {
+            
+            // 遍历order,查找当前order,并修改状态/移除
+            // Accept 移动order到processing
+            // Reject 删除Order及对应的detail、edit
+            NSMutableArray<NSDictionary *> *sections = [[home objectForKey:@"sections"] mutableCopy];
+            __block NSDictionary *curOrder = nil; // 当前order
+            __block NSDictionary *rmSection = nil; // 若section下没有order,则移除section
             
-            RAOrderStatus type = [[section objectForKey:@"type"] intValue];
-            if (type == RAOrderStatusNew) {
+            if (sections) {
                 
-                // 查找当前order
-                NSMutableArray<NSDictionary *> *orders = [[section objectForKey:@"orders"] mutableCopy];
-                [orders enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
-                   
-                    NSString *orderID = [obj objectForKey:@"orderID"];
-                    if ([orderId isEqualToString:orderID]) {
-                        curOrder = obj;
-                        *stop = YES;
+                // 查找New Order
+                for (int i = 0; i < sections.count; i++) {
+                    NSDictionary *section = [sections objectAtIndex:i];
+                    
+                    RAOrderStatus type = [[section objectForKey:@"type"] intValue];
+                    if (type == RAOrderStatusNew) {
+                        
+                        // 查找当前order
+                        NSMutableArray<NSDictionary *> *orders = [[section objectForKey:@"orders"] mutableCopy];
+                        [orders enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+                            
+                            NSString *orderID = [obj objectForKey:@"orderID"];
+                            if ([orderId isEqualToString:orderID]) {
+                                curOrder = obj;
+                                *stop = YES;
+                            }
+                        }];
+                        
+                        // 找到order并移除
+                        if (curOrder) {
+                            [orders removeObject:curOrder];
+                            
+                            // 移除过后如果orders为空,则移除section
+                            if (orders.count == 0) {
+                                rmSection = section;
+                            }
+                            else {
+                                // orders不为空,重新修改
+                                rmSection = nil;
+                                NSMutableDictionary *mSec = [section mutableCopy];
+                                [mSec setObject:orders forKey:@"orders"];
+                                [sections replaceObjectAtIndex:i withObject:mSec];
+                            }
+                        }
+                        
+                        // 删除Order 操作对应Edit文件
+                        [self deleteEditJsonFileForOrder:orderId withActionIndex:actionIdx];
+                        
+                        break;
                     }
-                }];
+                }
+                
+                if (rmSection) {
+                    [sections removeObject:rmSection];
+                }
                 
-                // 找到order并移除
-                if (curOrder) {
-                    [orders removeObject:curOrder];
+                // 如果是Accept则将order添加到Processing Order
+                if (action == RADetailActionSubTypeAccept) {
                     
-                    // 移除过后如果orders为空,则移除section
-                    if (orders.count == 0) {
-                        rmSection = section;
-                    }
-                    else {
-                        // orders不为空,重新修改
-                        rmSection = nil;
-                        NSMutableDictionary *mSec = [section mutableCopy];
-                        [mSec setObject:orders forKey:@"orders"];
-                        [sections replaceObjectAtIndex:i withObject:mSec];
+                    for (int i = 0; i < sections.count; i++) {
+                        NSMutableDictionary *section = [[sections objectAtIndex:i] mutableCopy];
+                        
+                        RAOrderStatus type = [[section objectForKey:@"type"] intValue];
+                        if (type == RAOrderStatusProcessing) {
+                            NSMutableArray<NSDictionary *> *orders = [[section objectForKey:@"orders"] mutableCopy];
+                            
+                            // 修改order状态为Processing
+                            [curOrder setValue:@(RAOrderStatusProcessing) forKey:@"status"];
+                            
+                            // 将当前order插入第一个位置
+                            [orders insertObject:curOrder atIndex:0];
+                            
+                            [section setObject:orders forKey:@"orders"];
+                            
+                            // 重新生成section
+                            [sections replaceObjectAtIndex:i withObject:section];
+                            break;
+                        }
                     }
                 }
                 
-                // 删除Order 操作对应Edit文件
-                [self deleteEditJsonFileForOrder:orderId withActionIndex:actionIdx];
+                // 重新生成sections
+                [home setObject:sections forKey:@"sections"];
                 
-                break;
             }
-        }
-        
-        if (rmSection) {
-            [sections removeObject:rmSection];
-        }
-        
-        // 如果是Accept则将order添加到Processing Order
-        if (action == RADetailActionSubTypeAccept) {
             
-            for (int i = 0; i < sections.count; i++) {
-                NSMutableDictionary *section = [[sections objectAtIndex:i] mutableCopy];
-                
-                RAOrderStatus type = [[section objectForKey:@"type"] intValue];
-                if (type == RAOrderStatusProcessing) {
-                    NSMutableArray<NSDictionary *> *orders = [[section objectForKey:@"orders"] mutableCopy];
-                    
-                    // 修改order状态为Processing
-                    [curOrder setValue:@(RAOrderStatusProcessing) forKey:@"status"];
-                    
-                    // 将当前order插入第一个位置
-                    [orders insertObject:curOrder atIndex:0];
-                    
-                    [section setObject:orders forKey:@"orders"];
-                    
-                    // 重新生成section
-                    [sections replaceObjectAtIndex:i withObject:section];
-                    break;
-                }
-            }
+            // 更新Home文件
+            [self updateHome:home];
         }
-        
-        // 重新生成sections
-        [home setObject:sections forKey:@"sections"];
-        
-        // 更新Home文件
-        [self updateHome:home];
     }
     
     // 更新Action
@@ -766,54 +778,61 @@ static dispatch_semaphore_t _lock;
     // 判断是否完成order 所有操作
     BOOL finish = [self isLastActionForOrder:orderId];
     if (finish) {
+        
         // 将order从Processing中删除
         // 加载Home
         NSMutableDictionary *home = [[self requestOfflineHome] mutableCopy];
-        
-        NSMutableArray<NSDictionary *> *sections = [[home objectForKey:@"sections"] mutableCopy];
-        __block NSDictionary *rmSection = nil; // 若section下没有order,则移除section
-        for (int i = 0; i < sections.count; i++) {
-            NSMutableDictionary *section = [[sections objectAtIndex:i] mutableCopy];
-            
-            RAOrderStatus type = [[section objectForKey:@"type"] intValue];
-            if (type == RAOrderStatusProcessing) {
-                NSMutableArray<NSDictionary *> *orders = [[section objectForKey:@"orders"] mutableCopy];
+        if (home) {
+            NSMutableArray<NSDictionary *> *sections = [[home objectForKey:@"sections"] mutableCopy];
+            if (sections) {
                 
-                __block NSDictionary *curOrder = nil; // 当前order
-                [orders enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
-                   
-                    NSString *orderID = [obj objectForKey:@"orderID"];
-                    if ([orderId isEqualToString:orderID]) {
-                        curOrder = obj;
-                        *stop = YES;
-                    }
+                __block NSDictionary *rmSection = nil; // 若section下没有order,则移除section
+                for (int i = 0; i < sections.count; i++) {
+                    NSMutableDictionary *section = [[sections objectAtIndex:i] mutableCopy];
                     
-                }];
-                [orders removeObject:curOrder];
+                    RAOrderStatus type = [[section objectForKey:@"type"] intValue];
+                    if (type == RAOrderStatusProcessing) {
+                        NSMutableArray<NSDictionary *> *orders = [[section objectForKey:@"orders"] mutableCopy];
+                        
+                        __block NSDictionary *curOrder = nil; // 当前order
+                        [orders enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+                            
+                            NSString *orderID = [obj objectForKey:@"orderID"];
+                            if ([orderId isEqualToString:orderID]) {
+                                curOrder = obj;
+                                *stop = YES;
+                            }
+                            
+                        }];
+                        [orders removeObject:curOrder];
+                        
+                        if (orders.count > 0) {
+                            [section setObject:orders forKey:@"orders"];
+                            
+                            // 重新生成section
+                            [sections replaceObjectAtIndex:i withObject:section];
+                        } else {
+                            
+                            rmSection = [sections objectAtIndex:i];
+                        }
+                        
+                        break;
+                    }
+                }
                 
-                if (orders.count > 0) {
-                    [section setObject:orders forKey:@"orders"];
-                    
-                    // 重新生成section
-                    [sections replaceObjectAtIndex:i withObject:section];
-                } else {
-                    
-                    rmSection = [sections objectAtIndex:i];
+                if (rmSection) {
+                    [sections removeObject:rmSection];
                 }
                 
-                break;
+                // 重新生成sections
+                [home setObject:sections forKey:@"sections"];
+                
             }
+            
+            // 更新Home文件
+            [self updateHome:home];
         }
         
-        if (rmSection) {
-            [sections removeObject:rmSection];
-        }
-        
-        // 重新生成sections
-        [home setObject:sections forKey:@"sections"];
-        
-        // 更新Home文件
-        [self updateHome:home];
     }
     
     return @{