// // MTStatefulBaseView.m // Meet // // Created by ko1o on 2018/10/26. // Copyright © 2018年 ko1o. All rights reserved. // #import "MTStatefulBaseView.h" #import "UIView+MTCreate.h" #import "UIImage+Scale.h" @implementation MTStatefulConfigItem @end @interface MTStatefulBaseView () @property (nonatomic, strong) UIActivityIndicatorView * refreshLoadingView; @property (nonatomic, strong) UIButton * stateButton; @property (nonatomic, strong) UIButton * refreshButton; @property (nonatomic, weak) id delegate; @end @implementation MTStatefulBaseView - (void)dealloc { ; } - (instancetype)initWithFrame:(CGRect)frame type:(MTStatefulViewType)statefulViewType delegate:(id)delegate { if (self = [super initWithFrame:frame]) { _statefulViewType = statefulViewType; self.delegate = delegate; if (MTStatefulViewTypeLoading == statefulViewType) { _refreshLoadingView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; _refreshLoadingView.color = UIColor.whiteColor; [self addSubview:_refreshLoadingView]; [_refreshLoadingView startAnimating]; } MTStatefulConfigItem *item = [self statefulConfigItemForStatefulViewType:statefulViewType]; if (MTStatefulViewTypeError == statefulViewType || MTStatefulViewTypeEmpty == statefulViewType) { self.stateButton = [[UIButton alloc]init]; [self.stateButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal]; self.stateButton.titleLabel.font = NORMAL_FONT; UIImage *image = nil; if (!item.shouldOnlyShowTitle) { image = ImageNamed(@"TH_empty_page_icon"); image = [UIImage imageWithImage:image scaledToSize:CGSizeMake(FIX_SIZE(150), FIX_SIZE(150))]; [self.stateButton setImage:image forState:UIControlStateNormal]; } NSString * title = item.statefulTitle; if (MTStatefulViewTypeError == statefulViewType) { [self.stateButton setTitle:title.length > 0 ? title : @"加载失败,请稍后再试" forState:UIControlStateNormal]; } if (MTStatefulViewTypeEmpty == statefulViewType) { [self.stateButton setTitle: title.length > 0 ? title : @"空空无也~" forState:UIControlStateNormal]; } [self.stateButton sizeToFit]; self.stateButton.userInteractionEnabled = NO; [self addSubview:self.stateButton]; if (image) { [self.stateButton mt_centerVerticallyWithPadding:FIX_SIZE(10)]; } } if (MTStatefulViewTypeError == statefulViewType ) { BOOL shouldShowRefresh = NO; if (item) { shouldShowRefresh = item.shouldShowErrorRefresh; } if (shouldShowRefresh) { self.refreshButton = [UIButton mt_commonButtonWithTitle:@"刷新" target:self action:@selector(handleRefresh:)]; self.refreshButton.layer.cornerRadius = 3; [self addSubview:self.refreshButton]; } } } return self; } - (MTStatefulConfigItem *)statefulConfigItemForStatefulViewType:(MTStatefulViewType)type { if ([self.delegate respondsToSelector:@selector(statefulConfigItemForStatefulViewType:)]) { MTStatefulConfigItem *item = [[self delegate]statefulConfigItemForStatefulViewType:type]; return item; } return nil; } - (void)handleRefresh:(UIButton *)button { dispatch_block_t block = [self statefulConfigItemForStatefulViewType:MTStatefulViewTypeError].didClickedRefreshBlock; if (block) { block(); } } - (void)layoutSubviews { [super layoutSubviews]; _refreshLoadingView.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); [_stateButton sizeToFit]; if (_refreshButton) { _refreshButton.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds)); _stateButton.centerX = CGRectGetMidX(self.bounds); _stateButton.bottom = _refreshButton.top - FIX_SIZE(40); } else { _stateButton.center = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds) - FIX_SIZE(40)); } self.superview.userInteractionEnabled = NO; self.userInteractionEnabled = NO; self.backgroundColor = BG_COLOR; } #pragma mark - BXYStatefulPlaceHolderView - (UIEdgeInsets)placeholderViewInsets { return UIEdgeInsetsMake(-SCREEN_HEIGHT * 0.3, 0, 0, 0); } @end