Skip to content

Commit

Permalink
fix: fix the issue when using canUseMultiplePresetFixedRatio with def…
Browse files Browse the repository at this point in the history
…ault ratio > 0 (#252)

* fix: fix the issue when using canUseMultiplePresetFixedRatio with defaultRatio > 0
  • Loading branch information
guoyingtao committed Jan 25, 2023
1 parent e1d8188 commit cbd66dd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
19 changes: 15 additions & 4 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -728,10 +728,19 @@ extension CropView {
}

extension CropView: CropViewProtocol {
func initialSetup(delegate: CropViewDelegate, alwaysUsingOnePresetFixedRatio: Bool = false) {
private func setForceFixedRatio(by presetFixedRatioType: PresetFixedRatioType) {
switch presetFixedRatioType {
case .alwaysUsingOnePresetFixedRatio(_):
forceFixedRatio = true
case .canUseMultiplePresetFixedRatio(let defaultRatio):
forceFixedRatio = defaultRatio > 0
}
}

func initialSetup(delegate: CropViewDelegate, presetFixedRatioType: PresetFixedRatioType) {
self.delegate = delegate
setViewDefaultProperties()
forceFixedRatio = alwaysUsingOnePresetFixedRatio
setForceFixedRatio(by: presetFixedRatioType)
}

func getRatioType(byImageIsOriginalisHorizontal isHorizontal: Bool) -> RatioType {
Expand Down Expand Up @@ -781,13 +790,15 @@ extension CropView: CropViewProtocol {
}
}

func setFixedRatio(_ ratio: Double, zoom: Bool = true, alwaysUsingOnePresetFixedRatio: Bool = false) {
func setFixedRatio(_ ratio: Double, zoom: Bool = true, presetFixedRatioType: PresetFixedRatioType) {
aspectRatioLockEnabled = true

if viewModel.aspectRatio != CGFloat(ratio) {
viewModel.aspectRatio = CGFloat(ratio)

if alwaysUsingOnePresetFixedRatio {
setForceFixedRatio(by: presetFixedRatioType)

if forceFixedRatio {
setFixedRatioCropBox(zoom: zoom)
} else {
UIView.animate(withDuration: 0.5) {
Expand Down
34 changes: 22 additions & 12 deletions Sources/Mantis/CropViewController/CropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class CropViewController: UIViewController {
#endif
view.backgroundColor = .black

cropView.initialSetup(delegate: self, alwaysUsingOnePresetFixedRatio: isAlwaysUsingOnePresetFixedRatio())
cropView.initialSetup(delegate: self, presetFixedRatioType: config.presetFixedRatioType)
createCropToolbar()
if config.cropToolbarConfig.ratioCandidatesShowType == .alwaysShowRatioList
&& config.cropToolbarConfig.includeFixedRatiosSettingButton {
Expand Down Expand Up @@ -186,17 +186,9 @@ public class CropViewController: UIViewController {

private func setFixedRatio(_ ratio: Double, zoom: Bool = true) {
cropToolbar.handleFixedRatioSetted(ratio: ratio)
cropView.setFixedRatio(ratio, zoom: zoom, alwaysUsingOnePresetFixedRatio: isAlwaysUsingOnePresetFixedRatio())
cropView.setFixedRatio(ratio, zoom: zoom, presetFixedRatioType: config.presetFixedRatioType)
}

private func isAlwaysUsingOnePresetFixedRatio() -> Bool {
if case .alwaysUsingOnePresetFixedRatio = config.presetFixedRatioType {
return true
}

return false
}

public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
cropView.processPresetTransformation { [weak self] transformation in
Expand All @@ -217,8 +209,23 @@ public class CropViewController: UIViewController {
cropToolbar.handleFixedRatioUnSetted()
}

private func isNeedToResetRatioButton() -> Bool {
var needToResetRatioButton = false

switch config.presetFixedRatioType {
case .canUseMultiplePresetFixedRatio(let defaultRatio):
if defaultRatio == 0 {
needToResetRatioButton = true
}
default:
break
}

return needToResetRatioButton
}

@objc private func handleSetRatio() {
if cropView.aspectRatioLockEnabled {
if cropView.aspectRatioLockEnabled && isNeedToResetRatioButton() {
resetRatioButton()
return
}
Expand Down Expand Up @@ -249,7 +256,10 @@ public class CropViewController: UIViewController {
}

private func handleReset() {
resetRatioButton()
if isNeedToResetRatioButton() {
resetRatioButton()
}

cropView.reset()
ratioSelector?.reset()
ratioSelector?.update(fixedRatioManager: getFixedRatioManager())
Expand Down
4 changes: 2 additions & 2 deletions Sources/Mantis/Protocols/CropViewProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ protocol CropViewProtocol: UIView {
var aspectRatioLockEnabled: Bool { get set }
var delegate: CropViewDelegate? { get set }

func initialSetup(delegate: CropViewDelegate, alwaysUsingOnePresetFixedRatio: Bool)
func initialSetup(delegate: CropViewDelegate, presetFixedRatioType: PresetFixedRatioType)
func setViewDefaultProperties()
func getRatioType(byImageIsOriginalisHorizontal isHorizontal: Bool) -> RatioType
func getImageHorizontalToVerticalRatio() -> Double
func resetComponents()
func prepareForDeviceRotation()
func handleDeviceRotated()
func setFixedRatio(_ ratio: Double, zoom: Bool, alwaysUsingOnePresetFixedRatio: Bool)
func setFixedRatio(_ ratio: Double, zoom: Bool, presetFixedRatioType: PresetFixedRatioType)
func rotateBy90(withRotateType rotateType: RotateBy90DegreeType, completion: @escaping () -> Void)
func handleAlterCropper90Degree()
func handlePresetFixedRatio(_ ratio: Double, transformation: Transformation)
Expand Down

0 comments on commit cbd66dd

Please sign in to comment.