Commit 26548f2f08a9decba01b756a117a5a80b653dfbb

Authored by phong
1 parent 920357ae44
Exists in master

add notice to Home screen

Showing 12 changed files with 639 additions and 451 deletions Side-by-side Diff

LifeLog/LifeLog.xcodeproj/project.pbxproj
... ... @@ -12,6 +12,8 @@
12 12 6E18CED41F2E39DB0029891F /* LoginViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6E18CED21F2E39DB0029891F /* LoginViewController.xib */; };
13 13 6E18CED81F2E3C840029891F /* ServerAPI.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E18CED71F2E3C840029891F /* ServerAPI.m */; };
14 14 6E18CEDC1F2E40F50029891F /* User.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E18CEDB1F2E40F50029891F /* User.m */; };
  15 + 6E27A74A1F9E41B400C390B3 /* HomeObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E27A7491F9E41B400C390B3 /* HomeObject.m */; };
  16 + 6E27A74D1F9E432900C390B3 /* NoticeInfor.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E27A74C1F9E432900C390B3 /* NoticeInfor.m */; };
15 17 6E48AF0D1F2F9D3300C1D184 /* CustomTextField.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E48AF0C1F2F9D3200C1D184 /* CustomTextField.m */; };
16 18 6E7F93421F35872B00EFBC71 /* TPKeyboardAvoidingCollectionView.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E7F933B1F35872B00EFBC71 /* TPKeyboardAvoidingCollectionView.m */; };
17 19 6E7F93431F35872B00EFBC71 /* TPKeyboardAvoidingScrollView.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E7F933D1F35872B00EFBC71 /* TPKeyboardAvoidingScrollView.m */; };
... ... @@ -23,7 +25,7 @@
23 25 6E84E3841F27A701001EB88E /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6E84E3821F27A701001EB88E /* LaunchScreen.storyboard */; };
24 26 6E84E38F1F27A703001EB88E /* LifeLogTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E84E38E1F27A703001EB88E /* LifeLogTests.m */; };
25 27 6E84E39A1F27A703001EB88E /* LifeLogUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E84E3991F27A703001EB88E /* LifeLogUITests.m */; };
26   - 6E84E3B01F27A975001EB88E /* HomeViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E84E3AE1F27A975001EB88E /* HomeViewController.m */; };
  28 + 6E84E3B01F27A975001EB88E /* HomeViewConb troller.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E84E3AE1F27A975001EB88E /* HomeViewConb troller.m */; };
