Makes collections update work!

multistore
Laurent 2 years ago
parent 355a068fcb
commit 32e27af1a7
  1. 9
      PadelClub/ContentView.swift
  2. 1
      PadelClub/Data/Club.swift
  3. 15
      PadelClub/Data/DataStore.swift
  4. 1
      PadelClub/Data/Tournament.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() {

@ -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) {

@ -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<Tournament>
@Published fileprivate(set) var clubs: StoredCollection<Club>
fileprivate(set) var tournaments: StoredCollection<Tournament>
fileprivate(set) var clubs: StoredCollection<Club>
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()
}
}

@ -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

Loading…
Cancel
Save