Skip to content

Commit

Permalink
feat: refactor configs and add zoom scale limitation settings (#184)
Browse files Browse the repository at this point in the history
* feat: refactor configs
split config into 3 config files
put dial config into CropView Config
add Global.swift

* Update Sources/Mantis/CropView/CropView+Touches.swift

Co-authored-by: Niklas Baudy <niklas.baudy@vanniktech.de>

* Update Sources/Mantis/CropView/CropView.swift

Co-authored-by: Niklas Baudy <niklas.baudy@vanniktech.de>

* Update Sources/Mantis/CropViewConfig.swift

Co-authored-by: Niklas Baudy <niklas.baudy@vanniktech.de>

* Update Sources/Mantis/CropViewConfig.swift

Co-authored-by: Niklas Baudy <niklas.baudy@vanniktech.de>

* refactor: address review feedbacks

Co-authored-by: Yingtao Guo <yingtguo@cisco.com>
Co-authored-by: Niklas Baudy <niklas.baudy@vanniktech.de>
  • Loading branch information
3 people committed Jul 20, 2022
1 parent 2ab58ab commit a46341d
Show file tree
Hide file tree
Showing 20 changed files with 276 additions and 194 deletions.
10 changes: 6 additions & 4 deletions Example/EmbeddedCropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class EmbeddedCropViewController: UIViewController {
cropViewController.mode = .customizable
cropViewController.delegate = self

var config = Mantis.Config()
config.presetFixedRatioType = .alwaysUsingOnePresetFixedRatio(ratio: 1/2)
let config = Mantis.Config()
cropViewController.config = config

self.cropViewController = cropViewController
Expand Down Expand Up @@ -76,8 +75,11 @@ extension EmbeddedCropViewController: CropViewControllerDelegate {
}

func cropViewControllerDidEndResize(_ cropViewController: CropViewController, original: UIImage, cropInfo: CropInfo) {
let croppedImage = Mantis.crop(image: original, by: cropInfo)
self.resolutionLabel.text = getResolution(image: croppedImage)
// cropViewController.getExpectedCropImageSize() uses floor() to get integer image width and height
// If you use this size to check if it is less than an upper limit, you may need to add 1 to width and height
// to make sure the size is valid
let size = cropViewController.getExpectedCropImageSize()
self.resolutionLabel.text = "\(Int(size.width)) x \(Int(size.height)) pixels"
}

}
10 changes: 5 additions & 5 deletions Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class ViewController: UIViewController, CropViewControllerDelegate {
width: 557.1387432741491,
height: 654.7511809035641))

config.presetTransformationType = .presetInfo(info: transform)
config.cropViewConfig.presetTransformationType = .presetInfo(info: transform)

let cropViewController = Mantis.cropViewController(image: image,
config: config)
Expand All @@ -76,7 +76,7 @@ class ViewController: UIViewController, CropViewControllerDelegate {
}

var config = Mantis.Config()
config.showRotationDial = false
config.cropViewConfig.dialConfig = nil
config.showAttachedCropToolbar = false

