Skip to content

Commit

Permalink
Refactor code (#413)
Browse files Browse the repository at this point in the history
* chore: change two functions to variables in Config.swift

* chore: improve examples by supporting restoring transformation

* refactor: rename createConfig to createConfigWithPresetTransformation
  • Loading branch information
guoyingtao authored Sep 28, 2024
1 parent 0d3572c commit 8cc128a
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 10 deletions.
19 changes: 18 additions & 1 deletion Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,22 @@ import Mantis

class ViewController: UIViewController {
var image = UIImage(named: "sunflower.jpg")
var transformation: Transformation?

@IBOutlet weak var croppedImageView: UIImageView!
var imagePicker: ImagePicker!
@IBOutlet weak var cropShapesButton: UIButton!

private func createConfigWithPresetTransformation() -> Config {
var config = Mantis.Config()

if let transformation = transformation {
config.cropViewConfig.presetTransformationType = .presetInfo(info: transformation)
}

return config
}

override func viewDidLoad() {
super.viewDidLoad()
imagePicker = ImagePicker(presentationController: self, delegate: self)
Expand All @@ -33,12 +44,17 @@ class ViewController: UIViewController {
guard let image = image else {
return
}
var config = Mantis.Config()
var config = createConfigWithPresetTransformation()
config.cropMode = .async

let indicatorFrame = CGRect(origin: .zero, size: config.cropViewConfig.cropActivityIndicatorSize)
config.cropViewConfig.cropActivityIndicator = CustomWaitingIndicator(frame: indicatorFrame)
config.cropToolbarConfig.toolbarButtonOptions = [.clockwiseRotate, .reset, .ratio, .autoAdjust, .horizontallyFlip]

if let transformation = transformation {
config.cropViewConfig.presetTransformationType = .presetInfo(info: transformation)
}

let cropViewController = Mantis.cropViewController(image: image,
config: config)
cropViewController.delegate = self
Expand Down Expand Up @@ -299,6 +315,7 @@ class ViewController: UIViewController {
print("transformation is \(transformation)")
print("cropInfo is \(cropInfo)")
croppedImageView.image = cropped
self.transformation = transformation
dismiss(animated: true)
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Mantis/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ public struct Config {
customRatios.append((height, width))
}

func hasCustomRatios() -> Bool {
var hasCustomRatios: Bool {
return !customRatios.isEmpty
}

func getCustomRatioItems() -> [RatioItemType?] {
var customRatioItems: [RatioItemType?] {
return customRatios.map {
RatioItemType(nameH: String("\($0.width):\($0.height)"), ratioH: Double($0.width)/Double($0.height),
nameV: String("\($0.height):\($0.width)"), ratioV: Double($0.height)/Double($0.width))
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mantis/CropViewController/CropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ open class CropViewController: UIViewController {
return FixedRatioManager(type: type,
originalRatioH: ratio,
ratioOptions: config.ratioOptions,
customRatios: config.getCustomRatioItems().compactMap { $0 })
customRatios: config.customRatioItems.compactMap { $0 })
}

override open func viewDidLoad() {
Expand Down
4 changes: 3 additions & 1 deletion SwiftUIExample/MantisSwiftUIExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ struct ContentView: View {
@State private var showSourceTypeSelection = false
@State private var sourceType: UIImagePickerController.SourceType?

@State private var transformation: Transformation?

@Environment(\.horizontalSizeClass) var horizontalSizeClass

var body: some View {
Expand All @@ -33,7 +35,7 @@ struct ContentView: View {
ImageCropper(image: $image,
cropShapeType: $cropShapeType,
presetFixedRatioType: $presetFixedRatioType,
type: $cropperType)
type: $cropperType, transformation: $transformation)
.onDisappear(perform: reset)
.ignoresSafeArea()
})
Expand Down
5 changes: 3 additions & 2 deletions SwiftUIExample/MantisSwiftUIExample/ImageCropper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct ImageCropper: UIViewControllerRepresentable {
@Binding var cropShapeType: Mantis.CropShapeType
@Binding var presetFixedRatioType: Mantis.PresetFixedRatioType
@Binding var type: ImageCropperType
@Binding var transformation: Transformation?

@Environment(\.presentationMode) var presentationMode

Expand All @@ -31,7 +32,7 @@ struct ImageCropper: UIViewControllerRepresentable {

func cropViewControllerDidCrop(_ cropViewController: Mantis.CropViewController, cropped: UIImage, transformation: Transformation, cropInfo: CropInfo) {
parent.image = cropped
print("transformation is \(transformation)")
parent.transformation = transformation
parent.presentationMode.wrappedValue.dismiss()
}

Expand Down Expand Up @@ -73,7 +74,7 @@ extension ImageCropper {

func makeImageCropperHiddingRotationDial(context: Context) -> UIViewController {
var config = Mantis.Config()
config.cropViewConfig.showRotationDial = false
config.cropViewConfig.showAttachedRotationControlView = false
let cropViewController = Mantis.cropViewController(image: image!, config: config)
cropViewController.delegate = context.coordinator

Expand Down
6 changes: 3 additions & 3 deletions SwiftUIExample/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Mantis (2.8.0)
- Mantis (2.22.0)

DEPENDENCIES:
- Mantis (from `../`)
Expand All @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
Mantis: 9d048431a9c4f4296c0d5b9c109c7e5dc392e7cd
Mantis: 415ed683c5bb9097641ceb111f7b09356517ec7b

PODFILE CHECKSUM: b22a0202a3162df1f336549fb492f179aac13f6c

COCOAPODS: 1.11.3
COCOAPODS: 1.15.2

0 comments on commit 8cc128a

Please sign in to comment.