X Tutup
The Wayback Machine - https://web.archive.org/web/20200915122711/https://github.com/SDWebImage/SDWebImage/issues/2701
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Allows the UIButton to check and set different state for current `progress` and `transition` #2701

Open
dreampiggy opened this issue Apr 24, 2019 · 11 comments

Comments

@dreampiggy
Copy link
Contributor

@dreampiggy dreampiggy commented Apr 24, 2019

New Issue Checklist

Feature Description

SDWebImage's UIView+WebCache, provide some properties, like

  • sd_imageURL
  • sd_imageProgress
  • sd_imageTransition

It works great for normal UIImageView, which have one single image to be query and load.

However, things become more complicated when using these properties on UIButton, because UIButton have different state, each state can represent a different image query pipelilne. See the already exist API:

- (void)sd_setImageWithURL:(nullable NSURL *)url
                  forState:(UIControlState)state NS_REFINED_FOR_SWIFT;

So, actually, these properties, which tied to single image request, should also have a correspond API for different UIButton state. Like the following:

  • sd_imageURLForState: Already Done
  • sd_imageProgressForState:
  • sd_imageTransitionForState:

Similar use case

The same thing, can also apply for the UIImageView+HighlightedWebCache, which should have the coorespond APIs like the following:

  • sd_highlightedImageURL
  • sd_highlightedImageProgress
  • sd_highlightedImageTransition

Note: The Indicator feature, is not under consideration for this case, because from all the usage, the indicator on UIButton is not a good idea. And it's a UIView's subview, it's not designed to dynamically removed and re-add base on state.

@stale
Copy link

@stale stale bot commented May 28, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@stale stale bot added the stale label May 28, 2019
@stale stale bot closed this Jun 4, 2019
Issue List automation moved this from To do to Done Jun 4, 2019
@dreampiggy dreampiggy reopened this Jun 4, 2019
Issue List automation moved this from Done to In progress Jun 4, 2019
@stale stale bot removed the stale label Jun 4, 2019
@dreampiggy dreampiggy mentioned this issue Jun 30, 2019
8 of 8 tasks complete
@dreampiggy
Copy link
Contributor Author

@dreampiggy dreampiggy commented Jun 30, 2019

@cntrump Hi. I see you have some use case for this State-like View API. Which means, a UIView have multiple image and their responding state, like URL, progress, indicator, transaction at the same time.

Could you explain your detail use case, to help me to solve this issue, with a more general, extensible solution ?

@dreampiggy
Copy link
Contributor Author

@dreampiggy dreampiggy commented Jun 30, 2019

I wonder, if the user who want to call UIView+WebCache methods, are all advanced users, we can treat this UIView+WebCache as a building block API for developer who want to make their own class with SDWebImage category.

For example, if this is true, I'll provide some powerful API (but hard to use for simple one line code) for this. This is my first design draft.

@dreampiggy
Copy link
Contributor Author

@dreampiggy dreampiggy commented Jun 30, 2019

/**
 A loading storage keeping all of the related property, associated to single image request
 */
@interface SDWebImageLoadStorage

// Description is same as current property
@property (nonatomic, strong, readonly, nullable) NSURL *imageURL;
@property (nonatomic, strong, null_resettable) NSProgress *imageProgress;
@property (nonatomic, strong, nullable) SDWebImageTransition *imageTransition;
@property (nonatomic, strong, nullable) id<SDWebImageIndicator> imageIndicator;

@end


@interface UIView (WebCache)

/**
 Return the loading storage for specify operation key.
 */
- (nullable SDWebImageLoadStorage *)sd_imageLoadStorageForKey:(nullable NSString *)key;
/**
 Specify a desired loading storage for opeartion key.
 */
- (void)sd_setImageLoadStorage:(nullable SDWebImageLoadStorage *)storage forKey:(nullable NSString *)key;

@end
@dreampiggy
Copy link
Contributor Author

@dreampiggy dreampiggy commented Jun 30, 2019

Usage code for cases when I want to observe different state's loading progress on UIButton:

