User.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // User.m
  2. //
  3. // Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
  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 "User.h"
  23. #import "AFHTTPRequestOperation.h"
  24. NSString * const kUserProfileImageDidLoadNotification = @"com.alamofire.user.profile-image.loaded";
  25. @interface User ()
  26. @property (readwrite, nonatomic, assign) NSUInteger userID;
  27. @property (readwrite, nonatomic, copy) NSString *username;
  28. @property (readwrite, nonatomic, copy) NSString *avatarImageURLString;
  29. #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
  30. @property (readwrite, nonatomic, strong) AFHTTPRequestOperation *avatarImageRequestOperation;
  31. #endif
  32. @end
  33. @implementation User
  34. - (instancetype)initWithAttributes:(NSDictionary *)attributes {
  35. self = [super init];
  36. if (!self) {
  37. return nil;
  38. }
  39. self.userID = (NSUInteger)[[attributes valueForKeyPath:@"id"] integerValue];
  40. self.username = [attributes valueForKeyPath:@"username"];
  41. self.avatarImageURLString = [attributes valueForKeyPath:@"avatar_image.url"];
  42. return self;
  43. }
  44. - (NSURL *)avatarImageURL {
  45. return [NSURL URLWithString:self.avatarImageURLString];
  46. }
  47. #pragma mark -
  48. #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
  49. + (NSOperationQueue *)sharedProfileImageRequestOperationQueue {
  50. static NSOperationQueue *_sharedProfileImageRequestOperationQueue = nil;
  51. static dispatch_once_t onceToken;
  52. dispatch_once(&onceToken, ^{
  53. _sharedProfileImageRequestOperationQueue = [[NSOperationQueue alloc] init];
  54. [_sharedProfileImageRequestOperationQueue setMaxConcurrentOperationCount:8];
  55. });
  56. return _sharedProfileImageRequestOperationQueue;
  57. }
  58. - (NSImage *)profileImage {
  59. if (!_profileImage && !_avatarImageRequestOperation) {
  60. NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:self.avatarImageURL];
  61. [mutableRequest setValue:@"image/*" forHTTPHeaderField:@"Accept"];
  62. AFHTTPRequestOperation *imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:mutableRequest];
  63. imageRequestOperation.responseSerializer = [AFImageResponseSerializer serializer];
  64. [imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, NSImage *responseImage) {
  65. self.profileImage = responseImage;
  66. _avatarImageRequestOperation = nil;
  67. [[NSNotificationCenter defaultCenter] postNotificationName:kUserProfileImageDidLoadNotification object:self userInfo:nil];
  68. } failure:nil];
  69. [imageRequestOperation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
  70. return [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response data:cachedResponse.data userInfo:cachedResponse.userInfo storagePolicy:NSURLCacheStorageAllowed];
  71. }];
  72. _avatarImageRequestOperation = imageRequestOperation;
  73. [[[self class] sharedProfileImageRequestOperationQueue] addOperation:_avatarImageRequestOperation];
  74. }
  75. return _profileImage;
  76. }
  77. #endif
  78. @end