Redo ReloadingContent with @ObservedObject
Proper way to listen to changes in elements is observing their parent.
This commit is contained in:
parent
923ea923ab
commit
17a383bc9c
|
@ -25,10 +25,16 @@
|
||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct ReloadingContent<T: Equatable, Content: View>: View {
|
struct ReloadingContent<O: ObservableObject, T: Equatable, Content: View>: View {
|
||||||
@Environment(\.scenePhase) private var scenePhase
|
@Environment(\.scenePhase) private var scenePhase
|
||||||
|
|
||||||
private let elements: [T]
|
@ObservedObject private var object: O
|
||||||
|
|
||||||
|
private let keyPath: KeyPath<O, [T]>
|
||||||
|
|
||||||
|
private var elements: [T] {
|
||||||
|
object[keyPath: keyPath]
|
||||||
|
}
|
||||||
|
|
||||||
private let equality: ([T], [T]) -> Bool
|
private let equality: ([T], [T]) -> Bool
|
||||||
|
|
||||||
|
@ -39,12 +45,14 @@ struct ReloadingContent<T: Equatable, Content: View>: View {
|
||||||
@State private var localElements: [T] = []
|
@State private var localElements: [T] = []
|
||||||
|
|
||||||
init(
|
init(
|
||||||
observing elements: [T],
|
observing object: O,
|
||||||
|
on keyPath: KeyPath<O, [T]>,
|
||||||
equality: @escaping ([T], [T]) -> Bool = { $0 == $1 },
|
equality: @escaping ([T], [T]) -> Bool = { $0 == $1 },
|
||||||
reload: (() -> Void)? = nil,
|
reload: (() -> Void)? = nil,
|
||||||
@ViewBuilder content: @escaping ([T]) -> Content
|
@ViewBuilder content: @escaping ([T]) -> Content
|
||||||
) {
|
) {
|
||||||
self.elements = elements
|
self.object = object
|
||||||
|
self.keyPath = keyPath
|
||||||
self.equality = equality
|
self.equality = equality
|
||||||
self.reload = reload
|
self.reload = reload
|
||||||
self.content = content
|
self.content = content
|
||||||
|
@ -61,7 +69,8 @@ struct ReloadingContent<T: Equatable, Content: View>: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
Group {
|
debugChanges()
|
||||||
|
return Group {
|
||||||
content(localElements)
|
content(localElements)
|
||||||
// }.onAppear {
|
// }.onAppear {
|
||||||
// localElements = elements
|
// localElements = elements
|
||||||
|
|
|
@ -90,7 +90,8 @@ struct DonateView: View {
|
||||||
.xxxThemeTruncation()
|
.xxxThemeTruncation()
|
||||||
) {
|
) {
|
||||||
ReloadingContent(
|
ReloadingContent(
|
||||||
observing: productManager.donations,
|
observing: productManager,
|
||||||
|
on: \.donations,
|
||||||
equality: {
|
equality: {
|
||||||
Set($0.map(\.productIdentifier)) == Set($1.map(\.productIdentifier))
|
Set($0.map(\.productIdentifier)) == Set($1.map(\.productIdentifier))
|
||||||
},
|
},
|
||||||
|
|
|
@ -54,7 +54,8 @@ extension OrganizerView {
|
||||||
var body: some View {
|
var body: some View {
|
||||||
debugChanges()
|
debugChanges()
|
||||||
return ReloadingContent(
|
return ReloadingContent(
|
||||||
observing: profileManager.headers,
|
observing: profileManager,
|
||||||
|
on: \.headers,
|
||||||
equality: {
|
equality: {
|
||||||
Set($0) == Set($1)
|
Set($0) == Set($1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,7 +105,8 @@ extension PaywallView {
|
||||||
footer: Text(L10n.Paywall.Sections.Products.footer)
|
footer: Text(L10n.Paywall.Sections.Products.footer)
|
||||||
) {
|
) {
|
||||||
ReloadingContent(
|
ReloadingContent(
|
||||||
observing: productManager.products,
|
observing: productManager,
|
||||||
|
on: \.products,
|
||||||
equality: {
|
equality: {
|
||||||
Set($0.map(\.productIdentifier)) == Set($1.map(\.productIdentifier))
|
Set($0.map(\.productIdentifier)) == Set($1.map(\.productIdentifier))
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue