Skip to content

Commit

Permalink
fix: solve the SwiftLint errors (#126)
Browse files Browse the repository at this point in the history
* fix: solve the SwiftLint errors

* refactor: rename xValue and yValue
  • Loading branch information
guoyingtao committed Aug 8, 2021
1 parent 33a8865 commit 5e147aa
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 111 deletions.
32 changes: 32 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
disabled_rules:
- trailing_whitespace
opt_in_rules:
- empty_count
- empty_string
excluded:
- Carthage
- Pods
- SwiftLint/Common/3rdPartyLib
line_length:
warning: 150
error: 200
ignores_function_declarations: true
ignores_comments: true
ignores_urls: true
function_body_length:
warning: 300
error: 500
function_parameter_count:
warning: 6
error: 8
type_body_length:
warning: 300
error: 500
file_length:
warning: 1000
error: 1500
ignore_comment_only_lines: true
cyclomatic_complexity:
warning: 15
error: 25
reporter: "xcode"
25 changes: 25 additions & 0 deletions Mantis.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

/* Begin PBXBuildFile section */
5F17E40A253535F300A3EB7D /* Orientation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F17E409253535F300A3EB7D /* Orientation.swift */; };
5F69CC5E26C0629400568B75 /* definitions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F69CC5D26C0629400568B75 /* definitions.swift */; };
5F7D22AE245BCA8D0015A0D5 /* CropToolbarProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5F7D22AD245BCA8D0015A0D5 /* CropToolbarProtocol.swift */; };
5FCE938724834C57002BBE65 /* ToolbarButtonOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5FCE938624834C57002BBE65 /* ToolbarButtonOptions.swift */; };
8EFB6F6725D16B0F00C0DDB2 /* MantisLocalizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 8EFB6F3525D154D900C0DDB2 /* MantisLocalizable.strings */; };
Expand Down Expand Up @@ -93,6 +94,7 @@

