User.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // User.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 "User.h"
  23. #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
  24. //#import "AFHTTPRequestOperation.h"
  25. #endif
  26. NSString * const kUserProfileImageDidLoadNotification = @"com.alamofire.user.profile-image.loaded";
  27. @interface User ()
  28. @property (readwrite, nonatomic, assign) NSUInteger userID;
  29. @property (readwrite, nonatomic, copy) NSString *username;
  30. @property (readwrite, nonatomic, copy) NSString *avatarImageURLString;
  31. //#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
  32. //@property (readwrite, nonatomic, strong) AFHTTPRequestOperation *avatarImageRequestOperation;
  33. //#endif
  34. @end
  35. @implementation User
  36. - (instancetype)initWithAttributes:(NSDictionary *)attributes {
  37. self = [super init];
  38. if (!self) {
  39. return nil;
  40. }
  41. self.userID = (NSUInteger)[[attributes valueForKeyPath:@"id"] integerValue];
  42. self.username = [attributes valueForKeyPath:@"username"];
  43. self.avatarImageURLString = [attributes valueForKeyPath:@"avatar_image.url"];
  44. return self;
  45. }
  46. - (NSURL *)avatarImageURL {
  47. return [NSURL URLWithString:self.avatarImageURLString];
  48. }
  49. #pragma mark -
  50. #ifdef __MAC_OS_X_VERSION_MIN_REQUIRED
  51. //+ (NSOperationQueue *)sharedProfileImageRequestOperationQueue {
  52. // static NSOperationQueue *_sharedProfileImageRequestOperationQueue = nil;
  53. // static dispatch_once_t onceToken;
  54. // dispatch_once(&onceToken, ^{
  55. // _sharedProfileImageRequestOperationQueue = [[NSOperationQueue alloc] init];
  56. // [_sharedProfileImageRequestOperationQueue setMaxConcurrentOperationCount:8];
  57. // });
  58. //
  59. // return _sharedProfileImageRequestOperationQueue;
  60. //}
  61. - (NSImage *)profileImage {
  62. return nil;
  63. // if (!_profileImage && !_avatarImageRequestOperation) {
  64. // NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:self.avatarImageURL];
  65. // [mutableRequest setValue:@"image/*" forHTTPHeaderField:@"Accept"];
  66. // AFHTTPRequestOperation *imageRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:mutableRequest];
  67. // imageRequestOperation.responseSerializer = [AFImageResponseSerializer serializer];
  68. // [imageRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, NSImage *responseImage) {
  69. // self.profileImage = responseImage;
  70. //
  71. // _avatarImageRequestOperation = nil;
  72. //
  73. // [[NSNotificationCenter defaultCenter] postNotificationName:kUserProfileImageDidLoadNotification object:self userInfo:nil];
  74. // } failure:nil];
  75. //
  76. // [imageRequestOperation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
  77. // return [[NSCachedURLResponse alloc] initWithResponse:cachedResponse.response data:cachedResponse.data userInfo:cachedResponse.userInfo storagePolicy:NSURLCacheStorageAllowed];
  78. // }];
  79. //
  80. // _avatarImageRequestOperation = imageRequestOperation;
  81. //
  82. // [[[self class] sharedProfileImageRequestOperationQueue] addOperation:_avatarImageRequestOperation];
  83. // }
  84. //
  85. // return _profileImage;
  86. }
  87. #endif
  88. @end
  89. @implementation User (NSCoding)
  90. - (void)encodeWithCoder:(NSCoder *)aCoder {
  91. [aCoder encodeInteger:(NSInteger)self.userID forKey:@"AF.userID"];
  92. [aCoder encodeObject:self.username forKey:@"AF.username"];
  93. [aCoder encodeObject:self.avatarImageURLString forKey:@"AF.avatarImageURLString"];
  94. }
  95. - (instancetype)initWithCoder:(NSCoder *)aDecoder {
  96. self = [super init];
  97. if (!self) {
  98. return nil;
  99. }
  100. self.userID = (NSUInteger)[aDecoder decodeIntegerForKey:@"AF.userID"];
  101. self.username = [aDecoder decodeObjectOfClass:[NSString class] forKey:@"AF.username"];
  102. self.avatarImageURLString = [aDecoder decodeObjectOfClass:[User class] forKey:@"AF.avatarImageURLString"];
  103. return self;
  104. }
  105. + (BOOL)supportsSecureCoding {
  106. return YES;
  107. }
  108. @end