From 46c8a2d99a7c92363a63b1f8b89cabe591371fa6 Mon Sep 17 00:00:00 2001 From: ohaiibuzzle <23693150+ohaiibuzzle@users.noreply.github.com> Date: Sat, 13 Jan 2024 21:51:54 +0900 Subject: [PATCH] feat: remove USBDetectorStatus as a separate Observable --- USBNotifier.xcodeproj/project.pbxproj | 4 --- USBNotifier/Core/USBDetector.swift | 32 ++++++++++++++++++--- USBNotifier/Resources/Localizable.xcstrings | 10 ------- USBNotifier/USBNotifierApp.swift | 2 +- USBNotifier/Values/USBDetectorStatus.swift | 14 --------- USBNotifier/Views/MenuBarView.swift | 15 ++++------ 6 files changed, 34 insertions(+), 43 deletions(-) delete mode 100644 USBNotifier/Values/USBDetectorStatus.swift diff --git a/USBNotifier.xcodeproj/project.pbxproj b/USBNotifier.xcodeproj/project.pbxproj index be4e4dd..a31fe42 100644 --- a/USBNotifier.xcodeproj/project.pbxproj +++ b/USBNotifier.xcodeproj/project.pbxproj @@ -13,7 +13,6 @@ ABA6E31A2B50EA9E0054F69E /* MenuBarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA6E3192B50EA9E0054F69E /* MenuBarView.swift */; }; ABA6E31E2B50EB500054F69E /* USBDetector.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA6E31D2B50EB500054F69E /* USBDetector.swift */; }; ABA6E3202B50ECE70054F69E /* Storage.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA6E31F2B50ECE70054F69E /* Storage.swift */; }; - ABA6E3242B5113FA0054F69E /* USBDetectorStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABA6E3232B5113FA0054F69E /* USBDetectorStatus.swift */; }; ABA6E3282B511C090054F69E /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = ABA6E3272B511C090054F69E /* Localizable.xcstrings */; }; ABF7E1A22B51501C0036DDC3 /* USBControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABF7E1A12B51501C0036DDC3 /* USBControl.swift */; }; ABF7E1A52B5152C90036DDC3 /* Dialogs.swift in Sources */ = {isa = PBXBuildFile; fileRef = ABF7E1A42B5152C90036DDC3 /* Dialogs.swift */; }; @@ -29,7 +28,6 @@ ABA6E3192B50EA9E0054F69E /* MenuBarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuBarView.swift; sourceTree = ""; }; ABA6E31D2B50EB500054F69E /* USBDetector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = USBDetector.swift; sourceTree = ""; }; ABA6E31F2B50ECE70054F69E /* Storage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Storage.swift; sourceTree = ""; }; - ABA6E3232B5113FA0054F69E /* USBDetectorStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = USBDetectorStatus.swift; sourceTree = ""; }; ABA6E3272B511C090054F69E /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = ""; }; ABF7E1A12B51501C0036DDC3 /* USBControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = USBControl.swift; sourceTree = ""; }; ABF7E1A42B5152C90036DDC3 /* Dialogs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dialogs.swift; sourceTree = ""; }; @@ -108,7 +106,6 @@ isa = PBXGroup; children = ( ABA6E31F2B50ECE70054F69E /* Storage.swift */, - ABA6E3232B5113FA0054F69E /* USBDetectorStatus.swift */, ); path = Values; sourceTree = ""; @@ -217,7 +214,6 @@ files = ( ABA6E30B2B50EA460054F69E /* USBNotifierApp.swift in Sources */, ABF7E1A52B5152C90036DDC3 /* Dialogs.swift in Sources */, - ABA6E3242B5113FA0054F69E /* USBDetectorStatus.swift in Sources */, ABF7E1A72B5159620036DDC3 /* LaunchAtStartup.swift in Sources */, ABF7E1A22B51501C0036DDC3 /* USBControl.swift in Sources */, ABA6E3202B50ECE70054F69E /* Storage.swift in Sources */, diff --git a/USBNotifier/Core/USBDetector.swift b/USBNotifier/Core/USBDetector.swift index 6393ec1..f929269 100644 --- a/USBNotifier/Core/USBDetector.swift +++ b/USBNotifier/Core/USBDetector.swift @@ -27,6 +27,10 @@ enum USBConnectionStatus { class USBDetector { static var shared = USBDetector() + fileprivate let observable = Observable() + + private var isRunning = false + private func unpackDevicesFromIterator(iterator: io_iterator_t) -> [USBDevice] { var devices = [USBDevice]() @@ -202,7 +206,7 @@ class USBDetector { private var notificationPort: IONotificationPortRef? - func startDetection() { + private func startDetection() { notificationPort = IONotificationPortCreate(kIOMainPortDefault) let runLoopSource = IONotificationPortGetRunLoopSource(notificationPort).takeUnretainedValue() @@ -238,19 +242,21 @@ class USBDetector { // Clear the initial devices. _ = unpackDevicesFromIterator(iterator: newDevicesIterator) _ = unpackDevicesFromIterator(iterator: removedDevicesIterator) - USBDetectorStatus.shared.isRunning = true + self.isRunning = true } else { stopDetection() } } - func stopDetection() { + private func stopDetection() { if let notificationPort = notificationPort { let runLoopSource = IONotificationPortGetRunLoopSource(notificationPort).takeUnretainedValue() CFRunLoopRemoveSource(CFRunLoopGetCurrent(), runLoopSource, CFRunLoopMode.defaultMode) IONotificationPortDestroy(notificationPort) } + notificationPort = nil + if newDevicesIterator != 0 { _ = unpackDevicesFromIterator(iterator: newDevicesIterator) IOObjectRelease(newDevicesIterator) @@ -263,10 +269,28 @@ class USBDetector { removedDevicesIterator = IO_OBJECT_NULL } - USBDetectorStatus.shared.isRunning = false + self.isRunning = false } deinit { stopDetection() } } + +extension USBDetector { + final class Observable: ObservableObject { + var status: Bool { + get { + return USBDetector.shared.isRunning + } + set { + if newValue { + USBDetector.shared.startDetection() + } else { + USBDetector.shared.stopDetection() + } + objectWillChange.send() + } + } + } +} diff --git a/USBNotifier/Resources/Localizable.xcstrings b/USBNotifier/Resources/Localizable.xcstrings index cfda39b..c3ad387 100644 --- a/USBNotifier/Resources/Localizable.xcstrings +++ b/USBNotifier/Resources/Localizable.xcstrings @@ -81,16 +81,6 @@ } } }, - "plural.ext" : { - "localizations" : { - "en" : { - "stringUnit" : { - "state" : "translated", - "value" : "s" - } - } - } - }, "settings" : { "localizations" : { "en" : { diff --git a/USBNotifier/USBNotifierApp.swift b/USBNotifier/USBNotifierApp.swift index fb4ce91..0a516eb 100644 --- a/USBNotifier/USBNotifierApp.swift +++ b/USBNotifier/USBNotifierApp.swift @@ -37,7 +37,7 @@ struct USBNotifierApp: App { } } - USBDetector.shared.startDetection() + USBDetector.Observable().status = true } var body: some Scene { diff --git a/USBNotifier/Values/USBDetectorStatus.swift b/USBNotifier/Values/USBDetectorStatus.swift deleted file mode 100644 index 93e2b63..0000000 --- a/USBNotifier/Values/USBDetectorStatus.swift +++ /dev/null @@ -1,14 +0,0 @@ -// -// USBDetectorStatus.swift -// USBNotifier -// -// Created by Venti on 12/01/2024. -// - -import Foundation - -class USBDetectorStatus: ObservableObject { - static var shared = USBDetectorStatus() - - @Published var isRunning: Bool = false -} diff --git a/USBNotifier/Views/MenuBarView.swift b/USBNotifier/Views/MenuBarView.swift index 82b6f1c..1917599 100644 --- a/USBNotifier/Views/MenuBarView.swift +++ b/USBNotifier/Views/MenuBarView.swift @@ -12,23 +12,23 @@ struct MenuBarView: View { @ObservedObject var autostart = LaunchAtStartup.Observable() @State private var possibleDetectionDelays = [1, 5, 10, 60] - @ObservedObject private var detectorStatus = USBDetectorStatus.shared + @ObservedObject private var detectorStatus = USBDetector.Observable() var body: some View { VStack { Text(String(localized: "usb.service.status:") + " " + - (detectorStatus.isRunning ? String(localized: "usb.service.running") + (detectorStatus.status ? String(localized: "usb.service.running") : String(localized: "usb.service.paused"))) Divider() - if !detectorStatus.isRunning { + if !detectorStatus.status { Button("usb.service.start") { - USBDetector.shared.startDetection() + detectorStatus.status = true } } else { Button("usb.service.pause") { - USBDetector.shared.stopDetection() + detectorStatus.status = false } } @@ -56,11 +56,6 @@ struct MenuBarView: View { } } } - - func restartDetection() { - USBDetector.shared.stopDetection() - USBDetector.shared.startDetection() - } } #Preview {