Cotton code
39 lines · 1.4 KB · SwiftRaw
// Re-reading a list when you come back to it.//// `.task` runs ONCE per view lifetime, and a `NavigationStack` POP is not a new// lifetime: the list view was only pushed over, so its `.task` never runs// again. Every screen in this app that pushes a detail which can WRITE has the// same defect without this — archive a place, tap Back, and it is still there;// mark a goal done, tap Back, and the line still says it is open. The previous// app had no equivalent because its query layer invalidated the list key on// every mutation, so the list was already stale-then-fresh by the time you// landed on it.//// `.onAppear` DOES fire again on a pop, which is the seam. The first appearance// is the `.task`'s, so it is skipped here; every one after it is a return.import SwiftUIextension View {    /// Run `reload` every time this view comes BACK on screen — never on its    /// first appearance, which its own `.task` already owns.    func reloadOnReturn(_ reload: @escaping () async -> Void) -> some View {        modifier(ReloadOnReturn(reload: reload))    }}private struct ReloadOnReturn: ViewModifier {    let reload: () async -> Void    @State private var appearedOnce = false    func body(content: Content) -> some View {        content.onAppear {            guard appearedOnce else {                appearedOnce = true                return            }            Task { await reload() }        }    }}