Skip to content
Merged
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
57 changes: 38 additions & 19 deletions Sources/FlowStack/FlowLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,14 @@ public struct FlowLink<Label>: View where Label: View {
private var value: (any (Equatable & Hashable))?
private var configuration: Configuration

@Environment(\.self) private var capturedEnvironment

@Environment(\.flowPath) private var path
@Environment(\.flowDepth) private var flowDepth
@Environment(\.flowTransaction) private var transaction
@Environment(\.flowAnimationDuration) private var flowDuration

@Environment(\.colorScheme) private var colorScheme
@Environment(\.self) private var fetchedEnvironment

@State private var overrideAnchor: Anchor<CGRect>?

@State private var size: CGSize?
Expand All @@ -261,6 +262,11 @@ public struct FlowLink<Label>: View where Label: View {
@State var isShowing: Bool = true
@State var buttonPressed: Bool = false

@State var environmentList: [EnvironmentValues] = []
@State private var snapshots: [ColorScheme: UIImage] = [:]
@State private var environment = EnvironmentValues()
@State private var refreshButton = UUID()

/// Creates a flow link that presents the view corresponding to a value.
///
/// When someone activates the flow link that this initializer
Expand Down Expand Up @@ -296,29 +302,23 @@ public struct FlowLink<Label>: View where Label: View {
return path?.wrappedValue.elements.map(\.context?.linkDepth).contains(flowDepth) ?? false
}

@State private var snapshot: UIImage?

@MainActor
private func updateSnapshot() -> UIImage? {
guard snapshot == nil else { return snapshot }

private func createSnapshot(colorScheme: ColorScheme) -> UIImage? {
guard let size = size else { return nil }

let frame = CGRect(origin: .zero, size: size)
environment = fetchedEnvironment
environment.colorScheme = colorScheme

let controller = UIHostingController(
rootView: label()
.transformEnvironment(\.self) { environment in
environment = capturedEnvironment
}
.environment(\.self, environment)
.environment(\.opacityTransitionPercent, 1)
.ignoresSafeArea()
)

let view = controller.view

guard let view = view else {
return nil
}
guard let view = view else { return nil}

view.bounds = CGRect(origin: .zero, size: size)
view.backgroundColor = .clear
Expand Down Expand Up @@ -370,7 +370,7 @@ public struct FlowLink<Label>: View where Label: View {
}
Task {
if configuration.transitionFromSnapshot {
context?.snapshot = await updateSnapshot()
context?.snapshot = snapshots[colorScheme]
}
if let value = value {
withTransaction(transaction) {
Expand All @@ -379,8 +379,8 @@ public struct FlowLink<Label>: View where Label: View {
}
}
}
.id(refreshButton)
}

public var body: some View {
Group {
if isContainedInPath && configuration.animateFromAnchor {
Expand All @@ -390,8 +390,8 @@ public struct FlowLink<Label>: View where Label: View {
if configuration.animateFromAnchor && overrideAnchor == nil {
button
.opacity(isShowing ? 1.0 : 0.0)
/// (Workaround) Override an animation with an animation that does nothing
/// Leaving a flowlayer too early can cause an un-wanted animation
/// (Workaround) Override an animation with an animation that does nothing
/// Leaving a flowlayer too early can cause an un-wanted animation
.ignoreAnimation()
} else if configuration.animateFromAnchor {
button
Expand All @@ -401,6 +401,12 @@ public struct FlowLink<Label>: View where Label: View {
}
}
}
.onChange(of: colorScheme) { newScheme in
refreshButton = UUID()
snapshots[newScheme]
path?.wrappedValue.updateSnapshots(from: newScheme)
}
.onAppear { initSnapshots() }
.background(
GeometryReader { proxy in
Color.clear
Expand All @@ -421,7 +427,8 @@ public struct FlowLink<Label>: View where Label: View {
return PathContext(
anchor: configuration.animateFromAnchor ? anchor : nil,
overrideAnchor: configuration.animateFromAnchor ? overrideAnchor : nil,
snapshot: configuration.animateFromAnchor && configuration.transitionFromSnapshot ? snapshot : nil,
snapshot: configuration.animateFromAnchor && configuration.transitionFromSnapshot ? snapshots[colorScheme] : nil,
snapshotDict: snapshots,
linkDepth: flowDepth,
cornerRadius: configuration.cornerRadius,
cornerStyle: configuration.cornerStyle,
Expand All @@ -440,6 +447,7 @@ public struct FlowLink<Label>: View where Label: View {
context?.overrideAnchor = overrideAnchor
}
}

private func handleFlowLinkOpacity() {
if isShowing == true, buttonPressed {
isShowing = false
Expand All @@ -450,6 +458,17 @@ public struct FlowLink<Label>: View where Label: View {
}}
}
}

private func initSnapshots() {
Task {
// Prevent Snapshot from being taken too early before Fetchable content loads
try? await Task.sleep(10000)
let lightImage = createSnapshot(colorScheme: .light)
let darkImage = createSnapshot(colorScheme: .dark)
snapshots[.light] = lightImage
snapshots[.dark] = darkImage
}
}
}

private struct IgnoreAnimationModifier: ViewModifier {
Expand Down
16 changes: 14 additions & 2 deletions Sources/FlowStack/FlowPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct PathContext: Equatable, Hashable {
var overrideAnchor: Anchor<CGRect>?

var snapshot: UIImage?
var snapshotDict: [ColorScheme: UIImage] = [:]
var linkDepth: Int = 0

var cornerRadius: CGFloat = 0
Expand All @@ -49,13 +50,11 @@ struct FlowElement: Equatable, Hashable {
static func == (lhs: FlowElement, rhs: FlowElement) -> Bool {
lhs.value.hashValue == rhs.value.hashValue &&
_mangledTypeName(type(of: lhs.value)) == _mangledTypeName(type(of: rhs.value)) &&
lhs.context == rhs.context &&
lhs.index == rhs.index
}

func hash(into hasher: inout Hasher) {
hasher.combine(_mangledTypeName(type(of: value)))
hasher.combine(context)
hasher.combine(index)
}
}
Expand Down Expand Up @@ -99,6 +98,19 @@ public struct FlowPath: Equatable, Hashable {
public mutating func append<P>(_ newElement: P) where P: Hashable {
self.append(newElement, context: nil)
}

/// Adds a method to tell flow path to use the correct snapshot for the currently set colorScheme
/// - Parameters:
/// - colorScheme: The new color scheme to be used for snapshots
public mutating func updateSnapshots(from colorScheme: ColorScheme) {
for i in elements.indices {
guard var context = elements[i].context else { continue }
if let newSnapshot = context.snapshotDict[colorScheme] {
context.snapshot = newSnapshot
elements[i].context?.snapshot = context.snapshot
}
}
}
}

struct FlowPathKey: EnvironmentKey {
Expand Down