GroupDetailViewController.m
7.58 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// GroupDetailViewController.m
// LifeLog
//
// Created by nvtu on 8/13/17.
// Copyright © 2017 PhongNV. All rights reserved.
//
#import "GroupDetailViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "Utilities.h"
#import "ServerAPI.h"
#import "SNSRecentTopicTableViewCell.h"
@interface GroupDetailViewController ()
@end
@implementation GroupDetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
isMemberList = false;
//register nib for table view
[self.tableBase registerNib:[UINib nibWithNibName:@"SNSRecentTopicTableViewCell" bundle:nil] forCellReuseIdentifier:@"RecentTopicCell"];
if(_curGroup != nil) {
[self requestGroupDetail];
}
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) setGroup : (GroupObject *) object {
_curGroup = object;
}
- (void)setupView {
if(_curGroup != nil) {
if(self.imgAva != nil && _curGroup.avatar && ![_curGroup.avatar isKindOfClass:[NSNull class]]) {
[self.imgAva sd_setImageWithURL:[NSURL URLWithString:[Utilities getImageLink:_curGroup.avatar]]];
}
if(self.lblGrpName != nil) {
[self.lblGrpName setText:_curGroup.name];
}
if(self.btJoinGrp != nil) {
[self.btJoinGrp setHidden:false];
}
NSString *mode = @"";
NSString *goalText = [NSString stringWithFormat:@"%@\n", _curGroup.goal];
if(_curGroup.runMode || _curGroup.walkMode || _curGroup.bikeMode) {
goalText = [goalText stringByAppendingString:@"1日 目標 "];
}
if(_curGroup.runMode) {
mode = [mode stringByAppendingString:NSLocalizedString(@"lifelog.grDetail.mode.running", nil)];
mode = [mode stringByAppendingString:@"\n"];
goalText = [goalText stringByAppendingFormat:@"RUN %dm, ", _curGroup.runGoal];
}
if(_curGroup.walkMode) {
mode = [mode stringByAppendingString:NSLocalizedString(@"lifelog.grDetail.mode.walking", nil)];
mode = [mode stringByAppendingString:@"\n"];
goalText = [goalText stringByAppendingFormat:@"WALK %dm, ", _curGroup.walkGoal];
}
if(_curGroup.bikeMode) {
mode = [mode stringByAppendingString:NSLocalizedString(@"lifelog.grDetail.mode.bike", nil)];
mode = [mode stringByAppendingString:@"\n"];
goalText = [goalText stringByAppendingFormat:@"BIKE %dm, ", _curGroup.bikeGoal];
}
if(_curGroup.stepMode) {
mode = [mode stringByAppendingString:NSLocalizedString(@"lifelog.grDetail.mode.step", nil)];
mode = [mode stringByAppendingString:@"\n"];
}
if(_curGroup.beginMode) {
mode = [mode stringByAppendingString:NSLocalizedString(@"lifelog.grDetail.mode.begin", nil)];
mode = [mode stringByAppendingString:@"\n"];
}
if(_curGroup.gymMode) {
mode = [mode stringByAppendingString:NSLocalizedString(@"lifelog.grDetail.mode.gym", nil)];
mode = [mode stringByAppendingString:@"\n"];
}
NSRange range = [mode rangeOfString:@"\n" options:NSBackwardsSearch];
if(range.length > 0) {
mode = [mode stringByReplacingOccurrencesOfString:@"\n" withString:@"" options:NSBackwardsSearch range:range];
}
range = [goalText rangeOfString:@", " options:NSBackwardsSearch];
if(range.length > 0) {
goalText = [goalText stringByReplacingOccurrencesOfString:@", " withString:@"" options:NSBackwardsSearch range:range];
}
[self.lbbGrpActiveMode setText:mode];
[self.lblGrpGoal setText:goalText];
}
}
- (void) requestGroupDetail {
NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
MBProgressHUD *hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
[[ServerAPI server] getGroupDetail:token withGroupID:_curGroup.groupID CompletionHandler:^(GroupObject *object, NSError *error) {
GroupDetailViewController __weak *weakSelf = self;
[_curGroup updateDate:object];
dispatch_async(dispatch_get_main_queue(), ^{
[hudView hideAnimated:true];
[weakSelf setupView];
[weakSelf callRequestToUpdateData];
});
}];
}
#pragma mark IBAction
- (IBAction)clickBack:(id)sender {
[self.navigationController popViewControllerAnimated:true];
}
- (IBAction)clickSwitch:(AutoTransButton *)sender {
isMemberList = !isMemberList;
if(isMemberList) {
[sender setTitle:NSLocalizedString(@"lifelog.grDetail.bt.viewTweet", nil) forState:UIControlStateNormal];
}
else {
[sender setTitle:NSLocalizedString(@"lifelog.grDetail.bt.viewMem", nil) forState:UIControlStateNormal];
}
[sender setUserInteractionEnabled:false];
[self resetData];
}
- (IBAction)clickJoin:(AutoTransButton *)sender {
NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
MBProgressHUD *hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
[[ServerAPI server] requestJoinGroup:token groupID:_curGroup.groupID CompletionHandler:^(NSError *error){
GroupDetailViewController __weak *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
[hudView hideAnimated:true];
if(error == nil) {
_curGroup.isJoin = true;
[weakSelf.btJoinGrp setHidden:_curGroup.isJoin];
[Utilities showMessage:@"Join successfully" withViewController:weakSelf];
}
});
}];
}
#pragma mark UITableView Delegate
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SNSRecentTopicTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"RecentTopicCell"];
if(isMemberList) {
MemberObject *object = [_curDataList objectAtIndex:indexPath.row];
[cell setMemberData:object];
}
else {
TweetObject *object = [_curDataList objectAtIndex:indexPath.row];
[cell setTweetsData:object];
}
return cell;
}
#pragma mark Private Function
-(void) callRequestToUpdateData {
[super callRequestToUpdateData];
NSString * token = [[NSUserDefaults standardUserDefaults] stringForKey:kToken];
MBProgressHUD *hudView = nil;
if(_curPage == 1 && !self.refreshControl.isRefreshing) {
hudView = [MBProgressHUD showHUDAddedTo:self.view animated:true];
}
if(isMemberList) {
[[ServerAPI server] requestMemberList:token groupID:_curGroup.groupID withPage:_curPage CompletionHandler:^(NSArray *array, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
if(hudView != nil) {
[hudView hideAnimated:true];
}
});
GroupDetailViewController __weak *weakSelf = self;
[weakSelf updateTableData:array error:error];
[weakSelf.btSwitch setUserInteractionEnabled:true];
}];
}
else {
[[ServerAPI server] requestTweetsList:token groupID:_curGroup.groupID withPage:_curPage CompletionHandler:^(NSArray *array, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
if(hudView != nil) {
[hudView hideAnimated:true];
}
});
GroupDetailViewController __weak *weakSelf = self;
[weakSelf updateTableData:array error:error];
[weakSelf.btSwitch setUserInteractionEnabled:true];
}];
}
}
@end