-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bump build and implement Mac Automations
Lots of changes but I'll attempt to get them all here. - Move MenuBarExtraView to MacSupport directory - MacSettingsUtils for GetMacAutomationSetting, for easily getting Mac Automation Settings - MacIOKit now provides GetMacIsCharging, GetMacPowerSource and HandleMacPowerStateChange, see the file for docs - MacAppDelegate uses these new functions - AutomationsView for configuration of Mac Automation Settings currently, other platforms get a notice - ContentView shows the AutomationsView now - DefaultsUsed now documents the new used defaults - Maybe more I forgot, but check the diff if unsure
- Loading branch information
1 parent
e542b3f
commit 5764d7e
Showing
10 changed files
with
264 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+16.1 KB
(110%)
...codeproj/project.xcworkspace/xcuserdata/Stella.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// | ||
// AutomationsView.swift | ||
// dcbattwebhook-swift | ||
// | ||
// Created by Stella Luna on 11/22/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct AutomationsView: View { | ||
@State private var macSendOnPluggedIn = false | ||
@State private var macSendOnUnplugged = false | ||
@State private var macSendOnHitFullCharge = false | ||
|
||
private var macSendSettings: [Bool] {[ | ||
macSendOnPluggedIn, | ||
macSendOnUnplugged, | ||
macSendOnHitFullCharge | ||
]} | ||
|
||
let defaults = UserDefaults.standard | ||
|
||
var body: some View { | ||
VStack { | ||
#if os(macOS) | ||
Form { | ||
Section(header: Text("Events"), footer: Text("You can choose to automatically send battery info when one or more of these things happen.\nConfigure Display name, pronoun, etc. in Settings.")) { | ||
Text("Send battery info automatically when...") | ||
Toggle(isOn: $macSendOnPluggedIn) { | ||
Text("Device is plugged in") | ||
} | ||
Toggle(isOn: $macSendOnUnplugged) { | ||
Text("Device is unplugged") | ||
} | ||
Toggle(isOn: $macSendOnHitFullCharge) { | ||
Text("Device finishes charging") | ||
}.disabled(true) | ||
} | ||
}.formStyle(.grouped) | ||
.onAppear() { | ||
if defaults.object(forKey: "MacSendOnPluggedIn") == nil { | ||
macSendOnPluggedIn = false | ||
} else { macSendOnPluggedIn = defaults.bool(forKey: "MacSendOnPluggedIn") } | ||
if defaults.object(forKey: "MacSendOnUnplugged") == nil { | ||
macSendOnUnplugged = false | ||
} else { macSendOnUnplugged = defaults.bool(forKey: "MacSendOnUnplugged") } | ||
if defaults.object(forKey: "MacSendOnHitFullCharge") == nil { | ||
macSendOnHitFullCharge = false | ||
} else { macSendOnHitFullCharge = defaults.bool(forKey: "MacSendOnHitFullCharge") } | ||
} | ||
.onDisappear() { | ||
defaults.set(macSendOnPluggedIn, forKey: "MacSendOnPluggedIn") | ||
defaults.set(macSendOnUnplugged, forKey: "MacSendOnUnplugged") | ||
defaults.set(macSendOnHitFullCharge, forKey: "MacSendOnHitFullCharge") | ||
}.onChange(of: macSendSettings) {_ in | ||
defaults.set(macSendOnPluggedIn, forKey: "MacSendOnPluggedIn") | ||
defaults.set(macSendOnUnplugged, forKey: "MacSendOnUnplugged") | ||
defaults.set(macSendOnHitFullCharge, forKey: "MacSendOnHitFullCharge") | ||
} | ||
#elseif os(watchOS) | ||
AutomationsViewNotEligibleView() | ||
#elseif os(visionOS) | ||
AutomationsViewNotEligibleView() | ||
#elseif os(tvOS) | ||
AutomationsViewNotEligibleView() | ||
#elseif os(iOS) | ||
AutomationsViewRequiresShortcutsView() | ||
#endif | ||
} | ||
.navigationTitle("Automations") | ||
.onAppear() { | ||
} | ||
|
||
} | ||
} | ||
|
||
struct AutomationsViewNotEligibleView: View { | ||
|
||
var body: some View { | ||
VStack { | ||
Form { | ||
VStack{ | ||
Image(systemName: "exclamationmark.triangle").foregroundStyle(.red).font(.system(size: 30)).padding() | ||
Text("This device does not support automations.") | ||
}.multilineTextAlignment(.center) | ||
} | ||
} | ||
.onAppear() { | ||
} | ||
|
||
} | ||
} | ||
|
||
struct AutomationsViewRequiresShortcutsView: View { | ||
|
||
var body: some View { | ||
VStack { | ||
Form { | ||
VStack{ | ||
Image(systemName: "info.circle").foregroundStyle(.yellow).font(.system(size: 30)).padding() | ||
Text("This device does not support in-app automations, but it can use Shortcuts Automations.\n\nPlease see the Help for more details about Shortcuts Automations.") | ||
} | ||
} | ||
} | ||
.onAppear() { | ||
} | ||
|
||
} | ||
} | ||
|
||
struct AutomationsView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
AutomationsView() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// MacSettingsUtils.swift | ||
// Battery Webhook | ||
// | ||
// Created by Stella Luna on 11/21/23. | ||
// | ||
|
||
#if os(macOS) | ||
import Foundation | ||
|
||
/** | ||
Gets a Mac Automation Setting and returns it | ||
|
||
- Parameters: | ||
- automationSetting: a valid setting to retrieve as described below | ||
|
||
A valid setting is one of the following strings: | ||
``` | ||
MacSendOnPluggedIn | ||
MacSendOnUnplugged | ||
MacSendOnHitFullCharge | ||
``` | ||
|
||
- Returns: | ||
Value of the Mac Automation Setting as Bool | ||
|
||
- Warning: Only available on macOS, and returns `false` if an invalid setting is specified | ||
*/ | ||
func GetMacAutomationSetting(automationSetting: String) -> Bool { | ||
let defaults = UserDefaults.standard | ||
|
||
var macSendOnPluggedIn = false | ||
var macSendOnUnplugged = false | ||
var macSendOnHitFullCharge = false | ||
|
||
if defaults.object(forKey: "MacSendOnPluggedIn") == nil { | ||
macSendOnPluggedIn = false | ||
} else { macSendOnPluggedIn = defaults.bool(forKey: "MacSendOnPluggedIn") } | ||
if defaults.object(forKey: "MacSendOnUnplugged") == nil { | ||
macSendOnUnplugged = false | ||
} else { macSendOnUnplugged = defaults.bool(forKey: "MacSendOnUnplugged") } | ||
if defaults.object(forKey: "MacSendOnHitFullCharge") == nil { | ||
macSendOnHitFullCharge = false | ||
} else { macSendOnHitFullCharge = defaults.bool(forKey: "MacSendOnHitFullCharge") } | ||
|
||
switch automationSetting { | ||
case "MacSendOnPluggedIn": | ||
return macSendOnPluggedIn | ||
case "MacSendOnUnplugged": | ||
return macSendOnUnplugged | ||
case "MacSendOnHitFullCharge": | ||
return macSendOnHitFullCharge | ||
default: | ||
return false | ||
} | ||
} | ||
|
||
#endif |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters