Repository navigation
Expand file tree
/
Copy pathViewController.m
More file actions
120 lines (95 loc) · 4.39 KB
/
Copy pathViewController.m
File metadata and controls
120 lines (95 loc) · 4.39 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
#import "ViewController.h"
#import <ObjectiveDDP/BSONIdGenerator.h>
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (copy, nonatomic) NSString *listName;
@end
@implementation ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
meteor:(MeteorClient *)meteor
listName:(NSString *) listName {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.meteor = meteor;
self.listName = listName;
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
self.navigationItem.title = self.listName;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(didTouchAdd:)];
[self.navigationItem setRightBarButtonItem:addButton];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveUpdate:)
name:@"added"
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveUpdate:)
name:@"removed"
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveUpdate:(NSNotification *)notification {
[self.tableview reloadData];
}
- (NSArray *)computedList {
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(listName like %@)", self.listName];
return [self.meteor.collections[@"things"] filteredArrayUsingPredicate:pred];
}
#pragma mark UI Actions
- (IBAction)didTouchAdd:(id)sender {
AddViewController *addController = [[AddViewController alloc] initWithNibName:@"AddViewController"
bundle:nil];
addController.delegate = self;
[self presentViewController:addController animated:YES completion:nil];
}
#pragma mark <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.computedList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"thing";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier];
}
NSDictionary *thing = self.computedList[indexPath.row];
cell.textLabel.text = thing[@"msg"];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
NSDictionary *thing = self.computedList[indexPath.row];
// Specifically NOT removing the object locally
// we'll get a notification from the meteor server when this has been done
// and that'll cause us to update our local cache:
//
// [self.things removeObject:thing];
// Could implement meteor style "latency compensation" by removing here
// and syncing subscription to server later
[self.meteor sendWithMethodName:@"/things/remove"
parameters:@[@{@"_id": thing[@"_id"]}]];
}
}
- (void)didAddThing:(NSString *)message {
[self dismissViewControllerAnimated:YES completion:nil];
NSString *uid = [BSONIdGenerator generate];
[self.meteor sendWithMethodName:@"/things/insert"
parameters:@[@{@"_id": uid,
@"msg": message,
@"owner": self.userId,
@"listName": self.listName}]];
}
@end