Skip to content

Commit

Permalink
Switch to TabView mid work commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
nagavcindriqim committed Sep 26, 2024
1 parent 8fcdb4a commit 960df31
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 73 deletions.
8 changes: 4 additions & 4 deletions Sources/UI/SwiftUI/View Modifiers/PhotoPreviewModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import SwiftUI

@available(iOS 15.0, *)
@available(iOS 16.0, *)
struct PhotoPreviewModifier: ViewModifier {
@Binding var presented: Bool
let items: [PhotoPreview.Item]
Expand All @@ -27,7 +27,7 @@ struct PhotoPreviewModifier: ViewModifier {
}
}

@available(iOS 15.0, *)
@available(iOS 16.0, *)
public extension View {
func photoPreview(
present: Binding<Bool>,
Expand All @@ -42,7 +42,7 @@ public extension View {
}
}

@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension PhotoPreviewModifier {
struct PresentationBackgroundModifier: ViewModifier {
let color: Color
Expand All @@ -58,7 +58,7 @@ extension PhotoPreviewModifier {
}
}

@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension View {
func presentationBackgroundIfAvailable(_ color: Color) -> some View {
modifier(PhotoPreviewModifier.PresentationBackgroundModifier(color: color))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import SwiftUI

@available(iOS 15.0, *)
@available(iOS 16.0, *)
public extension PhotoPreview {
struct Configuration {
public var backgroundColor: Color
Expand All @@ -30,7 +30,7 @@ public extension PhotoPreview {
}
}

@available(iOS 15.0, *)
@available(iOS 16.0, *)
public extension PhotoPreview.Configuration {
static var `default`: PhotoPreview.Configuration {
.init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import SwiftUI

@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension PhotoPreview {
@MainActor
class ImageLoader: ObservableObject {
Expand Down Expand Up @@ -39,7 +39,7 @@ extension PhotoPreview {
}

// MARK: - State
@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension PhotoPreview.ImageLoader {
enum State: Equatable {
case initial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import SwiftUI

@available(iOS 15.0, *)
@available(iOS 16.0, *)
public extension PhotoPreview {
struct Item {
public var image: Image?
Expand Down
44 changes: 12 additions & 32 deletions Sources/UI/SwiftUI/Views/PhotoPreview/PhotoPreview+ItemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Kingfisher
#endif
import SwiftUI

@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension PhotoPreview {
struct ItemView: View {
typealias VoidHandler = () -> Swift.Void
Expand Down Expand Up @@ -64,7 +64,7 @@ extension PhotoPreview {
}

// MARK: - Views
@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension PhotoPreview.ItemView {
var imageView: some View {
Group {
Expand All @@ -89,26 +89,14 @@ extension PhotoPreview.ItemView {
}
.scaledToFit()
#else
Group {
switch imageLoader.state {
case .loading, .initial:
loadingView
.task {
await imageLoader.loadImage(from: item.url)
}
case .loaded(let image):
Image(uiImage: image)
.resizable()
.scaledToFit()
case .failed:
if let placeholder = item.placeholder {
placeholder
.resizable()
.scaledToFit()
} else {
Color.clear
}
}
AsyncImage(url: item.url) { image in
image
.resizable()
.scaledToFit()
} placeholder: {
ProgressView()
.tint(.white)
.controlSize(.large)
}
#endif
}
Expand All @@ -128,7 +116,7 @@ extension PhotoPreview.ItemView {
}

// MARK: - Helper methods
@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension PhotoPreview.ItemView {
func endDrag(animated: Bool = true) {
initialDragOffset = .zero
Expand Down Expand Up @@ -194,7 +182,7 @@ extension PhotoPreview.ItemView {
}

// MARK: - Gestures
@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension PhotoPreview.ItemView {
var magnificationGesture: some Gesture {
MagnificationGesture()
Expand Down Expand Up @@ -224,15 +212,7 @@ extension PhotoPreview.ItemView {
if initialDragOffset == .zero {
initialDragOffset = value.translation
}
let imageWidth = screenSize.width * scale
let maxXOffset = max((imageWidth - screenSize.width) / 2, 0) + dragHorizontalPadding
let newXOffset = lastOffset.width + value.translation.width - initialDragOffset.width
if offset.width >= maxXOffset || offset.width <= -maxXOffset {
onDragChanged(value.translation.width)
dragEnabled = false
initialDragOffset = .zero
return
}
let newYOffset = lastOffset.height + value.translation.height - initialDragOffset.height
offset = CGSize(
width: newXOffset,
Expand Down
58 changes: 26 additions & 32 deletions Sources/UI/SwiftUI/Views/PhotoPreview/PhotoPreview.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
#if os(iOS)
import SwiftUI

@available(iOS 15.0, *)
@available(iOS 16.0, *)
public struct PhotoPreview: View {
let items: [Item]
let configuration: Configuration
@Binding var presented: Bool
@State var currentIndex = 0
@State var position: Int?
@State var currentIndex: Int = 0
@State var offset: CGFloat = 0
@State var verticalOffset: CGFloat = 0
@State var imageViewDragEnabled: Bool = false
Expand All @@ -24,6 +25,7 @@ public struct PhotoPreview: View {
@State var shouldSwitchDragDirection: Bool = true
@State var backgroundOpacity: CGFloat = 1
@State var initialDragOffset: CGFloat = .zero
@State var horizontalOffset: CGFloat = .zero

public init(
items: [Item],
Expand Down Expand Up @@ -53,38 +55,30 @@ public struct PhotoPreview: View {
}

// MARK: - ViewBuilders
@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension PhotoPreview {
var scrollView: some View {
GeometryReader { geometry in
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 0) {
ForEach(0..<items.count, id: \.self) { index in
ItemView(
dragEnabled: $imageViewDragEnabled,
currentIndex: $currentIndex,
verticalOffset: $verticalOffset,
item: items[index],
myIndex: index
) { newValue in
imageViewLastOffset = newValue
} onDragEnded: {
horizontalDragEnded(with: geometry.size.width)
}
.frame(width: geometry.size.width)
.id(index)
TabView {
ForEach(0..<items.count, id: \.self) { index in
ItemView(
dragEnabled: $imageViewDragEnabled,
currentIndex: $currentIndex,
verticalOffset: $verticalOffset,
item: items[index],
myIndex: index
) { newValue in
imageViewLastOffset = newValue
} onDragEnded: {
horizontalDragEnded(with: geometry.size.width)
}
.frame(width: geometry.size.width)
.id(index)
}
.drawingGroup()
}
.content
.offset(x: contentOffset(for: geometry))
.simultaneousGesture(dragGesture(with: geometry))
.background {
Color.clear
.contentShape(Rectangle())
.simultaneousGesture(dragGesture(with: geometry))
}
.simultaneousGesture(!imageViewDragEnabled ? dragGesture(with: geometry) : nil)
.tabViewStyle(.page(indexDisplayMode: .never))
.scrollDisabled(imageViewDragEnabled)
}
}

Expand All @@ -107,7 +101,7 @@ extension PhotoPreview {
}

// MARK: - Helpers
@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension PhotoPreview {
enum Direction {
case vertical
Expand Down Expand Up @@ -191,7 +185,7 @@ extension PhotoPreview {
imageViewLastOffset = 0

if dragDirection == .horizontal {
horizontalDragEnded(with: pageWidth)
// horizontalDragEnded(with: pageWidth)
} else {
verticalDragEnded()
}
Expand All @@ -211,7 +205,7 @@ extension PhotoPreview {
}

// MARK: - Gestures
@available(iOS 15.0, *)
@available(iOS 16.0, *)
extension PhotoPreview {
func dragGesture(with geometry: GeometryProxy) -> some Gesture {
DragGesture()
Expand All @@ -224,7 +218,7 @@ extension PhotoPreview {
dragDirection = abs(value.translation.width) > abs(value.translation.height) ? .horizontal : .vertical
}
if dragDirection == .horizontal {
horizontalDragChanged(with: value)
// horizontalDragChanged(with: value)
} else if imageViewLastOffset == .zero, offset == 0 {
verticalDragChanged(with: value)
}
Expand Down

0 comments on commit 960df31

Please sign in to comment.