Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification #36

Merged
merged 2 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Example/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class ViewController: UIViewController {
}

var orderedDictionary: OrderedDictionary<String, Any> = .init()

private let notificationCenter = NotificationCenter.default
private var notificationObservations = [Any]()

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions Sources/AppContainer/AppContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -101,6 +103,8 @@ public class AppContainer {
return
}

notificationCenter.post(name: Self.containerWillChangeNotification, object: nil)

try exportUserDefaults()
exportCookies()

Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions Sources/AppContainer/Notification.swift
Original file line number Diff line number Diff line change
@@ -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")
}