SNSViewController.m
4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//
// SNSViewController.m
// LifeLog
//
// Created by Nguyen Van Phong on 7/25/17.
// Copyright © 2017 PhongNV. All rights reserved.
//
#import "SNSViewController.h"
#import "SNSRecentTopicTableViewCell.h"
#import "ServerAPI.h"
#import "Utilities.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface SNSViewController ()
@end
@implementation SNSViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = NSLocalizedString(@"lifelog.tapbar.sns", nil);
_firstTime = true;
_curPage = 1;
_isEndOfResult = false;
_curTweetList = [[NSMutableArray alloc] init];
[self callRequestToUpdateData];
//register nib for table view
[self.tableRecent registerNib:[UINib nibWithNibName:@"SNSRecentTopicTableViewCell" bundle:nil] forCellReuseIdentifier:@"RecentTopicCell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark UITableView Delegate
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView {
if((_curTweetList == nil || _curTweetList.count == 0) && !_firstTime) {
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 _curTweetList.count;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SNSRecentTopicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecentTopicCell"];
TweetObject *object = [_curTweetList objectAtIndex:indexPath.row];
if(object.avatarLink && ![object.avatarLink isKindOfClass:[NSNull class]]) {
[cell.imgAvatar sd_setImageWithURL:[NSURL URLWithString:[Utilities getImageLink:object.avatarLink]]];
}
cell.lblDateTime.text = [Utilities stringFromDate:object.createDate withFormat:@"YYYY/MM/dd hh:mm"];
cell.lblUsername.text = object.userName;
cell.lblDes.text = object.content;
cell.lblMode.text = object.mode;
cell.lblDistance.text = [NSString stringWithFormat:@"%.0f m", object.distance];
cell.lblDuration.text = object.time;
return cell;
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSInteger lastRowIndex = [tableView numberOfRowsInSection:0] - 1;
if (indexPath.row == lastRowIndex) {
// This is the last cell
if(!_isLoading) {
_curPage += 1;
[self callRequestToUpdateData];
}
}
}
#pragma mark Private Function
-(void) callRequestToUpdateData {
_isLoading = true;
NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
MBProgressHUD *hudView = nil;
if(_curPage == 1) {
hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
}
[[ServerAPI server] requestRecentlyTweetsList:token withPage:_curPage CompletionHandler:^(NSArray *array, NSError *error){
SNSViewController __weak *weakSelf = self;
_isLoading = false;
dispatch_async(dispatch_get_main_queue(), ^{
if(hudView != nil) {
[hudView hideAnimated:true];
}
});
if(error == nil) {
if(_curPage == 1) {
[_curTweetList removeAllObjects];
}
if(array.count != 0) {
[_curTweetList addObjectsFromArray:array];
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf.tableRecent reloadData];
});
}
else {
_isEndOfResult = true;
_curPage = MAX(1, _curPage - 1);
}
}
else {
_curPage = MAX(1, _curPage - 1);
dispatch_async(dispatch_get_main_queue(), ^{
NSString *message = [error.userInfo objectForKey:@"message"];
[Utilities showErrorMessage:message withViewController:weakSelf];
});
}
}];
}
-(void) resetData {
_isLoading = false;
_isEndOfResult = false;
_firstTime = true;
_curPage = 1;
}
@end