Skip to content

Commit

Permalink
bugfix: hud rendering messed up on long running wifi manager
Browse files Browse the repository at this point in the history
  • Loading branch information
MSzturc committed Apr 26, 2020
1 parent e80cf01 commit e83ed82
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 13 deletions.
4 changes: 2 additions & 2 deletions ThinkpadAssistant.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.1;
MARKETING_VERSION = 1.2;
PRODUCT_BUNDLE_IDENTIFIER = de.mszturc.ThinkpadAssistant;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand All @@ -531,7 +531,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.1;
MARKETING_VERSION = 1.2;
PRODUCT_BUNDLE_IDENTIFIER = de.mszturc.ThinkpadAssistant;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE_SPECIFIER = "";
Expand Down
1 change: 0 additions & 1 deletion ThinkpadAssistant/MuteMicManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ final class MuteMicManager {
private var propertySize = UInt32(MemoryLayout<UInt32>.size)

init() {
print("init")
setupCurrentInputDeviceID()
startListener()
}
Expand Down
4 changes: 3 additions & 1 deletion ThinkpadAssistant/ShortcutManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ final class ShortcutManager {
})

MASShortcutMonitor.shared()?.register(disableWlanShortcut, withAction: {
if(WifiManager.isPowered() == true){
if(WifiManager.isPowered() == nil){
return
} else if(WifiManager.isPowered() == true){
HUD.showImage(Icons.wlanOff, status: "Wi-Fi\ndisabled")
WifiManager.disableWifi()
} else {
Expand Down
41 changes: 32 additions & 9 deletions ThinkpadAssistant/WifiManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,51 @@ import CoreWLAN

final class WifiManager {

static func toggleWifi() {
isPowered() ? disableWifi() : enableWifi()
}
private static var isRunning: Bool = false

static func disableWifi() {
toggleMasterSwitch(false)

}

static func enableWifi() {
toggleMasterSwitch(true)
}

static func isPowered() -> Bool {
static func isPowered() -> Bool? {
if(isRunning){
return nil
}
return CWWiFiClient.shared().interface()?.powerOn() ?? false
}

private static func toggleMasterSwitch(_ status:Bool) {
do {
try CWWiFiClient.shared().interface()?.setPower(status)
} catch let error as NSError {
print(error.localizedDescription)
if(isRunning){
return
}
isRunning = true
DispatchQueue.background(background: {
do {
try CWWiFiClient.shared().interface()?.setPower(status)
} catch let error as NSError {
print(error.localizedDescription)
}
}, completion:{
isRunning = false
})
}
}

extension DispatchQueue {

static func background(delay: Double = 0.0, background: (()->Void)? = nil, completion: (() -> Void)? = nil) {
DispatchQueue.global(qos: .background).async {
background?()
if let completion = completion {
DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: {
completion()
})
}
}
}

}

0 comments on commit e83ed82

Please sign in to comment.