From 32e27af1a7e651da94f7d7b06142608abfe3a4b7 Mon Sep 17 00:00:00 2001 From: Laurent Date: Tue, 6 Feb 2024 15:53:35 +0100 Subject: [PATCH] Makes collections update work! --- PadelClub/ContentView.swift | 9 ++++++--- PadelClub/Data/Club.swift | 1 + PadelClub/Data/DataStore.swift | 15 ++++++++++----- PadelClub/Data/Tournament.swift | 1 + 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/PadelClub/ContentView.swift b/PadelClub/ContentView.swift index abe1cad..76b4231 100644 --- a/PadelClub/ContentView.swift +++ b/PadelClub/ContentView.swift @@ -12,22 +12,25 @@ struct ContentView: View { @StateObject var dataStore = DataStore() + @State var uuid = UUID() + var body: some View { VStack { + Image(systemName: "globe") .imageScale(.large) .foregroundStyle(.tint) Text("Hello, world!") - List(self.dataStore.clubs.items) { club in - Text(club.name).id(club.id) + List(self.dataStore.clubs) { club in + Text(club.name) } Button("add") { self._add() }.padding() .buttonStyle(.bordered) - } + }.id(uuid) } func _add() { diff --git a/PadelClub/Data/Club.swift b/PadelClub/Data/Club.swift index bc0d334..436cb37 100644 --- a/PadelClub/Data/Club.swift +++ b/PadelClub/Data/Club.swift @@ -12,6 +12,7 @@ class Club : ModelObject, Storable { static func resourceName() -> String { return "clubs" } + var id: String = Store.randomId() var name: String init(name: String) { diff --git a/PadelClub/Data/DataStore.swift b/PadelClub/Data/DataStore.swift index 368c0c6..57c0214 100644 --- a/PadelClub/Data/DataStore.swift +++ b/PadelClub/Data/DataStore.swift @@ -9,22 +9,27 @@ import Foundation import LeStorage import SwiftUI -class DataStore : ObservableObject { +class DataStore: ObservableObject { // fileprivate var _store: Store - @Published fileprivate(set) var tournaments: StoredCollection - @Published fileprivate(set) var clubs: StoredCollection + fileprivate(set) var tournaments: StoredCollection + fileprivate(set) var clubs: StoredCollection init() { let store = Store.main store.synchronizationApiURL = "http://127.0.0.1:8000/api/" -// let store = Store(synchronizationApiURL: "https://padelclub.app/api/") self.clubs = store.registerCollection(synchronized: true) self.tournaments = store.registerCollection(synchronized: false) -// self._store = store + NotificationCenter.default.addObserver(self, selector: #selector(collectionWasUpdated), name: NSNotification.Name.CollectionDidLoad, object: nil) + NotificationCenter.default.addObserver(self, selector: #selector(collectionWasUpdated), name: NSNotification.Name.CollectionDidChange, object: nil) + + } + + @objc func collectionWasUpdated(notification: Notification) { + self.objectWillChange.send() } } diff --git a/PadelClub/Data/Tournament.swift b/PadelClub/Data/Tournament.swift index 93569cf..4a8514d 100644 --- a/PadelClub/Data/Tournament.swift +++ b/PadelClub/Data/Tournament.swift @@ -11,6 +11,7 @@ import LeStorage class Tournament : ModelObject, Storable { static func resourceName() -> String { "tournaments" } + var id: String = Store.randomId() var name: String var club_id: String