From 3c805063b5b987fbb80f35b76673c9d33c2ab091 Mon Sep 17 00:00:00 2001 From: phongnv Date: Sun, 12 Nov 2017 22:12:21 +0700 Subject: [PATCH] update api notice --- LifeLog/LifeLog/HomeViewConb troller.m | 43 +++++++++++++++++++++++++++++++- LifeLog/LifeLog/HomeViewController.xib | 24 ++++++------------ LifeLog/LifeLog/Info.plist | 2 +- LifeLog/LifeLog/ServerAPI.h | 2 ++ LifeLog/LifeLog/ServerAPI.m | 45 ++++++++++++++++++++++++++++++++++ 5 files changed, 97 insertions(+), 19 deletions(-) diff --git a/LifeLog/LifeLog/HomeViewConb troller.m b/LifeLog/LifeLog/HomeViewConb troller.m index 9951e27..a0b9c4d 100644 --- a/LifeLog/LifeLog/HomeViewConb troller.m +++ b/LifeLog/LifeLog/HomeViewConb troller.m @@ -117,6 +117,43 @@ static NSInteger numberTotal = 10000; progressHud.detailsLabel.text = NSLocalizedString(@"lifelog.home.progressHud.title", nil); // [self saveDataStep7LastDay]; targetStep = numberTotal; + [self requestNotice]; +} + +- (void)requestNotice +{ + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + [dateFormatter setDateFormat:@"yyyy-MM-dd"]; + NSString *dateString = [dateFormatter stringFromDate:_dateCurrent]; + HomeViewController __weak *weakSelf = self; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + //Do background work + [[ServerAPI server] getNoticeByDate:dateString CompletionHandler:^(NSArray *arrResults, NSError *error) { + if(weakSelf == nil) { + return ; + } + if (error == nil) { + // NSLog(@"List Notices: %@", arrResults); + dispatch_async(dispatch_get_main_queue(), ^{ + NSString *stringNotice = @""; + for (NSString *noticeInfor in arrResults) { + NSString *noticeString = [NSString stringWithFormat:@"%@\n", noticeInfor]; + stringNotice = [stringNotice stringByAppendingString:noticeString]; + } + weakSelf.tvNotice.text = stringNotice; + }); + } + else { + dispatch_async(dispatch_get_main_queue(), ^{ + weakSelf.tvNotice.text = @""; + /* + NSString *message = [error.userInfo objectForKey:@"message"]; + [Utilities showErrorMessage:message withViewController:weakSelf]; + */ + }); + } + }]; + }); } - (void)viewWillAppear:(BOOL)animated { @@ -169,6 +206,7 @@ static NSInteger numberTotal = 10000; [progressHud setHidden:YES]; }); }]; + /* [[ServerAPI server] requestHomeWithMode:mode andDate:dateString CompletionHandler:^(HomeObject *homeObject, NSError *error) { if(weakSelf == nil) { return ; @@ -192,6 +230,7 @@ static NSInteger numberTotal = 10000; [progressHud setHidden:YES]; }); }]; + */ } - (void)viewWillDisappear:(BOOL)animated { @@ -222,6 +261,7 @@ static NSInteger numberTotal = 10000; 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)]; [self resetStepUI]; [self saveStepForDay:self.dateCurrent]; + [self requestNotice]; } - (IBAction)rightButtonTouchUpInside:(id)sender @@ -231,6 +271,7 @@ static NSInteger numberTotal = 10000; [self resetStepUI]; //[self saveStepForDay:self.dateCurrent]; [self requestTopByDate:self.dateCurrent]; + [self requestNotice]; } - (IBAction)segmentValueChange:(id)sender { @@ -346,7 +387,7 @@ static NSInteger numberTotal = 10000; [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *dateBegin = [dateFormatter stringFromDate:activityExtra.activity.startDate]; NSString *dateEnd = [dateFormatter stringFromDate:activityExtra.endDate]; - NSLog(@"dateBegin: %@ | dateEnd: %@ | numberStep: %li", dateBegin, dateEnd, numberStep); + // NSLog(@"dateBegin: %@ | dateEnd: %@ | numberStep: %li", dateBegin, dateEnd, numberStep); [[ServerAPI server] requestCreateLog:mode withStep:(int)numberStep startDate:dateBegin endDate:dateEnd CompletionHandler:^(NSError *error) { if (error) { NSLog(@"Error: %@", error); diff --git a/LifeLog/LifeLog/HomeViewController.xib b/LifeLog/LifeLog/HomeViewController.xib index e2bf460..92b775a 100644 --- a/LifeLog/LifeLog/HomeViewController.xib +++ b/LifeLog/LifeLog/HomeViewController.xib @@ -1,11 +1,11 @@ - + - + @@ -153,22 +153,15 @@ - diff --git a/LifeLog/LifeLog/Info.plist b/LifeLog/LifeLog/Info.plist index d7dac00..a88ebcd 100644 --- a/LifeLog/LifeLog/Info.plist +++ b/LifeLog/LifeLog/Info.plist @@ -23,7 +23,7 @@ CFBundleVersion - 4 + 5 LSApplicationQueriesSchemes line diff --git a/LifeLog/LifeLog/ServerAPI.h b/LifeLog/LifeLog/ServerAPI.h index 91ab9e5..498e74e 100644 --- a/LifeLog/LifeLog/ServerAPI.h +++ b/LifeLog/LifeLog/ServerAPI.h @@ -54,4 +54,6 @@ extern NSString *const kNotificationToken; #pragma mark - Common API - (void)refreshToken: (NSString *)userID CompletionHandler:(void (^)(NSString *, NSError *))completion; +#pragma mark - Get Notice +- (void)getNoticeByDate: (NSString *)date CompletionHandler:(void (^)(NSArray *, NSError *)) completion; @end diff --git a/LifeLog/LifeLog/ServerAPI.m b/LifeLog/LifeLog/ServerAPI.m index 8f93dbd..32144e2 100644 --- a/LifeLog/LifeLog/ServerAPI.m +++ b/LifeLog/LifeLog/ServerAPI.m @@ -1045,6 +1045,51 @@ NSURLSessionDataTask * searchTask; }]; } +- (void)getNoticeByDate: (NSString *)date CompletionHandler:(void (^)(NSArray *, NSError *)) completion +{ + [self _request:[kServerAddress stringByAppendingFormat: @"api/getnoticesbydate/%@", date] method:@"GET" token:@"" paras:nil completion:^(NSData *data, NSError *error) { + + if (completion == NULL) { + return ; + } + + if (error == nil) + { + NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; + + int status = [dataResult[@"status"] intValue]; + if (status == 1) { // status = 1 success + NSArray *arrayResult = dataResult[@"result"][@"listNotice"]; + if (arrayResult.count > 0) { + NSMutableArray *notices = [[NSMutableArray alloc] init]; + for(NSDictionary *dict in arrayResult) { + NSString *object = [dict objectForKey:@"notice_content"]; + [notices addObject:object]; + } + completion(notices, nil); + } + else { + NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":@"Today is not notice"}]; + completion(nil, errorObject); + } + + } + else { // status = 0 error + NSString *message = dataResult[@"message"]; + if (message == nil) { + message = @"Unknown error"; + } + NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; + completion(nil, loginFaild); + } + } + else + { + completion(nil, error); + } + }]; +} + #pragma mark - Private Function - (void) checkToken { // [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationToken object:nil]; -- 1.8.5.3