TodayViewController.m 3.6 KB

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