diff --git a/ntfy/Persistence/Store.swift b/ntfy/Persistence/Store.swift index 8c4c74b..63d01e2 100644 --- a/ntfy/Persistence/Store.swift +++ b/ntfy/Persistence/Store.swift @@ -12,10 +12,11 @@ class Store: ObservableObject { } private var subscriptions: Set = [] - init() { - let directory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.io.heckel.ntfy")! - let storeUrl = directory.appendingPathComponent("ntfy.sqlite") - let description = NSPersistentStoreDescription(url: storeUrl) + init(inMemory: Bool = false) { + let storeUrl = (inMemory) ? URL(fileURLWithPath: "/dev/null") : FileManager.default + .containerURL(forSecurityApplicationGroupIdentifier: "group.io.heckel.ntfy")! + .appendingPathComponent("ntfy.sqlite") + let description = NSPersistentStoreDescription(url: storeUrl) description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) // Set up container and observe changes from app extension @@ -154,3 +155,47 @@ class Store: ObservableObject { context.refreshAllObjects() } } + + +extension Store { + private static let topics = [ + "stats": [ + Message(id: "1", time: 1653048956, message: "200 users/h\n123 IPs", title: nil), + Message(id: "2", time: 1653058956, message: "201 users/h\n80 IPs", title: nil) + ], + "backups": [], + "announcements": [], + "alerts": [], + "plaground": [] + ] + + static var preview: Store = { + let store = Store(inMemory: true) + store.context.perform { + topics.forEach { topic, messages in + let notifications = messages.map { store.makeNotification(store.context, $0) } + store.makeSubscription(store.context, topic, notifications) + } + } + return store + }() + + @discardableResult + func makeSubscription(_ context: NSManagedObjectContext, _ topic: String, _ notifications: [Notification]) -> Subscription { + let subscription = Subscription(context: context) + subscription.baseUrl = appBaseUrl + subscription.topic = topic + subscription.notifications = NSSet(array: notifications) + return subscription + } + + @discardableResult + func makeNotification(_ context: NSManagedObjectContext, _ message: Message) -> Notification { + let notification = Notification(context: context) + notification.id = message.id + notification.time = message.time + notification.message = message.message + notification.title = message.title + return notification + } +} diff --git a/ntfy/Views/SubscriptionsListView.swift b/ntfy/Views/SubscriptionsListView.swift index f38e591..70e710e 100644 --- a/ntfy/Views/SubscriptionsListView.swift +++ b/ntfy/Views/SubscriptionsListView.swift @@ -62,13 +62,11 @@ struct SubscriptionListView: View { } } -/* + struct SubscriptionsList_Previews: PreviewProvider { - static var previews: some View { - SubscriptionsList( - subscriptions: NtfySubscriptionList, - currentView: (.subscriptionList) - ) - } + static var previews: some View { + SubscriptionListView() + .environment(\.managedObjectContext, Store.preview.context) + .environmentObject(Store.preview) + } } -*/