Commit d83551e1 authored by 井庆林's avatar 井庆林

code review

parent deb90c17
//
// PhobosDataManager.h
// GMCache
//
// Created by Locus on 2020/1/21.
//
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, PhobosModelSendStatus) {
PhobosModelSendStatusNormal = 0,
PhobosModelSendStatusToBeSend,
PhobosModelSendStatusSending,
PhobosModelSendStatusFinish,
PhobosModelSendStatusError,
};
@interface PhobosSendModel : NSObject
@property (nonatomic, copy, readonly) NSString *sendId;
@property (nonatomic, strong) id data;
@property (nonatomic, copy) NSString *sendAPI;
@property (nonatomic, assign) PhobosModelSendStatus sendStatus;
- (instancetype)initWithData:(id)data sendAPI:(NSString *)api;
@end
@interface PhobosDataManager : NSObject
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)v immediately:(BOOL)immediately;
+ (void)updateDataModel:(PhobosSendModel *)model sendStatus:(PhobosModelSendStatus)sendStatus;
+ (void)deleteDataModel:(PhobosSendModel *)model;
+ (void)removeAllDataWithImmediately:(BOOL)immediately;
+ (NSArray<PhobosSendModel *> *)fetchDataModelsWithStatus:(PhobosModelSendStatus)stauts;
+ (void)sendData;
@end
NS_ASSUME_NONNULL_END
//
// PhobosDataManager.m
// GMCache
//
// Created by Locus on 2020/1/21.
//
#import "PhobosDataManager.h"
#import <GMCache/GMCache.h>
#import "PhobosUtil.h"
#import "PhobosConfig.h"
#import <MJExtension/MJExtension.h>
@implementation PhobosSendModel
- (instancetype)initWithData:(id)data sendAPI:(NSString *)sendAPI {
if (self = [super init]) {
_data = data;
_sendId = [PhobosUtil currentMMTime];
_sendAPI = sendAPI;
}
return self;
}
@end
@implementation PhobosDataManager
+ (NSArray<PhobosSendModel *> *)fetchCache {
NSArray *data = [GMCache fetchObjectAtDocumentPathWithkey:PhobosNewCacheKey];
NSArray<PhobosSendModel *> *sendModelArray = [PhobosSendModel mj_objectArrayWithKeyValuesArray:data];
return sendModelArray ?: [NSArray new];
}
+ (void)storeCache:(NSArray<PhobosSendModel *> *)sendModelArray {
NSArray *data = [PhobosSendModel mj_keyValuesArrayWithObjectArray:sendModelArray];
[GMCache storeObjectAtDocumentPathWithkey:PhobosNewCacheKey object:data];
}
+ (void)insertData:(NSDictionary *)data sendAPI:(NSString *)sendAPI immediately:(BOOL)immediately {
NSMutableArray<PhobosSendModel *> *datas = [[self fetchCache] mutableCopy];
PhobosSendModel *model = [[PhobosSendModel alloc] initWithData:data sendAPI:sendAPI];
model.sendStatus = immediately ? PhobosModelSendStatusToBeSend: PhobosModelSendStatusNormal;
[datas addObject:model];
[self storeCache:datas];
}
+ (void)updateDataModel:(PhobosSendModel *)model sendStatus:(PhobosModelSendStatus)sendStatus {
NSMutableArray<PhobosSendModel *> *datas = [[self fetchCache] mutableCopy];
__block NSInteger updateIdx = -1;
[datas enumerateObjectsUsingBlock:^(PhobosSendModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([model.sendId isEqualToString:obj.sendId]) {
updateIdx = idx;
model.sendStatus = sendStatus;
*stop = YES;
}
}];
if (updateIdx != -1) {
[datas replaceObjectAtIndex:updateIdx withObject:model];
}
[self storeCache:datas];
}
+ (void)deleteDataModel:(PhobosSendModel *)model {
NSMutableArray<PhobosSendModel *> *datas = [[self fetchCache] mutableCopy];
__block NSInteger updateIdx = -1;
[datas enumerateObjectsUsingBlock:^(PhobosSendModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if ([model.sendId isEqualToString:obj.sendId]) {
updateIdx = idx;
*stop = YES;
}
}];
[datas removeObjectAtIndex:updateIdx];
[self storeCache:datas];
}
+ (void)removeAllDataWithImmediately:(BOOL)immediately {
NSArray<PhobosSendModel *> *datas = [self fetchCache];
NSMutableArray<PhobosSendModel *> *temp = [datas mutableCopy];
[datas enumerateObjectsUsingBlock:^(PhobosSendModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (immediately) {
if (obj.sendStatus == PhobosModelSendStatusFinish) {
[temp removeObject:obj];
}
} else {
if (obj.sendStatus == PhobosModelSendStatusNormal) {
obj.sendStatus = PhobosModelSendStatusToBeSend;
[temp replaceObjectAtIndex:idx withObject:obj];
}
}
}];
[self storeCache:temp];
}
+ (NSArray<PhobosSendModel *> *)fetchDataModelsWithStatus:(PhobosModelSendStatus)status {
NSArray<PhobosSendModel *> *data = [self fetchCache];
NSMutableArray *temp = [NSMutableArray new];
[data enumerateObjectsUsingBlock:^(PhobosSendModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.sendStatus == status) {
[temp addObject:obj];
}
}];
return temp;
}
+ (void)sendData {
NSMutableDictionary *sendDataMap = [NSMutableDictionary new];
NSArray<PhobosSendModel *> *needSendData = [self fetchCache];
[needSendData enumerateObjectsUsingBlock:^(PhobosSendModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (obj.sendStatus == PhobosModelSendStatusToBeSend || obj.sendStatus == PhobosModelSendStatusError) {
NSMutableArray *temp = sendDataMap[obj.sendAPI] ?: [NSMutableArray new];
[temp addObject:obj];
[sendDataMap setValue:temp forKey:obj.sendAPI];
}
}];
[sendDataMap enumerateKeysAndObjectsUsingBlock:^(NSString *api, NSArray<PhobosSendModel *> *data, BOOL * _Nonnull stop) {
NSMutableArray *temp = [NSMutableArray new];
[data enumerateObjectsUsingBlock:^(PhobosSendModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self updateDataModel:obj sendStatus:PhobosModelSendStatusSending];
[temp addObject:obj.data];
}];
if (temp.count > 0) {
NSData *JSON = [PhobosUtil encodeJSON:temp];
NSData *compressedData = [PhobosUtil compressData:JSON];
[PhobosUtil sendData:compressedData currentAPI:api success:^(NSInteger code) {
[data enumerateObjectsUsingBlock:^(PhobosSendModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
dispatch_async(dispatch_get_main_queue(), ^{
if (code == 200) {
[self deleteDataModel:obj];
} else {
[self updateDataModel:obj sendStatus:PhobosModelSendStatusError];
}
});
}];
}];
}
}];
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment