TodayViewController.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // TodayViewController.m
  2. //
  3. // Copyright (c) 2015 Brian Nickel
  4. //
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #import <NotificationCenter/NotificationCenter.h>
  24. #import "TodayViewController.h"
  25. #import "Post.h"
  26. #import "User.h"
  27. #import "UIImageView+AFNetworking.h"
  28. @interface TodayViewController () <NCWidgetProviding>
  29. @property (strong, nonatomic) IBOutlet UIImageView *imageView;
  30. @property (strong, nonatomic) IBOutlet UILabel *titleLabel;
  31. @property (strong, nonatomic) IBOutlet UILabel *bodyLabel;
  32. @property (nonatomic, strong) Post *post;
  33. @end
  34. @implementation TodayViewController
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. NSURLCache *URLCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];
  38. [NSURLCache setSharedURLCache:URLCache];
  39. self.post = [self loadSavedPost];
  40. }
  41. - (void)viewWillAppear:(BOOL)animated {
  42. [super viewWillAppear:animated];
  43. }
  44. - (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler {
  45. [Post globalTimelinePostsWithBlock:^(NSArray *posts, NSError *error) {
  46. if (!error) {
  47. self.post = posts.firstObject;
  48. [self savePost:self.post];
  49. if (completionHandler) {
  50. completionHandler(self.post != nil ? NCUpdateResultNewData : NCUpdateResultNoData);
  51. }
  52. } else {
  53. if (completionHandler) {
  54. completionHandler(NCUpdateResultFailed);
  55. }
  56. }
  57. }];
  58. }
  59. - (void)setPost:(Post *)post {
  60. _post = post;
  61. self.titleLabel.hidden = post == nil;
  62. self.bodyLabel.hidden = post == nil;
  63. self.imageView.hidden = post == nil;
  64. if (post == nil) {
  65. return;
  66. }
  67. self.titleLabel.text = _post.user.username;
  68. self.bodyLabel.text = _post.text;
  69. [self.imageView setImageWithURL:_post.user.avatarImageURL placeholderImage:[UIImage imageNamed:@"profile-image-placeholder"]];
  70. }
  71. - (void)savePost:(Post *)post {
  72. if (post == nil) {
  73. [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"AF.post"];
  74. [[NSUserDefaults standardUserDefaults] synchronize];
  75. return;
  76. }
  77. NSData *postData = [NSKeyedArchiver archivedDataWithRootObject:post];
  78. [[NSUserDefaults standardUserDefaults] setObject:postData forKey:@"AF.post"];
  79. [[NSUserDefaults standardUserDefaults] synchronize];
  80. }
  81. - (Post *)loadSavedPost {
  82. NSData *postData = [[NSUserDefaults standardUserDefaults] objectForKey:@"AF.post"];
  83. if (postData == nil || ![postData isKindOfClass:[NSData class]]) {
  84. return nil;
  85. }
  86. return [NSKeyedUnarchiver unarchiveObjectWithData:postData];
  87. }
  88. @end