Commit 9e6f8831480ca136de80b4fc82d788ab23380431

Authored by nvtu11790@gmail.com
1 parent 7aa2027583

Refactor and merge logic when request data for tableView in History screen, SNS …

…main screen. Add refresh control, load more action

Showing 10 changed files with 206 additions and 145 deletions Side-by-side Diff

LifeLog/LifeLog.xcodeproj/project.pbxproj
... ... @@ -58,6 +58,7 @@
58 58 E99E13B11F33720600C78787 /* LabelCollectionViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E99E13AF1F33720600C78787 /* LabelCollectionViewCell.m */; };
59 59 E99E13B21F33720600C78787 /* LabelCollectionViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = E99E13B01F33720600C78787 /* LabelCollectionViewCell.xib */; };
60 60 E9D909211F3D559D004CF99F /* TweetObject.m in Sources */ = {isa = PBXBuildFile; fileRef = E9D909201F3D559D004CF99F /* TweetObject.m */; };
  61 + E9D909241F3D6E8C004CF99F /* BaseTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E9D909231F3D6E8C004CF99F /* BaseTableViewController.m */; };
61 62 E9F73DCC1F338A0F004A3A6F /* HistoryListTableViewCell.m in Sources */ = {isa = PBXBuildFile; fileRef = E9F73DCA1F338A0F004A3A6F /* HistoryListTableViewCell.m */; };
62 63 E9F73DCD1F338A0F004A3A6F /* HistoryListTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = E9F73DCB1F338A0F004A3A6F /* HistoryListTableViewCell.xib */; };
63 64 /* End PBXBuildFile section */
... ... @@ -170,6 +171,8 @@
170 171 E99E13B01F33720600C78787 /* LabelCollectionViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = LabelCollectionViewCell.xib; sourceTree = "<group>"; };
171 172 E9D9091F1F3D559D004CF99F /* TweetObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TweetObject.h; sourceTree = "<group>"; };
172 173 E9D909201F3D559D004CF99F /* TweetObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TweetObject.m; sourceTree = "<group>"; };
  174 + E9D909221F3D6E8C004CF99F /* BaseTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BaseTableViewController.h; sourceTree = "<group>"; };
  175 + E9D909231F3D6E8C004CF99F /* BaseTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BaseTableViewController.m; sourceTree = "<group>"; };
173 176 E9F73DC91F338A0F004A3A6F /* HistoryListTableViewCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HistoryListTableViewCell.h; sourceTree = "<group>"; };
174 177 E9F73DCA1F338A0F004A3A6F /* HistoryListTableViewCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HistoryListTableViewCell.m; sourceTree = "<group>"; };
175 178 E9F73DCB1F338A0F004A3A6F /* HistoryListTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = HistoryListTableViewCell.xib; sourceTree = "<group>"; };
... ... @@ -469,6 +472,8 @@
469 472 9CBDA0F91F2ECE2C0055DED1 /* BaseViewController.m */,
470 473 6E48AF0B1F2F9D3200C1D184 /* CustomTextField.h */,
471 474 6E48AF0C1F2F9D3200C1D184 /* CustomTextField.m */,
  475 + E9D909221F3D6E8C004CF99F /* BaseTableViewController.h */,
  476 + E9D909231F3D6E8C004CF99F /* BaseTableViewController.m */,
472 477 );
473 478 name = BaseViewController;
474 479 sourceTree = "<group>";
... ... @@ -752,6 +757,7 @@
752 757 6E18CEDC1F2E40F50029891F /* User.m in Sources */,
753 758 6ECC40711F2A5FEB00AAEB97 /* NSDate+helper.m in Sources */,
754 759 6E18CED31F2E39DB0029891F /* LoginViewController.m in Sources */,
  760 + E9D909241F3D6E8C004CF99F /* BaseTableViewController.m in Sources */,
755 761 6E84E3761F27A701001EB88E /* main.m in Sources */,
756 762 6E84E3B51F27A98B001EB88E /* HistoryViewController.m in Sources */,
757 763 E9373E4D1F361A230059355A /* HistoryGraphObject.m in Sources */,
LifeLog/LifeLog/BaseTableViewController.h
  1 +//
  2 +// BaseTableViewController.h
  3 +// LifeLog
  4 +//
  5 +// Created by nvtu on 8/11/17.
  6 +// Copyright © 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import <UIKit/UIKit.h>
  10 +
  11 +@interface BaseTableViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
  12 + NSMutableArray * _curDataList;
  13 + BOOL _firstTime;
  14 + BOOL _isLoading;
  15 + BOOL _isEndOfResult;
  16 + int _curPage;
  17 +}
  18 +
  19 +@property (weak, nonatomic) IBOutlet UITableView *tableBase;
  20 +@property (strong, nonatomic) UIRefreshControl *refreshControl;
  21 +
  22 +-(void) callRequestToUpdateData;
  23 +-(void) updateTableData : (NSArray *) array error: (NSError *) error;
  24 +-(void) resetData;
  25 +@end
