@@ -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
474455private struct IgnoreAnimationModifier : ViewModifier {
0 commit comments