diff --git a/LifeLog/LifeLog/AppDelegate.m b/LifeLog/LifeLog/AppDelegate.m index 2ceb599..08ceefe 100644 --- a/LifeLog/LifeLog/AppDelegate.m +++ b/LifeLog/LifeLog/AppDelegate.m @@ -13,6 +13,7 @@ #import "RankingViewController.h" #import "MapViewController.h" #import "SNSViewController.h" +#import "ServerAPI.h" @interface AppDelegate () @@ -28,15 +29,17 @@ // Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; - if ([[NSUserDefaults standardUserDefaults] objectForKey:@"User_Login"]) { - [self gotoMainMenu]; - } - else { + NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kUser]; + User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData:data]; + if (user == nil) { LoginViewController *loginVC = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil]; UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:loginVC]; self.window.rootViewController = navigation; [self.window makeKeyAndVisible]; } + else { + [self gotoMainMenu]; + } return YES; } diff --git a/LifeLog/LifeLog/HomeViewController.m b/LifeLog/LifeLog/HomeViewController.m index a8fe9c1..3e5ae52 100644 --- a/LifeLog/LifeLog/HomeViewController.m +++ b/LifeLog/LifeLog/HomeViewController.m @@ -13,6 +13,7 @@ #import "TodayViewController.h" #import "Utilities.h" #import +#import "ServerAPI.h" static NSInteger numberTotal = 10000; @@ -59,6 +60,34 @@ static NSInteger numberTotal = 10000; self.avatar.layer.borderColor = [[UIColor whiteColor] CGColor]; self.avatar.layer.cornerRadius = self.avatar.frame.size.width/2.0f; self.avatar.layer.masksToBounds = YES; + NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kUser]; + User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData:data]; + if (user != nil) { + NSString *linkImage = [NSString stringWithFormat:@"%@%@", kServerAddress, user.profile_image]; + NSURL *urlImage = [NSURL URLWithString:linkImage]; + NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; + sessionConfig.timeoutIntervalForRequest = 30.0; + sessionConfig.timeoutIntervalForResource = 60.0; + sessionConfig.HTTPMaximumConnectionsPerHost = 20; + sessionConfig.allowsCellularAccess = YES; + HomeViewController __weak *weakSelf = self; + NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig]; + NSURLSessionDataTask *downloadPhotoTask = [session + dataTaskWithURL:urlImage completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { + if (weakSelf == nil) + { + return; + } + if (error == nil) { + UIImage *image = [[UIImage alloc] initWithData:data]; + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + weakSelf.avatar.image = image; + }]; + } + }]; + [downloadPhotoTask resume]; + } + self.lblNotice.text = NSLocalizedString(@"lifelog.home.notice", nil); _dateCurrent = [NSDate date]; diff --git a/LifeLog/LifeLog/LoginViewController.m b/LifeLog/LifeLog/LoginViewController.m index c20d044..9187fb7 100644 --- a/LifeLog/LifeLog/LoginViewController.m +++ b/LifeLog/LifeLog/LoginViewController.m @@ -59,6 +59,11 @@ [[ServerAPI server] loginWithEmail:_tfEmail.text Password:_tfPassword.text CompletionHandler:^(User *user, NSString *token, NSError *error) { if (error == nil) { // save user and goto MainMenu + // For Saving + NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:user]; + [[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:kUser]; + [[NSUserDefaults standardUserDefaults] setObject:token forKey:kToken]; + [[NSUserDefaults standardUserDefaults] synchronize]; dispatch_async(dispatch_get_main_queue(), ^{ [[AppDelegate sharedAppDelegate] gotoMainMenu]; }); diff --git a/LifeLog/LifeLog/RegisterViewController.m b/LifeLog/LifeLog/RegisterViewController.m index 033b991..96f584e 100644 --- a/LifeLog/LifeLog/RegisterViewController.m +++ b/LifeLog/LifeLog/RegisterViewController.m @@ -534,6 +534,11 @@ static int const kPickerTagSex = 2; return ; } if (error == nil) { + // For Saving + NSData *dataSave = [NSKeyedArchiver archivedDataWithRootObject:user]; + [[NSUserDefaults standardUserDefaults] setObject:dataSave forKey:kUser]; + [[NSUserDefaults standardUserDefaults] setObject:token forKey:kToken]; + [[NSUserDefaults standardUserDefaults] synchronize]; dispatch_async(dispatch_get_main_queue(), ^{ [[AppDelegate sharedAppDelegate] gotoMainMenu]; }); diff --git a/LifeLog/LifeLog/ServerAPI.h b/LifeLog/LifeLog/ServerAPI.h index 6c870ed..ba32715 100644 --- a/LifeLog/LifeLog/ServerAPI.h +++ b/LifeLog/LifeLog/ServerAPI.h @@ -8,6 +8,9 @@ #import #import "Entities.h" +extern NSString *const kServerAddress; +extern NSString *const kUser; +extern NSString *const kToken; @interface ServerAPI : NSObject + (instancetype) server; diff --git a/LifeLog/LifeLog/ServerAPI.m b/LifeLog/LifeLog/ServerAPI.m index 578d5b7..3c1c076 100644 --- a/LifeLog/LifeLog/ServerAPI.m +++ b/LifeLog/LifeLog/ServerAPI.m @@ -9,6 +9,8 @@ #import "ServerAPI.h" NSString *const kServerAddress = @"http://clover.timesfun.jp:9001/"; +NSString *const kUser = @"KEY_USER"; +NSString *const kToken = @"KEY_TOKEN"; @implementation NSString (NSString_Extended) - (NSString *)urlencode { @@ -86,6 +88,17 @@ static ServerAPI *_server = nil; user.height = [[NSString stringWithFormat:@"%@",dictUser[@"height"]] floatValue]; user.weight = [[NSString stringWithFormat:@"%@",dictUser[@"weight"]] floatValue]; user.user_description = [NSString stringWithFormat:@"%@",dictUser[@"description"]]; + user.created_at = [NSString stringWithFormat:@"%@",dictUser[@"created_at"]]; + user.physical_activity = [NSString stringWithFormat:@"%@",dictUser[@"physical_activity"]]; + user.profile_image = [NSString stringWithFormat:@"%@",dictUser[@"profile_image"]]; + user.updated_at = [NSString stringWithFormat:@"%@",dictUser[@"updated_at"]]; + user.delete_flag = [[NSString stringWithFormat:@"%@",dictUser[@"delete_flag"]] intValue]; + user.fat_rate = [[NSString stringWithFormat:@"%@",dictUser[@"fat_rate"]] intValue]; + user.profiles_share = [[NSString stringWithFormat:@"%@",dictUser[@"profiles_share"]] intValue]; + user.remember_me = [[NSString stringWithFormat:@"%@",dictUser[@"remember_me"]] intValue]; + user.sound_notifications_share = [[NSString stringWithFormat:@"%@",dictUser[@"sound_notifications_share"]] intValue]; + user.spend_calo_in_day = [[NSString stringWithFormat:@"%@",dictUser[@"spend_calo_in_day"]] intValue]; + user.target = [[NSString stringWithFormat:@"%@",dictUser[@"target"]] intValue]; completion(user, token, nil); } else { // status = 0 error @@ -130,6 +143,17 @@ static ServerAPI *_server = nil; user.height = [[NSString stringWithFormat:@"%@",dictUser[@"height"]] floatValue]; user.weight = [[NSString stringWithFormat:@"%@",dictUser[@"weight"]] floatValue]; user.user_description = [NSString stringWithFormat:@"%@",dictUser[@"description"]]; + user.created_at = [NSString stringWithFormat:@"%@",dictUser[@"created_at"]]; + user.physical_activity = [NSString stringWithFormat:@"%@",dictUser[@"physical_activity"]]; + user.profile_image = [NSString stringWithFormat:@"%@",dictUser[@"profile_image"]]; + user.updated_at = [NSString stringWithFormat:@"%@",dictUser[@"updated_at"]]; + user.delete_flag = [[NSString stringWithFormat:@"%@",dictUser[@"delete_flag"]] intValue]; + user.fat_rate = [[NSString stringWithFormat:@"%@",dictUser[@"fat_rate"]] intValue]; + user.profiles_share = [[NSString stringWithFormat:@"%@",dictUser[@"profiles_share"]] intValue]; + user.remember_me = [[NSString stringWithFormat:@"%@",dictUser[@"remember_me"]] intValue]; + user.sound_notifications_share = [[NSString stringWithFormat:@"%@",dictUser[@"sound_notifications_share"]] intValue]; + user.spend_calo_in_day = [[NSString stringWithFormat:@"%@",dictUser[@"spend_calo_in_day"]] intValue]; + user.target = [[NSString stringWithFormat:@"%@",dictUser[@"target"]] intValue]; completion(user, token, nil); } else { // status = 0 error diff --git a/LifeLog/LifeLog/User.h b/LifeLog/LifeLog/User.h index 26f19b6..d013ffc 100644 --- a/LifeLog/LifeLog/User.h +++ b/LifeLog/LifeLog/User.h @@ -22,4 +22,15 @@ @property (nonatomic, strong) NSString *user_description; @property (nonatomic, strong) NSString *email; @property (nonatomic, strong) NSString *phone; +@property (nonatomic, strong) NSString *created_at; +@property (nonatomic, assign) int delete_flag; +@property (nonatomic, assign) int fat_rate; +@property (nonatomic, strong) NSString *physical_activity; +@property (nonatomic, strong) NSString *profile_image; +@property (nonatomic, assign) int profiles_share; +@property (nonatomic, assign) int remember_me; +@property (nonatomic, assign) int sound_notifications_share; +@property (nonatomic, assign) int spend_calo_in_day; +@property (nonatomic, assign) int target; +@property (nonatomic, strong) NSString *updated_at; @end diff --git a/LifeLog/LifeLog/User.m b/LifeLog/LifeLog/User.m index 92e7e0f..9b8ac24 100644 --- a/LifeLog/LifeLog/User.m +++ b/LifeLog/LifeLog/User.m @@ -28,6 +28,17 @@ self.user_description = [decoder decodeObjectForKey:@"user_description"]; self.email = [decoder decodeObjectForKey:@"email"]; self.phone = [decoder decodeObjectForKey:@"phone"]; + self.created_at = [decoder decodeObjectForKey:@"created_at"]; + self.physical_activity = [decoder decodeObjectForKey:@"physical_activity"]; + self.profile_image = [decoder decodeObjectForKey:@"profile_image"]; + self.updated_at = [decoder decodeObjectForKey:@"updated_at"]; + self.delete_flag = [decoder decodeIntForKey:@"delete_flag"]; + self.fat_rate = [decoder decodeIntForKey:@"fat_rate"]; + self.profiles_share = [decoder decodeIntForKey:@"profiles_share"]; + self.remember_me = [decoder decodeIntForKey:@"remember_me"]; + self.sound_notifications_share = [decoder decodeIntForKey:@"sound_notifications_share"]; + self.spend_calo_in_day = [decoder decodeIntForKey:@"spend_calo_in_day"]; + self.target = [decoder decodeIntForKey:@"target"]; return self; } @@ -46,5 +57,16 @@ [encoder encodeObject:self.user_description forKey:@"user_description"]; [encoder encodeObject:self.email forKey:@"email"]; [encoder encodeObject:self.phone forKey:@"phone"]; + [encoder encodeObject:self.created_at forKey:@"created_at"]; + [encoder encodeObject:self.physical_activity forKey:@"physical_activity"]; + [encoder encodeObject:self.profile_image forKey:@"profile_image"]; + [encoder encodeObject:self.updated_at forKey:@"updated_at"]; + [encoder encodeInt:self.delete_flag forKey:@"delete_flag"]; + [encoder encodeInt:self.fat_rate forKey:@"fat_rate"]; + [encoder encodeInt:self.profiles_share forKey:@"profiles_share"]; + [encoder encodeInt:self.remember_me forKey:@"remember_me"]; + [encoder encodeInt:self.sound_notifications_share forKey:@"sound_notifications_share"]; + [encoder encodeInt:self.spend_calo_in_day forKey:@"spend_calo_in_day"]; + [encoder encodeInt:self.target forKey:@"target"]; } @end