Skip to content

Commit a997081

Browse files
authored
added battery item
1 parent 09a209c commit a997081

1 file changed

Lines changed: 84 additions & 1 deletion

File tree

‎weather-cal.js‎

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const padding = 10
3434
// Set the width of the column, or set to 0 for an automatic width.
3535

3636
// You can add items to the column:
37-
// date, greeting, events, current, future, text("Your text here")
37+
// date, greeting, events, current, future, battery, text("Your text here")
3838
// You can also add a left, center, or right to the list. Everything after it will be aligned that way.
3939

4040
// Make sure to always put a comma after each item.
@@ -173,6 +173,8 @@ const textFormat = {
173173
tinyTemp: { size: 12, color: "", font: "" },
174174

175175
customText: { size: 14, color: "", font: "" },
176+
177+
battery: { size: 18, color: "", font: "medium" }
176178
}
177179

178180
/*
@@ -871,6 +873,87 @@ function text(input = null) {
871873
return displayText
872874
}
873875

876+
/ Provide the SF Symbols battery icon depending on the battery level
877+
function provideBatteryIcon() {
878+
879+
const batteryLevel = Device.batteryLevel()
880+
881+
if ( Device.isCharging() ) {
882+
883+
let batIcon = SFSymbol.named("battery.100.bolt")
884+
batIcon.applyLightWeight()
885+
886+
return batIcon.image
887+
888+
} else
889+
890+
if ( Math.round(batteryLevel * 100) > 65 ) {
891+
892+
let batIcon = SFSymbol.named("battery.100")
893+
batIcon.applyLightWeight()
894+
895+
return batIcon.image
896+
897+
} else if ( Math.round(batteryLevel * 100) > 30 ) {
898+
899+
let batIcon = SFSymbol.named("battery.25")
900+
batIcon.applyLightWeight()
901+
902+
return batIcon.image
903+
904+
} else {
905+
906+
let batIcon = SFSymbol.named("battery.0")
907+
batIcon.applySemiboldWeight()
908+
909+
return batIcon.image
910+
}
911+
912+
}
913+
914+
// Add a battery element to the widget; consisting of a battery icon and percentage
915+
async function battery(column, alignment) {
916+
917+
// Get battery level via Scriptable function and format it in a convenient way
918+
function getBatteryLevel() {
919+
920+
const batteryLevel = Device.batteryLevel()
921+
const batteryPercentage = `${Math.round(batteryLevel * 100)}%`
922+
923+
return batteryPercentage
924+
}
925+
926+
const batteryLevel = Device.batteryLevel()
927+
928+
// Set up the battery level item
929+
let batteryStack = align(column, alignment)
930+
batteryStack.layoutHorizontally()
931+
batteryStack.centerAlignContent()
932+
933+
let batteryIcon = batteryStack.addImage(provideBatteryIcon())
934+
batteryIcon.imageSize = new Size(30,30)
935+
936+
937+
// Change the battery icon to red if battery level is <= 20 to match system behavior
938+
if ( Math.round(batteryLevel * 100) > 20 || Device.isCharging() ) {
939+
940+
batteryIcon.tintColor = Color.white()
941+
942+
} else {
943+
944+
batteryIcon.tintColor = Color.red()
945+
946+
}
947+
948+
batteryStack.addSpacer(8)
949+
950+
// Display the battery status
951+
let batteryInfo = provideText(getBatteryLevel(), batteryStack, textFormat.battery)
952+
953+
batteryStack.setPadding(10, 10, 10, 10)
954+
955+
}
956+
874957
/*
875958
* HELPER FUNCTIONS
876959
* These functions perform duties for other functions.

0 commit comments

Comments
 (0)