123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- //
- // PKDownloadButton.m
- // PKDownloadButton
- //
- // Created by Pavel on 28/05/15.
- // Copyright (c) 2015 Katunin. All rights reserved.
- //
- #import "PKDownloadButton.h"
- #import "PKMacros.h"
- #import "NSLayoutConstraint+PKDownloadButton.h"
- #import "UIImage+PKDownloadButton.h"
- #import "PKPendingView.h"
- @interface PKDownloadButton ()
- @property (nonatomic, weak) PKBorderedButton *startDownloadButton;
- @property (nonatomic, weak) PKStopDownloadButton *stopDownloadButton;
- @property (nonatomic, weak) PKBorderedButton *downloadedButton;
- @property (nonatomic, weak) PKPendingView *pendingView;
- @property (nonatomic, strong) NSMutableArray *stateViews;
- - (PKBorderedButton *)createStartDownloadButton;
- - (PKStopDownloadButton *)createStopDownloadButton;
- - (PKBorderedButton *)createDownloadedButton;
- - (PKPendingView *)createPendingView;
- - (void)currentButtonTapped:(id)sender;
- - (void)createSubviews;
- - (NSArray *)createConstraints;
- @end
- static PKDownloadButton *CommonInit(PKDownloadButton *self) {
- if (self != nil) {
- [self createSubviews];
- [self addConstraints:[self createConstraints]];
-
- self.state = kPKDownloadButtonState_StartDownload;
- self.backgroundColor = [UIColor clearColor];
- }
- return self;
- }
- @implementation PKDownloadButton
- #pragma mark - Properties
- - (void)setState:(PKDownloadButtonState)state {
- _state = state;
-
- [self.stateViews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- SafeObjClassCast(UIView, view, obj);
- view.hidden = YES;
- }];
-
- switch (state) {
- case kPKDownloadButtonState_StartDownload:
- self.startDownloadButton.hidden = NO;
- break;
- case kPKDownloadButtonState_Pending:
- self.pendingView.hidden = NO;
- [self.pendingView startSpin];
- break;
- case kPKDownloadButtonState_Downloading:
- self.stopDownloadButton.hidden = NO;
- self.stopDownloadButton.progress = 0.f;
- break;
- case kPKDownloadButtonState_Downloaded:
- self.downloadedButton.hidden = NO;
- break;
- default:
- NSAssert(NO, @"unsupported state");
- break;
- }
- }
- #pragma mark - Initialization
- - (id)initWithCoder:(NSCoder *)decoder {
- return CommonInit([super initWithCoder:decoder]);
- }
- - (instancetype)initWithFrame:(CGRect)frame {
- return CommonInit([super initWithFrame:frame]);
- }
- - (void)tintColorDidChange {
- [super tintColorDidChange];
-
- [self updateButton:self.startDownloadButton title:[self.startDownloadButton titleForState:UIControlStateNormal] font:self.startDownloadButton.titleLabel.font];
- [self updateButton:self.downloadedButton title:[self.downloadedButton titleForState:UIControlStateNormal] font:self.downloadedButton.titleLabel.font];
- }
- #pragma mark - appearance
- -(void)updateStartDownloadButtonText:(NSString *)title {
- [self updateButton:self.startDownloadButton title:title];
- }
- -(void)updateDownloadedButtonText:(NSString *)title {
- [self updateButton:self.downloadedButton title:title];
- }
- -(void)updateStartDownloadButtonText:(NSString *)title font:(UIFont *)font {
- [self updateButton:self.startDownloadButton title:title font: font];
- }
- -(void)updateDownloadedButtonText:(NSString *)title font:(UIFont *)font {
- [self updateButton:self.downloadedButton title:title font: font];
- }
- - (void)updateButton:(UIButton *)button title:(NSString *)title {
- [self updateButton:button title:title font:[UIFont systemFontOfSize:14.f]];
- }
- - (void)updateButton:(UIButton *)button title:(NSString *)title font:(UIFont *)font {
- if (title == nil) {
- title = @"";
- }
-
- [button setTitle:title forState:UIControlStateNormal];
- [button setTitleColor:self.tintColor forState:UIControlStateNormal];
- [button setTitleColor:UIColor.whiteColor forState:UIControlStateHighlighted];
-
- button.titleLabel.font = font;
- }
- #pragma mark - private methods
- - (PKBorderedButton *)createStartDownloadButton {
- PKBorderedButton *startDownloadButton = [PKBorderedButton buttonWithType:UIButtonTypeCustom];
- [startDownloadButton configureDefaultAppearance];
-
- [self updateButton:startDownloadButton title:@"DOWNLOAD"];
-
- [startDownloadButton addTarget:self
- action:@selector(currentButtonTapped:)
- forControlEvents:UIControlEventTouchUpInside];
- return startDownloadButton;
- }
- - (PKStopDownloadButton *)createStopDownloadButton {
- PKStopDownloadButton *stopDownloadButton = [[PKStopDownloadButton alloc] init];
- [stopDownloadButton.stopButton addTarget:self action:@selector(currentButtonTapped:)
- forControlEvents:UIControlEventTouchUpInside];
- return stopDownloadButton;
- }
- - (PKBorderedButton *)createDownloadedButton {
- PKBorderedButton *downloadedButton = [PKBorderedButton buttonWithType:UIButtonTypeCustom];
- [downloadedButton configureDefaultAppearance];
- [self updateButton:downloadedButton title:@"REMOVE"];
-
- [downloadedButton addTarget:self
- action:@selector(currentButtonTapped:)
- forControlEvents:UIControlEventTouchUpInside];
- return downloadedButton;
- }
- - (PKPendingView *)createPendingView {
- PKPendingView *pendingView = [[PKPendingView alloc] init];
- [pendingView addTarget:self action:@selector(currentButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
- return pendingView;
- }
- - (void)currentButtonTapped:(id)sender {
- [self.delegate downloadButtonTapped:self currentState:self.state];
- BlockSafeRun(self.callback, self, self.state);
- }
- - (void)createSubviews {
- self.stateViews = (__bridge_transfer NSMutableArray *)CFArrayCreateMutable(nil, 0, nil);
-
- PKBorderedButton *startDownloadButton = [self createStartDownloadButton];
- startDownloadButton.translatesAutoresizingMaskIntoConstraints = NO;
- [self addSubview:startDownloadButton];
- self.startDownloadButton = startDownloadButton;
- [self.stateViews addObject:startDownloadButton];
-
- PKStopDownloadButton *stopDownloadButton = [self createStopDownloadButton];
- stopDownloadButton.translatesAutoresizingMaskIntoConstraints = NO;
- [self addSubview:stopDownloadButton];
- self.stopDownloadButton = stopDownloadButton;
- [self.stateViews addObject:stopDownloadButton];
-
- PKBorderedButton *downloadedButton = [self createDownloadedButton];
- downloadedButton.translatesAutoresizingMaskIntoConstraints = NO;
- [self addSubview:downloadedButton];
- self.downloadedButton = downloadedButton;
- [self.stateViews addObject:downloadedButton];
-
- PKPendingView *pendingView = [self createPendingView];
- pendingView.translatesAutoresizingMaskIntoConstraints = NO;
- [self addSubview:pendingView];
- self.pendingView = pendingView;
- [self.stateViews addObject:pendingView];
- }
- - (NSArray *)createConstraints {
- NSMutableArray *constraints = [NSMutableArray array];
-
- [self.stateViews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- SafeObjClassCast(UIView, view, obj);
- [constraints addObjectsFromArray:[NSLayoutConstraint constraintsForWrappedSubview:view
- withInsets:UIEdgeInsetsZero]];
- }];
-
- return constraints;
- }
- @end
|