LifeLog/LifeLog/BaseTableViewController.m
  1 +//
  2 +// BaseTableViewController.m
  3 +// LifeLog
  4 +//
  5 +// Created by nvtu on 8/11/17.
  6 +// Copyright © 2017 PhongNV. All rights reserved.
  7 +//
  8 +
  9 +#import "BaseTableViewController.h"
  10 +#import "Utilities.h"
  11 +
  12 +@interface BaseTableViewController ()
  13 +
  14 +@end
  15 +
  16 +@implementation BaseTableViewController
  17 +
  18 +- (void)viewDidLoad {
  19 + [super viewDidLoad];
  20 + _firstTime = false;
  21 + _curPage = 1;
  22 + _isEndOfResult = false;
  23 + _curDataList = [[NSMutableArray alloc] init];
  24 + self.refreshControl = [[UIRefreshControl alloc] init];
  25 + [self.refreshControl addTarget:self action:@selector(refreshTable) forControlEvents:UIControlEventValueChanged];
  26 + [self.tableBase addSubview:self.refreshControl];
  27 + // Do any additional setup after loading the view.
  28 +}
  29 +
  30 +- (void)didReceiveMemoryWarning {
  31 + [super didReceiveMemoryWarning];
  32 + // Dispose of any resources that can be recreated.
  33 +}
  34 +
  35 +-(void) callRequestToUpdateData {
  36 + _isLoading = true;
  37 +}
  38 +
  39 +-(void) resetData {
  40 + _isLoading = false;
  41 + _isEndOfResult = false;
  42 + _firstTime = false;
  43 + _curPage = 1;
  44 + [_curDataList removeAllObjects];
  45 + [self.tableBase reloadData];
  46 + [self callRequestToUpdateData];
  47 +}
  48 +
  49 +-(void) refreshTable {
  50 + [self resetData];
  51 +}
  52 +
  53 +-(void) updateTableData : (NSArray *) array error: (NSError *) error {
  54 + BaseTableViewController __weak *weakSelf = self;
  55 + _isLoading = false;
  56 + _firstTime = true;
  57 + if(self.refreshControl != nil) {
  58 + dispatch_async(dispatch_get_main_queue(), ^{
  59 + [self.refreshControl endRefreshing];
  60 + });
  61 + }
  62 + if(error == nil) {
  63 + if(array.count != 0) {
  64 + [_curDataList addObjectsFromArray:array];
  65 + dispatch_async(dispatch_get_main_queue(), ^{
  66 + [weakSelf.tableBase reloadData];
  67 + });
  68 + }
  69 + else {
  70 + if(_curPage != 1) {
  71 + _isEndOfResult = true;
  72 + _curPage -= 1;
  73 + }
  74 + else {
  75 + dispatch_async(dispatch_get_main_queue(), ^{
  76 + [weakSelf.tableBase reloadData];
  77 + });
  78 + }
  79 + }
  80 + }
  81 + else {
  82 + _curPage = MAX(1, _curPage - 1);
  83 + dispatch_async(dispatch_get_main_queue(), ^{
  84 + NSString *message = [error.userInfo objectForKey:@"message"];
  85 + [Utilities showErrorMessage:message withViewController:weakSelf];
  86 + });
  87 + }
  88 +}
  89 +
  90 +#pragma mark UITableView Delegate
  91 +- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
  92 + if((_curDataList == nil || _curDataList.count == 0) && _firstTime) {
  93 + UILabel * noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
  94 + noDataLabel.text = @"No data available";
  95 + noDataLabel.backgroundColor = [UIColor clearColor];
  96 + noDataLabel.textColor = [UIColor whiteColor];
  97 + noDataLabel.textAlignment = NSTextAlignmentCenter;
  98 + tableView.backgroundView = noDataLabel;
  99 + tableView.backgroundView.layer.zPosition -= 1;
  100 + return 0;
  101 + }
  102 + tableView.backgroundView = nil;
  103 + return 1;
  104 +}
  105 +
  106 +- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  107 + return _curDataList.count;
  108 +}
  109 +
  110 +- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  111 + NSInteger lastRowIndex = [tableView numberOfRowsInSection:0] - 1;
  112 + if (indexPath.row == lastRowIndex) {
  113 + // This is the last cell
  114 + if(!_isLoading) {
  115 + _curPage += 1;
  116 + [self callRequestToUpdateData];
  117 + }
  118 + }
  119 +}
  120 +@end
