diff --git a/Example/Example/ViewController.swift b/Example/Example/ViewController.swift index b399e90..74583d5 100644 --- a/Example/Example/ViewController.swift +++ b/Example/Example/ViewController.swift @@ -27,6 +27,9 @@ class ViewController: UIViewController { } var orderedDictionary: OrderedDictionary = .init() + + private let notificationCenter = NotificationCenter.default + private var notificationObservations = [Any]() override func viewDidLoad() { super.viewDidLoad() @@ -40,11 +43,14 @@ class ViewController: UIViewController { super.viewWillAppear(animated) title = appContainer.activeContainer?.name + + registerNotifications() } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) + unregisterNotifications() } private func setupViews() { @@ -273,6 +279,26 @@ extension ViewController: UITableViewDelegate { } } +extension ViewController { + func registerNotifications() { + let willChangeObservation = notificationCenter.addObserver(forName: AppContainer.containerWillChangeNotification, object: nil, queue: .current) { _ in + print("container will change") + } + + let didChangeObservation = notificationCenter.addObserver(forName: AppContainer.containerWillChangeNotification, object: nil, queue: .current) { _ in + print("container did change") + } + + self.notificationObservations = [willChangeObservation, didChangeObservation] + } + + func unregisterNotifications() { + notificationObservations.forEach { + notificationCenter.removeObserver($0) + } + } +} + extension ViewController { class View: UIView { diff --git a/Sources/AppContainer/AppContainer.swift b/Sources/AppContainer/AppContainer.swift index 1393cc7..d616e61 100644 --- a/Sources/AppContainer/AppContainer.swift +++ b/Sources/AppContainer/AppContainer.swift @@ -8,6 +8,8 @@ public class AppContainer { private let fileManager = FileManager.default + private let notificationCenter = NotificationCenter.default + /// home directory url private lazy var homeDirectoryUrl: URL = { URL(fileURLWithPath: NSHomeDirectory()) @@ -101,6 +103,8 @@ public class AppContainer { return } + notificationCenter.post(name: Self.containerWillChangeNotification, object: nil) + try exportUserDefaults() exportCookies() @@ -119,6 +123,8 @@ public class AppContainer { incrementActivatedCount(uuid: container.uuid) // update last activated date try? updateInfo(of: container, keyValue: .init(\.lastActivatedDate, Date())) + + notificationCenter.post(name: Self.containerDidChangeNotification, object: nil) } /// activate selected container diff --git a/Sources/AppContainer/Notification.swift b/Sources/AppContainer/Notification.swift new file mode 100644 index 0000000..468c17f --- /dev/null +++ b/Sources/AppContainer/Notification.swift @@ -0,0 +1,14 @@ +// +// AppContainer.Notification.swift +// +// +// Created by p-x9 on 2023/02/27. +// +// + +import Foundation + +extension AppContainer { + public static let containerWillChangeNotification = Notification.Name("com.p-x9.appcontainer.containerWillChange") + public static let containerDidChangeNotification = Notification.Name("com.p-x9.appcontainer.containerDidChange") +}