125 lines
3.8 KiB
Objective-C
125 lines
3.8 KiB
Objective-C
//
|
|
// XPGiftStorage.m
|
|
// xplan-ios
|
|
//
|
|
// Created by 冯硕 on 2021/11/17.
|
|
//
|
|
|
|
#import "XPGiftStorage.h"
|
|
#import "GiftInfoModel.h"
|
|
#import "NSObject+MJExtension.h"
|
|
#import "NSArray+Safe.h"
|
|
@interface XPGiftStorage ()
|
|
///key:房间id value:房间对应的礼物列表
|
|
@property (nonatomic, strong) NSCache<NSString *, NSArray<GiftInfoModel *> *> *roomGiftCache;
|
|
///当前房间的uid
|
|
@property (nonatomic,copy) NSString *currentRoomUid;
|
|
///
|
|
@property (nonatomic,assign) BOOL isWriteToFile;
|
|
@end
|
|
|
|
@implementation XPGiftStorage
|
|
|
|
+ (instancetype)shareStorage {
|
|
static dispatch_once_t onceToken;
|
|
static XPGiftStorage * storage;
|
|
dispatch_once(&onceToken, ^{
|
|
storage = [[XPGiftStorage alloc] init];
|
|
});
|
|
return storage;
|
|
}
|
|
|
|
|
|
- (void)saveGiftDatasource:(NSArray<GiftInfoModel *> *)giftArray roomUid:(NSString *)roomUid {
|
|
if (giftArray.count > 0 && roomUid.length > 0) {
|
|
[self.roomGiftCache setObject:giftArray forKey:roomUid];
|
|
}
|
|
}
|
|
|
|
- (NSArray<GiftInfoModel *> *)getGiftDatasource:(NSString *)roomuid {
|
|
if (roomuid.length > 0) {
|
|
self.currentRoomUid = roomuid;
|
|
NSArray *giftLists = [self.roomGiftCache objectForKey:roomuid];
|
|
if (giftLists.count >= 0) {
|
|
return giftLists;
|
|
}
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
/// 获取当前房间的数据中的礼物
|
|
/// @param giftId 礼物的id
|
|
- (GiftInfoModel *)findGiftInfo:(NSString *)giftId {
|
|
GiftInfoModel * giftInfo = [self getGiftInfoFromDirectory:giftId];
|
|
if (giftInfo) {
|
|
return giftInfo;
|
|
}
|
|
|
|
if (self.currentRoomUid) {
|
|
NSArray<GiftInfoModel *> *giftLists = [self.roomGiftCache objectForKey:self.currentRoomUid];
|
|
if (giftLists.count > 0) {
|
|
__block GiftInfoModel * giftInfo;
|
|
[giftLists enumerateObjectsUsingBlock:^(GiftInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
if (obj.giftId == giftId.integerValue) {
|
|
giftInfo = obj;
|
|
*stop = YES;
|
|
}
|
|
}];
|
|
return giftInfo;
|
|
}
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
|
|
- (NSCache<NSString *,NSArray<GiftInfoModel *> *> *)roomGiftCache {
|
|
if (!_roomGiftCache) {
|
|
_roomGiftCache = [[NSCache alloc] init];
|
|
// 设置缓存数据的数目
|
|
_roomGiftCache.countLimit = 10;
|
|
}
|
|
return _roomGiftCache;
|
|
}
|
|
|
|
- (void)writeGiftToDirectory:(NSArray *)array {
|
|
///因为不想每次都要大量的IO操作 就保存一份吧
|
|
if (self.isWriteToFile) {
|
|
return;
|
|
}
|
|
NSString * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
|
|
NSString * giftDirPath = [path stringByAppendingPathComponent:@"Gift"];
|
|
//判断有没有文件夹
|
|
BOOL isDir =NO;
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
BOOL existed = [fileManager fileExistsAtPath:giftDirPath isDirectory:&isDir];
|
|
if ( !(isDir ==YES && existed == YES) ){
|
|
//如果没有文件夹则创建
|
|
[fileManager createDirectoryAtPath:giftDirPath withIntermediateDirectories:YES attributes:nil error:nil];
|
|
}
|
|
for (int i = 0; i< array.count; i++) {
|
|
GiftInfoModel * giftInfoModel = [array safeObjectAtIndex1:i];
|
|
NSString *giftPath = [giftDirPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld", giftInfoModel.giftId]];
|
|
[NSKeyedArchiver archiveRootObject:giftInfoModel.model2dictionary toFile:giftPath];
|
|
}
|
|
self.isWriteToFile = YES;
|
|
}
|
|
|
|
- (GiftInfoModel *)getGiftInfoFromDirectory:(NSString *)giftId {
|
|
//找到相应的目录
|
|
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
|
|
NSString *pathFile = [path stringByAppendingPathComponent:@"Gift"];
|
|
//判断有没有文件夹
|
|
BOOL isDir =NO;
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
BOOL existed = [fileManager fileExistsAtPath:pathFile isDirectory:&isDir];
|
|
GiftInfoModel * giftInfo;
|
|
if (existed) {
|
|
NSString *aPath = [pathFile stringByAppendingPathComponent:giftId];
|
|
NSDictionary * dic = [NSKeyedUnarchiver unarchiveObjectWithFile:aPath];
|
|
giftInfo = [GiftInfoModel modelWithDictionary:dic];
|
|
}
|
|
return giftInfo;
|
|
}
|
|
|
|
@end
|