123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- //
- // YBIBToolViewHandler.m
- // YBImageBrowserDemo
- //
- // Created by 波儿菜 on 2019/7/7.
- // Copyright © 2019 杨波. All rights reserved.
- //
- #import "YBIBToolViewHandler.h"
- #import "YBIBCopywriter.h"
- #import "YBIBUtilities.h"
- @interface YBIBToolViewHandler ()
- @property (nonatomic, strong) YBIBSheetView *sheetView;
- @property (nonatomic, strong) YBIBSheetAction *saveAction;
- @property (nonatomic, strong) YBIBTopView *topView;
- @end
- @implementation YBIBToolViewHandler
- #pragma mark - <YBIBToolViewHandler>
- @synthesize yb_containerView = _yb_containerView;
- @synthesize yb_containerSize = _yb_containerSize;
- @synthesize yb_currentPage = _yb_currentPage;
- @synthesize yb_totalPage = _yb_totalPage;
- @synthesize yb_currentOrientation = _yb_currentOrientation;
- @synthesize yb_currentData = _yb_currentData;
- - (void)yb_containerViewIsReadied {
- [self.yb_containerView addSubview:self.topView];
- [self layoutWithExpectOrientation:self.yb_currentOrientation()];
- }
- - (void)yb_pageChanged {
- if (self.topView.operationType == YBIBTopViewOperationTypeSave) {
- self.topView.operationButton.hidden = [self currentDataShouldHideSaveButton];
- }
- [self.topView setPage:self.yb_currentPage() totalPage:self.yb_totalPage()];
- }
- - (void)yb_respondsToLongPress {
- [self showSheetView];
- }
- - (void)yb_hide:(BOOL)hide {
- self.topView.hidden = hide;
- [self.sheetView hideWithAnimation:NO];
- }
- - (void)yb_orientationWillChangeWithExpectOrientation:(UIDeviceOrientation)orientation {
- [self.sheetView hideWithAnimation:NO];
- }
- - (void)yb_orientationChangeAnimationWithExpectOrientation:(UIDeviceOrientation)orientation {
- [self layoutWithExpectOrientation:orientation];
- }
- #pragma mark - private
- - (BOOL)currentDataShouldHideSaveButton {
- id<YBIBDataProtocol> data = self.yb_currentData();
- BOOL allow = [data respondsToSelector:@selector(yb_allowSaveToPhotoAlbum)] && [data yb_allowSaveToPhotoAlbum];
- BOOL can = [data respondsToSelector:@selector(yb_saveToPhotoAlbum)];
- return !(allow && can);
- }
- - (void)layoutWithExpectOrientation:(UIDeviceOrientation)orientation {
- CGSize containerSize = self.yb_containerSize(orientation);
- UIEdgeInsets padding = YBIBPaddingByBrowserOrientation(orientation);
-
- self.topView.frame = CGRectMake(padding.left, padding.top, containerSize.width - padding.left - padding.right, [YBIBTopView defaultHeight]);
- }
- - (void)showSheetView {
- if ([self currentDataShouldHideSaveButton]) {
- [self.sheetView.actions removeObject:self.saveAction];
- } else {
- if (![self.sheetView.actions containsObject:self.saveAction]) {
- [self.sheetView.actions addObject:self.saveAction];
- }
- }
- [self.sheetView showToView:self.yb_containerView orientation:self.yb_currentOrientation()];
- }
- #pragma mark - getters
- - (YBIBSheetView *)sheetView {
- if (!_sheetView) {
- _sheetView = [YBIBSheetView new];
- __weak typeof(self) wSelf = self;
- [_sheetView setCurrentdata:^id<YBIBDataProtocol>{
- __strong typeof(wSelf) self = wSelf;
- if (!self) return nil;
- return self.yb_currentData();
- }];
- }
- return _sheetView;
- }
- - (YBIBSheetAction *)saveAction {
- if (!_saveAction) {
- __weak typeof(self) wSelf = self;
- _saveAction = [YBIBSheetAction actionWithName:[YBIBCopywriter sharedCopywriter].saveToPhotoAlbum action:^(id<YBIBDataProtocol> data) {
- __strong typeof(wSelf) self = wSelf;
- if (!self) return;
- if ([data respondsToSelector:@selector(yb_saveToPhotoAlbum)]) {
- [data yb_saveToPhotoAlbum];
- }
- [self.sheetView hideWithAnimation:YES];
- }];
- }
- return _saveAction;
- }
- - (YBIBTopView *)topView {
- if (!_topView) {
- _topView = [YBIBTopView new];
- _topView.operationType = YBIBTopViewOperationTypeMore;
- __weak typeof(self) wSelf = self;
- [_topView setClickOperation:^(YBIBTopViewOperationType type) {
- __strong typeof(wSelf) self = wSelf;
- if (!self) return;
- switch (type) {
- case YBIBTopViewOperationTypeSave: {
- id<YBIBDataProtocol> data = self.yb_currentData();
- if ([data respondsToSelector:@selector(yb_saveToPhotoAlbum)]) {
- [data yb_saveToPhotoAlbum];
- }
- }
- break;
- case YBIBTopViewOperationTypeMore: {
- [self showSheetView];
- }
- break;
- default:
- break;
- }
- }];
- }
- return _topView;
- }
- @end
|