27 29 6E84E3B11F27A975001EB88E /* HomeViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6E84E3AF1F27A975001EB88E /* HomeViewController.xib */; };
28 30 6E84E3B51F27A98B001EB88E /* HistoryViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 6E84E3B31F27A98B001EB88E /* HistoryViewController.m */; };
29 31 6E84E3B61F27A98B001EB88E /* HistoryViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 6E84E3B41F27A98B001EB88E /* HistoryViewController.xib */; };
... ... @@ -107,6 +109,10 @@
107 109 6E18CEDA1F2E40F50029891F /* User.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = User.h; sourceTree = "<group>"; };
108 110 6E18CEDB1F2E40F50029891F /* User.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = User.m; sourceTree = "<group>"; };
109 111 6E18CEDD1F2E46570029891F /* Entities.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Entities.h; sourceTree = "<group>"; };
  112 + 6E27A7481F9E41B300C390B3 /* HomeObject.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HomeObject.h; sourceTree = "<group>"; };
  113 + 6E27A7491F9E41B400C390B3 /* HomeObject.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HomeObject.m; sourceTree = "<group>"; };
  114 + 6E27A74B1F9E432900C390B3 /* NoticeInfor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NoticeInfor.h; sourceTree = "<group>"; };
  115 + 6E27A74C1F9E432900C390B3 /* NoticeInfor.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NoticeInfor.m; sourceTree = "<group>"; };
110 116 6E48AF0B1F2F9D3200C1D184 /* CustomTextField.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomTextField.h; sourceTree = "<group>"; };
111 117 6E48AF0C1F2F9D3200C1D184 /* CustomTextField.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CustomTextField.m; sourceTree = "<group>"; };
112 118 6E7F933A1F35872B00EFBC71 /* TPKeyboardAvoidingCollectionView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TPKeyboardAvoidingCollectionView.h; sourceTree = "<group>"; };
... ... @@ -131,7 +137,7 @@
131 137 6E84E3991F27A703001EB88E /* LifeLogUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LifeLogUITests.m; sourceTree = "<group>"; };
132 138 6E84E39B1F27A703001EB88E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
133 139 6E84E3AD1F27A975001EB88E /* HomeViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HomeViewController.h; sourceTree = "<group>"; };
134   - 6E84E3AE1F27A975001EB88E /* HomeViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HomeViewController.m; sourceTree = "<group>"; };
  140 + 6E84E3AE1F27A975001EB88E /* HomeViewConb troller.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "HomeViewConb troller.m"; sourceTree = "<group>"; };
135 141 6E84E3AF1F27A975001EB88E /* HomeViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = HomeViewController.xib; sourceTree = "<group>"; };
136 142 6E84E3B21F27A98B001EB88E /* HistoryViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HistoryViewController.h; sourceTree = "<group>"; };
137 143 6E84E3B31F27A98B001EB88E /* HistoryViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HistoryViewController.m; sourceTree = "<group>"; };
... ... @@ -296,6 +302,10 @@
296 302 E9497FD41F46EC1100E6AF8D /* MemberObject.m */,
297 303 E97588FA1F513BAD00782484 /* RankingObject.h */,
298 304 E97588FB1F513BAD00782484 /* RankingObject.m */,
  305 + 6E27A7481F9E41B300C390B3 /* HomeObject.h */,
  306 + 6E27A7491F9E41B400C390B3 /* HomeObject.m */,
  307 + 6E27A74B1F9E432900C390B3 /* NoticeInfor.h */,
  308 + 6E27A74C1F9E432900C390B3 /* NoticeInfor.m */,
299 309 );
300 310 name = Model;
301 311 sourceTree = "<group>";
... ... @@ -411,7 +421,7 @@
411 421 children = (
412 422 6E8DA0F31F2BAC0800CCE941 /* Today */,
413 423 6E84E3AD1F27A975001EB88E /* HomeViewController.h */,
414   - 6E84E3AE1F27A975001EB88E /* HomeViewController.m */,
  424 + 6E84E3AE1F27A975001EB88E /* HomeViewConb troller.m */,
415 425 6E84E3AF1F27A975001EB88E /* HomeViewController.xib */,
416 426 );
417 427 name = Home;
... ... @@ -845,6 +855,7 @@
845 855 E968550A1F3C444900317CEF /* AutoTransButton.m in Sources */,
846 856 6EA4AF321F40BB7A00F52CFE /* TargetInfor.m in Sources */,
847 857 E933E6741F3E0E4E002926F5 /* GroupObject.m in Sources */,
  858 + 6E27A74D1F9E432900C390B3 /* NoticeInfor.m in Sources */,
848 859 9CBDA0FA1F2ECE2C0055DED1 /* BaseViewController.m in Sources */,
849 860 6E8DA0F71F2BAC3400CCE941 /* TodayViewController.m in Sources */,
850 861 6E7F93451F35872B00EFBC71 /* UIScrollView+TPKeyboardAvoidingAdditions.m in Sources */,
851 862  
... ... @@ -852,8 +863,9 @@
852 863 E9D909211F3D559D004CF99F /* TweetObject.m in Sources */,
853 864 6E48AF0D1F2F9D3300C1D184 /* CustomTextField.m in Sources */,
854 865 E99E13AB1F336F3600C78787 /* CollectionView.m in Sources */,
855   - 6E84E3B01F27A975001EB88E /* HomeViewController.m in Sources */,
  866 + 6E84E3B01F27A975001EB88E /* HomeViewConb troller.m in Sources */,
