-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPAPrayerDetailViewController.m
More file actions
291 lines (251 loc) · 11.9 KB
/
Copy pathPAPrayerDetailViewController.m
File metadata and controls
291 lines (251 loc) · 11.9 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// PAPrayerDetailViewController.m
// prayApp
//
// Created by Jeff Wang on 8/16/13.
// Copyright (c) 2013 Jeff Wang. All rights reserved.
//
#import "PAPrayerDetailViewController.h"
#import "PAPrayerService.h"
#import "PAComment.h"
#import "PACommentTableViewCell.h"
#define kLeftMargin 20
#define kTopMargin 10
#define kDetailSection 0
#define kCommentSection 1
@interface PAPrayerDetailViewController ()
@end
@implementation PAPrayerDetailViewController
@synthesize prayerDetailLabel, prayerGroupLabel, prayerItem, prayerTitleLabel, lastUpdatedLabel, contentTableView, descriptionImageView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self){
commentListing = [[NSMutableArray alloc] init];
descriptionImageView = [[UIImageView alloc] init];
prayerDetailView = [[UIView alloc] init];
prayerTitleLabel = [[UILabel alloc] init];
prayerGroupLabel = [[UILabel alloc] init];
prayerDetailLabel = [[UILabel alloc] init];
prayerTitleLabel.font = [UIFont fontWithName:@"OpenSans" size:20];
prayerGroupLabel.font = [UIFont fontWithName:@"OpenSans-Regular" size:12];
prayerDetailLabel.font = [UIFont fontWithName:@"OpenSans" size:14];
[prayerDetailView addSubview:prayerTitleLabel];
[prayerDetailView addSubview:prayerGroupLabel];
[prayerDetailView addSubview:prayerDetailLabel];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Custom initialization
descriptionImageView = [[UIImageView alloc] init];
[prayerDetailView addSubview:prayerTitleLabel];
[prayerDetailView addSubview:prayerGroupLabel];
[prayerDetailView addSubview:prayerDetailLabel];
[contentTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[contentTableView setBackgroundColor:[UIColor colorWithRed:(242.0f/255.0f) green:(242.0f/255.0f) blue:(242.0f/255.0f) alpha:1]];
//encourage view
encourageView = [[PAEncourageView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44)];
encourageView.delegate = self;
encourageView.backgroundColor = self.navigationController.navigationBar.barTintColor;
[self.view addSubview:encourageView];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
encourageView.frame = CGRectMake(0, self.view.frame.size.height - 44, self.view.frame.size.width, 44);
isEncourageViewHidden = NO;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) updateWithPrayerItem:(PAPrayer*)prayer{
self.prayerItem = prayer;
[commentListing removeAllObjects];
prayerTitleLabel.text = self.prayerItem.prayerTitle;
prayerGroupLabel.text = [NSString stringWithFormat:@"%@ in %@", [self.prayerItem.userNameString uppercaseString],[self.prayerItem.groupName uppercaseString]];
NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithString:prayerGroupLabel.text];
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"OpenSans-semibold" size:12] range:[prayerGroupLabel.text rangeOfString:[self.prayerItem.userNameString uppercaseString]]];
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"OpenSans" size:12] range:[prayerGroupLabel.text rangeOfString:@" in "]];
[attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"OpenSans-semibold" size:12] range:[prayerGroupLabel.text rangeOfString:[self.prayerItem.groupName uppercaseString]]];
prayerGroupLabel.attributedText = attributedString;
prayerGroupLabel.textColor = [UIColor grayColor];
prayerDetailLabel.text = self.prayerItem.prayerContext;
descriptionImageView.image = [UIImage imageNamed:@"pray_detail_icon_example.png"];
// descriptionImageView.frame = CGRectMake(0, 0, self.view.bounds.size.width, descriptionImageView.image.size.height);
[self updateprayerDetailViewLayout];
// isLoadingChats = YES;
[commentListing addObjectsFromArray:prayer.commentList];
NSLog(@"%@", commentListing);
[contentTableView reloadData];
// [[PAPrayerService shareInstance] getChatListing:self.prayerItem.prayerID offset:0 limits:10 success:^(PANetWorking *service, id responseObject) {
// isLoadingChats = NO;
//
// [commentListing addObjectsFromArray:responseObject];
//
// [contentTableView reloadData];
// } failure:^(PANetWorking *service, NSError *error) {
// isLoadingChats = NO;
// [contentTableView reloadData];
// }];
}
- (void)updateprayerDetailViewLayout{
float labelWidth = self.view.bounds.size.width - 2 * kLeftMargin;
prayerTitleLabel.frame = CGRectMake(kLeftMargin, kTopMargin, labelWidth, 40);
[prayerTitleLabel setNumberOfLines:0];
[prayerTitleLabel setLineBreakMode:NSLineBreakByWordWrapping];
[prayerTitleLabel sizeToFit];
prayerGroupLabel.frame = CGRectMake(kLeftMargin, prayerTitleLabel.frame.origin.y + prayerTitleLabel.frame.size.height + kTopMargin, labelWidth, 40);
[prayerGroupLabel setNumberOfLines:0];
[prayerGroupLabel setLineBreakMode:NSLineBreakByWordWrapping];
[prayerGroupLabel sizeToFit];
prayerDetailLabel.frame = CGRectMake(kLeftMargin, prayerGroupLabel.frame.origin.y + prayerGroupLabel.frame.size.height + kTopMargin, labelWidth, 40);
[prayerDetailLabel setNumberOfLines:0];
[prayerDetailLabel setLineBreakMode:NSLineBreakByWordWrapping];
[prayerDetailLabel sizeToFit];
prayerDetailView.frame = CGRectMake(0, 0, self.view.bounds.size.width, prayerDetailLabel.frame.origin.y + prayerDetailLabel.frame.size.height + kTopMargin);
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == kDetailSection){
if (indexPath.row == 0){
return prayerDetailView.frame.size.height;
}
else if (indexPath.row == 1){
if (isLoadingChats)
return 40;
else
return 0;
}
else
return 0;
}
else if (indexPath.section == kCommentSection){
//TODO: Dynamic height
return [PACommentTableViewCell estimateCellheightWithComment:[commentListing objectAtIndex:indexPath.row]];
}
else
return 0;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (section == kDetailSection)
return 2;
else if (section == kCommentSection)
return [commentListing count];
else
return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == kDetailSection) {
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
// if (indexPath.row == 0){
// descriptionImageView.image = [UIImage imageNamed:@"pray_detail_icon_example.png"];
// descriptionImageView.frame = CGRectMake(0, 0, self.view.bounds.size.width, descriptionImageView.image.size.height);
// [cell.contentView addSubview:descriptionImageView];
// }
// else if (indexPath.row == 1){
if (indexPath.row == 0){
[cell.contentView addSubview:prayerDetailView];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}
else{
if (loadingChatIndicator == nil){
loadingChatIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[loadingChatIndicator startAnimating];
[loadingChatIndicator setHidesWhenStopped:YES];
loadingChatIndicator.center = cell.contentView.center;
[cell.contentView addSubview:loadingChatIndicator];
[cell.contentView setBackgroundColor:[UIColor colorWithRed:(242.0f/255.0f) green:(242.0f/255.0f) blue:(242.0f/255.0f) alpha:1]];
}
else
[loadingChatIndicator startAnimating];
}
return cell;
}
else {
PACommentTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"commentCell"];
if (!cell){
cell = [[PACommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"commentCell"];
}
PAComment * comment = [commentListing objectAtIndex:indexPath.row];
[cell updateWithComment:comment];
return cell;
}
}
//// scrollview delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if ([scrollView.panGestureRecognizer translationInView:scrollView].y > 0) {
// scroll up.. present the encourage section.
if (isEncourageViewHidden == YES){
isEncourageViewHidden = NO;
[UIView animateWithDuration:0.35 animations:^{
CGRect frame = encourageView.frame;
frame.origin.y -= frame.size.height;
encourageView.frame = frame;
}];
}
} else {
// scroll to reveal more chats, hide the encourageview
if (isEncourageViewHidden == NO){
isEncourageViewHidden = YES;
[UIView animateWithDuration:0.35 animations:^{
CGRect frame = encourageView.frame;
frame.origin.y += frame.size.height;
encourageView.frame = frame;
}];
}
}
}
//encourageView delegate
- (void)encourageViewDidPray:(id)encourageView{
isLoadingChats = YES;
[contentTableView reloadData];
// [[PAPrayerService shareInstance] setPrayActionForlisting:prayerItem.prayerID userId:@"9912" success:^(PANetWorking *service, id responseObject) {
// isLoadingChats = NO;
// [contentTableView reloadData];
//
// if ([responseObject isKindOfClass:[PAComment class]]){
// PAComment* comment = responseObject;
// [commentListing insertObject:comment atIndex:0];
// NSIndexPath *path1 = [NSIndexPath indexPathForRow:0 inSection:kCommentSection];
// NSArray *indexArray = [NSArray arrayWithObjects:path1,nil];
//
// [contentTableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];
// }
// } failure:^(PANetWorking *service, NSError *error) {
// NSLog(@"Do nothing....");
// }];
// NSTimer* timer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:30] interval:3 target:self selector:@selector(test) userInfo:Nil repeats:NO];
// [timer fire];
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(test) userInfo:nil repeats:NO];
}
- (void)test{
[[PAPrayerService shareInstance] setPrayActionForlisting:prayerItem.prayerID userId:@"9912" success:^(PANetWorking *service, id responseObject) {
isLoadingChats = NO;
[contentTableView reloadData];
if ([responseObject isKindOfClass:[PAComment class]]){
PAComment* comment = responseObject;
[commentListing insertObject:comment atIndex:0];
NSIndexPath *path1 = [NSIndexPath indexPathForRow:0 inSection:kCommentSection];
NSArray *indexArray = [NSArray arrayWithObjects:path1,nil];
[contentTableView insertRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationAutomatic];
}
} failure:^(PANetWorking *service, NSError *error) {
NSLog(@"Do nothing....");
}];
}
@end