Skip to content

Library refactor #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "ImageViewer",
dependencies: ["URLImage"]),
dependencies: []),
.target(
name: "ImageViewerRemote",
dependencies: ["URLImage"])
dependencies: ["ImageViewer", "URLImage"])
]
)
25 changes: 25 additions & 0 deletions Sources/ImageViewer/ImageDetails.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import SwiftUI

public struct ImageDetails {
var image: Image?
var aspectRatio: CGFloat?
var caption: String?

public init(image: Image?, aspectRatio: CGFloat? = nil, caption: String? = nil) {
self.image = image
self.aspectRatio = aspectRatio
self.caption = caption
}
}

public struct ImageDetailsRemote {
public var imageURL: String
var aspectRatio: CGFloat?
var caption: String?

public init(imageURL: String, aspectRatio: CGFloat? = nil, caption: String? = nil) {
self.imageURL = imageURL
self.aspectRatio = aspectRatio
self.caption = caption
}
}
82 changes: 82 additions & 0 deletions Sources/ImageViewer/ImageView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import SwiftUI

public struct ImageView: View {

var imageDetails: ImageDetails
@Binding var viewerShown: Bool

@State var dragOffset: CGSize = .zero
@State var dragOffsetPredicted: CGSize = .zero

public init(imageDetails: ImageDetails, viewerShown: Binding<Bool>) {
self.imageDetails = imageDetails
_viewerShown = viewerShown
}

public var body: some View {
VStack {
ZStack {
(imageDetails.image ?? Image(systemName: "questionmark.diamond"))
.resizable()
.aspectRatio(imageDetails.aspectRatio, contentMode: .fit)
.offset(x: self.dragOffset.width, y: self.dragOffset.height)
.rotationEffect(.init(degrees: Double(self.dragOffset.width / 30)))
.pinchToZoom()
.gesture(DragGesture()
.onChanged { value in
self.dragOffset = value.translation
self.dragOffsetPredicted = value.predictedEndTranslation
}
.onEnded { value in
if((abs(self.dragOffset.height) + abs(self.dragOffset.width) > 570) || ((abs(self.dragOffsetPredicted.height)) / (abs(self.dragOffset.height)) > 3) || ((abs(self.dragOffsetPredicted.width)) / (abs(self.dragOffset.width))) > 3) {
withAnimation(.spring()) {
self.dragOffset = self.dragOffsetPredicted
}
self.viewerShown = false

return
}
withAnimation(.interactiveSpring()) {
self.dragOffset = .zero
}
}
)

if let caption = imageDetails.caption {
VStack {
Spacer()

VStack {
Spacer()

HStack {
Spacer()

Text(caption)
.foregroundColor(.white)
.multilineTextAlignment(.center)

Spacer()
}
}
.padding()
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
}
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.background(Color(red: 0.12, green: 0.12, blue: 0.12, opacity: (1.0 - Double(abs(self.dragOffset.width) + abs(self.dragOffset.height)) / 1000)).edgesIgnoringSafeArea(.all))
.zIndex(1)
}
}

struct SwiftUIView_Previews: PreviewProvider {

static var details = ImageDetails(image: Image(systemName: "gear"), caption: "Gear")

static var previews: some View {
ImageView(imageDetails: details, viewerShown: .constant(true))
}
}
Loading