LifeLog/LifeLog/HistoryViewController.h
... ... @@ -9,17 +9,15 @@
9 9 #import <UIKit/UIKit.h>
10 10 #import <Charts/Charts-Swift.h>
11 11  
  12 +#import "BaseTableViewController.h"
12 13 #import "CollectionView.h"
13 14  
14 15 #import "Entities.h"
15 16  
16   -@interface HistoryViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate> {
  17 +@interface HistoryViewController : BaseTableViewController <UIScrollViewDelegate> {
17 18 NSDate * _curDate;
18 19 HistoryObject * _curHisObj;
19 20 HistoryGraphObject * _curHisGraphObj;
20   - NSMutableArray * _curHisList;
21   -
22   - int _curPageIndex;
23 21 }
24 22  
25 23 @property (weak, nonatomic) IBOutlet UILabel *lblCircleStep;
... ... @@ -38,7 +36,6 @@
38 36  
39 37 @property (weak, nonatomic) IBOutlet BarChartView *viewBarChart;
40 38  
41   -@property (weak, nonatomic) IBOutlet UITableView *tableListHistory;
42 39 @property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
43 40  
44 41 - (IBAction)swipeAction:(UISwipeGestureRecognizer *)sender;
LifeLog/LifeLog/HistoryViewController.m
... ... @@ -27,12 +27,11 @@
27 27 [self setupChartView];
28 28  
29 29 _curDate = [NSDate date];
30   - _curHisList = [[NSMutableArray alloc] init];
31 30  
32   - [self callRequestToUpdateData];
  31 + [self checkRequestData];
33 32  
34 33 //register nib for table view
35   - [self.tableListHistory registerNib:[UINib nibWithNibName:@"HistoryListTableViewCell" bundle:nil] forCellReuseIdentifier:@"HistoryListCell"];
  34 + [self.tableBase registerNib:[UINib nibWithNibName:@"HistoryListTableViewCell" bundle:nil] forCellReuseIdentifier:@"HistoryListCell"];
36 35 }
37 36  
38 37 - (void)viewDidAppear:(BOOL) animated
... ... @@ -55,7 +54,7 @@
55 54 [self.viewCollectionType setSpacing:2];
56 55 [self.viewCollectionType setArrayTitle:typeTitle];
57 56 self.viewCollectionType.changeCurrentIndex = ^(int index){
58   - [self callRequestToUpdateData];
  57 + [self checkRequestData];
59 58 };
60 59  
61 60 NSArray *modeTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.history.mode.1", nil), NSLocalizedString(@"lifelog.history.mode.2", nil), NSLocalizedString(@"lifelog.history.mode.3", nil), nil];
... ... @@ -65,7 +64,7 @@
65 64 [self.viewCollectionMode setNormalColor:[Utilities convertHecToColor:0x191919] highlightColor:[Utilities convertHecToColor:0x474747] textColor:[UIColor whiteColor]];
66 65 [self.viewCollectionMode setArrayTitle:modeTitle];
67 66 self.viewCollectionMode.changeCurrentIndex = ^(int index){
68   - [self callRequestToUpdateData];
  67 + [self checkRequestData];
69 68 };
70 69  
71 70 NSArray *shareTitle = [NSArray arrayWithObjects:NSLocalizedString(@"lifelog.history.share.1", nil), NSLocalizedString(@"lifelog.history.share.2", nil), NSLocalizedString(@"lifelog.history.share.3", nil), NSLocalizedString(@"lifelog.history.share.4", nil), NSLocalizedString(@"lifelog.history.share.5", nil), nil];
... ... @@ -75,10 +74,10 @@
75 74 [self.viewCollectionShare disableSelection];
76 75  
77 76 //add tap gesture for enable tap on gesture on CollectionView in ScrollView
78   - UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
79   - [recognizer setNumberOfTapsRequired:1];
80   - self.scrollView.userInteractionEnabled = YES;
81   - [self.scrollView addGestureRecognizer:recognizer];
  77 +// UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gestureAction:)];
  78 +// [recognizer setNumberOfTapsRequired:1];
  79 +// self.scrollView.userInteractionEnabled = YES;
  80 +// [self.scrollView addGestureRecognizer:recognizer];
