Skip to content

Commit 492244b

Browse files
committed
Customizable low battery notification percentage
Signed-off-by: Lilly Rose Berner <lilly@lostluma.net>
1 parent f854137 commit 492244b

File tree

6 files changed

+82
-14
lines changed

6 files changed

+82
-14
lines changed

platforms/common/src/main/java/dynamic_fps/impl/DynamicFPSMod.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ public static boolean shouldShowLevels() {
186186
}
187187

188188
public static void onBatteryChargeChanged(int before, int after) {
189-
if (before > 10 && after <= 10) {
189+
int percentage = DynamicFPSConfig.INSTANCE.batteryTracker().notifications().percent();
190+
191+
if (before > percentage && after <= percentage) {
190192
showNotification("battery_critical", "reminder");
191193
}
192194
}
@@ -230,7 +232,7 @@ private static void initClickHandler() {
230232
}
231233
}
232234
private static void showNotification(String titleTranslationKey, String iconPath) {
233-
if (!DynamicFPSConfig.INSTANCE.batteryTracker().notifications()) {
235+
if (!DynamicFPSConfig.INSTANCE.batteryTracker().notifications().enabled()) {
234236
return;
235237
}
236238

platforms/common/src/main/java/dynamic_fps/impl/compat/ClothConfig.java

+19-5
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,28 @@ public static Screen genConfigScreen(Screen parent) {
167167
battery.add(
168168
entryBuilder.startBooleanToggle(
169169
Components.translatable("config", "battery_tracker_notifications"),
170-
batteryTracker.notifications()
170+
batteryTracker.notifications().enabled()
171171
)
172-
.setDefaultValue(defaultConfig.batteryTracker().notifications())
173-
.setSaveConsumer(batteryTracker::setNotifications)
172+
.setDefaultValue(defaultConfig.batteryTracker().notifications().enabled())
173+
.setSaveConsumer(batteryTracker.notifications()::setEnabled)
174174
.setTooltip(Components.translatable("config", "battery_tracker_notifications_tooltip"))
175175
.build()
176176
);
177177

178+
battery.add(
179+
entryBuilder.startIntSlider(
180+
Components.translatable("config", "battery_notification_percentage"),
181+
batteryTracker.notifications().percent(),
182+
1,
183+
50
184+
)
185+
.setDefaultValue(defaultConfig.batteryTracker().notifications().percent())
186+
.setSaveConsumer(batteryTracker.notifications()::setPercent)
187+
.setTextGetter(ClothConfig::valueAsPercentMessage)
188+
.setTooltip(Components.translatable("config", "battery_notification_percentage_tooltip"))
189+
.build()
190+
);
191+
178192
battery.add(
179193
entryBuilder.startEnumSelector(
180194
Components.translatable("config", "battery_indicator_condition"),
@@ -259,7 +273,7 @@ public static Screen genConfigScreen(Screen parent) {
259273
)
260274
.setDefaultValue((int) (standard.rawVolumeMultiplier(source) * 100))
261275
.setSaveConsumer(value -> instance.setVolumeMultiplier(source, value / 100f))
262-
.setTextGetter(ClothConfig::volumeMultiplierMessage)
276+
.setTextGetter(ClothConfig::valueAsPercentMessage)
263277
.build()
264278
);
265279
}
@@ -387,7 +401,7 @@ private static Component fpsTargetMessage(int step) {
387401
}
388402
}
389403

390-
private static Component volumeMultiplierMessage(int value) {
404+
private static Component valueAsPercentMessage(int value) {
391405
return Components.literal(Integer.toString(value) + "%");
392406
}
393407

platforms/common/src/main/java/dynamic_fps/impl/config/BatteryTrackerConfig.java

+23-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class BatteryTrackerConfig {
77
private boolean enabled;
88
private boolean switchStates;
9-
private boolean notifications;
9+
private NotificationConfig notifications;
1010
private DisplayConfig display;
1111

1212
public boolean enabled() {
@@ -25,18 +25,35 @@ public void setSwitchStates(boolean value) {
2525
this.switchStates = value;
2626
}
2727

28-
public boolean notifications() {
28+
public NotificationConfig notifications() {
2929
return this.notifications;
3030
}
3131

32-
public void setNotifications(boolean value) {
33-
this.notifications = value;
34-
}
35-
3632
public DisplayConfig display() {
3733
return this.display;
3834
}
3935

36+
public static class NotificationConfig {
37+
private boolean enabled;
38+
private int percent;
39+
40+
public boolean enabled() {
41+
return this.enabled;
42+
}
43+
44+
public void setEnabled(boolean value) {
45+
this.enabled = value;
46+
}
47+
48+
public int percent() {
49+
return this.percent;
50+
}
51+
52+
public void setPercent(int value) {
53+
this.percent = value;
54+
}
55+
}
56+
4057
public static class DisplayConfig {
4158
private BatteryIndicatorCondition condition;
4259
private BatteryIndicatorPlacement placement;

platforms/common/src/main/java/dynamic_fps/impl/config/Serialization.java

+29
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ private static void upgradeConfig(JsonObject config) {
139139
// v3.5.0
140140
upgradeIdleConfig(config);
141141

142+
// v3.9.0
143+
upgradeBatteryNotificationConfig(config);
144+
142145
// version agnostic
143146
addMissingFields(config, (JsonObject) JsonUtil.toJsonTree(DynamicFPSConfig.DEFAULTS));
144147
}
@@ -224,6 +227,32 @@ private static void upgradeIdleConfig(JsonObject root) {
224227
root.add("idle", idle);
225228
}
226229

230+
private static void upgradeBatteryNotificationConfig(JsonObject root) {
231+
// Convert battery notification to object
232+
// - { "notifications": true, ... }
233+
// + { "notifications": { "enabled": true, "percent": 10 }, ... }
234+
if (!root.has("battery_tracker")) {
235+
return;
236+
}
237+
238+
JsonObject battery = root.getAsJsonObject("battery_tracker");
239+
240+
if (!battery.has("notifications")) {
241+
return;
242+
}
243+
244+
JsonPrimitive field = battery.getAsJsonPrimitive("notifications");
245+
246+
if (!field.isBoolean()) {
247+
return;
248+
}
249+
250+
JsonObject notifications = new JsonObject();
251+
notifications.add("enabled", field);
252+
253+
battery.add("notifications", notifications);
254+
}
255+
227256
private static @Nullable JsonObject getStatesAsObject(JsonObject root) {
228257
if (!root.has("states")) {
229258
return null;

platforms/common/src/main/resources/assets/dynamic_fps/data/default_config.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"battery_tracker": {
1010
"enabled": false,
1111
"switch_states": false,
12-
"notifications": true,
12+
"notifications": {
13+
"enabled": true,
14+
"percent": 10
15+
},
1316
"display": {
1417
"condition": "critical",
1518
"placement": "top_left"

platforms/common/src/main/resources/assets/dynamic_fps/lang/en_us.json

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
"config.dynamic_fps.battery_tracker_notifications": "Battery Notifications",
5656
"config.dynamic_fps.battery_tracker_notifications_tooltip": "Receive in-game notifications about your battery status.",
5757

58+
"config.dynamic_fps.battery_notification_percentage": "Notification Threshold",
59+
"config.dynamic_fps.battery_notification_percentage_tooltip": "Set at which battery level a charging reminder is sent.",
60+
5861
"config.dynamic_fps.battery_indicator_condition": "Battery Indicator Condition",
5962
"config.dynamic_fps.battery_indicator_condition_disabled": "Disabled",
6063
"config.dynamic_fps.battery_indicator_condition_draining": "While discharging",

0 commit comments

Comments
 (0)