TodayViewController.m 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "TodayViewController.h"
  19. #import <NotificationCenter/NotificationCenter.h>
  20. #import "Tick.h"
  21. @interface TodayViewController () <NCWidgetProviding>
  22. @property (nonatomic, strong) UIButton *button;
  23. @property (nonatomic, strong) Tick *tick;
  24. @property (nonatomic, strong) RLMNotificationToken *notificationToken;
  25. @end
  26. @implementation TodayViewController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. self.preferredContentSize = CGSizeMake(0, 200.0);
  30. RLMRealmConfiguration *configuration = [RLMRealmConfiguration defaultConfiguration];
  31. configuration.fileURL = [[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.io.realm.examples.extension"] URLByAppendingPathComponent:@"extension.realm"];
  32. [RLMRealmConfiguration setDefaultConfiguration:configuration];
  33. self.tick = [Tick allObjects].firstObject;
  34. if (!self.tick) {
  35. [[RLMRealm defaultRealm] transactionWithBlock:^{
  36. self.tick = [Tick createInDefaultRealmWithValue:@[@"", @0]];
  37. }];
  38. }
  39. self.notificationToken = [self.tick.realm addNotificationBlock:^(NSString *notification, RLMRealm *realm) {
  40. // Occasionally, respond immediately to the notification by triggering a new notification.
  41. if (self.tick.count % 19 == 0) {
  42. [self tock];
  43. }
  44. [self updateLabel];
  45. }];
  46. self.button = [UIButton buttonWithType:UIButtonTypeSystem];
  47. self.button.frame = self.view.bounds;
  48. [self.button addTarget:self action:@selector(tock) forControlEvents:UIControlEventTouchUpInside];
  49. self.button.titleLabel.textAlignment = NSTextAlignmentCenter;
  50. [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  51. [self.view addSubview:self.button];
  52. [self updateLabel];
  53. }
  54. - (void)viewWillAppear:(BOOL)animated {
  55. [super viewWillAppear:animated];
  56. self.button.frame = self.view.bounds;
  57. [self updateLabel];
  58. }
  59. - (void)viewDidAppear:(BOOL)animated {
  60. [super viewDidAppear:animated];
  61. [self tock];
  62. [self updateLabel];
  63. }
  64. - (void)updateLabel {
  65. [self.button setTitle:@(self.tick.count).stringValue forState:UIControlStateNormal];
  66. }
  67. - (void)tock {
  68. [[RLMRealm defaultRealm] transactionWithBlock:^{
  69. self.tick.count++;
  70. }];
  71. }
  72. @end