Authored by DreamPiggy

Add the support for watchOS to use View Category method (sd_setImageWithURL:) on WKInterfaceImage

@@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
7 */ 7 */
8 8
9 #import "InterfaceController.h" 9 #import "InterfaceController.h"
10 -#import <SDWebImage/SDWebImageManager.h> 10 +#import <SDWebImage/UIImageView+WebCache.h>
11 11
12 12
13 @interface InterfaceController() 13 @interface InterfaceController()
@@ -30,9 +30,8 @@ @@ -30,9 +30,8 @@
30 [super willActivate]; 30 [super willActivate];
31 31
32 NSString *urlString = @"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png"; 32 NSString *urlString = @"https://nr-platform.s3.amazonaws.com/uploads/platform/published_extension/branding_icon/275/AmazonS3.png";
33 - [[SDWebImageManager sharedManager] loadImageWithURL:[NSURL URLWithString:urlString] options:0 progress:nil completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL) {  
34 - self.imageInterface.image = image;  
35 - }]; 33 + WKInterfaceImage *imageInterface = self.imageInterface;
  34 + [imageInterface sd_setImageWithURL:[NSURL URLWithString:urlString]];
36 } 35 }
37 36
38 - (void)didDeactivate { 37 - (void)didDeactivate {
@@ -70,6 +70,12 @@ @@ -70,6 +70,12 @@
70 #endif 70 #endif
71 #if SD_WATCH 71 #if SD_WATCH
72 #import <WatchKit/WatchKit.h> 72 #import <WatchKit/WatchKit.h>
  73 + #ifndef UIView
  74 + #define UIView WKInterfaceObject
  75 + #endif
  76 + #ifndef UIImageView
  77 + #define UIImageView WKInterfaceImage
  78 + #endif
73 #endif 79 #endif
74 #endif 80 #endif
75 81
@@ -7,9 +7,6 @@ @@ -7,9 +7,6 @@
7 */ 7 */
8 8
9 #import "SDWebImageCompat.h" 9 #import "SDWebImageCompat.h"
10 -  
11 -#if SD_UIKIT || SD_MAC  
12 -  
13 #import "SDWebImageManager.h" 10 #import "SDWebImageManager.h"
14 11
15 /** 12 /**
@@ -195,5 +192,3 @@ @@ -195,5 +192,3 @@
195 #endif 192 #endif
196 193
197 @end 194 @end
198 -  
199 -#endif  
@@ -7,9 +7,6 @@ @@ -7,9 +7,6 @@
7 */ 7 */
8 8
9 #import "UIImageView+WebCache.h" 9 #import "UIImageView+WebCache.h"
10 -  
11 -#if SD_UIKIT || SD_MAC  
12 -  
13 #import "objc/runtime.h" 10 #import "objc/runtime.h"
14 #import "UIView+WebCacheOperation.h" 11 #import "UIView+WebCacheOperation.h"
15 #import "UIView+WebCache.h" 12 #import "UIView+WebCache.h"
@@ -140,5 +137,3 @@ static char animationLoadOperationKey; @@ -140,5 +137,3 @@ static char animationLoadOperationKey;
140 #endif 137 #endif
141 138
142 @end 139 @end
143 -  
144 -#endif  
@@ -7,9 +7,6 @@ @@ -7,9 +7,6 @@
7 */ 7 */
8 8
9 #import "SDWebImageCompat.h" 9 #import "SDWebImageCompat.h"
10 -  
11 -#if SD_UIKIT || SD_MAC  
12 -  
13 #import "SDWebImageManager.h" 10 #import "SDWebImageManager.h"
14 #import "SDWebImageTransition.h" 11 #import "SDWebImageTransition.h"
15 12
@@ -105,6 +102,8 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima @@ -105,6 +102,8 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima
105 */ 102 */
106 - (void)sd_cancelCurrentImageLoad; 103 - (void)sd_cancelCurrentImageLoad;
107 104
  105 +#if SD_UIKIT || SD_MAC
  106 +
108 #pragma mark - Image Transition 107 #pragma mark - Image Transition
109 108
110 /** 109 /**
@@ -135,6 +134,6 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima @@ -135,6 +134,6 @@ typedef void(^SDSetImageBlock)(UIImage * _Nullable image, NSData * _Nullable ima
135 134
136 #endif 135 #endif
137 136
138 -@end  
139 -  
140 #endif 137 #endif
  138 +
  139 +@end
@@ -7,9 +7,6 @@ @@ -7,9 +7,6 @@
7 */ 7 */
8 8
9 #import "UIView+WebCache.h" 9 #import "UIView+WebCache.h"
10 -  
11 -#if SD_UIKIT || SD_MAC  
12 -  
13 #import "objc/runtime.h" 10 #import "objc/runtime.h"
14 #import "UIView+WebCacheOperation.h" 11 #import "UIView+WebCacheOperation.h"
15 12
@@ -23,8 +20,8 @@ static char imageURLKey; @@ -23,8 +20,8 @@ static char imageURLKey;
23 #if SD_UIKIT 20 #if SD_UIKIT
24 static char TAG_ACTIVITY_INDICATOR; 21 static char TAG_ACTIVITY_INDICATOR;
25 static char TAG_ACTIVITY_STYLE; 22 static char TAG_ACTIVITY_STYLE;
26 -#endif  
27 static char TAG_ACTIVITY_SHOW; 23 static char TAG_ACTIVITY_SHOW;
  24 +#endif
28 25
29 @implementation UIView (WebCache) 26 @implementation UIView (WebCache)
30 27
@@ -78,10 +75,12 @@ static char TAG_ACTIVITY_SHOW; @@ -78,10 +75,12 @@ static char TAG_ACTIVITY_SHOW;
78 } 75 }
79 76
80 if (url) { 77 if (url) {
  78 +#if SD_UIKIT
81 // check if activityView is enabled or not 79 // check if activityView is enabled or not
82 if ([self sd_showActivityIndicatorView]) { 80 if ([self sd_showActivityIndicatorView]) {
83 [self sd_addActivityIndicator]; 81 [self sd_addActivityIndicator];
84 } 82 }
  83 +#endif
85 84
86 // reset the progress 85 // reset the progress
87 self.sd_imageProgress.totalUnitCount = 0; 86 self.sd_imageProgress.totalUnitCount = 0;
@@ -105,7 +104,9 @@ static char TAG_ACTIVITY_SHOW; @@ -105,7 +104,9 @@ static char TAG_ACTIVITY_SHOW;
105 id <SDWebImageOperation> operation = [manager loadImageWithURL:url options:options progress:combinedProgressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { 104 id <SDWebImageOperation> operation = [manager loadImageWithURL:url options:options progress:combinedProgressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
106 __strong __typeof (wself) sself = wself; 105 __strong __typeof (wself) sself = wself;
107 if (!sself) { return; } 106 if (!sself) { return; }
  107 +#if SD_UIKIT
108 [sself sd_removeActivityIndicator]; 108 [sself sd_removeActivityIndicator];
  109 +#endif
109 // if the progress not been updated, mark it to complete state 110 // if the progress not been updated, mark it to complete state
110 if (finished && !error && sself.sd_imageProgress.totalUnitCount == 0 && sself.sd_imageProgress.completedUnitCount == 0) { 111 if (finished && !error && sself.sd_imageProgress.totalUnitCount == 0 && sself.sd_imageProgress.completedUnitCount == 0) {
111 sself.sd_imageProgress.totalUnitCount = SDWebImageProgressUnitCountUnknown; 112 sself.sd_imageProgress.totalUnitCount = SDWebImageProgressUnitCountUnknown;
@@ -144,16 +145,22 @@ static char TAG_ACTIVITY_SHOW; @@ -144,16 +145,22 @@ static char TAG_ACTIVITY_SHOW;
144 targetData = nil; 145 targetData = nil;
145 } 146 }
146 147
  148 +#if SD_UIKIT || SD_MAC
147 // check whether we should use the image transition 149 // check whether we should use the image transition
148 SDWebImageTransition *transition = nil; 150 SDWebImageTransition *transition = nil;
149 if (finished && (options & SDWebImageForceTransition || cacheType == SDImageCacheTypeNone)) { 151 if (finished && (options & SDWebImageForceTransition || cacheType == SDImageCacheTypeNone)) {
150 transition = sself.sd_imageTransition; 152 transition = sself.sd_imageTransition;
151 } 153 }
  154 +#endif
152 if ([context valueForKey:SDWebImageInternalSetImageGroupKey]) { 155 if ([context valueForKey:SDWebImageInternalSetImageGroupKey]) {
153 dispatch_group_t group = [context valueForKey:SDWebImageInternalSetImageGroupKey]; 156 dispatch_group_t group = [context valueForKey:SDWebImageInternalSetImageGroupKey];
154 dispatch_group_enter(group); 157 dispatch_group_enter(group);
155 dispatch_main_async_safe(^{ 158 dispatch_main_async_safe(^{
  159 +#if SD_UIKIT || SD_MAC
156 [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL]; 160 [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL];
  161 +#else
  162 + [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock];
  163 +#endif
157 }); 164 });
158 // ensure completion block is called after custom setImage process finish 165 // ensure completion block is called after custom setImage process finish
159 dispatch_group_notify(group, dispatch_get_main_queue(), ^{ 166 dispatch_group_notify(group, dispatch_get_main_queue(), ^{
@@ -161,7 +168,11 @@ static char TAG_ACTIVITY_SHOW; @@ -161,7 +168,11 @@ static char TAG_ACTIVITY_SHOW;
161 }); 168 });
162 } else { 169 } else {
163 dispatch_main_async_safe(^{ 170 dispatch_main_async_safe(^{
  171 +#if SD_UIKIT || SD_MAC
164 [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL]; 172 [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:transition cacheType:cacheType imageURL:imageURL];
  173 +#else
  174 + [sself sd_setImage:targetImage imageData:targetData basedOnClassOrViaCustomSetImageBlock:setImageBlock];
  175 +#endif
165 callCompletedBlockClojure(); 176 callCompletedBlockClojure();
166 }); 177 });
167 } 178 }
@@ -169,7 +180,9 @@ static char TAG_ACTIVITY_SHOW; @@ -169,7 +180,9 @@ static char TAG_ACTIVITY_SHOW;
169 [self sd_setImageLoadOperation:operation forKey:validOperationKey]; 180 [self sd_setImageLoadOperation:operation forKey:validOperationKey];
170 } else { 181 } else {
171 dispatch_main_async_safe(^{ 182 dispatch_main_async_safe(^{
  183 +#if SD_UIKIT
172 [self sd_removeActivityIndicator]; 184 [self sd_removeActivityIndicator];
  185 +#endif
173 if (completedBlock) { 186 if (completedBlock) {
174 NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}]; 187 NSError *error = [NSError errorWithDomain:SDWebImageErrorDomain code:-1 userInfo:@{NSLocalizedDescriptionKey : @"Trying to load a nil url"}];
175 completedBlock(nil, error, SDImageCacheTypeNone, url); 188 completedBlock(nil, error, SDImageCacheTypeNone, url);
@@ -183,23 +196,31 @@ static char TAG_ACTIVITY_SHOW; @@ -183,23 +196,31 @@ static char TAG_ACTIVITY_SHOW;
183 } 196 }
184 197
185 - (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock { 198 - (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock {
  199 +#if SD_UIKIT || SD_MAC
186 [self sd_setImage:image imageData:imageData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:nil cacheType:0 imageURL:nil]; 200 [self sd_setImage:image imageData:imageData basedOnClassOrViaCustomSetImageBlock:setImageBlock transition:nil cacheType:0 imageURL:nil];
  201 +#else
  202 + // watchOS does not support view transition. Simplify the logic
  203 + if (setImageBlock) {
  204 + setImageBlock(image, imageData);
  205 + } else if ([self isKindOfClass:[UIImageView class]]) {
  206 + UIImageView *imageView = (UIImageView *)self;
  207 + [imageView setImage:image];
  208 + }
  209 +#endif
187 } 210 }
188 211
  212 +#if SD_UIKIT || SD_MAC
189 - (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock transition:(SDWebImageTransition *)transition cacheType:(SDImageCacheType)cacheType imageURL:(NSURL *)imageURL { 213 - (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock transition:(SDWebImageTransition *)transition cacheType:(SDImageCacheType)cacheType imageURL:(NSURL *)imageURL {
190 UIView *view = self; 214 UIView *view = self;
191 SDSetImageBlock finalSetImageBlock; 215 SDSetImageBlock finalSetImageBlock;
192 if (setImageBlock) { 216 if (setImageBlock) {
193 finalSetImageBlock = setImageBlock; 217 finalSetImageBlock = setImageBlock;
194 - }  
195 -#if SD_UIKIT || SD_MAC  
196 - else if ([view isKindOfClass:[UIImageView class]]) { 218 + } else if ([view isKindOfClass:[UIImageView class]]) {
197 UIImageView *imageView = (UIImageView *)view; 219 UIImageView *imageView = (UIImageView *)view;
198 finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData) { 220 finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData) {
199 imageView.image = setImage; 221 imageView.image = setImage;
200 }; 222 };
201 } 223 }
202 -#endif  
203 #if SD_UIKIT 224 #if SD_UIKIT
204 else if ([view isKindOfClass:[UIButton class]]) { 225 else if ([view isKindOfClass:[UIButton class]]) {
205 UIButton *button = (UIButton *)view; 226 UIButton *button = (UIButton *)view;
@@ -257,15 +278,20 @@ static char TAG_ACTIVITY_SHOW; @@ -257,15 +278,20 @@ static char TAG_ACTIVITY_SHOW;
257 } 278 }
258 } 279 }
259 } 280 }
  281 +#endif
260 282
261 - (void)sd_setNeedsLayout { 283 - (void)sd_setNeedsLayout {
262 #if SD_UIKIT 284 #if SD_UIKIT
263 [self setNeedsLayout]; 285 [self setNeedsLayout];
264 #elif SD_MAC 286 #elif SD_MAC
265 [self setNeedsLayout:YES]; 287 [self setNeedsLayout:YES];
  288 +#elif SD_WATCH
  289 + // Do nothing because WatchKit automatically layout the view after property change
266 #endif 290 #endif
267 } 291 }
268 292
  293 +#if SD_UIKIT || SD_MAC
  294 +
269 #pragma mark - Image Transition 295 #pragma mark - Image Transition
270 - (SDWebImageTransition *)sd_imageTransition { 296 - (SDWebImageTransition *)sd_imageTransition {
271 return objc_getAssociatedObject(self, @selector(sd_imageTransition)); 297 return objc_getAssociatedObject(self, @selector(sd_imageTransition));
@@ -275,10 +301,9 @@ static char TAG_ACTIVITY_SHOW; @@ -275,10 +301,9 @@ static char TAG_ACTIVITY_SHOW;
275 objc_setAssociatedObject(self, @selector(sd_imageTransition), sd_imageTransition, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 301 objc_setAssociatedObject(self, @selector(sd_imageTransition), sd_imageTransition, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
276 } 302 }
277 303
278 -#pragma mark - Activity indicator  
279 -  
280 -#pragma mark -  
281 #if SD_UIKIT 304 #if SD_UIKIT
  305 +
  306 +#pragma mark - Activity indicator
282 - (UIActivityIndicatorView *)activityIndicator { 307 - (UIActivityIndicatorView *)activityIndicator {
283 return (UIActivityIndicatorView *)objc_getAssociatedObject(self, &TAG_ACTIVITY_INDICATOR); 308 return (UIActivityIndicatorView *)objc_getAssociatedObject(self, &TAG_ACTIVITY_INDICATOR);
284 } 309 }
@@ -286,7 +311,6 @@ static char TAG_ACTIVITY_SHOW; @@ -286,7 +311,6 @@ static char TAG_ACTIVITY_SHOW;
286 - (void)setActivityIndicator:(UIActivityIndicatorView *)activityIndicator { 311 - (void)setActivityIndicator:(UIActivityIndicatorView *)activityIndicator {
287 objc_setAssociatedObject(self, &TAG_ACTIVITY_INDICATOR, activityIndicator, OBJC_ASSOCIATION_RETAIN); 312 objc_setAssociatedObject(self, &TAG_ACTIVITY_INDICATOR, activityIndicator, OBJC_ASSOCIATION_RETAIN);
288 } 313 }
289 -#endif  
290 314
291 - (void)sd_setShowActivityIndicatorView:(BOOL)show { 315 - (void)sd_setShowActivityIndicatorView:(BOOL)show {
292 objc_setAssociatedObject(self, &TAG_ACTIVITY_SHOW, @(show), OBJC_ASSOCIATION_RETAIN); 316 objc_setAssociatedObject(self, &TAG_ACTIVITY_SHOW, @(show), OBJC_ASSOCIATION_RETAIN);
@@ -296,7 +320,6 @@ static char TAG_ACTIVITY_SHOW; @@ -296,7 +320,6 @@ static char TAG_ACTIVITY_SHOW;
296 return [objc_getAssociatedObject(self, &TAG_ACTIVITY_SHOW) boolValue]; 320 return [objc_getAssociatedObject(self, &TAG_ACTIVITY_SHOW) boolValue];
297 } 321 }
298 322
299 -#if SD_UIKIT  
300 - (void)sd_setIndicatorStyle:(UIActivityIndicatorViewStyle)style{ 323 - (void)sd_setIndicatorStyle:(UIActivityIndicatorViewStyle)style{
301 objc_setAssociatedObject(self, &TAG_ACTIVITY_STYLE, [NSNumber numberWithInt:style], OBJC_ASSOCIATION_RETAIN); 324 objc_setAssociatedObject(self, &TAG_ACTIVITY_STYLE, [NSNumber numberWithInt:style], OBJC_ASSOCIATION_RETAIN);
302 } 325 }
@@ -304,10 +327,8 @@ static char TAG_ACTIVITY_SHOW; @@ -304,10 +327,8 @@ static char TAG_ACTIVITY_SHOW;
304 - (int)sd_getIndicatorStyle{ 327 - (int)sd_getIndicatorStyle{
305 return [objc_getAssociatedObject(self, &TAG_ACTIVITY_STYLE) intValue]; 328 return [objc_getAssociatedObject(self, &TAG_ACTIVITY_STYLE) intValue];
306 } 329 }
307 -#endif  
308 330
309 - (void)sd_addActivityIndicator { 331 - (void)sd_addActivityIndicator {
310 -#if SD_UIKIT  
311 dispatch_main_async_safe(^{ 332 dispatch_main_async_safe(^{
312 if (!self.activityIndicator) { 333 if (!self.activityIndicator) {
313 self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:[self sd_getIndicatorStyle]]; 334 self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:[self sd_getIndicatorStyle]];
@@ -332,20 +353,19 @@ static char TAG_ACTIVITY_SHOW; @@ -332,20 +353,19 @@ static char TAG_ACTIVITY_SHOW;
332 } 353 }
333 [self.activityIndicator startAnimating]; 354 [self.activityIndicator startAnimating];
334 }); 355 });
335 -#endif  
336 } 356 }
337 357
338 - (void)sd_removeActivityIndicator { 358 - (void)sd_removeActivityIndicator {
339 -#if SD_UIKIT  
340 dispatch_main_async_safe(^{ 359 dispatch_main_async_safe(^{
341 if (self.activityIndicator) { 360 if (self.activityIndicator) {
342 [self.activityIndicator removeFromSuperview]; 361 [self.activityIndicator removeFromSuperview];
343 self.activityIndicator = nil; 362 self.activityIndicator = nil;
344 } 363 }
345 }); 364 });
346 -#endif  
347 } 365 }
348 366
349 -@end 367 +#endif
350 368
351 #endif 369 #endif
  370 +
  371 +@end
@@ -7,10 +7,7 @@ @@ -7,10 +7,7 @@
7 */ 7 */
8 8
9 #import "SDWebImageCompat.h" 9 #import "SDWebImageCompat.h"
10 -  
11 -#if SD_UIKIT || SD_MAC  
12 -  
13 -#import "SDWebImageManager.h" 10 +#import "SDWebImageOperation.h"
14 11
15 // These methods are used to support canceling for UIView image loading, it's designed to be used internal but not external. 12 // These methods are used to support canceling for UIView image loading, it's designed to be used internal but not external.
16 // All the stored operations are weak, so it will be dalloced after image loading finished. If you need to store operations, use your own class to keep a strong reference for them. 13 // All the stored operations are weak, so it will be dalloced after image loading finished. If you need to store operations, use your own class to keep a strong reference for them.
@@ -39,5 +36,3 @@ @@ -39,5 +36,3 @@
39 - (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key; 36 - (void)sd_removeImageLoadOperationWithKey:(nullable NSString *)key;
40 37
41 @end 38 @end
42 -  
43 -#endif  
@@ -7,9 +7,6 @@ @@ -7,9 +7,6 @@
7 */ 7 */
8 8
9 #import "UIView+WebCacheOperation.h" 9 #import "UIView+WebCacheOperation.h"
10 -  
11 -#if SD_UIKIT || SD_MAC  
12 -  
13 #import "objc/runtime.h" 10 #import "objc/runtime.h"
14 11
15 static char loadOperationKey; 12 static char loadOperationKey;
@@ -71,5 +68,3 @@ typedef NSMapTable<NSString *, id<SDWebImageOperation>> SDOperationsDictionary; @@ -71,5 +68,3 @@ typedef NSMapTable<NSString *, id<SDWebImageOperation>> SDOperationsDictionary;
71 } 68 }
72 69
73 @end 70 @end
74 -  
75 -#endif