/* Begin PBXFileReference section */
5F17E409253535F300A3EB7D /* Orientation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Orientation.swift; sourceTree = "<group>"; };
5F69CC5D26C0629400568B75 /* definitions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = definitions.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>"; };
8EFB6F3625D154D900C0DDB2 /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/MantisLocalizable.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -330,6 +332,7 @@
OBJ_36 /* Mantis.swift */,
OBJ_41 /* RatioOptions.swift */,
5FCE938624834C57002BBE65 /* ToolbarButtonOptions.swift */,
5F69CC5D26C0629400568B75 /* definitions.swift */,
OBJ_10 /* Info.plist */,
OBJ_11 /* CropView */,
OBJ_23 /* CropViewController */,
Expand Down Expand Up @@ -370,6 +373,7 @@
OBJ_68 /* Sources */,
OBJ_102 /* Frameworks */,
8EFB6F3325D1548800C0DDB2 /* Resources */,
5F69CC5726BF89F900568B75 /* ShellScript */,
);
buildRules = (
);
Expand Down Expand Up @@ -481,6 +485,26 @@
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXShellScriptBuildPhase section */
5F69CC5726BF89F900568B75 /* ShellScript */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
);
outputFileListPaths = (
);
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi\n";
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
8EFB6F5025D16A9E00C0DDB2 /* Sources */ = {
isa = PBXSourcesBuildPhase;
Expand Down Expand Up @@ -530,6 +554,7 @@
5FCE938724834C57002BBE65 /* ToolbarButtonOptions.swift in Sources */,
OBJ_85 /* CGImageExtensions.swift in Sources */,
OBJ_86 /* CoreGraphicsExtensions.swift in Sources */,
5F69CC5E26C0629400568B75 /* definitions.swift in Sources */,
OBJ_87 /* UIImageExtensions.swift in Sources */,
OBJ_88 /* GeometryHelper.swift in Sources */,
OBJ_89 /* LocalizedHelper.swift in Sources */,
Expand Down
9 changes: 8 additions & 1 deletion Sources/Mantis/CropView/CropBoxFreeAspectFrameUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@ struct CropBoxFreeAspectFrameUpdater {
}

func getNewCropFrameSize(byTappedEdge tappedEdge: CropViewOverlayEdge) -> CGSize {
let tappedEdgeCropFrameUpdateRule: [CropViewOverlayEdge: (xDelta: CGFloat, yDelta: CGFloat)] = [.left: (-xDelta, 0), .right: (xDelta, 0), .top: (0, -yDelta), .bottom: (0, yDelta), .topLeft: (-xDelta, -yDelta), .topRight: (xDelta, -yDelta), .bottomLeft: (-xDelta, yDelta), .bottomRight: (xDelta, yDelta)]
let tappedEdgeCropFrameUpdateRule: TappedEdgeCropFrameUpdateRule = [.left: (-xDelta, 0),
.right: (xDelta, 0),
.top: (0, -yDelta),
.bottom: (0, yDelta),
.topLeft: (-xDelta, -yDelta),
.topRight: (xDelta, -yDelta),
.bottomLeft: (-xDelta, yDelta),
.bottomRight: (xDelta, yDelta)]

guard let delta = tappedEdgeCropFrameUpdateRule[tappedEdge] else {
return cropOriginFrame.size
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ struct CropBoxLockedAspectFrameUpdater {
cropBoxFrame.size.width = cropBoxFrame.size.height * aspectRatio
}

let tappedEdgeCropFrameUpdateRule: [CropViewOverlayEdge: (xDelta: CGFloat, yDelta: CGFloat)] = [.topLeft: (xDelta, yDelta), .topRight: (-xDelta, yDelta), .bottomLeft: (xDelta, -yDelta), .bottomRight: (-xDelta, -yDelta)]
let tappedEdgeCropFrameUpdateRule: TappedEdgeCropFrameUpdateRule = [.topLeft: (xDelta, yDelta),
.topRight: (-xDelta, yDelta),
.bottomLeft: (xDelta, -yDelta),
.bottomRight: (-xDelta, -yDelta)]

func setCropBoxSize() {
guard let delta = tappedEdgeCropFrameUpdateRule[tappedEdge] else {
Expand Down
18 changes: 9 additions & 9 deletions Sources/Mantis/CropView/CropOverlayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class CropOverlayView: UIView {
private var horizontalGridLines: [UIView] = []
private var verticalGridLines: [UIView] = []
private var borderLine: UIView = UIView()
private var corner: [UIView] = []
private var corners: [UIView] = []
private let borderThickness = CGFloat(1.0)
private let hineLineThickness = CGFloat(2.0)

override var frame: CGRect {
didSet {
if corner.count > 0 {
if !corners.isEmpty {
layoutLines()
handleEdgeTouched(with: tappedEdge)
}
Expand Down Expand Up @@ -66,7 +66,7 @@ class CropOverlayView: UIView {
borderLine.layer.borderColor = boarderNormalColor.cgColor

for _ in 0..<8 {
corner.append(createNewLine())
corners.append(createNewLine())
}

setupGridLines()
Expand All @@ -76,7 +76,7 @@ class CropOverlayView: UIView {
override func didMoveToSuperview() {
super.didMoveToSuperview()

if corner.count > 0 {
if !corners.isEmpty {
layoutLines()
}
}
Expand All @@ -98,9 +98,9 @@ class CropOverlayView: UIView {
}

private func layoutGridLines() {
for i in 0..<gridLineNumberType.rawValue {
horizontalGridLines[i].frame = CGRect(x: 0, y: CGFloat(i + 1) * frame.height / CGFloat(gridLineNumberType.rawValue + 1), width: frame.width, height: 1)
verticalGridLines[i].frame = CGRect(x: CGFloat(i + 1) * frame.width / CGFloat(gridLineNumberType.rawValue + 1), y: 0, width: 1, height: frame.height)
for index in 0..<gridLineNumberType.rawValue {
horizontalGridLines[index].frame = CGRect(x: 0, y: CGFloat(index + 1) * frame.height / CGFloat(gridLineNumberType.rawValue + 1), width: frame.width, height: 1)
verticalGridLines[index].frame = CGRect(x: CGFloat(index + 1) * frame.width / CGFloat(gridLineNumberType.rawValue + 1), y: 0, width: 1, height: frame.height)
}
}

Expand Down Expand Up @@ -147,8 +147,8 @@ class CropOverlayView: UIView {
let horizontalDistanceForVCorner = bounds.width + borderThickness
let veticalDistanceForVCorner = bounds.height + 2 * borderThickness - cropOverLayerCornerWidth

for (i, line) in corner.enumerated() {
let lineType: CornerLineType = CropOverlayView.CornerLineType(rawValue: i) ?? .topLeftVertical
for (index, line) in corners.enumerated() {
let lineType: CornerLineType = CropOverlayView.CornerLineType(rawValue: index) ?? .topLeftVertical
switch lineType {
case .topLeftHorizontal:
line.frame = topLeftHorizonalLayerFrame
Expand Down
10 changes: 5 additions & 5 deletions Sources/Mantis/CropView/CropView+Touches.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import UIKit

extension CropView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
let p = self.convert(point, to: self)
let newPoint = self.convert(point, to: self)

if let rotationDial = rotationDial, rotationDial.frame.contains(p) {
if let rotationDial = rotationDial, rotationDial.frame.contains(newPoint) {
return rotationDial
}

if (gridOverlayView.frame.insetBy(dx: -hotAreaUnit/2, dy: -hotAreaUnit/2).contains(p) &&
!gridOverlayView.frame.insetBy(dx: hotAreaUnit/2, dy: hotAreaUnit/2).contains(p))
if (gridOverlayView.frame.insetBy(dx: -hotAreaUnit/2, dy: -hotAreaUnit/2).contains(newPoint) &&
!gridOverlayView.frame.insetBy(dx: hotAreaUnit/2, dy: hotAreaUnit/2).contains(newPoint))
{
return self
}

if self.bounds.contains(p) {
if self.bounds.contains(newPoint) {
return self.scrollView
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/Mantis/CropView/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ extension CropView {
case .heart(maskOnly: false):
return (croppedImage.heart, transformation)
case .polygon(let sides, let offset, maskOnly: false):
let points = polygonPointArray(sides: sides, x: 0.5, y: 0.5, radius: 0.5, offset: 90 + offset)
let points = polygonPointArray(sides: sides, originX: 0.5, originY: 0.5, radius: 0.5, offset: 90 + offset)
return (croppedImage.clipPath(points), transformation)
}
}
Expand Down Expand Up @@ -657,7 +657,7 @@ extension CropView {
}
}

func RotateBy90(rotateAngle: CGFloat, completion: @escaping ()->Void = {}) {
func rotateBy90(rotateAngle: CGFloat, completion: @escaping ()->Void = {}) {
viewModel.setDegree90RotatingStatus()
let rorateDuration = 0.25

Expand All @@ -669,7 +669,7 @@ extension CropView {
self.viewModel.setRotatingStatus(by: angle)
}) {[weak self] _ in
guard let self = self else { return }
self.viewModel.RotateBy90(rotateAngle: rotateAngle)
self.viewModel.rotateBy90(rotateAngle: rotateAngle)
self.viewModel.setBetweenOperationStatus()
completion()
}
Expand All @@ -693,7 +693,7 @@ extension CropView {
}) {[weak self] _ in
guard let self = self else { return }
self.scrollView.updateMinZoomScale()
self.viewModel.RotateBy90(rotateAngle: rotateAngle)
self.viewModel.rotateBy90(rotateAngle: rotateAngle)
self.viewModel.setBetweenOperationStatus()
completion()
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Mantis/CropView/CropViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class CropViewModel: NSObject {
setInitialStatus()
}

func RotateBy90(rotateAngle: CGFloat) {
func rotateBy90(rotateAngle: CGFloat) {
if (rotateAngle < 0) {
rotationType.counterclockwiseRotate90()
} else {
Expand Down
9 changes: 6 additions & 3 deletions Sources/Mantis/CropViewController/CropViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ public class CropViewController: UIViewController {

cropToolbar.createToolbarUI(config: config.cropToolbarConfig)

cropToolbar.initConstraints(heightForVerticalOrientation: config.cropToolbarConfig.cropToolbarHeightForVertialOrientation, widthForHorizonOrientation: config.cropToolbarConfig.cropToolbarWidthForHorizontalOrientation)
let heightForVerticalOrientation = config.cropToolbarConfig.cropToolbarHeightForVertialOrientation
let widthForHorizonOrientation = config.cropToolbarConfig.cropToolbarWidthForHorizontalOrientation
cropToolbar.initConstraints(heightForVerticalOrientation: heightForVerticalOrientation,
widthForHorizonOrientation: widthForHorizonOrientation)
}

private func getRatioType() -> RatioType {
Expand Down Expand Up @@ -382,7 +385,7 @@ public class CropViewController: UIViewController {

let fixedRatioManager = getFixedRatioManager()

guard fixedRatioManager.ratios.count > 0 else { return }
guard !fixedRatioManager.ratios.isEmpty else { return }

if fixedRatioManager.ratios.count == 1 {
let ratioItem = fixedRatioManager.ratios[0]
Expand Down Expand Up @@ -411,7 +414,7 @@ public class CropViewController: UIViewController {
private func handleRotate(rotateAngle: CGFloat) {
if !disableRotation {
disableRotation = true
cropView.RotateBy90(rotateAngle: rotateAngle) { [weak self] in
cropView.rotateBy90(rotateAngle: rotateAngle) { [weak self] in
self?.disableRotation = false
self?.ratioSelector?.update(fixedRatioManager: self?.getFixedRatioManager())
}
Expand Down
24 changes: 12 additions & 12 deletions Sources/Mantis/CropViewController/FixedRatioManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ class FixedRatioManager {
// MARK: - Private methods
extension FixedRatioManager {
private func addExtraDefaultRatios() {
let scale3_2 = RatioItemType("3:2", 3.0/2.0, "2:3", 2.0/3.0)
let scale5_3 = RatioItemType("5:3", 5.0/3.0, "3:5", 3.0/5.0)
let scale4_3 = RatioItemType("4:3", 4.0/3.0, "3:4", 3.0/4.0)
let scale5_4 = RatioItemType("5:4", 5.0/4.0, "4:5", 4.0/5.0)
let scale7_5 = RatioItemType("7:5", 7.0/5.0, "5:7", 5.0/7.0)
let scale16_9 = RatioItemType("16:9", 16.0/9.0, "9:16", 9.0/16.0)
let scale3to2 = RatioItemType("3:2", 3.0/2.0, "2:3", 2.0/3.0)
let scale5to3 = RatioItemType("5:3", 5.0/3.0, "3:5", 3.0/5.0)
let scale4to3 = RatioItemType("4:3", 4.0/3.0, "3:4", 3.0/4.0)
let scale5to4 = RatioItemType("5:4", 5.0/4.0, "4:5", 4.0/5.0)
let scale7to5 = RatioItemType("7:5", 7.0/5.0, "5:7", 5.0/7.0)
let scale16to9 = RatioItemType("16:9", 16.0/9.0, "9:16", 9.0/16.0)

appendToTail(ratioItem: scale3_2)
appendToTail(ratioItem: scale5_3)
appendToTail(ratioItem: scale4_3)
appendToTail(ratioItem: scale5_4)
appendToTail(ratioItem: scale7_5)
appendToTail(ratioItem: scale16_9)
appendToTail(ratioItem: scale3to2)
appendToTail(ratioItem: scale5to3)
appendToTail(ratioItem: scale4to3)
appendToTail(ratioItem: scale5to4)
appendToTail(ratioItem: scale7to5)
appendToTail(ratioItem: scale16to9)
}

private func contains(ratioItem: RatioItemType) -> Bool {
Expand Down
26 changes: 17 additions & 9 deletions Sources/Mantis/CropViewController/RatioSelector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,22 @@ public class RatioSelector: UIView {

func update(fixedRatioManager: FixedRatioManager?) {
guard let fixedRatioManager = fixedRatioManager else { return }
self.ratios = fixedRatioManager.ratios
self.type = fixedRatioManager.type
self.originalRatioH = fixedRatioManager.originalRatioH
for ratioView in self.stackView.arrangedSubviews as! [RatioItemView] {
ratioView.type = type
ratios = fixedRatioManager.ratios
type = fixedRatioManager.type
originalRatioH = fixedRatioManager.originalRatioH

if let ratioItemViews = stackView.arrangedSubviews as? [RatioItemView] {
for ratioView in ratioItemViews {
ratioView.type = type
}
}
}

func reset() {
for ratioView in stackView.arrangedSubviews as! [RatioItemView] {
ratioView.selected = self.originalRatioH == ratioView.ratio.ratioH ? true : false
if let ratioItemViews = stackView.arrangedSubviews as? [RatioItemView] {
for ratioView in ratioItemViews {
ratioView.selected = originalRatioH == ratioView.ratio.ratioH ? true : false
}
}
}

Expand All @@ -77,8 +82,11 @@ public class RatioSelector: UIView {
itemView.didGetRatio = {[weak self] ratio in
let ratioValue = (self?.type == .horizontal) ? ratio.ratioH : ratio.ratioV
self?.didGetRatio(ratioValue)
for ratioView in self?.stackView.arrangedSubviews as! [RatioItemView] {
ratioView.selected = ratio.nameH == ratioView.ratio.nameH ? true : false

if let ratioItemViews = self?.stackView.arrangedSubviews as? [RatioItemView] {
for ratioView in ratioItemViews {
ratioView.selected = ratio.nameH == ratioView.ratio.nameH ? true : false
}
}
}
}
Expand Down

0 comments on commit 5e147aa

Please sign in to comment.