856 867 E9373E471F3596940059355A /* AutoTransLabel.m in Sources */,
  868 + 6E27A74A1F9E41B400C390B3 /* HomeObject.m in Sources */,
857 869 6E84E3791F27A701001EB88E /* AppDelegate.m in Sources */,
858 870 6E84E3BA1F27A9A0001EB88E /* RankingViewController.m in Sources */,
859 871 6E7F93441F35872B00EFBC71 /* TPKeyboardAvoidingTableView.m in Sources */,
860 872  
... ... @@ -1025,8 +1037,10 @@
1025 1037 baseConfigurationReference = 0FC2D57087AF4571C57FAE08 /* Pods-LifeLog.debug.xcconfig */;
1026 1038 buildSettings = {
1027 1039 ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
  1040 + CODE_SIGN_IDENTITY = "iPhone Distribution";
1028 1041 "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
1029   - DEVELOPMENT_TEAM = "";
  1042 + CODE_SIGN_STYLE = Automatic;
  1043 + DEVELOPMENT_TEAM = UXU4V3VUN4;
1030 1044 INFOPLIST_FILE = LifeLog/Info.plist;
1031 1045 IPHONEOS_DEPLOYMENT_TARGET = 9.0;
1032 1046 LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
1033 1047  
... ... @@ -1049,8 +1063,10 @@
1049 1063 baseConfigurationReference = 4CFDA5714AF852358A1EB515 /* Pods-LifeLog.release.xcconfig */;
1050 1064 buildSettings = {
1051 1065 ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
  1066 + CODE_SIGN_IDENTITY = "iPhone Distribution";
1052 1067 "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
1053   - DEVELOPMENT_TEAM = "";
  1068 + CODE_SIGN_STYLE = Automatic;
  1069 + DEVELOPMENT_TEAM = UXU4V3VUN4;
1054 1070 INFOPLIST_FILE = LifeLog/Info.plist;
1055 1071 IPHONEOS_DEPLOYMENT_TARGET = 9.0;
1056 1072 LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
LifeLog/LifeLog/Entities.h
... ... @@ -16,8 +16,8 @@
16 16 #import "TweetObject.h"
17 17 #import "HistoryObject.h"
18 18 #import "User.h"
19   -#import "TopObject.h"
20 19 #import "RankingObject.h"
  20 +#import "HomeObject.h"
21 21  
22 22 #endif /* Entities_h */
LifeLog/LifeLog/HomeObject.h
  1 +//
  2 +// HomeObject.h
  3 +// LifeLog
  4 +//
  5 +// Created by Nguyen Van Phong on 10/23/17.
  6 +// Copyright ยฉ 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import <Foundation/Foundation.h>
  10 +#import "TopObject.h"
  11 +#import "NoticeInfor.h"
  12 +
  13 +@interface HomeObject : NSObject
  14 +@property (nonatomic, strong) TargetInfor *targetInfor;
  15 +@property (nonatomic, strong) NSMutableArray *listNotice;
  16 +@end
LifeLog/LifeLog/HomeObject.m
  1 +//
  2 +// HomeObject.m
  3 +// LifeLog
  4 +//
  5 +// Created by Nguyen Van Phong on 10/23/17.
  6 +// Copyright ยฉ 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import "HomeObject.h"
  10 +
  11 +@implementation HomeObject
  12 +
  13 +@end
LifeLog/LifeLog/HomeViewConb troller.m
  1 +//
  2 +// HomeViewController.m
  3 +// LifeLog
  4 +//
  5 +// Created by Nguyen Van Phong on 7/25/17.
  6 +// Copyright ยฉ 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import "HomeViewController.h"
  10 +#import "NSDate+helper.h"
  11 +#import <CoreMotion/CoreMotion.h>
  12 +#import "CMMotionActivityExtra.h"
  13 +#import "TodayViewController.h"
  14 +#import "Utilities.h"
  15 +#import <MBProgressHUD/MBProgressHUD.h>
  16 +#import "ServerAPI.h"
  17 +
  18 +static NSInteger maxRequest = 20;
  19 +static NSInteger numberTotal = 10000;
  20 +
  21 +@interface HomeViewController ()
  22 +{
  23 + MBProgressHUD *progressHud;
  24 + NSInteger targetStep;
  25 +}
  26 +@property (nonatomic, weak) IBOutlet UILabel *lblTitle;
  27 +@property (nonatomic, weak) IBOutlet UIImageView *avatar;
  28 +@property (nonatomic, weak) IBOutlet UILabel *lblDateCurrent;
  29 +@property (nonatomic, weak) IBOutlet UILabel *lblValueStep;
  30 +@property (nonatomic, weak) IBOutlet UILabel *lblUnitStep;
  31 +@property (nonatomic, weak) IBOutlet UILabel *lblValueStepOther;
  32 +@property (nonatomic, weak) IBOutlet UILabel *lblPercent;
  33 +@property (nonatomic, weak) IBOutlet UILabel *lblNotice;
  34 +@property (weak, nonatomic) IBOutlet UISegmentedControl *segmentHome;
  35 +@property (weak, nonatomic) IBOutlet UITextView *tvNotice;
  36 +
  37 +@property (nonatomic, strong) CMPedometer *pedometer;
  38 +@property (nonatomic, strong) CMMotionActivityManager *motionActivityManager;
  39 +@property (nonatomic, strong) NSOperationQueue *operationQueue;
  40 +@property (nonatomic, strong) NSTimer *timer;
  41 +@property (nonatomic, assign) NSInteger bike;
  42 +@property (nonatomic, assign) NSInteger walking;
  43 +@property (nonatomic, assign) NSInteger running;
  44 +@property (nonatomic, strong) NSDate *dateCurrent;
  45 +
  46 +@property (nonatomic, assign) int totalRequest;
  47 +@property (nonatomic, assign) int countComplete;
  48 +@property (nonatomic, assign) int currentIndex;
  49 +
  50 +@end
  51 +
  52 +@implementation HomeViewController
  53 +
  54 +- (void)viewDidLoad {
  55 + [super viewDidLoad];
  56 +
  57 + self.lblTitle.text = NSLocalizedString(@"lifelog.home.title", nil);
  58 +
  59 + [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.lblTitle attribute:NSLayoutAttributeTop multiplier:1 constant:0]];
  60 +
  61 + self.avatar.backgroundColor = [UIColor whiteColor];
  62 + self.avatar.layer.borderWidth = 2.0f;
  63 + self.avatar.layer.borderColor = [[UIColor whiteColor] CGColor];
  64 + self.avatar.layer.cornerRadius = self.avatar.frame.size.width/2.0f;
  65 + self.avatar.layer.masksToBounds = YES;
  66 + NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:kUser];
  67 + User *user = (User *)[NSKeyedUnarchiver unarchiveObjectWithData:data];
  68 + if (user != nil) {
  69 + NSString *linkImage = [NSString stringWithFormat:@"%@%@", kServerAddress, user.profile_image];
  70 + NSURL *urlImage = [NSURL URLWithString:linkImage];
  71 + NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
  72 + sessionConfig.timeoutIntervalForRequest = 30.0;
  73 + sessionConfig.timeoutIntervalForResource = 60.0;
  74 + sessionConfig.HTTPMaximumConnectionsPerHost = 20;
  75 + sessionConfig.allowsCellularAccess = YES;
  76 + HomeViewController __weak *weakSelf = self;
  77 + NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];
  78 + NSURLSessionDataTask *downloadPhotoTask = [session
  79 + dataTaskWithURL:urlImage completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  80 + if (weakSelf == nil)
  81 + {
  82 + return;
  83 + }
  84 + if (error == nil) {
  85 + UIImage *image = [[UIImage alloc] initWithData:data];
  86 + [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  87 + weakSelf.avatar.image = image;
  88 + }];
  89 + }
  90 + }];
  91 + [downloadPhotoTask resume];
  92 + }
  93 +
  94 + self.lblNotice.text = NSLocalizedString(@"lifelog.home.notice", nil);
  95 +
  96 + _dateCurrent = [NSDate date];
  97 + self.lblDateCurrent.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[_dateCurrent getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[_dateCurrent getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[_dateCurrent getDay], NSLocalizedString(@"lifelog.common.day", nil)];
  98 +
  99 + self.lblUnitStep.text = NSLocalizedString(@"lifelog.home.unit.step", nil);
  100 +
  101 + if ([CMPedometer isStepCountingAvailable]) {
  102 + _pedometer = [[CMPedometer alloc] init];
  103 + }
  104 + if ([CMMotionActivityManager isActivityAvailable]) {
  105 + _motionActivityManager = [[CMMotionActivityManager alloc] init];
  106 + }
  107 + _operationQueue = [[NSOperationQueue alloc] init];
  108 + _bike = 0;
  109 + _walking = 0;
  110 + _running = 0;
  111 + _segmentHome.selectedSegmentIndex = 1;
  112 + _totalRequest = 0;
  113 + _countComplete = 0;
  114 +
  115 + progressHud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  116 + progressHud.mode = MBProgressHUDModeIndeterminate;
  117 + progressHud.detailsLabel.text = NSLocalizedString(@"lifelog.home.progressHud.title", nil);
  118 + // [self saveDataStep7LastDay];
  119 + targetStep = numberTotal;
  120 +}
  121 +
  122 +- (void)viewWillAppear:(BOOL)animated {
  123 + [super viewWillAppear:animated];
  124 + NSDate *dateNow = [NSDate date];
  125 + [self saveStepForDay:dateNow];
  126 +}
  127 +
  128 +- (void)requestTopByDate:(NSDate *)date {
  129 + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  130 + [dateFormatter setDateFormat:@"yyyy-MM-dd"];
  131 + NSString *dateString = [dateFormatter stringFromDate:date];
  132 + [progressHud showAnimated:YES];
  133 + [progressHud setHidden:NO];
  134 + HomeViewController __weak *weakSelf = self;
  135 + int mode = 1;
  136 + switch (_segmentHome.selectedSegmentIndex) {
  137 + case 0:
  138 + mode = 3;
  139 + break;
  140 +
  141 + case 1:
  142 + mode = 1;
  143 + break;
  144 +
  145 + case 2:
  146 + mode = 2;
  147 + break;
  148 +
  149 + default:
  150 + break;
  151 + }
  152 + [[ServerAPI server] requestTopWithMode:mode andDate:dateString CompletionHandler:^(TopObject *topObject, NSError *error) {
  153 + if(weakSelf == nil) {
  154 + return ;
  155 + }
  156 + if (error == nil) {
  157 + NSLog(@"TopObject: %@", topObject);
  158 + dispatch_async(dispatch_get_main_queue(), ^{
  159 + [weakSelf updateStepUI:topObject.targetInfor];
  160 + });
  161 + }
  162 + else {
  163 + dispatch_async(dispatch_get_main_queue(), ^{
  164 + NSString *message = [error.userInfo objectForKey:@"message"];
  165 + [Utilities showErrorMessage:message withViewController:weakSelf];
  166 + });
  167 + }
  168 + dispatch_async(dispatch_get_main_queue(), ^{
  169 + [progressHud setHidden:YES];
  170 + });
  171 + }];
  172 + [[ServerAPI server] requestHomeWithMode:mode andDate:dateString CompletionHandler:^(HomeObject *homeObject, NSError *error) {
  173 + if(weakSelf == nil) {
  174 + return ;
  175 + }
  176 + if (error == nil) {
  177 + NSLog(@"List Notices: %@", homeObject.listNotice);
  178 + for (NoticeInfor *noticeInfor in homeObject.listNotice) {
  179 + NSLog(@"noticeInfor: %@", noticeInfor.notice_content);
  180 + }
  181 + dispatch_async(dispatch_get_main_queue(), ^{
  182 + [weakSelf updateNoticeUI:homeObject];
  183 + });
  184 + }
  185 + else {
  186 + dispatch_async(dispatch_get_main_queue(), ^{
  187 + NSString *message = [error.userInfo objectForKey:@"message"];
  188 + [Utilities showErrorMessage:message withViewController:weakSelf];
  189 + });
  190 + }
  191 + dispatch_async(dispatch_get_main_queue(), ^{
  192 + [progressHud setHidden:YES];
  193 + });
  194 + }];
  195 +}
  196 +
  197 +- (void)viewWillDisappear:(BOOL)animated {
  198 + [super viewWillDisappear:animated];
  199 +}
  200 +
  201 +- (void)didReceiveMemoryWarning {
  202 + [super didReceiveMemoryWarning];
  203 + // Dispose of any resources that can be recreated.
  204 +}
  205 +
  206 +#pragma mark - IBAction
  207 +- (IBAction)menuButtonTouchUpInside:(id)sender
  208 +{
  209 +
  210 +}
  211 +
  212 +- (IBAction)todayButtonTouchUpInside:(id)sender
  213 +{
  214 + TodayViewController *todayVC = [[TodayViewController alloc] initWithNibName:@"TodayViewController" bundle:nil];
  215 + todayVC.targetStep = targetStep;
  216 + [self.navigationController pushViewController:todayVC animated:YES];
  217 +}
  218 +
  219 +- (IBAction)leftButtonTouchUpInside:(id)sender
  220 +{
  221 + self.dateCurrent = [self.dateCurrent dateByAddingTimeInterval:-(24*60*60)];
  222 + self.lblDateCurrent.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[_dateCurrent getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[_dateCurrent getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[_dateCurrent getDay], NSLocalizedString(@"lifelog.common.day", nil)];
  223 + [self resetStepUI];
  224 + [self saveStepForDay:self.dateCurrent];
  225 +}
  226 +
  227 +- (IBAction)rightButtonTouchUpInside:(id)sender
  228 +{
  229 + self.dateCurrent = [self.dateCurrent dateByAddingTimeInterval:24*60*60];
  230 + self.lblDateCurrent.text = [NSString stringWithFormat:@"%ld%@%ld%@%ld%@", (long)[_dateCurrent getYear], NSLocalizedString(@"lifelog.common.year", nil), (long)[_dateCurrent getMonth], NSLocalizedString(@"lifelog.common.month", nil), (long)[_dateCurrent getDay], NSLocalizedString(@"lifelog.common.day", nil)];
  231 + [self resetStepUI];
  232 + //[self saveStepForDay:self.dateCurrent];
  233 + [self requestTopByDate:self.dateCurrent];
  234 +}
  235 +
  236 +- (IBAction)segmentValueChange:(id)sender {
  237 + [self resetStepUI];
  238 + [self requestTopByDate:self.dateCurrent];
  239 +}
  240 +
  241 +#pragma mark - Functions Private
  242 +- (void)saveStepForDay:(NSDate *)date
  243 +{
  244 + if ([CMMotionActivityManager isActivityAvailable]) {
  245 + [progressHud showAnimated:YES];
  246 + [progressHud setHidden:NO];
  247 + self.bike = 0;
  248 + self.walking = 0;
  249 + self.running = 0;
  250 + NSDate *startDate = [date beginningAtMidnightOfDay];
  251 + NSDate *endDate = [startDate dateByAddingTimeInterval:(24*60*60 - 1)];
  252 +
  253 + HomeViewController __weak *weakSelf = self;
  254 + dispatch_queue_t myQueue = dispatch_queue_create("mobileworld.jp.lifelog.forDay", NULL);
  255 + dispatch_async(myQueue, ^{
  256 + if (weakSelf == nil) {
  257 + return ;
  258 + }
  259 +
  260 + [weakSelf.motionActivityManager queryActivityStartingFromDate:startDate toDate:endDate toQueue:_operationQueue withHandler:^(NSArray<CMMotionActivity *> * _Nullable activities, NSError * _Nullable error) {
  261 + if (error || activities.count <= 0) {
  262 + dispatch_async(dispatch_get_main_queue(), ^{
  263 + [weakSelf updateStepUI];
  264 + });
  265 + return ;
  266 + }
  267 + // set EndDate
  268 + NSMutableArray *arrayActivities = [[NSMutableArray alloc] init];
  269 + for (int i = 0; i < activities.count; i++) {
  270 + CMMotionActivity *activity = [activities objectAtIndex:i];
  271 + CMMotionActivityExtra *activityExtra = [[CMMotionActivityExtra alloc] init];
  272 + activityExtra.activity = activity;
  273 + if (i == activities.count - 1) {
  274 + activityExtra.endDate = endDate;
  275 + }
  276 + else {
  277 + CMMotionActivity *activityNext = [activities objectAtIndex:i+1];
  278 + // activityExtra.endDate = [activityNext.startDate dateByAddingTimeInterval:-1];
  279 + activityExtra.endDate = activityNext.startDate;
  280 + }
  281 + [arrayActivities addObject:activityExtra];
  282 + }
  283 + // PhongNV
  284 + weakSelf.currentIndex = 0;
  285 + [weakSelf save20objectOfActivityExtras:arrayActivities];
  286 + }];
  287 + });
  288 + }
  289 +}
  290 +
  291 +- (void)complete20Request:(NSArray *)activities {
  292 + if (self.totalRequest == self.countComplete) {
  293 + self.totalRequest = 0;
  294 + self.countComplete = 0;
  295 + self.currentIndex++;
  296 + [self save20objectOfActivityExtras:activities];
  297 + }
  298 +}
  299 +- (void)save20objectOfActivityExtras:(NSArray *)activities
  300 +{
  301 + int result = floor(activities.count / maxRequest);
  302 + if (activities.count <= 0 || self.currentIndex > result) {
  303 + self.totalRequest = 0;
  304 + self.countComplete = 0;
  305 + self.currentIndex = 0;
  306 + dispatch_async(dispatch_get_main_queue(), ^{
  307 + [self updateStepUI];
  308 + });
  309 + return;
  310 + }
  311 +
  312 + HomeViewController __weak *weakSelf = self;
  313 + for (NSInteger index = self.currentIndex*maxRequest; index < self.currentIndex*maxRequest + maxRequest; index++) {
  314 + if ((self.currentIndex*maxRequest + maxRequest) >= activities.count) {
  315 + self.totalRequest = (int)(activities.count - self.currentIndex*maxRequest);
  316 + if (self.totalRequest <= 0) {
  317 + [weakSelf complete20Request:activities];
  318 + }
  319 + }
  320 + else {
  321 + self.totalRequest = (int)maxRequest;
  322 + }
  323 + if (index < activities.count) {
  324 + CMMotionActivityExtra *activityExtra = [activities objectAtIndex:index];
  325 + [self.pedometer queryPedometerDataFromDate:activityExtra.activity.startDate toDate:activityExtra.endDate withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
  326 + if (weakSelf == nil) {
  327 + return ;
  328 + }
  329 + NSInteger numberStep = [pedometerData.numberOfSteps integerValue];