From 9e57fbeabf1b12f7f1da8c5bfbd523e0cb8da7cd Mon Sep 17 00:00:00 2001 From: Muukii Date: Fri, 23 Feb 2024 02:38:52 +0900 Subject: [PATCH] Update --- .../xcshareddata/swiftpm/Package.resolved | 4 +- .../Shared/Components/Crop/CropView.swift | 19 ++- .../PhotosCropRotating.swift | 126 ++++++++---------- 3 files changed, 77 insertions(+), 72 deletions(-) diff --git a/Dev/Brightroom.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Dev/Brightroom.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 49b2755e..7285cf92 100644 --- a/Dev/Brightroom.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Dev/Brightroom.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -140,8 +140,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/VergeGroup/Verge.git", "state" : { - "revision" : "a338c22b82c9a686a7131b54fa84ddad773f5c2a", - "version" : "12.0.0-beta.4" + "revision" : "790f1ef9af22f62fc7944f924ef4466dde071c2a", + "version" : "12.1.0" } } ], diff --git a/Sources/BrightroomUI/Shared/Components/Crop/CropView.swift b/Sources/BrightroomUI/Shared/Components/Crop/CropView.swift index d0190514..c19b4013 100644 --- a/Sources/BrightroomUI/Shared/Components/Crop/CropView.swift +++ b/Sources/BrightroomUI/Shared/Components/Crop/CropView.swift @@ -63,7 +63,7 @@ public final class CropView: UIView, UIScrollViewDelegate { Returns aspect ratio. Would not be affected by rotation. */ - var preferredAspectRatio: PixelAspectRatio? + public fileprivate(set) var preferredAspectRatio: PixelAspectRatio? } /** @@ -507,6 +507,10 @@ public final class CropView: UIView, UIScrollViewDelegate { store.commit { + guard $0.proposedCrop?.rotation != rotation else { + return + } + if let crop = $0.proposedCrop { $0.proposedCrop?.updateCropExtent( @@ -524,12 +528,21 @@ public final class CropView: UIView, UIScrollViewDelegate { public func setAdjustmentAngle(_ angle: EditingCrop.AdjustmentAngle) { - store.commit { + let records = store.commit { + + guard $0.proposedCrop?.adjustmentAngle != angle else { + return false + } + $0.proposedCrop?.adjustmentAngle = angle $0.layoutVersion += 1 + + return true } - record() + if records { + record() + } } diff --git a/Sources/BrightroomUIPhotosCrop/PhotosCropRotating/PhotosCropRotating.swift b/Sources/BrightroomUIPhotosCrop/PhotosCropRotating/PhotosCropRotating.swift index ab12e25b..2015be88 100644 --- a/Sources/BrightroomUIPhotosCrop/PhotosCropRotating/PhotosCropRotating.swift +++ b/Sources/BrightroomUIPhotosCrop/PhotosCropRotating/PhotosCropRotating.swift @@ -43,17 +43,11 @@ public struct PhotosCropRotating: View { } } - private enum AspectRatio: Equatable { - case original(PixelAspectRatio) - case freeform - case specific(PixelAspectRatio) - } - @StateObject var editingStack: EditingStack @State private var rotation: EditingCrop.Rotation? @State private var adjustmentAngle: EditingCrop.AdjustmentAngle? - @State private var croppingAspectRatio: AspectRatio = .freeform + @State private var croppingAspectRatio: PixelAspectRatio? // TODO: use init value from original aspect ratio. @State private var croppingAspectRationDirection: Direction = .horizontal @@ -92,13 +86,15 @@ public struct PhotosCropRotating: View { self.rotation = self.rotation!.next() } + croppingAspectRatio = cropViewState?.preferredAspectRatio?.swapped() + }, label: { Image(systemName: "rotate.left") .resizable() .aspectRatio(contentMode: .fit) .frame(width: 22) - .foregroundStyle(.secondary) + .foregroundStyle(rotation == .angle_0 ? .secondary : .primary) } ) .disabled(isLoading) @@ -109,7 +105,7 @@ public struct PhotosCropRotating: View { Button { rotation = nil adjustmentAngle = nil - croppingAspectRatio = .freeform + croppingAspectRatio = nil // switch croppingAspectRatio { // case .fixed(let ratio): @@ -150,21 +146,32 @@ public struct PhotosCropRotating: View { SwiftUICropView( editingStack: editingStack, stateHandler: { state in - cropViewState = state + + Task { @MainActor in + + cropViewState = state + + if let proposedCrop = state.proposedCrop { + if rotation != proposedCrop.rotation { + rotation = proposedCrop.rotation + } + + if adjustmentAngle != proposedCrop.adjustmentAngle { + adjustmentAngle = proposedCrop.adjustmentAngle + } + + } + + if croppingAspectRatio != state.preferredAspectRatio { + croppingAspectRatio = state.preferredAspectRatio + } + + } } ) .rotation(rotation) .adjustmentAngle(adjustmentAngle) - .croppingAspectRatio({ - switch croppingAspectRatio { - case .freeform: - return nil - case .original(let ratio): - return ratio - case .specific(let ratio): - return ratio - } - }()) + .croppingAspectRatio(croppingAspectRatio) .zIndex(0) Group { @@ -270,17 +277,25 @@ public struct PhotosCropRotating: View { private var aspectRatioView: some View { let isDirectionButtonDisabled: Bool = { - switch croppingAspectRatio { - case .original(_): + + guard let loaded = editingStack.state.loadedState else { return true - case .freeform: + } + + guard let croppingAspectRatio else { + return true + } + + if croppingAspectRatio == .init(loaded.imageSize) { + return true + } + + if croppingAspectRatio == .square { return true - case .specific(let ratio): - guard ratio != .square else { - return true - } - return false } + + return false + }() return VStack(spacing: 20) { @@ -289,20 +304,13 @@ public struct PhotosCropRotating: View { // vertical Button { if croppingAspectRationDirection != .vertical { - switch croppingAspectRatio { - case .freeform: - assertionFailure() - case .original: - assertionFailure() - case .specific(let ratio): - croppingAspectRatio = .specific(ratio.swapped()) - croppingAspectRationDirection = .vertical - } + croppingAspectRatio = croppingAspectRatio?.swapped() + croppingAspectRationDirection = .vertical } } label: { ZStack { RoundedRectangle(cornerRadius: 2) - .stroke(style: .init(lineWidth: 5)) + .stroke(style: .init(lineWidth: 2)) .fill(Color(white: 0.7, opacity: 1)) RoundedRectangle(cornerRadius: 2) @@ -327,20 +335,13 @@ public struct PhotosCropRotating: View { // horizontal Button { if croppingAspectRationDirection != .horizontal { - switch croppingAspectRatio { - case .freeform: - assertionFailure() - case .original: - assertionFailure() - case .specific(let ratio): - croppingAspectRatio = .specific(ratio.swapped()) - croppingAspectRationDirection = .horizontal - } + croppingAspectRatio = croppingAspectRatio?.swapped() + croppingAspectRationDirection = .horizontal } } label: { ZStack { RoundedRectangle(cornerRadius: 2) - .stroke(style: .init(lineWidth: 5)) + .stroke(style: .init(lineWidth: 2)) .fill(Color(white: 0.7, opacity: 1)) RoundedRectangle(cornerRadius: 2) @@ -371,35 +372,26 @@ public struct PhotosCropRotating: View { AspectRationButton( title: Text("ORIGINAL"), - isSelected: { - switch croppingAspectRatio { - case .original(let pixelAspectRatio): - return pixelAspectRatio.asCGSize() == editingStack.state.loadedState?.imageSize - case .freeform: - return false - case .specific: - return false - } - }() + isSelected: croppingAspectRatio == editingStack.state.loadedState.map { .init($0.imageSize) } ) { guard let imageSize = editingStack.state.loadedState?.imageSize else { return } - croppingAspectRatio = .original(.init(imageSize)) + croppingAspectRatio = .init(imageSize) } AspectRationButton( title: Text("FREEFORM"), - isSelected: croppingAspectRatio == .freeform + isSelected: croppingAspectRatio == nil ) { - croppingAspectRatio = .freeform + croppingAspectRatio = nil } AspectRationButton( title: Text("SQUARE"), - isSelected: croppingAspectRatio == .specific(.square) + isSelected: croppingAspectRatio == .square ) { - croppingAspectRatio = .specific(.square) + croppingAspectRatio = .square } ForEach(Self.horizontalRectangleApectRatios) { ratio in @@ -407,16 +399,16 @@ public struct PhotosCropRotating: View { case .vertical: AspectRationButton( title: Text("\(Int(ratio.height)):\(Int(ratio.width))"), - isSelected: croppingAspectRatio == .specific(ratio.swapped()) + isSelected: croppingAspectRatio == ratio.swapped() ) { - croppingAspectRatio = .specific(ratio.swapped()) + croppingAspectRatio = ratio.swapped() } case .horizontal: AspectRationButton( title: Text("\(Int(ratio.width)):\(Int(ratio.height))"), - isSelected: croppingAspectRatio == .specific(ratio) + isSelected: croppingAspectRatio == ratio ) { - croppingAspectRatio = .specific(ratio) + croppingAspectRatio = ratio } } }