From e796f8345cf407df174166eb3d8c949a6b12f59f Mon Sep 17 00:00:00 2001 From: nvtu Date: Sat, 26 Aug 2017 13:06:37 +0700 Subject: [PATCH] Add ranking API in Ranking Screen, display result in screen --- LifeLog/LifeLog.xcodeproj/project.pbxproj | 12 ++--- LifeLog/LifeLog/BaseTableViewController.m | 6 +-- LifeLog/LifeLog/Entities.h | 2 +- LifeLog/LifeLog/HistoryGraphObject.h | 18 ------- LifeLog/LifeLog/HistoryGraphObject.m | 33 ------------- LifeLog/LifeLog/RankingObject.h | 24 ++++++++++ LifeLog/LifeLog/RankingObject.m | 38 +++++++++++++++ LifeLog/LifeLog/RankingTableViewCell.h | 4 ++ LifeLog/LifeLog/RankingTableViewCell.m | 15 ++++++ LifeLog/LifeLog/RankingViewController.h | 9 ++-- LifeLog/LifeLog/RankingViewController.m | 78 +++++++++++++++++++++++-------- LifeLog/LifeLog/RankingViewController.xib | 7 +-- LifeLog/LifeLog/ServerAPI.h | 3 ++ LifeLog/LifeLog/ServerAPI.m | 51 +++++++++++++++++++- 14 files changed, 212 insertions(+), 88 deletions(-) delete mode 100644 LifeLog/LifeLog/HistoryGraphObject.h delete mode 100644 LifeLog/LifeLog/HistoryGraphObject.m create mode 100644 LifeLog/LifeLog/RankingObject.h create mode 100644 LifeLog/LifeLog/RankingObject.m diff --git a/LifeLog/LifeLog.xcodeproj/project.pbxproj b/LifeLog/LifeLog.xcodeproj/project.pbxproj index 4fcd6f5..744d1f0 100644 --- a/LifeLog/LifeLog.xcodeproj/project.pbxproj +++ b/LifeLog/LifeLog.xcodeproj/project.pbxproj @@ -56,7 +56,6 @@ E933E67A1F40513C002926F5 /* GroupDetailViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = E933E6781F40513C002926F5 /* GroupDetailViewController.xib */; }; E9373E471F3596940059355A /* AutoTransLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = E9373E461F3596940059355A /* AutoTransLabel.m */; }; E9373E4A1F35AA180059355A /* HistoryObject.m in Sources */ = {isa = PBXBuildFile; fileRef = E9373E491F35AA180059355A /* HistoryObject.m */; }; - E9373E4D1F361A230059355A /* HistoryGraphObject.m in Sources */ = {isa = PBXBuildFile; fileRef = E9373E4C1F361A230059355A /* HistoryGraphObject.m */; }; E9497FD51F46EC1100E6AF8D /* MemberObject.m in Sources */ = {isa = PBXBuildFile; fileRef = E9497FD41F46EC1100E6AF8D /* MemberObject.m */; }; E95F4CED1F475C6A00D27E80 /* CreateGroupViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E95F4CEB1F475C6A00D27E80 /* CreateGroupViewController.m */; }; E95F4CEE1F475C6A00D27E80 /* CreateGroupViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = E95F4CEC1F475C6A00D27E80 /* CreateGroupViewController.xib */; }; @@ -67,6 +66,7 @@ E968550A1F3C444900317CEF /* AutoTransButton.m in Sources */ = {isa = PBXBuildFile; fileRef = E96855091F3C444900317CEF /* AutoTransButton.m */; }; E96855111F3C5D9A00317CEF /* SNSRecentTopicTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E968550F1F3C5D9A00317CEF /* SNSRecentTopicTableViewCell.m */; }; E96855121F3C5D9A00317CEF /* SNSRecentTopicTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = E96855101F3C5D9A00317CEF /* SNSRecentTopicTableViewCell.xib */; }; + E97588FC1F513BAD00782484 /* RankingObject.m in Sources */ = {isa = PBXBuildFile; fileRef = E97588FB1F513BAD00782484 /* RankingObject.m */; }; E997E0261F3AB66500709FB1 /* Social.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E997E0251F3AB66500709FB1 /* Social.framework */; }; E99E13AB1F336F3600C78787 /* CollectionView.m in Sources */ = {isa = PBXBuildFile; fileRef = E99E13AA1F336F3600C78787 /* CollectionView.m */; }; E99E13AD1F336F4500C78787 /* CollectionView.xib in Resources */ = {isa = PBXBuildFile; fileRef = E99E13AC1F336F4500C78787 /* CollectionView.xib */; }; @@ -184,8 +184,6 @@ E9373E461F3596940059355A /* AutoTransLabel.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AutoTransLabel.m; sourceTree = ""; }; E9373E481F35AA180059355A /* HistoryObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HistoryObject.h; sourceTree = ""; }; E9373E491F35AA180059355A /* HistoryObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HistoryObject.m; sourceTree = ""; }; - E9373E4B1F361A230059355A /* HistoryGraphObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HistoryGraphObject.h; sourceTree = ""; }; - E9373E4C1F361A230059355A /* HistoryGraphObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HistoryGraphObject.m; sourceTree = ""; }; E9497FD31F46EC1100E6AF8D /* MemberObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemberObject.h; sourceTree = ""; }; E9497FD41F46EC1100E6AF8D /* MemberObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MemberObject.m; sourceTree = ""; }; E95F4CEA1F475C6A00D27E80 /* CreateGroupViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CreateGroupViewController.h; sourceTree = ""; }; @@ -202,6 +200,8 @@ E968550E1F3C5D9A00317CEF /* SNSRecentTopicTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SNSRecentTopicTableViewCell.h; sourceTree = ""; }; E968550F1F3C5D9A00317CEF /* SNSRecentTopicTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SNSRecentTopicTableViewCell.m; sourceTree = ""; }; E96855101F3C5D9A00317CEF /* SNSRecentTopicTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = SNSRecentTopicTableViewCell.xib; sourceTree = ""; }; + E97588FA1F513BAD00782484 /* RankingObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RankingObject.h; sourceTree = ""; }; + E97588FB1F513BAD00782484 /* RankingObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RankingObject.m; sourceTree = ""; }; E997E0251F3AB66500709FB1 /* Social.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Social.framework; path = System/Library/Frameworks/Social.framework; sourceTree = SDKROOT; }; E99E13A91F336F3600C78787 /* CollectionView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CollectionView.h; sourceTree = ""; }; E99E13AA1F336F3600C78787 /* CollectionView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CollectionView.m; sourceTree = ""; }; @@ -288,14 +288,14 @@ 6E18CEDB1F2E40F50029891F /* User.m */, E9373E481F35AA180059355A /* HistoryObject.h */, E9373E491F35AA180059355A /* HistoryObject.m */, - E9373E4B1F361A230059355A /* HistoryGraphObject.h */, - E9373E4C1F361A230059355A /* HistoryGraphObject.m */, E9D9091F1F3D559D004CF99F /* TweetObject.h */, E9D909201F3D559D004CF99F /* TweetObject.m */, E933E6721F3E0E4E002926F5 /* GroupObject.h */, E933E6731F3E0E4E002926F5 /* GroupObject.m */, E9497FD31F46EC1100E6AF8D /* MemberObject.h */, E9497FD41F46EC1100E6AF8D /* MemberObject.m */, + E97588FA1F513BAD00782484 /* RankingObject.h */, + E97588FB1F513BAD00782484 /* RankingObject.m */, ); name = Model; sourceTree = ""; @@ -867,6 +867,7 @@ 9CA8AEEA1F31BB4600C5FC74 /* ConfirmForgetPassViewController.m in Sources */, E99E13B11F33720600C78787 /* LabelCollectionViewCell.m in Sources */, 6EDC8BEE1F2C2FB800765ACC /* Utilities.m in Sources */, + E97588FC1F513BAD00782484 /* RankingObject.m in Sources */, E96855111F3C5D9A00317CEF /* SNSRecentTopicTableViewCell.m in Sources */, 6E84E3C51F27ACAE001EB88E /* MapViewController.m in Sources */, 6E18CEDC1F2E40F50029891F /* User.m in Sources */, @@ -879,7 +880,6 @@ E933E6701F3E09B6002926F5 /* SearchGroupTableViewCell.m in Sources */, 6E84E3B51F27A98B001EB88E /* HistoryViewController.m in Sources */, E95F4CF31F48B39200D27E80 /* MyGroupViewController.m in Sources */, - E9373E4D1F361A230059355A /* HistoryGraphObject.m in Sources */, E933E66B1F3E04A6002926F5 /* SearchGroupViewController.m in Sources */, 6E84E3BF1F27A9B1001EB88E /* SNSViewController.m in Sources */, ); diff --git a/LifeLog/LifeLog/BaseTableViewController.m b/LifeLog/LifeLog/BaseTableViewController.m index 1dcfa75..0c97128 100644 --- a/LifeLog/LifeLog/BaseTableViewController.m +++ b/LifeLog/LifeLog/BaseTableViewController.m @@ -61,10 +61,10 @@ }); } if(error == nil) { + if(_curPage == 1) { + [_curDataList removeAllObjects]; + } if(array.count != 0) { - if(_curPage == 1) { - [_curDataList removeAllObjects]; - } [_curDataList addObjectsFromArray:array]; dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.tableBase reloadData]; diff --git a/LifeLog/LifeLog/Entities.h b/LifeLog/LifeLog/Entities.h index faabca1..1893a0f 100644 --- a/LifeLog/LifeLog/Entities.h +++ b/LifeLog/LifeLog/Entities.h @@ -14,9 +14,9 @@ #import "MemberObject.h" #import "GroupObject.h" #import "TweetObject.h" -#import "HistoryGraphObject.h" #import "HistoryObject.h" #import "User.h" #import "TopObject.h" +#import "RankingObject.h" #endif /* Entities_h */ diff --git a/LifeLog/LifeLog/HistoryGraphObject.h b/LifeLog/LifeLog/HistoryGraphObject.h deleted file mode 100644 index b4ad41d..0000000 --- a/LifeLog/LifeLog/HistoryGraphObject.h +++ /dev/null @@ -1,18 +0,0 @@ -// -// HistoryGraphObject.h -// LifeLog -// -// Created by nvtu on 8/5/17. -// Copyright © 2017 PhongNV. All rights reserved. -// - -#import - -@interface HistoryGraphObject : NSObject - -@property (nonatomic, strong) NSMutableArray * arrayTime; -@property (nonatomic, strong) NSMutableArray * arrayStep; - --(id) initWithData : (NSDictionary *) dict; - -@end diff --git a/LifeLog/LifeLog/HistoryGraphObject.m b/LifeLog/LifeLog/HistoryGraphObject.m deleted file mode 100644 index a04d27f..0000000 --- a/LifeLog/LifeLog/HistoryGraphObject.m +++ /dev/null @@ -1,33 +0,0 @@ -// -// HistoryGraphObject.m -// LifeLog -// -// Created by nvtu on 8/5/17. -// Copyright © 2017 PhongNV. All rights reserved. -// - -#import "HistoryGraphObject.h" - -@implementation HistoryGraphObject - --(id) initWithData : (NSDictionary *) dict { - - if([dict objectForKey:@"data"] != nil) { - self.arrayStep = [[NSMutableArray alloc] init]; - self.arrayTime = [[NSMutableArray alloc] init]; - NSArray *arrayGraph = dict[@"data"]; - for(NSDictionary *sub in arrayGraph) { - if([sub objectForKey:@"hour"] != nil && [sub objectForKey:@"step"] != nil) { - [self.arrayTime addObject:[sub objectForKey:@"hour"]]; - [self.arrayStep addObject:[sub objectForKey:@"step"]]; - } - } - } - else { - return nil; - } - return self; -} - - -@end diff --git a/LifeLog/LifeLog/RankingObject.h b/LifeLog/LifeLog/RankingObject.h new file mode 100644 index 0000000..4f4fe11 --- /dev/null +++ b/LifeLog/LifeLog/RankingObject.h @@ -0,0 +1,24 @@ +// +// RankingObject.h +// LifeLog +// +// Created by nvtu on 8/26/17. +// Copyright © 2017 PhongNV. All rights reserved. +// + +#import + +@interface RankingObject : NSObject + +@property (nonatomic) int userID; +@property (nonatomic) int rank; +@property (nonatomic) int steps; +@property (nonatomic) int time; +@property (nonatomic) float distance; + +@property (nonatomic, strong) NSString * userName; +@property (nonatomic, strong) NSString * avatar; + +-(id) initWithData : (NSDictionary *) dict; + +@end diff --git a/LifeLog/LifeLog/RankingObject.m b/LifeLog/LifeLog/RankingObject.m new file mode 100644 index 0000000..6215a33 --- /dev/null +++ b/LifeLog/LifeLog/RankingObject.m @@ -0,0 +1,38 @@ +// +// RankingObject.m +// LifeLog +// +// Created by nvtu on 8/26/17. +// Copyright © 2017 PhongNV. All rights reserved. +// + +#import "RankingObject.h" + +@implementation RankingObject + +-(id) initWithData : (NSDictionary *) dict { + if([dict objectForKey:@"user_id"] != nil) { + self.userID = [dict[@"user_id"] intValue]; + } + if([dict objectForKey:@"rank"] != nil) { + self.rank = [dict[@"rank"] intValue]; + } + if([dict objectForKey:@"steps"] != nil) { + self.steps = [dict[@"steps"] intValue]; + } + if([dict objectForKey:@"distance"] != nil) { + self.distance = [dict[@"distance"] floatValue]; + } + if([dict objectForKey:@"time"] != nil) { + self.time = [dict[@"time"] intValue]; + } + if([dict objectForKey:@"username"] != nil) { + self.userName = dict[@"username"]; + } + if([dict objectForKey:@"profile_image"] != nil) { + self.avatar = dict[@"profile_image"]; + } + return self; +} + +@end diff --git a/LifeLog/LifeLog/RankingTableViewCell.h b/LifeLog/LifeLog/RankingTableViewCell.h index 237335a..a29319c 100644 --- a/LifeLog/LifeLog/RankingTableViewCell.h +++ b/LifeLog/LifeLog/RankingTableViewCell.h @@ -7,6 +7,7 @@ // #import +#import "RankingObject.h" @interface RankingTableViewCell : UITableViewCell @property (weak, nonatomic) IBOutlet UILabel *lblIndex; @@ -16,4 +17,7 @@ @property (weak, nonatomic) IBOutlet UILabel *lblDistance; @property (weak, nonatomic) IBOutlet UIImageView *imgAvatar; + +-(void) setRankingData : (RankingObject *) object; + @end diff --git a/LifeLog/LifeLog/RankingTableViewCell.m b/LifeLog/LifeLog/RankingTableViewCell.m index 315fc41..878b37e 100644 --- a/LifeLog/LifeLog/RankingTableViewCell.m +++ b/LifeLog/LifeLog/RankingTableViewCell.m @@ -7,7 +7,9 @@ // #import "RankingTableViewCell.h" +#import +#import "Utilities.h" @implementation RankingTableViewCell - (void)awakeFromNib { @@ -21,4 +23,17 @@ // Configure the view for the selected state } +-(void) setRankingData : (RankingObject *) object { + if(object.avatar && ![object.avatar isKindOfClass:[NSNull class]]) { + [self.imgAvatar sd_setImageWithURL:[NSURL URLWithString:[Utilities getImageLink:object.avatar]]]; + } + else { + [self.imgAvatar setImage:[UIImage imageNamed:@"avatar_default"]]; + } + self.lblIndex.text = [NSString stringWithFormat:@"%d", object.rank]; + self.lblName.text = object.userName; + self.lblDistance.text = [NSString stringWithFormat:@"%0.1f km", object.distance]; + self.lblStep.text = [NSString stringWithFormat:@"%d 歩", object.steps]; + self.lblTime.text = [Utilities convertSecondToShortTime:object.time]; +} @end diff --git a/LifeLog/LifeLog/RankingViewController.h b/LifeLog/LifeLog/RankingViewController.h index d146c99..cc55d1e 100644 --- a/LifeLog/LifeLog/RankingViewController.h +++ b/LifeLog/LifeLog/RankingViewController.h @@ -8,15 +8,18 @@ #import +#import "BaseTableViewController.h" #import "CollectionView.h" -@interface RankingViewController : UIViewController +@interface RankingViewController : BaseTableViewController { + NSDate * _startDate; + NSDate * _endDate; +} @property (weak, nonatomic) IBOutlet CollectionView *viewCollectionTime; @property (weak, nonatomic) IBOutlet CollectionView *viewCollectionMode; @property (weak, nonatomic) IBOutlet CollectionView *viewCollectionType; - -@property (weak, nonatomic) IBOutlet UITableView *tableRank; +@property (weak, nonatomic) IBOutlet UILabel *lblDateTime; - (IBAction)clickButton:(UIButton *)sender; @end diff --git a/LifeLog/LifeLog/RankingViewController.m b/LifeLog/LifeLog/RankingViewController.m index f048053..8593b6d 100644 --- a/LifeLog/LifeLog/RankingViewController.m +++ b/LifeLog/LifeLog/RankingViewController.m @@ -9,6 +9,9 @@ #import "RankingViewController.h" #import "RankingTableViewCell.h" +#import "ServerAPI.h" +#import "Utilities.h" + @interface RankingViewController () @end @@ -22,8 +25,14 @@ [self setupView]; + _startDate = [NSDate date]; + _endDate = _startDate; + self.lblDateTime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日 EEEE" locale:@"ja_JP"]; + //register nib for table view - [self.tableRank registerNib:[UINib nibWithNibName:@"RankingTableViewCell" bundle:nil] forCellReuseIdentifier:@"RankCell"]; + [self.tableBase registerNib:[UINib nibWithNibName:@"RankingTableViewCell" bundle:nil] forCellReuseIdentifier:@"RankCell"]; + + [self callRequestToUpdateData]; } - (void)didReceiveMemoryWarning { @@ -39,7 +48,7 @@ [self.viewCollectionTime setSpacing:1]; [self.viewCollectionTime setArrayTitle:timeTitle]; self.viewCollectionTime.changeCurrentIndex = ^(int index){ - [self callRequestToUpdateData]; + [self changeDate]; }; NSArray *modeTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.rank.mode.1", nil), NSLocalizedString(@"lifelog.rank.mode.2", nil), NSLocalizedString(@"lifelog.rank.mode.3", nil), NSLocalizedString(@"lifelog.rank.mode.4", nil), nil]; @@ -66,31 +75,60 @@ } #pragma mark UITableView Delegate -- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView { -// if(_curHisList == nil || _curHisList.count == 0) { -// UILabel * noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)]; -// noDataLabel.text = @"No data available"; -// noDataLabel.backgroundColor = [UIColor clearColor]; -// noDataLabel.textColor = [UIColor whiteColor]; -// noDataLabel.textAlignment = NSTextAlignmentCenter; -// tableView.backgroundView = noDataLabel; -// tableView.backgroundView.layer.zPosition -= 1; -// return 0; -// } -// tableView.backgroundView = nil; - return 1; -} - -- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return 6; -} - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { RankingTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RankCell"]; + [cell setRankingData:[_curDataList objectAtIndex:indexPath.row]]; return cell; } #pragma mark Private function - (void)callRequestToUpdateData { + [super callRequestToUpdateData]; + + NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken]; + MBProgressHUD *hudView = nil; + int mode = self.viewCollectionMode.getCurrentIndex + 1; + //dont have all + mode = fmin(mode, 3); + + if(_curPage == 1 && !self.refreshControl.isRefreshing) { + hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true]; + } + [[ServerAPI server] requestRankingList:token startDate:_startDate endDate:_endDate mode:mode page:_curPage CompletionHandler:^(NSArray *array, NSError *error){ + dispatch_async(dispatch_get_main_queue(), ^{ + if(hudView != nil) { + [hudView hideAnimated:true]; + } + }); + RankingViewController __weak *weakSelf = self; + [weakSelf updateTableData:array error:error]; + }]; +} + +- (void) changeDate { + switch (self.viewCollectionTime.getCurrentIndex) { + case 1: + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 7]; + break; + case 2: + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 30]; + break; + case 3: + _startDate = [_endDate dateByAddingTimeInterval:-86400 * 365]; + break; + default: + _startDate = _endDate; + break; + } + if(_startDate == _endDate) { + self.lblDateTime.text = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日 EEEE" locale:@"ja_JP"]; + } + else { + NSString * startDateString = [Utilities stringFromDate:_startDate withFormat:@"YYYY年MM月dd日" locale:@"ja_JP"]; + NSString * endDateString = [Utilities stringFromDate:_endDate withFormat:@"YYYY年MM月dd日" locale:@"ja_JP"]; + self.lblDateTime.text = [NSString stringWithFormat:@"%@-%@", startDateString, endDateString]; + } + [self callRequestToUpdateData]; } @end diff --git a/LifeLog/LifeLog/RankingViewController.xib b/LifeLog/LifeLog/RankingViewController.xib index 4ffdb65..60ad430 100644 --- a/LifeLog/LifeLog/RankingViewController.xib +++ b/LifeLog/LifeLog/RankingViewController.xib @@ -1,16 +1,17 @@ - + - + - + + diff --git a/LifeLog/LifeLog/ServerAPI.h b/LifeLog/LifeLog/ServerAPI.h index 72ed655..09db55d 100644 --- a/LifeLog/LifeLog/ServerAPI.h +++ b/LifeLog/LifeLog/ServerAPI.h @@ -48,6 +48,9 @@ extern NSString *const kNotificationToken; - (void) getGroupDetail:(NSString *)token withGroupID:(int)groupID CompletionHandler:(void (^)(GroupObject *, NSError *)) completion; - (void) requestMemberList:(NSString *)token groupID: (int) groupID withPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion; +#pragma mark - Ranking API +- (void) requestRankingList:(NSString *)token startDate:(NSDate *)startDate endDate:(NSDate *)endDate mode:(int) mode page:(int) page CompletionHandler:(void (^)(NSArray *, NSError *)) completion; + #pragma mark - Common API - (void)refreshToken: (NSString *)userID CompletionHandler:(void (^)(NSString *, NSError *))completion; @end diff --git a/LifeLog/LifeLog/ServerAPI.m b/LifeLog/LifeLog/ServerAPI.m index d75a876..09c858a 100644 --- a/LifeLog/LifeLog/ServerAPI.m +++ b/LifeLog/LifeLog/ServerAPI.m @@ -532,7 +532,7 @@ NSURLSessionDataTask * searchTask; #pragma mark - SNS Screen Function - (void) requestTweetsList:(NSString *)token groupID: (int) groupID withPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion { - NSString *url = [kServerAddress stringByAppendingFormat:@"/api/sns/%d", page]; + NSString *url = [kServerAddress stringByAppendingFormat:@"api/tweet/list?page=%d&record=10", page]; if(groupID > -1) { url = [kServerAddress stringByAppendingFormat:@"api/tweet/list?group_id=%d&page=%d&record=10", groupID, page]; } @@ -892,6 +892,55 @@ NSURLSessionDataTask * searchTask; } }]; } + +#pragma mark - Ranking API +- (void) requestRankingList:(NSString *)token startDate:(NSDate *)startDate endDate:(NSDate *)endDate mode:(int) mode page:(int) page CompletionHandler:(void (^)(NSArray *, NSError *)) completion { + NSString *url = [kServerAddress stringByAppendingFormat:@"api/ranking/%d/%@/%@/%d", mode, [Utilities stringFromDate:startDate withFormat:@"YYYYMMdd" locale:@""], [Utilities stringFromDate:endDate withFormat:@"YYYYMMdd" locale:@""], page]; + NSLog(@"requestRaningList link %@", url); + [self _request:url method:@"GET" token:token paras:nil completion:^(NSData *data, NSError *error) { + + if (completion == NULL) { + return ; + } + + if (error == nil) + { + NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; + NSLog(@"%@", dataResult); + int status = [dataResult[@"status"] intValue]; + if (status == 1) { // status = 1 success + NSArray * array = dataResult[@"result"]; + NSMutableArray * arrayRanking = [[NSMutableArray alloc] init]; + for(NSDictionary * dict in array) { + RankingObject * object = [[RankingObject alloc] initWithData:dict]; + [arrayRanking addObject:object]; + } + completion(arrayRanking, nil); + } + else { + NSString *message = dataResult[@"message"]; + if (message == nil) { + message = @"Unknown error"; + } + + if ([message isEqualToString:@"Token is invalid"]) { + [self performSelectorOnMainThread:@selector(checkToken) withObject:nil waitUntilDone:YES]; + NSString *tokenNew = [[NSUserDefaults standardUserDefaults] objectForKey:kToken]; + [self requestRankingList:tokenNew startDate:startDate endDate:endDate mode:mode page:page CompletionHandler:completion]; + } + else { + NSError *errorObject = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; + completion(nil, errorObject); + } + } + } + else + { + completion(nil, error); + } + }]; +} + #pragma mark - Common API - (void)refreshToken: (NSString *)userID CompletionHandler:(void (^)(NSString *, NSError *))completion { [self _request:[kServerAddress stringByAppendingFormat: @"refreshToken"] method:@"POST" token:@"" paras:@{@"userId":userID} completion:^(NSData *data, NSError *error) { -- 1.8.5.3