|
|
|
|
@ -7,6 +7,8 @@ |
|
|
|
|
|
|
|
|
|
import SwiftUI |
|
|
|
|
import CoreData |
|
|
|
|
import Combine |
|
|
|
|
import CloudKit |
|
|
|
|
|
|
|
|
|
class BoringContext : ObservableObject { |
|
|
|
|
|
|
|
|
|
@ -64,6 +66,11 @@ struct ContentView<T : AbstractTimer>: View { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var coreDataPublisher: NotificationCenter.Publisher { NotificationCenter.default |
|
|
|
|
.publisher(for: .NSManagedObjectContextDidSave, object: viewContext) } |
|
|
|
|
var cloudkitPublisher: NotificationCenter.Publisher { NotificationCenter.default |
|
|
|
|
.publisher(for: Notification.Name(rawValue: "NSPersistentStoreRemoteChangeNotificationOptionKey"), object: viewContext) } |
|
|
|
|
|
|
|
|
|
@State private var isEditing: Bool = false { |
|
|
|
|
didSet { |
|
|
|
|
if self.isEditing == false { |
|
|
|
|
@ -80,10 +87,6 @@ struct ContentView<T : AbstractTimer>: View { |
|
|
|
|
GridItem(spacing: 10.0), |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
var timersArray: [T] { |
|
|
|
|
return Array(timers) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var body: some View { |
|
|
|
|
|
|
|
|
|
GeometryReader { reader in |
|
|
|
|
@ -158,11 +161,6 @@ struct ContentView<T : AbstractTimer>: View { |
|
|
|
|
.sheet(isPresented: $boringContext.isShowingNewData, content: { |
|
|
|
|
self._newView(isPresented: $boringContext.isShowingNewData) |
|
|
|
|
.environment(\.managedObjectContext, viewContext) |
|
|
|
|
.onDisappear { |
|
|
|
|
withAnimation { |
|
|
|
|
self._buildItemsList() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
.toolbar { |
|
|
|
|
ToolbarItem(placement: .navigationBarTrailing) { |
|
|
|
|
@ -184,6 +182,17 @@ struct ContentView<T : AbstractTimer>: View { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// .onReceive(cloudkitPublisher, perform: { _ in |
|
|
|
|
// print(">>>cloudkitPublisher") |
|
|
|
|
// withAnimation { |
|
|
|
|
// self._buildItemsList() |
|
|
|
|
// } |
|
|
|
|
// }) |
|
|
|
|
.onReceive(coreDataPublisher, perform: { _ in |
|
|
|
|
withAnimation { |
|
|
|
|
self._buildItemsList() |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
.onAppear { |
|
|
|
|
self._buildItemsList() |
|
|
|
|
self._askPermissions() |
|
|
|
|
@ -246,18 +255,18 @@ struct ContentView<T : AbstractTimer>: View { |
|
|
|
|
self.model.spots = spots |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileprivate func _reorder(from: IndexSet, to: Int) { |
|
|
|
|
var timers: [AbstractTimer] = self.timersArray |
|
|
|
|
timers.move(fromOffsets: from, toOffset: to) |
|
|
|
|
for (i, countdown) in timers.enumerated() { |
|
|
|
|
countdown.order = Int16(i) |
|
|
|
|
} |
|
|
|
|
do { |
|
|
|
|
try viewContext.save() |
|
|
|
|
} catch { |
|
|
|
|
self.boringContext.error = error |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
// fileprivate func _reorder(from: IndexSet, to: Int) { |
|
|
|
|
// var timers: [AbstractTimer] = Array(self.timers) |
|
|
|
|
// timers.move(fromOffsets: from, toOffset: to) |
|
|
|
|
// for (i, countdown) in timers.enumerated() { |
|
|
|
|
// countdown.order = Int16(i) |
|
|
|
|
// } |
|
|
|
|
// do { |
|
|
|
|
// try viewContext.save() |
|
|
|
|
// } catch { |
|
|
|
|
// self.boringContext.error = error |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
fileprivate func _askPermissions() { |
|
|
|
|
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .criticalAlert]) { success, error in |
|
|
|
|
|