let cropToolbar = MyNavigationCropToolbar(frame: .zero)
Expand Down Expand Up @@ -209,7 +209,7 @@ class ViewController: UIViewController, CropViewControllerDelegate {
let action = UIAlertAction(title: item.title, style: .default) {[weak self] _ in
guard let self = self else {return}
var config = Mantis.Config()
config.cropShapeType = item.type
config.cropViewConfig.cropShapeType = item.type

let cropViewController = Mantis.cropViewController(image: image, config: config)
cropViewController.modalPresentationStyle = .fullScreen
Expand All @@ -227,13 +227,13 @@ class ViewController: UIViewController, CropViewControllerDelegate {
present(actionSheet, animated: true)
}

private func presentWith(backgroundEffect effect: CropVisualEffectType) {
private func presentWith(backgroundEffect effect: CropMaskVisualEffectType) {
guard let image = image else {
return
}

var config = Mantis.Config()
config.cropVisualEffectType = effect
config.cropViewConfig.cropMaskVisualEffectType = effect
let cropViewController = Mantis.cropViewController(image: image,
config: config)
cropViewController.modalPresentationStyle = .fullScreen
Expand Down
16 changes: 12 additions & 4 deletions Mantis.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
5F69CC5E26C0629400568B75 /* Definition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F69CC5D26C0629400568B75 /* Definition.swift */; };
5F7D22AE245BCA8D0015A0D5 /* CropToolbarProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F7D22AD245BCA8D0015A0D5 /* CropToolbarProtocol.swift */; };
5FCE938724834C57002BBE65 /* ToolbarButtonOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FCE938624834C57002BBE65 /* ToolbarButtonOptions.swift */; };
664A7E9C28825B8400DF7C79 /* CropViewConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 664A7E9B28825B8400DF7C79 /* CropViewConfig.swift */; };
663FCF3928866DDA003D3B9E /* CropViewConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663FCF3728866DDA003D3B9E /* CropViewConfig.swift */; };
663FCF3A28866DDA003D3B9E /* CropToolbarConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 663FCF3828866DDA003D3B9E /* CropToolbarConfig.swift */; };
664A7E9F2884606800DF7C79 /* Global.swift in Sources */ = {isa = PBXBuildFile; fileRef = 664A7E9E2884606800DF7C79 /* Global.swift */; };
6659429D2877DAE80096A5DB /* Config.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6659429C2877DAE80096A5DB /* Config.swift */; };
6659429F2877DC8A0096A5DB /* Enum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6659429E2877DC8A0096A5DB /* Enum.swift */; };
665942A12877DD1C0096A5DB /* TypeAlias.swift in Sources */ = {isa = PBXBuildFile; fileRef = 665942A02877DD1C0096A5DB /* TypeAlias.swift */; };
Expand Down Expand Up @@ -103,7 +105,9 @@
5F69CC5D26C0629400568B75 /* Definition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Definition.swift; sourceTree = "<group>"; };
5F7D22AD245BCA8D0015A0D5 /* CropToolbarProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CropToolbarProtocol.swift; sourceTree = "<group>"; };
5FCE938624834C57002BBE65 /* ToolbarButtonOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToolbarButtonOptions.swift; sourceTree = "<group>"; };
664A7E9B28825B8400DF7C79 /* CropViewConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CropViewConfig.swift; sourceTree = "<group>"; };
663FCF3728866DDA003D3B9E /* CropViewConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CropViewConfig.swift; sourceTree = "<group>"; };
663FCF3828866DDA003D3B9E /* CropToolbarConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CropToolbarConfig.swift; sourceTree = "<group>"; };
664A7E9E2884606800DF7C79 /* Global.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Global.swift; sourceTree = "<group>"; };
6659429C2877DAE80096A5DB /* Config.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Config.swift; sourceTree = "<group>"; };
6659429E2877DC8A0096A5DB /* Enum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Enum.swift; sourceTree = "<group>"; };
665942A02877DD1C0096A5DB /* TypeAlias.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TypeAlias.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -209,7 +213,6 @@
OBJ_11 /* CropView */ = {
isa = PBXGroup;
children = (
664A7E9B28825B8400DF7C79 /* CropViewConfig.swift */,
OBJ_12 /* CropBoxFreeAspectFrameUpdater.swift */,
OBJ_13 /* CropBoxLockedAspectFrameUpdater.swift */,
OBJ_14 /* CropMaskViewManager.swift */,
Expand Down Expand Up @@ -343,11 +346,14 @@
OBJ_9 /* Mantis.h */,
OBJ_36 /* Mantis.swift */,
6659429C2877DAE80096A5DB /* Config.swift */,
663FCF3728866DDA003D3B9E /* CropViewConfig.swift */,
663FCF3828866DDA003D3B9E /* CropToolbarConfig.swift */,
6659429E2877DC8A0096A5DB /* Enum.swift */,
665942A02877DD1C0096A5DB /* TypeAlias.swift */,
OBJ_41 /* RatioOptions.swift */,
5FCE938624834C57002BBE65 /* ToolbarButtonOptions.swift */,
5F69CC5D26C0629400568B75 /* Definition.swift */,
664A7E9E2884606800DF7C79 /* Global.swift */,
OBJ_10 /* Info.plist */,
OBJ_11 /* CropView */,
OBJ_23 /* CropViewController */,
Expand Down Expand Up @@ -557,6 +563,7 @@
66F909BF287D22E7007E2FC6 /* ToolBarButtonImageBuilder+DrawImage.swift in Sources */,
OBJ_74 /* CropView+Touches.swift in Sources */,
OBJ_75 /* CropView+UIScrollViewDelegate.swift in Sources */,
664A7E9F2884606800DF7C79 /* Global.swift in Sources */,
OBJ_76 /* CropView.swift in Sources */,
OBJ_77 /* CropViewModel.swift in Sources */,
6659429F2877DC8A0096A5DB /* Enum.swift in Sources */,
Expand All @@ -574,6 +581,7 @@
5F69CC5E26C0629400568B75 /* Definition.swift in Sources */,
OBJ_87 /* UIImageExtensions.swift in Sources */,
OBJ_88 /* GeometryHelper.swift in Sources */,
663FCF3928866DDA003D3B9E /* CropViewConfig.swift in Sources */,
OBJ_89 /* LocalizedHelper.swift in Sources */,
OBJ_90 /* Mantis.swift in Sources */,
5F17E40A253535F300A3EB7D /* Orientation.swift in Sources */,
Expand All @@ -589,10 +597,10 @@
OBJ_97 /* RotationCalculator.swift in Sources */,
OBJ_98 /* RotationDial+Touches.swift in Sources */,
OBJ_99 /* RotationDial.swift in Sources */,
664A7E9C28825B8400DF7C79 /* CropViewConfig.swift in Sources */,
6659429D2877DAE80096A5DB /* Config.swift in Sources */,
OBJ_100 /* RotationDialPlate.swift in Sources */,
OBJ_101 /* RotationDialViewModel.swift in Sources */,
663FCF3A28866DDA003D3B9E /* CropToolbarConfig.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
92 changes: 7 additions & 85 deletions Sources/Mantis/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,95 +29,18 @@ public class LocalizationConfig {
public var bundle: Bundle? = Mantis.Config.bundle
public var tableName = "MantisLocalizable"
}

public protocol CropToolbarConfigProtocol {
var heightForVerticalOrientation: CGFloat { get set }
var widthForHorizontalOrientation: CGFloat { get set }

var backgroundColor: UIColor { get set }
var foregroundColor: UIColor { get set }

var optionButtonFontSize: CGFloat { get set }
var optionButtonFontSizeForPad: CGFloat { get set }

var toolbarButtonOptions: ToolbarButtonOptions { get set }

// The properties below are required by CropViewController and for some specific scenarios
// Most of time you can set just their default values as CropToolbarConfig does

/**
- .presentRatioListFromButton shows ratio list after tapping a button
- .alwaysShowRatioList shows ratio list without tapping any button
*/
var ratioCandidatesShowType: RatioCandidatesShowType { get set }

/**
.adaptive will show vertical / horizontal or horizontal / vertical size
based on the device orientations.
*/
var fixedRatiosShowType: FixedRatiosShowType { get set }

/**
When Config.presetFixedRatioType is canUseMultiplePresetFixedRatio and default ratio is not 0,
this property will be set to true.
*/
var presetRatiosButtonSelected: Bool { get set }

/**
When Config.presetFixedRatioType is alwaysUsingOnePresetFixedRatio,
this property will be set to false.
Then the FixedRatioSettingButton should not show up.
*/
var includeFixedRatiosSettingButton: Bool { get set }

/**
- For .normal, the CropToolBar has cancel and crop buttons
- For .simple, the CropToolBar does not have cancel and crop buttons
When embeding CropViewController to another UIViewController, that UIViewController should be
in charge of cancel and crop buttons
*/
var mode: CropToolbarMode { get set }
}

// MARK: - CropToolbarConfig
public struct CropToolbarConfig: CropToolbarConfigProtocol {
public var heightForVerticalOrientation: CGFloat = 44
public var widthForHorizontalOrientation: CGFloat = 80

public var optionButtonFontSize: CGFloat = 14
public var optionButtonFontSizeForPad: CGFloat = 20

/**
The color settings are not for Mac Catalyst (Optimize Interface for Mac) for now
I haven't figured out a correct way to set button title color for this scenario
*/
public var backgroundColor: UIColor = .black
public var foregroundColor: UIColor = .white

public var toolbarButtonOptions: ToolbarButtonOptions = .default
public var ratioCandidatesShowType: RatioCandidatesShowType = .presentRatioListFromButton
public var fixedRatiosShowType: FixedRatiosShowType = .adaptive
public var presetRatiosButtonSelected = false
public var includeFixedRatiosSettingButton = true
public var mode: CropToolbarMode = .normal

public init() {}
}

// MARK: - Config
public struct Config {
public var presetTransformationType: PresetTransformationType = .none
public var cropShapeType: CropShapeType = .rect
public var cropVisualEffectType: CropVisualEffectType = .blurDark
public var cropViewConfig = CropViewConfig()
public var cropToolbarConfig: CropToolbarConfigProtocol = CropToolbarConfig()

public var ratioOptions: RatioOptions = .all
public var presetFixedRatioType: PresetFixedRatioType = .canUseMultiplePresetFixedRatio()
public var cropViewConfig = CropViewConfig()
public var showRotationDial = true
public var dialConfig = DialConfig()
public var cropToolbarConfig: CropToolbarConfigProtocol = CropToolbarConfig()
public private(set) var localizationConfig = Mantis.localizationConfig
public var showAttachedCropToolbar = true


public private(set) var localizationConfig = Mantis.localizationConfig

var customRatios: [(width: Int, height: Int)] = []

static private var bundleIdentifier: String = {
Expand All @@ -136,8 +59,7 @@ public struct Config {
return nil
}()

public init() {
}
public init() {}

mutating public func addCustomRatio(byHorizontalWidth width: Int, andHorizontalHeight height: Int) {
customRatios.append((width, height))
Expand Down
82 changes: 82 additions & 0 deletions Sources/Mantis/CropToolbarConfig.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// CropToolbarConfig.swift
// Mantis
//
// Created by yingtguo on 7/19/22.
//

import UIKit

public protocol CropToolbarConfigProtocol {
var heightForVerticalOrientation: CGFloat { get set }
var widthForHorizontalOrientation: CGFloat { get set }

var backgroundColor: UIColor { get set }
var foregroundColor: UIColor { get set }

var optionButtonFontSize: CGFloat { get set }
var optionButtonFontSizeForPad: CGFloat { get set }

var toolbarButtonOptions: ToolbarButtonOptions { get set }

// The properties below are required by CropViewController and for some specific scenarios
// Most of time you can set just their default values as CropToolbarConfig does

/**
- .presentRatioListFromButton shows ratio list after tapping a button
- .alwaysShowRatioList shows ratio list without tapping any button
*/
var ratioCandidatesShowType: RatioCandidatesShowType { get set }

/**
.adaptive will show vertical / horizontal or horizontal / vertical size
based on the device orientations.
*/
var fixedRatiosShowType: FixedRatiosShowType { get set }

/**
When Config.presetFixedRatioType is canUseMultiplePresetFixedRatio and default ratio is not 0,
this property will be set to true.
*/
var presetRatiosButtonSelected: Bool { get set }

/**
When Config.presetFixedRatioType is alwaysUsingOnePresetFixedRatio,
this property will be set to false.
Then the FixedRatioSettingButton should not show up.
*/
var includeFixedRatiosSettingButton: Bool { get set }

/**
- For .normal, the CropToolBar has cancel and crop buttons
- For .simple, the CropToolBar does not have cancel and crop buttons
When embeding CropViewController to another UIViewController, that UIViewController should be
in charge of cancel and crop buttons
*/
var mode: CropToolbarMode { get set }
}

// MARK: - CropToolbarConfig
public struct CropToolbarConfig: CropToolbarConfigProtocol {
public var heightForVerticalOrientation: CGFloat = 44
public var widthForHorizontalOrientation: CGFloat = 80

public var optionButtonFontSize: CGFloat = 14
public var optionButtonFontSizeForPad: CGFloat = 20

/**
The color settings are not for Mac Catalyst (Optimize Interface for Mac) for now
I haven't figured out a correct way to set button title color for this scenario
*/
public var backgroundColor: UIColor = .black
public var foregroundColor: UIColor = .white

public var toolbarButtonOptions: ToolbarButtonOptions = .default
public var ratioCandidatesShowType: RatioCandidatesShowType = .presentRatioListFromButton
public var fixedRatiosShowType: FixedRatiosShowType = .adaptive
public var presetRatiosButtonSelected = false
public var includeFixedRatiosSettingButton = true
public var mode: CropToolbarMode = .normal

public init() {}
}
12 changes: 6 additions & 6 deletions Sources/Mantis/CropView/CropMaskViewManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import UIKit

class CropMaskViewManager {
fileprivate var dimmingView: CropDimmingView!
fileprivate var visualEffectView: CropVisualEffectView!
fileprivate var visualEffectView: CropMaskVisualEffectView!

var cropShapeType: CropShapeType = .rect
var cropVisualEffectType: CropVisualEffectType = .blurDark
var cropMaskVisualEffectType: CropMaskVisualEffectType = .blurDark

init(with superview: UIView,
cropRatio: CGFloat = 1.0,
cropShapeType: CropShapeType = .rect,
cropVisualEffectType: CropVisualEffectType = .blurDark) {
cropMaskVisualEffectType: CropMaskVisualEffectType = .blurDark) {

setup(in: superview, cropRatio: cropRatio)
self.cropShapeType = cropShapeType
self.cropVisualEffectType = cropVisualEffectType
self.cropMaskVisualEffectType = cropMaskVisualEffectType
}

private func setupOverlayView(in view: UIView, cropRatio: CGFloat = 1.0) {
Expand All @@ -33,8 +33,8 @@ class CropMaskViewManager {
}

private func setupTranslucencyView(in view: UIView, cropRatio: CGFloat = 1.0) {
visualEffectView = CropVisualEffectView(cropShapeType: cropShapeType,
effectType: cropVisualEffectType,
visualEffectView = CropMaskVisualEffectView(cropShapeType: cropShapeType,
effectType: cropMaskVisualEffectType,
cropRatio: cropRatio)
visualEffectView.isUserInteractionEnabled = false
view.addSubview(visualEffectView)
Expand Down

0 comments on commit a46341d

Please sign in to comment.