Skip to content

Commit

Permalink
feat: remove USBDetectorStatus as a separate Observable
Browse files Browse the repository at this point in the history
  • Loading branch information
ohaiibuzzle committed Jan 13, 2024
1 parent 93ef744 commit 46c8a2d
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 43 deletions.
4 changes: 0 additions & 4 deletions USBNotifier.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand All @@ -29,7 +28,6 @@
ABA6E3192B50EA9E0054F69E /* MenuBarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuBarView.swift; sourceTree = "<group>"; };
ABA6E31D2B50EB500054F69E /* USBDetector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = USBDetector.swift; sourceTree = "<group>"; };
ABA6E31F2B50ECE70054F69E /* Storage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Storage.swift; sourceTree = "<group>"; };
ABA6E3232B5113FA0054F69E /* USBDetectorStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = USBDetectorStatus.swift; sourceTree = "<group>"; };
ABA6E3272B511C090054F69E /* Localizable.xcstrings */ = {isa = PBXFileReference; lastKnownFileType = text.json.xcstrings; path = Localizable.xcstrings; sourceTree = "<group>"; };
ABF7E1A12B51501C0036DDC3 /* USBControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = USBControl.swift; sourceTree = "<group>"; };
ABF7E1A42B5152C90036DDC3 /* Dialogs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dialogs.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -108,7 +106,6 @@
isa = PBXGroup;
children = (
ABA6E31F2B50ECE70054F69E /* Storage.swift */,
ABA6E3232B5113FA0054F69E /* USBDetectorStatus.swift */,
);
path = Values;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
32 changes: 28 additions & 4 deletions USBNotifier/Core/USBDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]()

Expand Down Expand Up @@ -202,7 +206,7 @@ class USBDetector {

private var notificationPort: IONotificationPortRef?

func startDetection() {
private func startDetection() {
notificationPort = IONotificationPortCreate(kIOMainPortDefault)

let runLoopSource = IONotificationPortGetRunLoopSource(notificationPort).takeUnretainedValue()
Expand Down Expand Up @@ -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)
Expand All @@ -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()
}
}
}
}
10 changes: 0 additions & 10 deletions USBNotifier/Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,6 @@
}
}
},
"plural.ext" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "s"
}
}
}
},
"settings" : {
"localizations" : {
"en" : {
Expand Down
2 changes: 1 addition & 1 deletion USBNotifier/USBNotifierApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct USBNotifierApp: App {
}
}

USBDetector.shared.startDetection()
USBDetector.Observable().status = true
}

var body: some Scene {
Expand Down
14 changes: 0 additions & 14 deletions USBNotifier/Values/USBDetectorStatus.swift

This file was deleted.

15 changes: 5 additions & 10 deletions USBNotifier/Views/MenuBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -56,11 +56,6 @@ struct MenuBarView: View {
}
}
}

func restartDetection() {
USBDetector.shared.stopDetection()
USBDetector.shared.startDetection()
}
}

#Preview {
Expand Down

0 comments on commit 46c8a2d

Please sign in to comment.