- (void)testUIButton {
    NSURL *url;
    NSURL *disabledUrl;
    
    // Normal
    SDWebImageLoadStorage *storage = [SDWebImageLoadStorage new];
    UIButton *button = [UIButton new];
    UIControlState state = UIControlStateNormal;
    NSString *key = [UIButton sd_imageOperationKeyForState:state];
    [button sd_setImageLoadStorage:storage forKey:key];
    
    [self.KVOController observe:storage.imageProgress keyPath:NSStringFromSelector(@selector(fractionCompleted)) options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
        // ....
    }];
    [button sd_setImageWithURL:url forState:state];
    
    // Disabled
    state = UIControlStateDisabled;
    key = [UIButton sd_imageOperationKeyForState:state];
    SDWebImageLoadStorage *disabledStorage = [SDWebImageLoadStorage new];
    [button sd_setImageLoadStorage:disabledStorage forKey:key];
    
    [self.KVOController observe:disabledStorage.imageProgress keyPath:NSStringFromSelector(@selector(fractionCompleted)) options:NSKeyValueObservingOptionNew block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSString *,id> * _Nonnull change) {
        // ....
    }];
    [button sd_setImageWithURL:disabledUrl forState:state];
}
@dreampiggy
Copy link
Contributor Author

@dreampiggy dreampiggy commented Jun 30, 2019

Usage code for cases when I want to set different transaction animation for normal image and highlighted image on UIImageView:

- (void)testUIImageView {
    NSURL *url;
    NSURL *highlightedUrl;
    
    // image
    UIImageView *imageView = [UIImageView new];
    SDWebImageLoadStorage *storage = [SDWebImageLoadStorage new];
    storage.imageTransition = SDWebImageTransition.fadeTransition;
    NSString *key = [UIImageView sd_imageOperationKey]; // actually is nil, or `NSStringFromClass`
    [imageView sd_setImageLoadStorage:storage forKey:key];
    [imageView sd_setImageWithURL:url];
    
    // highlighted image
    key = [UIImageView sd_highlightedImageOperationKey];
    SDWebImageLoadStorage *backgroundStorage = [SDWebImageLoadStorage new];
    backgroundStorage.imageTransition = SDWebImageTransition.flipFromTopTransition;
    [imageView sd_setHighlightedImageWithURL:highlightedUrl];
}
@dreampiggy
Copy link
Contributor Author

@dreampiggy dreampiggy commented Jun 30, 2019

Those UIView+WebCache is for advanced user, for convenience API on UIButton+WebCache.h, we can create one like this:

@interface UIButton (WebCache)
/**
 Get the image progress for a control state.
 */
- (nullable NSProgress *)sd_imageProgressForState:(UIControlState)state;
/**
 Get the background image progress for a control state.
 */
- (nullable NSProgress *)sd_backgroundImageProgressForState:(UIControlState)state;
@end

@implementation UIButton (WebCache)

- (NSProgress *)sd_imageProgressForState:(UIControlState)state {
    NSString *key = [UIButton sd_imageOperationKeyForState:state];
    SDWebImageLoadStorage *storage = [self sd_imageLoadStorageForKey:key];
    return storage.imageProgress;
}

- (NSProgress *)sd_backgroundImageProgressForState:(UIControlState)state {
    NSString *key = [UIButton sd_backgroundImageOperationKeyForState:state];
    SDWebImageLoadStorage *storage = [self sd_imageLoadStorageForKey:key];
    return storage.imageProgress;
}

@end
@stale
Copy link

@stale stale bot commented Jul 30, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@stale stale bot added the stale label Jul 30, 2019
@dreampiggy dreampiggy removed the stale label Jul 30, 2019
@stale
Copy link

@stale stale bot commented Sep 28, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@stale stale bot added the stale label Sep 28, 2019
@dreampiggy
Copy link
Contributor Author

@dreampiggy dreampiggy commented Sep 28, 2019

Fix in future

@stale stale bot removed the stale label Sep 28, 2019
@stale
Copy link

@stale stale bot commented Nov 27, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, please make sure it is up to date and if so, add a comment that this is still an issue to keep it open. Thank you for your contributions.

@stale stale bot added the stale label Nov 27, 2019
@dreampiggy dreampiggy added important and removed stale labels Nov 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Issue List
  
In progress
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.
X Tutup