YBIBToolViewHandler.m 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // YBIBToolViewHandler.m
  3. // YBImageBrowserDemo
  4. //
  5. // Created by 波儿菜 on 2019/7/7.
  6. // Copyright © 2019 杨波. All rights reserved.
  7. //
  8. #import "YBIBToolViewHandler.h"
  9. #import "YBIBCopywriter.h"
  10. #import "YBIBUtilities.h"
  11. @interface YBIBToolViewHandler ()
  12. @property (nonatomic, strong) YBIBSheetView *sheetView;
  13. @property (nonatomic, strong) YBIBSheetAction *saveAction;
  14. @property (nonatomic, strong) YBIBTopView *topView;
  15. @end
  16. @implementation YBIBToolViewHandler
  17. #pragma mark - <YBIBToolViewHandler>
  18. @synthesize yb_containerView = _yb_containerView;
  19. @synthesize yb_containerSize = _yb_containerSize;
  20. @synthesize yb_currentPage = _yb_currentPage;
  21. @synthesize yb_totalPage = _yb_totalPage;
  22. @synthesize yb_currentOrientation = _yb_currentOrientation;
  23. @synthesize yb_currentData = _yb_currentData;
  24. - (void)yb_containerViewIsReadied {
  25. [self.yb_containerView addSubview:self.topView];
  26. [self layoutWithExpectOrientation:self.yb_currentOrientation()];
  27. }
  28. - (void)yb_pageChanged {
  29. if (self.topView.operationType == YBIBTopViewOperationTypeSave) {
  30. self.topView.operationButton.hidden = [self currentDataShouldHideSaveButton];
  31. }
  32. [self.topView setPage:self.yb_currentPage() totalPage:self.yb_totalPage()];
  33. }
  34. - (void)yb_respondsToLongPress {
  35. [self showSheetView];
  36. }
  37. - (void)yb_hide:(BOOL)hide {
  38. self.topView.hidden = hide;
  39. [self.sheetView hideWithAnimation:NO];
  40. }
  41. - (void)yb_orientationWillChangeWithExpectOrientation:(UIDeviceOrientation)orientation {
  42. [self.sheetView hideWithAnimation:NO];
  43. }
  44. - (void)yb_orientationChangeAnimationWithExpectOrientation:(UIDeviceOrientation)orientation {
  45. [self layoutWithExpectOrientation:orientation];
  46. }
  47. #pragma mark - private
  48. - (BOOL)currentDataShouldHideSaveButton {
  49. id<YBIBDataProtocol> data = self.yb_currentData();
  50. BOOL allow = [data respondsToSelector:@selector(yb_allowSaveToPhotoAlbum)] && [data yb_allowSaveToPhotoAlbum];
  51. BOOL can = [data respondsToSelector:@selector(yb_saveToPhotoAlbum)];
  52. return !(allow && can);
  53. }
  54. - (void)layoutWithExpectOrientation:(UIDeviceOrientation)orientation {
  55. CGSize containerSize = self.yb_containerSize(orientation);
  56. UIEdgeInsets padding = YBIBPaddingByBrowserOrientation(orientation);
  57. self.topView.frame = CGRectMake(padding.left, padding.top, containerSize.width - padding.left - padding.right, [YBIBTopView defaultHeight]);
  58. }
  59. - (void)showSheetView {
  60. if ([self currentDataShouldHideSaveButton]) {
  61. [self.sheetView.actions removeObject:self.saveAction];
  62. } else {
  63. if (![self.sheetView.actions containsObject:self.saveAction]) {
  64. [self.sheetView.actions addObject:self.saveAction];
  65. }
  66. }
  67. [self.sheetView showToView:self.yb_containerView orientation:self.yb_currentOrientation()];
  68. }
  69. #pragma mark - getters
  70. - (YBIBSheetView *)sheetView {
  71. if (!_sheetView) {
  72. _sheetView = [YBIBSheetView new];
  73. __weak typeof(self) wSelf = self;
  74. [_sheetView setCurrentdata:^id<YBIBDataProtocol>{
  75. __strong typeof(wSelf) self = wSelf;
  76. if (!self) return nil;
  77. return self.yb_currentData();
  78. }];
  79. }
  80. return _sheetView;
  81. }
  82. - (YBIBSheetAction *)saveAction {
  83. if (!_saveAction) {
  84. __weak typeof(self) wSelf = self;
  85. _saveAction = [YBIBSheetAction actionWithName:[YBIBCopywriter sharedCopywriter].saveToPhotoAlbum action:^(id<YBIBDataProtocol> data) {
  86. __strong typeof(wSelf) self = wSelf;
  87. if (!self) return;
  88. if ([data respondsToSelector:@selector(yb_saveToPhotoAlbum)]) {
  89. [data yb_saveToPhotoAlbum];
  90. }
  91. [self.sheetView hideWithAnimation:YES];
  92. }];
  93. }
  94. return _saveAction;
  95. }
  96. - (YBIBTopView *)topView {
  97. if (!_topView) {
  98. _topView = [YBIBTopView new];
  99. _topView.operationType = YBIBTopViewOperationTypeMore;
  100. __weak typeof(self) wSelf = self;
  101. [_topView setClickOperation:^(YBIBTopViewOperationType type) {
  102. __strong typeof(wSelf) self = wSelf;
  103. if (!self) return;
  104. switch (type) {
  105. case YBIBTopViewOperationTypeSave: {
  106. id<YBIBDataProtocol> data = self.yb_currentData();
  107. if ([data respondsToSelector:@selector(yb_saveToPhotoAlbum)]) {
  108. [data yb_saveToPhotoAlbum];
  109. }
  110. }
  111. break;
  112. case YBIBTopViewOperationTypeMore: {
  113. [self showSheetView];
  114. }
  115. break;
  116. default:
  117. break;
  118. }
  119. }];
  120. }
  121. return _topView;
  122. }
  123. @end