From 17a383bc9c8a3986e75da2eb4d63c9d7c503a2ba Mon Sep 17 00:00:00 2001 From: Davide De Rosa Date: Tue, 19 Apr 2022 19:33:40 +0200 Subject: [PATCH] Redo ReloadingContent with @ObservedObject Proper way to listen to changes in elements is observing their parent. --- .../App/Reusable/ReloadingContent.swift | 19 ++++++++++++++----- Passepartout/App/Views/DonateView.swift | 3 ++- .../App/Views/OrganizerView+Profiles.swift | 3 ++- .../App/Views/PaywallView+Purchase.swift | 3 ++- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/Passepartout/App/Reusable/ReloadingContent.swift b/Passepartout/App/Reusable/ReloadingContent.swift index e9b23d02..08093715 100644 --- a/Passepartout/App/Reusable/ReloadingContent.swift +++ b/Passepartout/App/Reusable/ReloadingContent.swift @@ -25,10 +25,16 @@ import SwiftUI -struct ReloadingContent: View { +struct ReloadingContent: View { @Environment(\.scenePhase) private var scenePhase - private let elements: [T] + @ObservedObject private var object: O + + private let keyPath: KeyPath + + private var elements: [T] { + object[keyPath: keyPath] + } private let equality: ([T], [T]) -> Bool @@ -39,12 +45,14 @@ struct ReloadingContent: View { @State private var localElements: [T] = [] init( - observing elements: [T], + observing object: O, + on keyPath: KeyPath, equality: @escaping ([T], [T]) -> Bool = { $0 == $1 }, reload: (() -> Void)? = nil, @ViewBuilder content: @escaping ([T]) -> Content ) { - self.elements = elements + self.object = object + self.keyPath = keyPath self.equality = equality self.reload = reload self.content = content @@ -61,7 +69,8 @@ struct ReloadingContent: View { } var body: some View { - Group { + debugChanges() + return Group { content(localElements) // }.onAppear { // localElements = elements diff --git a/Passepartout/App/Views/DonateView.swift b/Passepartout/App/Views/DonateView.swift index 8ac16349..4511459c 100644 --- a/Passepartout/App/Views/DonateView.swift +++ b/Passepartout/App/Views/DonateView.swift @@ -90,7 +90,8 @@ struct DonateView: View { .xxxThemeTruncation() ) { ReloadingContent( - observing: productManager.donations, + observing: productManager, + on: \.donations, equality: { Set($0.map(\.productIdentifier)) == Set($1.map(\.productIdentifier)) }, diff --git a/Passepartout/App/Views/OrganizerView+Profiles.swift b/Passepartout/App/Views/OrganizerView+Profiles.swift index f1f11c06..41ca6d66 100644 --- a/Passepartout/App/Views/OrganizerView+Profiles.swift +++ b/Passepartout/App/Views/OrganizerView+Profiles.swift @@ -54,7 +54,8 @@ extension OrganizerView { var body: some View { debugChanges() return ReloadingContent( - observing: profileManager.headers, + observing: profileManager, + on: \.headers, equality: { Set($0) == Set($1) } diff --git a/Passepartout/App/Views/PaywallView+Purchase.swift b/Passepartout/App/Views/PaywallView+Purchase.swift index 118c6efa..952d4a50 100644 --- a/Passepartout/App/Views/PaywallView+Purchase.swift +++ b/Passepartout/App/Views/PaywallView+Purchase.swift @@ -105,7 +105,8 @@ extension PaywallView { footer: Text(L10n.Paywall.Sections.Products.footer) ) { ReloadingContent( - observing: productManager.products, + observing: productManager, + on: \.products, equality: { Set($0.map(\.productIdentifier)) == Set($1.map(\.productIdentifier)) },