forked from martinc/ObjectiveDDP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListViewController.m
More file actions
145 lines (112 loc) · 5.59 KB
/
ListViewController.m
File metadata and controls
145 lines (112 loc) · 5.59 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
#import "ListViewController.h"
#import "ViewController.h"
#import "MeteorClient.h"
@interface ListViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (strong, nonatomic) NSMutableArray *lists;
@end
@implementation ListViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
meteor:(MeteorClient *)meteor {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.meteor = meteor;
self.lists = self.meteor.collections[@"lists"];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
self.navigationItem.title = @"My Lists";
self.navigationController.navigationBarHidden = NO;
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *logoutButton = [[UIBarButtonItem alloc] initWithTitle:@"Logout" style:UIBarButtonItemStylePlain target:self action:@selector(logout)];
self.navigationItem.rightBarButtonItem = logoutButton;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveUpdate:)
name:@"added"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveUpdate:)
name:@"removed"
object:nil];
}
- (void)didReceiveUpdate:(NSNotification *)notification {
[self.tableview reloadData];
}
- (void)logout {
[self.meteor logout];
[self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.lists count];
}
static NSDictionary *selectedList;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"list";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
NSDictionary *list = self.lists[indexPath.row];
selectedList = list;
cell.textLabel.text = list[@"name"];
UIButton *shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
shareButton.frame = CGRectMake(255.0f, 5.0f, 55.0f, 34.0f);
shareButton.backgroundColor = [UIColor greenColor];
[shareButton setTitle:@"Share" forState:UIControlStateNormal];
[shareButton addTarget:self action:@selector(didClickShareButton:forEvent:) forControlEvents:UIControlEventTouchUpInside];
// XXX: shareButton needs to be able to link to its list
// shareButton
[cell addSubview:shareButton];
return cell;
}
static UITextField *shareWithTF;
- (void)didClickShareButton:(id)sender forEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, location.y, 320.0, 100.0)];
view.backgroundColor = [UIColor whiteColor];
UITextField *shareWithTextField = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 50.0, 240.0, 44.0)];
shareWithTF = shareWithTextField;
shareWithTextField.borderStyle = UITextBorderStyleLine;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(255.0, 50.0, 60.0, 44.0);
button.backgroundColor = [UIColor greenColor];
[button setTitle:@"Send" forState:UIControlStateNormal];
[button addTarget:self action:@selector(didClickShareWithButton:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:shareWithTextField];
[view addSubview:button];
UIView *modalBackground = [[UIView alloc] initWithFrame:self.view.frame];
modalBackground.backgroundColor = [UIColor blackColor];
modalBackground.alpha = 0.7;
[self.view addSubview:modalBackground];
[self.view addSubview:view];
}
- (void)didClickShareWithButton:(id)sender {
[self.meteor sendWithMethodName:@"/lists/update"
parameters:@[@{@"_id": selectedList[@"_id"]}, @{@"$set":@{@"share_with": shareWithTF.text}}]];
[[[self.view subviews] lastObject] removeFromSuperview];
[[[self.view subviews] lastObject] removeFromSuperview];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *list = self.lists[indexPath.row];
[self.meteor sendWithMethodName:@"/lists/remove"
parameters:@[@{@"_id": list[@"_id"]}]];
}
#pragma mark <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *list = self.lists[indexPath.row];
ViewController *controller = [[ViewController alloc] initWithNibName:@"ViewController"
bundle:nil
meteor:self.meteor
listName:list[@"name"]];
controller.userId = self.userId;
[self.navigationController pushViewController:controller animated:YES];
}
@end