TableViewController.m 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2015 Realm Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. ////////////////////////////////////////////////////////////////////////////
  18. #import "TableViewController.h"
  19. #import <Realm/Realm.h>
  20. #import <ReactiveCocoa/ReactiveCocoa.h>
  21. // Data models: GroupParent contains all of the data for a TableView, with a
  22. // Group per section and an Entry per row in each section
  23. RLM_ARRAY_TYPE(Entry)
  24. RLM_ARRAY_TYPE(Group)
  25. @interface Entry : RLMObject
  26. @property (nonatomic, strong) NSString *title;
  27. @property (nonatomic, strong) NSDate *date;
  28. @end
  29. @interface Group : RLMObject
  30. @property (nonatomic, strong) NSString *name;
  31. @property (nonatomic, strong) RLMArray<Entry *><Entry> *entries;
  32. @end
  33. @interface GroupParent : RLMObject
  34. @property (nonatomic, strong) RLMArray<Group *><Group> *groups;
  35. @end
  36. @implementation Entry
  37. // Nothing needed
  38. @end
  39. @implementation Group
  40. // Nothing needed
  41. @end
  42. @implementation GroupParent
  43. // Nothing needed
  44. @end
  45. @interface Cell : UITableViewCell
  46. @property (nonatomic, strong) Entry *entry;
  47. @end
  48. @implementation Cell
  49. - (instancetype)initWithStyle:(__unused UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
  50. return self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
  51. }
  52. - (void)attach:(Entry *)entry {
  53. // If this is the first time this Cell is used, bind its UILabels to the
  54. // fields of its Entry. If it's been used before, the existing bindings
  55. // will continue to work
  56. if (self.entry == nil) {
  57. RAC(self.textLabel, text) = RACObserve(self, entry.title);
  58. RAC(self.detailTextLabel, text) = [RACObserve(self, entry.date) map:^(NSDate *date) { return date.description; }];
  59. }
  60. self.entry = entry;
  61. }
  62. @end
  63. @interface TableViewController ()
  64. @property (nonatomic, strong) GroupParent *parent;
  65. @end
  66. @implementation TableViewController
  67. #pragma mark - View Lifecycle
  68. - (void)viewDidLoad {
  69. [super viewDidLoad];
  70. // Get the singleton GroupParent object from the Realm, creating it
  71. // if needed. In a more complete example with more than one view, this
  72. // would be supplied as the data source by whatever is displaying this
  73. // table view
  74. self.parent = GroupParent.allObjects.firstObject;
  75. if (!self.parent) {
  76. self.parent = [GroupParent new];
  77. RLMRealm *realm = RLMRealm.defaultRealm;
  78. [realm transactionWithBlock:^{
  79. [realm addObject:self.parent];
  80. }];
  81. }
  82. [self setupUI];
  83. [self.tableView reloadData];
  84. }
  85. #pragma mark - UI
  86. - (void)setupUI {
  87. [self.tableView registerClass:[Cell class] forCellReuseIdentifier:@"cell"];
  88. self.title = @"ReactiveCocoa GroupedTableView";
  89. RACCommand *addGroup = [[RACCommand alloc] initWithSignalBlock:^(id unused) {
  90. [self modifyInBackground:^(RLMArray *groups) {
  91. NSString *name = [NSString stringWithFormat:@"Group %d", (int)arc4random()];
  92. [groups addObject:[Group createInDefaultRealmWithValue:@[name, @[]]]];
  93. }];
  94. return [RACSignal empty];
  95. }];
  96. RACCommand *addEntry = [[RACCommand alloc]
  97. initWithEnabled:[RACObserve(self.parent, groups) map:^(RLMArray *groups) {
  98. return @(groups.count > 0);
  99. }]
  100. signalBlock:^(id unused) {
  101. [self modifyInBackground:^(RLMArray *groups) {
  102. Group *group = groups[arc4random_uniform((uint32_t)groups.count)];
  103. NSString *name = [NSString stringWithFormat:@"Entry %d", (int)arc4random()];
  104. [group.entries addObject:[Entry createInDefaultRealmWithValue:@[name, NSDate.date]]];
  105. }];
  106. return [RACSignal empty];
  107. }];
  108. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Add Group"
  109. style:UIBarButtonItemStylePlain
  110. target:nil action:nil];
  111. self.navigationItem.leftBarButtonItem.rac_command = addGroup;
  112. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
  113. target:nil action:nil];
  114. self.navigationItem.rightBarButtonItem.rac_command = addEntry;
  115. // Subscribe to changes to the list of groups, telling the TableView to
  116. // insert new sections when new groups are added to the list
  117. @weakify(self);
  118. [[self.parent rac_valuesAndChangesForKeyPath:@"groups" options:0 observer:self]
  119. subscribeNext:^(RACTuple *info) { // tuple is value, change dictionary
  120. @strongify(self);
  121. NSDictionary *change = info.second;
  122. NSKeyValueChange kind = [change[NSKeyValueChangeKindKey] intValue];
  123. NSIndexSet *indexes = change[NSKeyValueChangeIndexesKey];
  124. if (indexes && kind == NSKeyValueChangeInsertion) {
  125. [self.tableView insertSections:indexes withRowAnimation:UITableViewRowAnimationAutomatic];
  126. [self bindGroup:self.parent.groups.lastObject];
  127. }
  128. else {
  129. [self.tableView reloadData];
  130. }
  131. }];
  132. for (Group *group in self.parent.groups) {
  133. [self bindGroup:group];
  134. }
  135. }
  136. #pragma mark - UITableViewDataSource
  137. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  138. return self.parent.groups.count;
  139. }
  140. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  141. return [self.parent.groups[section] name];
  142. }
  143. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  144. return [self.parent.groups[section] entries].count;
  145. }
  146. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  147. Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  148. [cell attach:[self objectForIndexPath:indexPath]];
  149. return cell;
  150. }
  151. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  152. if (editingStyle == UITableViewCellEditingStyleDelete) {
  153. RLMRealm *realm = RLMRealm.defaultRealm;
  154. [realm beginWriteTransaction];
  155. [realm deleteObject:[self objectForIndexPath:indexPath]];
  156. [realm commitWriteTransaction];
  157. }
  158. }
  159. - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
  160. // Update the date of any row selected in the UI. The display of the date
  161. // in the UI is automatically updated by the binding estabished in Cell.attach
  162. RLMRealm *realm = RLMRealm.defaultRealm;
  163. [realm transactionWithBlock:^{
  164. [self objectForIndexPath:indexPath].date = NSDate.date;
  165. }];
  166. }
  167. #pragma - Helpers
  168. // Get the Entry at a given index path
  169. - (Entry *)objectForIndexPath:(NSIndexPath *)indexPath {
  170. return self.parent.groups[indexPath.section].entries[indexPath.row];
  171. }
  172. // Convert an NSIndexSet to an array of NSIndexPaths
  173. - (NSArray<NSIndexPath *> *)indexSetToIndexPathArray:(NSIndexSet *)indexes section:(NSInteger)section {
  174. NSMutableArray<NSIndexPath *> *paths = [NSMutableArray arrayWithCapacity:indexes.count];
  175. NSUInteger index = [indexes firstIndex];
  176. while (index != NSNotFound) {
  177. [paths addObject:[NSIndexPath indexPathForRow:index inSection:section]];
  178. index = [indexes indexGreaterThanIndex:index];
  179. }
  180. return paths;
  181. }
  182. - (void)modifyInBackground:(void (^)(RLMArray *))block {
  183. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  184. @autoreleasepool {
  185. GroupParent *parent = GroupParent.allObjects.firstObject;
  186. [parent.realm beginWriteTransaction];
  187. block(parent.groups);
  188. [parent.realm commitWriteTransaction];
  189. }
  190. });
  191. }
  192. // Listen for changes to the list of entries in a Group, and tell the UI to
  193. // update when entries are added or removed
  194. - (void)bindGroup:(Group *)group {
  195. @weakify(self);
  196. [[group rac_valuesAndChangesForKeyPath:@"entries" options:0 observer:self]
  197. subscribeNext:^(RACTuple *info) { // tuple is value, change dictionary
  198. @strongify(self);
  199. NSDictionary *change = info.second;
  200. NSKeyValueChange kind = [change[NSKeyValueChangeKindKey] intValue];
  201. NSIndexSet *indexes = change[NSKeyValueChangeIndexesKey];
  202. if (indexes) {
  203. NSInteger section = [self.parent.groups indexOfObject:group];
  204. NSArray *paths = [self indexSetToIndexPathArray:indexes section:section];
  205. if (kind == NSKeyValueChangeInsertion) {
  206. [self.tableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationAutomatic];
  207. }
  208. else if (kind == NSKeyValueChangeRemoval) {
  209. [self.tableView deleteRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationAutomatic];
  210. }
  211. else {
  212. [self.tableView reloadData];
  213. }
  214. }
  215. else {
  216. [self.tableView reloadData];
  217. }
  218. }];
  219. }
  220. @end