Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lgaches committed Aug 14, 2023
1 parent 586f140 commit 0977683
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# file options

--swiftversion 5.7
--swiftversion 5.8

# format options

--self insert
--self remove
--patternlet inline

# rules
2 changes: 1 addition & 1 deletion BeaconEmitter/BeaconEmitterView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ContentView.swift
// BeaconEmitterView.swift
// BeaconEmitter
//
// Created by Laurent Gaches.
Expand Down
72 changes: 36 additions & 36 deletions BeaconEmitter/BeaconEmitterViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,85 +43,85 @@ class BeaconEmitterViewModel: NSObject, ObservableObject {
override init() {
super.init()

self.loadSavedValue()
loadSavedValue()

self.emitter = CBPeripheralManager(delegate: self, queue: nil)
emitter = CBPeripheralManager(delegate: self, queue: nil)

self.majorMinorFormatter.allowsFloats = false
self.majorMinorFormatter.maximum = NSNumber(value: UInt16.max)
self.majorMinorFormatter.minimum = NSNumber(value: UInt16.min)
majorMinorFormatter.allowsFloats = false
majorMinorFormatter.maximum = NSNumber(value: UInt16.max)
majorMinorFormatter.minimum = NSNumber(value: UInt16.min)

self.powerFormatter.allowsFloats = false
self.powerFormatter.maximum = NSNumber(value: Int8.max)
self.powerFormatter.minimum = NSNumber(value: Int8.min)
powerFormatter.allowsFloats = false
powerFormatter.maximum = NSNumber(value: Int8.max)
powerFormatter.minimum = NSNumber(value: Int8.min)

NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(self.receiveSleepNotification), name: NSWorkspace.willSleepNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(self.receiveAwakeNotification), name: NSWorkspace.didWakeNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(receiveSleepNotification), name: NSWorkspace.willSleepNotification, object: nil)
NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(receiveAwakeNotification), name: NSWorkspace.didWakeNotification, object: nil)
}

func startStop() {
guard let emitter else { return }

if emitter.isAdvertising {
emitter.stopAdvertising()
self.isStarted = false
isStarted = false
} else {
if let proximityUUID = NSUUID(uuidString: self.uuid) {
let region = BeaconRegion(proximityUUID: proximityUUID, major: self.major, minor: self.minor)
if let proximityUUID = NSUUID(uuidString: uuid) {
let region = BeaconRegion(proximityUUID: proximityUUID, major: major, minor: minor)
emitter.startAdvertising(region.peripheralDataWithMeasuredPower())
} else {
self.status = "The UUID format is invalid"
status = "The UUID format is invalid"
}
}
}

func refreshUUID() {
self.uuid = UUID().uuidString
uuid = UUID().uuidString
}

func copyPaste() {
let pasteboard = NSPasteboard.general
pasteboard.clearContents()
pasteboard.setString(self.uuid, forType: .string)
pasteboard.setString(uuid, forType: .string)
}

@objc
func receiveSleepNotification(_: Notification) {
if let emitter, emitter.isAdvertising {
self.advertiseBeforeSleep = true
self.startStop()
advertiseBeforeSleep = true
startStop()
}
}

@objc
func receiveAwakeNotification(_: Notification) {
if self.advertiseBeforeSleep {
self.startStop()
if advertiseBeforeSleep {
startStop()
}
}

func save() {
self.savedUUID = self.uuid
self.savedMajor = Int(self.major)
self.savedMinor = Int(self.minor)
self.savedPower = Int(self.power)
savedUUID = uuid
savedMajor = Int(major)
savedMinor = Int(minor)
savedPower = Int(power)
}

private func loadSavedValue() {
if let savedUUID {
self.uuid = savedUUID
uuid = savedUUID
}

if let savedMajor {
self.major = UInt16(savedMajor)
major = UInt16(savedMajor)
}

if let savedMinor {
self.minor = UInt16(savedMinor)
minor = UInt16(savedMinor)
}

if let savedPower {
self.power = Int8(savedPower)
power = Int8(savedPower)
}
}
}
Expand All @@ -130,25 +130,25 @@ extension BeaconEmitterViewModel: CBPeripheralManagerDelegate {
func peripheralManagerDidUpdateState(_ peripheral: CBPeripheralManager) {
switch peripheral.state {
case .poweredOff:
self.status = "Bluetooth is currently powered off"
status = "Bluetooth is currently powered off"
case .poweredOn:
self.status = "Bluetooth is currently powered on and is available to use."
status = "Bluetooth is currently powered on and is available to use."
case .unauthorized:
self.status = "The app is not authorized to use the Bluetooth low energy peripheral/server role."
status = "The app is not authorized to use the Bluetooth low energy peripheral/server role."
case .unknown:
self.status = "The current state of the peripheral manager is unknown; an update is imminent."
status = "The current state of the peripheral manager is unknown; an update is imminent."
case .resetting:
self.status = "The connection with the system service was momentarily lost; an update is imminent."
status = "The connection with the system service was momentarily lost; an update is imminent."
case .unsupported:
self.status = "The platform doesn't support the Bluetooth low energy peripheral/server role."
status = "The platform doesn't support the Bluetooth low energy peripheral/server role."
@unknown default:
self.status = "The current state of the peripheral manager is unknown; an update is imminent."
status = "The current state of the peripheral manager is unknown; an update is imminent."
}
}

func peripheralManagerDidStartAdvertising(_: CBPeripheralManager, error _: Error?) {
if let emitter, emitter.isAdvertising {
self.isStarted = true
isStarted = true
}
}
}
10 changes: 5 additions & 5 deletions BeaconEmitter/BeaconRegion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ struct BeaconRegion {
let beaconKey = "kCBAdvDataAppleBeaconKey"
var advBytes = [CUnsignedChar](repeating: 0, count: 21)

self.proximityUUID.getBytes(&advBytes)
proximityUUID.getBytes(&advBytes)

advBytes[16] = CUnsignedChar((self.major >> 8) & 255)
advBytes[17] = CUnsignedChar(self.major & 255)
advBytes[16] = CUnsignedChar((major >> 8) & 255)
advBytes[17] = CUnsignedChar(major & 255)

advBytes[18] = CUnsignedChar((self.minor >> 8) & 255)
advBytes[19] = CUnsignedChar(self.minor & 255)
advBytes[18] = CUnsignedChar((minor >> 8) & 255)
advBytes[19] = CUnsignedChar(minor & 255)

advBytes[20] = CUnsignedChar(bitPattern: measuredPower)

Expand Down

0 comments on commit 0977683

Please sign in to comment.