82 81 }
83 82  
84 83 - (void)setupChartView {
... ... @@ -141,13 +140,12 @@
141 140 }
142 141 }
143 142  
144   --(void) callRequestToUpdateData {
145   - MBProgressHUD *hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
146   - NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
147   - int type = self.viewCollectionType.getCurrentIndex;
148   - int mode = self.viewCollectionMode.getCurrentIndex + 1;
149   -
150   - if(self.tableListHistory.alpha == 0.0) {
  143 +-(void) checkRequestData {
  144 + if(self.tableBase.alpha == 0.0) {
  145 + NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
  146 + int type = self.viewCollectionType.getCurrentIndex;
  147 + int mode = self.viewCollectionMode.getCurrentIndex + 1;
  148 + MBProgressHUD *hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
151 149 [[ServerAPI server] requestHistory:token atDate:_curDate withType:type andMode:mode CompletionHandler:^(HistoryObject *object, NSError *error) {
152 150 HistoryViewController __weak *weakSelf = self;
153 151 dispatch_async(dispatch_get_main_queue(), ^{
154 152  
155 153  
156 154  
... ... @@ -186,37 +184,39 @@
186 184 }];
187 185 }
188 186 else {
189   - [[ServerAPI server] requestHistoryList:token withType:type andMode:mode AtPage:1 CompletionHandler:^(NSMutableArray *object, NSError *error) {
190   - HistoryViewController __weak *weakSelf = self;
191   - dispatch_async(dispatch_get_main_queue(), ^{
192   - if(hudView != nil) {
193   - [hudView hideAnimated:true];
194   - }
195   - });
196   - if(error == nil) {
197   - _curHisList = object;
198   - dispatch_async(dispatch_get_main_queue(), ^{
199   - [weakSelf.tableListHistory reloadData];
200   - });
201   - }
202   - else {
203   - dispatch_async(dispatch_get_main_queue(), ^{
204   - NSString *message = [error.userInfo objectForKey:@"message"];
205   - [Utilities showErrorMessage:message withViewController:weakSelf];
206   - });
207   - }
208   - }];
  187 + [self resetData];
209 188 }
210 189 }
211 190  
  191 +-(void) callRequestToUpdateData {
  192 + [super callRequestToUpdateData];
  193 + NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
  194 + int type = self.viewCollectionType.getCurrentIndex;
  195 + int mode = self.viewCollectionMode.getCurrentIndex + 1;
  196 +
  197 + MBProgressHUD *hudView = nil;
  198 + if(_curPage == 1 && !self.refreshControl.isRefreshing) {
  199 + hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
  200 + }
  201 + [[ServerAPI server] requestHistoryList:token withType:type andMode:mode AtPage:_curPage CompletionHandler:^(NSArray *array, NSError *error) {
  202 + dispatch_async(dispatch_get_main_queue(), ^{
  203 + if(hudView != nil) {
  204 + [hudView hideAnimated:true];
  205 + }
  206 + });
  207 + HistoryViewController __weak *weakSelf = self;
  208 + [weakSelf updateTableData:array error:error];
  209 + }];
  210 +}
  211 +
212 212 #pragma mark IBAction
213 213 -(void) swipeAction:(UISwipeGestureRecognizer *)sender {
214 214 bool alphaValue = self.scrollView.alpha == 1.0 ? 1.0 : 0.0;
215 215 [UIView animateWithDuration:0.5 animations:^{
216   - self.tableListHistory.alpha = alphaValue;
  216 + self.tableBase.alpha = alphaValue;
217 217 self.scrollView.alpha = 1.0 - alphaValue;
218 218 } completion:^(BOOL completed) {
219   - [self callRequestToUpdateData];
  219 + [self checkRequestData];
220 220 }];
221 221 }
222 222  
223 223  
... ... @@ -247,28 +247,9 @@
247 247 }
248 248  
249 249 #pragma mark UITableView Delegate
250   -- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
251   - if(_curHisList == nil || _curHisList.count == 0) {
252   - UILabel * noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
253   - noDataLabel.text = @"No data available";
254   - noDataLabel.backgroundColor = [UIColor clearColor];
255   - noDataLabel.textColor = [UIColor whiteColor];
256   - noDataLabel.textAlignment = NSTextAlignmentCenter;
257   - tableView.backgroundView = noDataLabel;
258   - tableView.backgroundView.layer.zPosition -= 1;
259   - return 0;
260   - }
261   - tableView.backgroundView = nil;
262   - return 1;
263   -}
264   -
265   -- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
266   - return _curHisList.count;
267   -}
268   -
269 250 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
270 251 HistoryListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HistoryListCell"];
271   - HistoryObject * obj = [_curHisList objectAtIndex:indexPath.row];
  252 + HistoryObject * obj = [_curDataList objectAtIndex:indexPath.row];
272 253 cell.lblStep.text = [NSString stringWithFormat:@"%d", obj.step];
273 254 cell.lblPower.text = [NSString stringWithFormat:@"%0.2f", obj.calories];
274 255 cell.lblDistance.text = [NSString stringWithFormat:@"%0.1f", obj.distance];
LifeLog/LifeLog/HistoryViewController.xib
... ... @@ -20,7 +20,7 @@
20 20 <outlet property="lblStep" destination="0pf-fX-QXT" id="jK4-9Y-89Q"/>
21 21 <outlet property="lblTime" destination="PfZ-7x-LAR" id="NQv-fs-rl7"/>
22 22 <outlet property="scrollView" destination="rey-N3-l8b" id="s3w-fi-n5l"/>
23   - <outlet property="tableListHistory" destination="FXQ-4O-sRc" id="VNN-sx-9xu"/>
  23 + <outlet property="tableBase" destination="FXQ-4O-sRc" id="UHy-Nk-f7u"/>
24 24 <outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
25 25 <outlet property="viewBarChart" destination="VqD-Y3-cYQ" id="RdJ-G5-pPy"/>
26 26 <outlet property="viewCollectionMode" destination="BVv-qD-EHM" id="A6n-32-oxg"/>
LifeLog/LifeLog/SNSViewController.h
... ... @@ -7,16 +7,11 @@
7 7 //
8 8  
9 9 #import <UIKit/UIKit.h>
  10 +#import "BaseTableViewController.h"
10 11  
11   -@interface SNSViewController : UIViewController<UITableViewDelegate, UITableViewDataSource> {
12   - BOOL _firstTime;
13   - BOOL _isLoading;
14   - BOOL _isEndOfResult;
15   - int _curPage;
16   - NSMutableArray * _curTweetList;
17   -}
  12 +@interface SNSViewController : BaseTableViewController {
18 13  
19   -@property (weak, nonatomic) IBOutlet UITableView *tableRecent;
  14 +}
20 15  
21 16 @end
LifeLog/LifeLog/SNSViewController.m
... ... @@ -22,16 +22,11 @@
22 22 [super viewDidLoad];
23 23 // Do any additional setup after loading the view from its nib.
24 24 self.title = NSLocalizedString(@"lifelog.tapbar.sns", nil);
25   - _firstTime = true;
26   - _curPage = 1;
27   - _isEndOfResult = false;
28   -
29   - _curTweetList = [[NSMutableArray alloc] init];
30   -
  25 +
31 26 [self callRequestToUpdateData];
32 27  
33 28 //register nib for table view
34   - [self.tableRecent registerNib:[UINib nibWithNibName:@"SNSRecentTopicTableViewCell" bundle:nil] forCellReuseIdentifier:@"RecentTopicCell"];
  29 + [self.tableBase registerNib:[UINib nibWithNibName:@"SNSRecentTopicTableViewCell" bundle:nil] forCellReuseIdentifier:@"RecentTopicCell"];
35 30 }
36 31  
37 32 - (void)didReceiveMemoryWarning {
38 33  
... ... @@ -41,28 +36,9 @@
41 36  
42 37  
43 38 #pragma mark UITableView Delegate
44   -- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
45   - if((_curTweetList == nil || _curTweetList.count == 0) && !_firstTime) {
46   - UILabel * noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, tableView.frame.size.height)];
47   - noDataLabel.text = @"No data available";
48   - noDataLabel.backgroundColor = [UIColor clearColor];
49   - noDataLabel.textColor = [UIColor whiteColor];
50   - noDataLabel.textAlignment = NSTextAlignmentCenter;
51   - tableView.backgroundView = noDataLabel;
52   - tableView.backgroundView.layer.zPosition -= 1;
53   - return 0;
54   - }
55   - tableView.backgroundView = nil;
56   - return 1;
57   -}
58   -
59   -- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
60   - return _curTweetList.count;
61   -}
62   -
63 39 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
64 40 SNSRecentTopicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecentTopicCell"];
65   - TweetObject *object = [_curTweetList objectAtIndex:indexPath.row];
  41 + TweetObject *object = [_curDataList objectAtIndex:indexPath.row];
66 42 if(object.avatarLink && ![object.avatarLink isKindOfClass:[NSNull class]]) {
67 43 [cell.imgAvatar sd_setImageWithURL:[NSURL URLWithString:[Utilities getImageLink:object.avatarLink]]];
68 44 }
69 45  
70 46  
71 47  
72 48  
73 49  
... ... @@ -75,64 +51,25 @@
75 51 return cell;
76 52 }
77 53  
78   -- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
79   - NSInteger lastRowIndex = [tableView numberOfRowsInSection:0] - 1;
80   - if (indexPath.row == lastRowIndex) {
81   - // This is the last cell
82   - if(!_isLoading) {
83   - _curPage += 1;
84   - [self callRequestToUpdateData];
85   - }
86   - }
87   -}
88 54 #pragma mark Private Function
89 55  
90 56 -(void) callRequestToUpdateData {
91   - _isLoading = true;
  57 + [super callRequestToUpdateData];
  58 +
92 59 NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
93 60 MBProgressHUD *hudView = nil;
94   - if(_curPage == 1) {
  61 + if(_curPage == 1 && !self.refreshControl.isRefreshing) {
95 62 hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
96 63 }
97 64 [[ServerAPI server] requestRecentlyTweetsList:token withPage:_curPage CompletionHandler:^(NSArray *array, NSError *error){
98   - SNSViewController __weak *weakSelf = self;
99   - _isLoading = false;
100 65 dispatch_async(dispatch_get_main_queue(), ^{
101 66 if(hudView != nil) {
102 67 [hudView hideAnimated:true];
103 68 }
104 69 });
105   - if(error == nil) {
106   - if(_curPage == 1) {
107   - [_curTweetList removeAllObjects];
108   - }
109   - if(array.count != 0) {
110   - [_curTweetList addObjectsFromArray:array];
111   - dispatch_async(dispatch_get_main_queue(), ^{
112   - [weakSelf.tableRecent reloadData];
113   - });
114   - }
115   - else {
116   - _isEndOfResult = true;
117   - _curPage = MAX(1, _curPage - 1);
118   - }
119   - }
120   - else {
121   - _curPage = MAX(1, _curPage - 1);
122   - dispatch_async(dispatch_get_main_queue(), ^{
123   - NSString *message = [error.userInfo objectForKey:@"message"];
124   - [Utilities showErrorMessage:message withViewController:weakSelf];
125   - });
126   - }
127   -
  70 + SNSViewController __weak *weakSelf = self;
  71 + [weakSelf updateTableData:array error:error];
128 72 }];
129   -}
130   -
131   --(void) resetData {
132   - _isLoading = false;
133   - _isEndOfResult = false;
134   - _firstTime = true;
135   - _curPage = 1;
136 73 }
137 74  
138 75 @end
LifeLog/LifeLog/SNSViewController.xib
... ... @@ -10,7 +10,7 @@
10 10 <objects>
11 11 <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="SNSViewController">
12 12 <connections>
13   - <outlet property="tableRecent" destination="KcG-O5-UhK" id="80p-fP-6Pb"/>
  13 + <outlet property="tableBase" destination="KcG-O5-UhK" id="gDL-iU-n52"/>
14 14 <outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
15 15 </connections>
16 16 </placeholder>
LifeLog/LifeLog/ServerAPI.h
... ... @@ -25,7 +25,7 @@
25 25 #pragma mark - History Screen Function
26 26 - (void) requestHistory:(NSString *)token atDate:(NSDate *)date withType:(int)type andMode:(int) mode CompletionHandler:(void (^)(HistoryObject *, NSError *)) completion;
27 27 - (void) requestHistoryGraph:(NSString *)token withType:(int)type andMode:(int) mode CompletionHandler:(void (^)(HistoryGraphObject *, NSError *)) completion;
28   -- (void) requestHistoryList:(NSString *)token withType:(int)type andMode:(int) mode AtPage:(int) page CompletionHandler:(void (^)(NSMutableArray *, NSError *)) completion;
  28 +- (void) requestHistoryList:(NSString *)token withType:(int)type andMode:(int) mode AtPage:(int) page CompletionHandler:(void (^)(NSArray *, NSError *)) completion;
29 29  
30 30 #pragma mark - SNS Screen Function
31 31 - (void) requestRecentlyTweetsList:(NSString *)token withPage:(int)page CompletionHandler:(void (^)(NSArray *, NSError *)) completion;