Skip to content

Commit 4ec527f

Browse files
authored
Revert "Snapshot Theme Toggling (#38)"
This reverts commit e9c3462.
1 parent e9c3462 commit 4ec527f

2 files changed

Lines changed: 21 additions & 52 deletions

File tree

Sources/FlowStack/FlowLink.swift

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,13 @@ public struct FlowLink<Label>: View where Label: View {
246246
private var value: (any (Equatable & Hashable))?
247247
private var configuration: Configuration
248248

249+
@Environment(\.self) private var capturedEnvironment
250+
249251
@Environment(\.flowPath) private var path
250252
@Environment(\.flowDepth) private var flowDepth
251253
@Environment(\.flowTransaction) private var transaction
252254
@Environment(\.flowAnimationDuration) private var flowDuration
253255

254-
@Environment(\.colorScheme) private var colorScheme
255-
@Environment(\.self) private var fetchedEnvironment
256-
257256
@State private var overrideAnchor: Anchor<CGRect>?
258257

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

265-
@State var environmentList: [EnvironmentValues] = []
266-
@State private var snapshots: [ColorScheme: UIImage] = [:]
267-
@State private var environment = EnvironmentValues()
268-
@State private var refreshButton = UUID()
269-
270264
/// Creates a flow link that presents the view corresponding to a value.
271265
///
272266
/// When someone activates the flow link that this initializer
@@ -302,23 +296,29 @@ public struct FlowLink<Label>: View where Label: View {
302296
return path?.wrappedValue.elements.map(\.context?.linkDepth).contains(flowDepth) ?? false
303297
}
304298

305-
private func createSnapshot(colorScheme: ColorScheme) -> UIImage? {
299+
@State private var snapshot: UIImage?
300+
301+
@MainActor
302+
private func updateSnapshot() -> UIImage? {
303+
guard snapshot == nil else { return snapshot }
304+
306305
guard let size = size else { return nil }
307306

308307
let frame = CGRect(origin: .zero, size: size)
309-
environment = fetchedEnvironment
310-
environment.colorScheme = colorScheme
311308

312309
let controller = UIHostingController(
313310
rootView: label()
314-
.environment(\.self, environment)
311+
.transformEnvironment(\.self) { environment in
312+
environment = capturedEnvironment
313+
}
315314
.environment(\.opacityTransitionPercent, 1)
316315
.ignoresSafeArea()
317316
)
318-
319317
let view = controller.view
320318

321-
guard let view = view else { return nil}
319+
guard let view = view else {
320+
return nil
321+
}
322322

323323
view.bounds = CGRect(origin: .zero, size: size)
324324
view.backgroundColor = .clear
@@ -370,7 +370,7 @@ public struct FlowLink<Label>: View where Label: View {
370370
}
371371
Task {
372372
if configuration.transitionFromSnapshot {
373-
context?.snapshot = snapshots[colorScheme]
373+
context?.snapshot = await updateSnapshot()
374374
}
375375
if let value = value {
376376
withTransaction(transaction) {
@@ -379,8 +379,8 @@ public struct FlowLink<Label>: View where Label: View {
379379
}
380380
}
381381
}
382-
.id(refreshButton)
383382
}
383+
384384
public var body: some View {
385385
Group {
386386
if isContainedInPath && configuration.animateFromAnchor {
@@ -390,8 +390,8 @@ public struct FlowLink<Label>: View where Label: View {
390390
if configuration.animateFromAnchor && overrideAnchor == nil {
391391
button
392392
.opacity(isShowing ? 1.0 : 0.0)
393-
/// (Workaround) Override an animation with an animation that does nothing
394-
/// Leaving a flowlayer too early can cause an un-wanted animation
393+
/// (Workaround) Override an animation with an animation that does nothing
394+
/// Leaving a flowlayer too early can cause an un-wanted animation
395395
.ignoreAnimation()
396396
} else if configuration.animateFromAnchor {
397397
button
@@ -401,12 +401,6 @@ public struct FlowLink<Label>: View where Label: View {
401401
}
402402
}
403403
}
404-
.onChange(of: colorScheme) { newScheme in
405-
refreshButton = UUID()
406-
snapshots[newScheme]
407-
path?.wrappedValue.updateSnapshots(from: newScheme)
408-
}
409-
.onAppear { initSnapshots() }
410404
.background(
411405
GeometryReader { proxy in
412406
Color.clear
@@ -427,8 +421,7 @@ public struct FlowLink<Label>: View where Label: View {
427421
return PathContext(
428422
anchor: configuration.animateFromAnchor ? anchor : nil,
429423
overrideAnchor: configuration.animateFromAnchor ? overrideAnchor : nil,
430-
snapshot: configuration.animateFromAnchor && configuration.transitionFromSnapshot ? snapshots[colorScheme] : nil,
431-
snapshotDict: snapshots,
424+
snapshot: configuration.animateFromAnchor && configuration.transitionFromSnapshot ? snapshot : nil,
432425
linkDepth: flowDepth,
433426
cornerRadius: configuration.cornerRadius,
434427
cornerStyle: configuration.cornerStyle,
@@ -447,7 +440,6 @@ public struct FlowLink<Label>: View where Label: View {
447440
context?.overrideAnchor = overrideAnchor
448441
}
449442
}
450-
451443
private func handleFlowLinkOpacity() {
452444
if isShowing == true, buttonPressed {
453445
isShowing = false
@@ -458,17 +450,6 @@ public struct FlowLink<Label>: View where Label: View {
458450
}}
459451
}
460452
}
461-
462-
private func initSnapshots() {
463-
Task {
464-
// Prevent Snapshot from being taken too early before Fetchable content loads
465-
try? await Task.sleep(10000)
466-
let lightImage = createSnapshot(colorScheme: .light)
467-
let darkImage = createSnapshot(colorScheme: .dark)
468-
snapshots[.light] = lightImage
469-
snapshots[.dark] = darkImage
470-
}
471-
}
472453
}
473454

474455
private struct IgnoreAnimationModifier: ViewModifier {

Sources/FlowStack/FlowPath.swift

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ struct PathContext: Equatable, Hashable {
2828
var overrideAnchor: Anchor<CGRect>?
2929

3030
var snapshot: UIImage?
31-
var snapshotDict: [ColorScheme: UIImage] = [:]
3231
var linkDepth: Int = 0
3332

3433
var cornerRadius: CGFloat = 0
@@ -50,11 +49,13 @@ struct FlowElement: Equatable, Hashable {
5049
static func == (lhs: FlowElement, rhs: FlowElement) -> Bool {
5150
lhs.value.hashValue == rhs.value.hashValue &&
5251
_mangledTypeName(type(of: lhs.value)) == _mangledTypeName(type(of: rhs.value)) &&
52+
lhs.context == rhs.context &&
5353
lhs.index == rhs.index
5454
}
5555

5656
func hash(into hasher: inout Hasher) {
5757
hasher.combine(_mangledTypeName(type(of: value)))
58+
hasher.combine(context)
5859
hasher.combine(index)
5960
}
6061
}
@@ -98,19 +99,6 @@ public struct FlowPath: Equatable, Hashable {
9899
public mutating func append<P>(_ newElement: P) where P: Hashable {
99100
self.append(newElement, context: nil)
100101
}
101-
102-
/// Adds a method to tell flow path to use the correct snapshot for the currently set colorScheme
103-
/// - Parameters:
104-
/// - colorScheme: The new color scheme to be used for snapshots
105-
public mutating func updateSnapshots(from colorScheme: ColorScheme) {
106-
for i in elements.indices {
107-
guard var context = elements[i].context else { continue }
108-
if let newSnapshot = context.snapshotDict[colorScheme] {
109-
context.snapshot = newSnapshot
110-
elements[i].context?.snapshot = context.snapshot
111-
}
112-
}
113-
}
114102
}
115103

116104
struct FlowPathKey: EnvironmentKey {

0 commit comments

Comments
 (0)