//
//  UserArchive.m
//
//  Created by ls on 2019/7/22.
//  Copyright © 2019 CY. All rights reserved.
//

#import "UserArchive.h"

@implementation UserArchive

#pragma mark - NSCoding
- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.username forKey:@"username"];
    [aCoder encodeObject:self.phone forKey:@"phone"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
    [aCoder encodeObject:self.height forKey:@"height"];
    [aCoder encodeObject:self.weight forKey:@"weight"];
    [aCoder encodeObject:self.age forKey:@"age"];
    [aCoder encodeBool:self.isAutoLogin forKey:@"isAutoLogin"];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super init]) {
        self.username = [aDecoder decodeObjectForKey:@"username"];
        self.phone = [aDecoder decodeObjectForKey:@"phone"];
        self.sex = [aDecoder decodeObjectForKey:@"sex"];
        self.height = [aDecoder decodeObjectForKey:@"height"];
        self.weight = [aDecoder decodeObjectForKey:@"weight"];
        self.age = [aDecoder decodeObjectForKey:@"age"];
        self.isAutoLogin = [aDecoder decodeBoolForKey:@"isAutoLogin"];
    }
    return self;
}

+(void)encodeUser:(UserArchive*)lastUser
{
    NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString* docpath = [path stringByAppendingPathComponent:@"CYUSER.achive"];
    [NSKeyedArchiver archiveRootObject:lastUser toFile:docpath];
}

+(UserArchive*)aDecoderUser
{
    NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString* docpath = [path stringByAppendingPathComponent:@"CYUSER.achive"];
    UserArchive* user = [NSKeyedUnarchiver unarchiveObjectWithFile:docpath];
    if(user == nil)
    {
        user = [[UserArchive alloc] init];
    }
    return user;
}

@end
