Skip to content

Commit

Permalink
Merge pull request #36 from p-x9/feature/notification
Browse files Browse the repository at this point in the history
Notification
  • Loading branch information
p-x9 authored Mar 5, 2023
2 parents 9d89445 + 05d3ff0 commit a7045dd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
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")
}

0 comments on commit a7045dd

Please sign in to comment.