
- Removed YuMi/Library/ (138 MB, not tracked) - Removed YuMi/Resources/ (23 MB, not tracked) - Removed old version assets (566 files, not tracked) - Excluded Pods/, xcuserdata/ and other build artifacts - Clean repository optimized for company server deployment
172 lines
5.5 KiB
Objective-C
172 lines
5.5 KiB
Objective-C
//
|
||
// DataUtils.m
|
||
// YYFaceAuth
|
||
//
|
||
// Created by chenran on 16/10/18.
|
||
// Copyright © 2016年 zhangji. All rights reserved.
|
||
//
|
||
#define kFileName @"AccountInfo.data"
|
||
#define kDataKey @"accountInfo"
|
||
#define kHomeUserDataKey @"homeUserInfo"
|
||
#define kHomeUserName @"HomeUserModel.data"
|
||
|
||
#import "AccountInfoStorage.h"
|
||
#import "AccountModel.h"
|
||
#import "HomeTagModel.h"
|
||
|
||
#define kTagListDataKey @"homeaaNewTagModel"
|
||
#define kTagListDataName @"HomeTagNewModel.data"
|
||
@interface AccountInfoStorage()
|
||
@property (nonatomic,strong) HomeUserModel *homeUserInfo;
|
||
@property (nonatomic, copy) NSString *ticket;
|
||
@property (nonatomic, strong) AccountModel *accountModel;
|
||
@property (nonatomic,strong) NSArray<HomeTagModel *> *tagList;
|
||
@end
|
||
|
||
@implementation AccountInfoStorage
|
||
|
||
static AccountInfoStorage *_instance = nil;
|
||
|
||
+ (AccountInfoStorage *)instance {
|
||
|
||
static dispatch_once_t onceToken;
|
||
|
||
dispatch_once(&onceToken, ^{
|
||
_instance = [[self alloc] init];
|
||
}) ;
|
||
|
||
return _instance;
|
||
}
|
||
|
||
-(NSString *) getFilePath{
|
||
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||
NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:kFileName];
|
||
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
|
||
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
|
||
}
|
||
return path;
|
||
}
|
||
|
||
- (AccountModel *)getCurrentAccountInfo
|
||
{
|
||
if (self.accountModel != nil) {
|
||
return self.accountModel;
|
||
}
|
||
|
||
if ([[NSFileManager defaultManager] fileExistsAtPath:[self getFilePath]] == NO) {
|
||
return nil;
|
||
}
|
||
|
||
NSData *data = [[NSData alloc] initWithContentsOfFile:[self getFilePath]];
|
||
if (data == nil) {
|
||
return nil;
|
||
}
|
||
|
||
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
|
||
//解档出数据模型
|
||
self.accountModel = [unarchiver decodeObjectForKey:kDataKey];
|
||
[unarchiver finishDecoding];//一定不要忘记finishDecoding,否则会报错
|
||
return self.accountModel;
|
||
}
|
||
|
||
- (void)saveAccountInfo:(AccountModel *)accountInfo
|
||
{
|
||
if (accountInfo == nil) {
|
||
accountInfo = [[AccountModel alloc] init];
|
||
}
|
||
self.accountModel = accountInfo;
|
||
NSMutableData *data = [[NSMutableData alloc] init];
|
||
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
|
||
[archiver encodeObject:accountInfo forKey:kDataKey];
|
||
[archiver finishEncoding];
|
||
[data writeToFile:[self getFilePath] atomically:YES];
|
||
}
|
||
- (HomeUserModel *)getCurrentHomeUserInfo
|
||
{
|
||
if (self.homeUserInfo != nil) {
|
||
return self.homeUserInfo;
|
||
}
|
||
NSData *data = [[NSData alloc] initWithContentsOfFile:[self getHomeUserFilePath]];
|
||
|
||
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
|
||
//解档出数据模型
|
||
self.homeUserInfo = [unarchiver decodeObjectForKey:kHomeUserDataKey];
|
||
[unarchiver finishDecoding];//一定不要忘记finishDecoding,否则会报错
|
||
return self.homeUserInfo;
|
||
}
|
||
- (void)saveHomeUserInfo:(HomeUserModel *)homeUserInfo
|
||
{
|
||
if (homeUserInfo == nil) {
|
||
homeUserInfo = [[HomeUserModel alloc] init];
|
||
}
|
||
self.homeUserInfo = homeUserInfo;
|
||
NSMutableData *data = [[NSMutableData alloc] init];
|
||
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
|
||
[archiver encodeObject:homeUserInfo forKey:kHomeUserDataKey];
|
||
[archiver finishEncoding];
|
||
[data writeToFile:[self getHomeUserFilePath] atomically:YES];
|
||
}
|
||
-(NSString *) getHomeUserFilePath{
|
||
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||
NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:kHomeUserName];
|
||
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
|
||
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
|
||
}
|
||
return path;
|
||
}
|
||
- (void)saveTicket:(NSString *)t
|
||
{
|
||
self.ticket = t;
|
||
}
|
||
|
||
- (NSString *)getTicket
|
||
{
|
||
if (self.ticket == nil) {
|
||
return @"";
|
||
}
|
||
return self.ticket;
|
||
}
|
||
|
||
- (NSString *)getUid
|
||
{
|
||
AccountModel *am = [self getCurrentAccountInfo];
|
||
if (am == nil) {
|
||
return @"";
|
||
}
|
||
return am.uid;
|
||
}
|
||
|
||
- (NSArray<HomeTagModel *> *)getCurrentTagList{
|
||
if (self.tagList != nil) {
|
||
return self.tagList;
|
||
}
|
||
NSData *data = [[NSData alloc] initWithContentsOfFile:[self getTagListFilePath]];
|
||
|
||
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
|
||
//解档出数据模型
|
||
self.tagList = [unarchiver decodeObjectForKey:kTagListDataKey];
|
||
[unarchiver finishDecoding];//一定不要忘记finishDecoding,否则会报错
|
||
return self.tagList != nil ? self.tagList : @[];
|
||
}
|
||
- (void)saveTagList:(NSArray<HomeTagModel *> *)tagList{
|
||
if(tagList == nil){
|
||
tagList = @[];
|
||
}
|
||
self.tagList = tagList;
|
||
NSMutableData *data = [[NSMutableData alloc] init];
|
||
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
|
||
[archiver encodeObject:tagList forKey:kTagListDataKey];
|
||
[archiver finishEncoding];
|
||
[data writeToFile:[self getTagListFilePath] atomically:YES];
|
||
}
|
||
|
||
-(NSString *) getTagListFilePath{
|
||
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
||
NSString *path = [[array objectAtIndex:0] stringByAppendingPathComponent:kTagListDataName];
|
||
if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
|
||
[[NSFileManager defaultManager] createFileAtPath:path contents:nil attributes:nil];
|
||
}
|
||
return path;
|
||
}
|
||
@end
|