Skip to content

Commit

Permalink
Updated logic with square button. Now it is not showing when multiple…
Browse files Browse the repository at this point in the history
… selection is enabled, like in instagramm.
  • Loading branch information
NikKovIos committed Jan 6, 2022
1 parent a2edaa9 commit 2526259
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
43 changes: 24 additions & 19 deletions Source/Pages/Gallery/YPAssetViewContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Pages/Gallery/YPAssetZoomableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion Source/Pages/Gallery/YPLibraryVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2526259

Please sign in to comment.