From 2526259d331f47d1c80d8cd5467303c0d20998b0 Mon Sep 17 00:00:00 2001 From: NikKovIos Date: Thu, 6 Jan 2022 13:46:52 +0300 Subject: [PATCH] Updated logic with square button. Now it is not showing when multiple selection is enabled, like in instagramm. --- .../Pages/Gallery/YPAssetViewContainer.swift | 43 +++++++++++-------- .../Pages/Gallery/YPAssetZoomableView.swift | 4 +- Source/Pages/Gallery/YPLibraryVC.swift | 3 +- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/Source/Pages/Gallery/YPAssetViewContainer.swift b/Source/Pages/Gallery/YPAssetViewContainer.swift index 4f12cc393..832d49b2f 100644 --- a/Source/Pages/Gallery/YPAssetViewContainer.swift +++ b/Source/Pages/Gallery/YPAssetViewContainer.swift @@ -29,7 +29,7 @@ final class YPAssetViewContainer: UIView { private let spinner = UIActivityIndicatorView(style: .white) private var shouldCropToSquare = YPConfig.library.isSquareByDefault - private var isMultipleSelection = false + private var isMultipleSelectionEnabled = false public var itemOverlayType = YPConfig.library.itemOverlayType @@ -102,36 +102,41 @@ final class YPAssetViewContainer: UIView { // MARK: - Square button @objc public func squareCropButtonTapped() { -// if let zoomableView = zoomableView { - let z = zoomableView.zoomScale - shouldCropToSquare = (z >= 1 && z < zoomableView.squaredZoomScale) -// } + let z = zoomableView.zoomScale + shouldCropToSquare = (z >= 1 && z < zoomableView.squaredZoomScale) zoomableView.fitImage(shouldCropToSquare, animated: true) } - - public func refreshSquareCropButton() { - if onlySquare { + + /// Update only UI of square crop button. + public func updateSquareCropButtonState() { + guard !isMultipleSelectionEnabled else { + // If multiple selection enabled, the squareCropButton is not visible squareCropButton.isHidden = true - } else { - if let image = zoomableView.assetImageView.image { - let isImageASquare = image.size.width == image.size.height - squareCropButton.isHidden = isImageASquare - } + return } - - let shouldFit = YPConfig.library.onlySquare ? true : shouldCropToSquare - zoomableView.fitImage(shouldFit) - zoomableView.layoutSubviews() + guard !onlySquare else { + // If only square enabled, than the squareCropButton is not visible + squareCropButton.isHidden = true + return + } + guard let selectedAssetImage = zoomableView.assetImageView.image else { + // If no selected asset, than the squareCropButton is not visible + squareCropButton.isHidden = true + return + } + + let isImageASquare = selectedAssetImage.size.width == selectedAssetImage.size.height + squareCropButton.isHidden = isImageASquare } // MARK: - Multiple selection /// Use this to update the multiple selection mode UI state for the YPAssetViewContainer public func setMultipleSelectionMode(on: Bool) { - isMultipleSelection = on + isMultipleSelectionEnabled = on let image = on ? YPConfig.icons.multipleSelectionOnIcon : YPConfig.icons.multipleSelectionOffIcon multipleSelectionButton.setImage(image, for: .normal) - refreshSquareCropButton() + updateSquareCropButtonState() } } diff --git a/Source/Pages/Gallery/YPAssetZoomableView.swift b/Source/Pages/Gallery/YPAssetZoomableView.swift index bc0717c4b..60cb87147 100644 --- a/Source/Pages/Gallery/YPAssetZoomableView.swift +++ b/Source/Pages/Gallery/YPAssetZoomableView.swift @@ -23,7 +23,7 @@ final class YPAssetZoomableView: UIScrollView { public var photoImageView = UIImageView() public var videoView = YPVideoView() public var squaredZoomScale: CGFloat = 1 - public var minWidth: CGFloat? = YPConfig.library.minWidthForItem + public var minWidthForItem: CGFloat? = YPConfig.library.minWidthForItem fileprivate var currentAsset: PHAsset? @@ -198,7 +198,7 @@ fileprivate extension YPAssetZoomableView { view.frame.size.width = screenWidth * aspectRatio view.frame.size.height = screenWidth - if let minWidth = minWidth { + if let minWidth = minWidthForItem { let k = minWidth / screenWidth zoomScale = (h / w) * k } diff --git a/Source/Pages/Gallery/YPLibraryVC.swift b/Source/Pages/Gallery/YPLibraryVC.swift index 9e36ba03f..18bb7d2f1 100644 --- a/Source/Pages/Gallery/YPLibraryVC.swift +++ b/Source/Pages/Gallery/YPLibraryVC.swift @@ -181,6 +181,7 @@ internal final class YPLibraryVC: UIViewController, YPPermissionCheckable { func showMultipleSelection() { // Prevent desactivating multiple selection when using `minNumberOfItems` if YPConfig.library.minNumberOfItems > 1 && multipleSelectionEnabled { + print("Selected minNumberOfItems greater than one :\(YPConfig.library.minNumberOfItems). Don't deselect multiple selection.") return } @@ -281,7 +282,7 @@ internal final class YPLibraryVC: UIViewController, YPPermissionCheckable { let completion = { (isLowResIntermediaryImage: Bool) in self.v.hideOverlayView() - self.v.assetViewContainer.refreshSquareCropButton() + self.v.assetViewContainer.updateSquareCropButtonState() self.updateCropInfo() if !isLowResIntermediaryImage { self.v.hideLoader()