mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-21 22:42:17 +01:00
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWYndZhdUrRGFEjBazQ
70 lines
2.2 KiB
Dart
70 lines
2.2 KiB
Dart
// The debounced daily-kcal-goal field and the battery-exemption request.
|
|
//
|
|
// Split out of `settings_screen.dart` for the repo's 250-line cap. A `part`
|
|
// mixin: these drive the screen's private state and call `setState`, which is
|
|
// `@protected` and therefore unreachable from an extension.
|
|
|
|
part of 'settings_screen.dart';
|
|
|
|
mixin _SettingsKcalGoal on State<SettingsScreen> {
|
|
/// The latest outcome as an icon cue; null until something happened.
|
|
Widget? _status;
|
|
|
|
void _showCue(CueKind kind, String semanticLabel) {
|
|
if (!mounted) return;
|
|
setState(
|
|
() => _status = StatusCue(
|
|
key: UniqueKey(),
|
|
kind: kind,
|
|
semanticLabel: semanticLabel,
|
|
),
|
|
);
|
|
}
|
|
|
|
Timer? _kcalGoalDebounce;
|
|
int? _pendingKcalGoal;
|
|
|
|
/// Persists the pending goal, if any, and clears it.
|
|
void _flushKcalGoal() {
|
|
final goal = _pendingKcalGoal;
|
|
if (goal == null) return;
|
|
_pendingKcalGoal = null;
|
|
unawaited(AppSettingsService.instance.saveDailyKcalGoal(goal));
|
|
}
|
|
|
|
/// Saves the typed goal once typing settles.
|
|
///
|
|
/// Debounced because every keystroke would otherwise be a real edit:
|
|
/// typing "2000" saved 2, 20, 200, 2000 in turn, and a sync tick landing
|
|
/// between keystrokes would push a nonsense budget to the other device.
|
|
void _onKcalGoalChanged(String value) {
|
|
_kcalGoalDebounce?.cancel();
|
|
final goal = int.tryParse(value);
|
|
if (goal == null || goal <= 0) return;
|
|
_pendingKcalGoal = goal;
|
|
_kcalGoalDebounce = Timer(
|
|
const Duration(milliseconds: 600),
|
|
_flushKcalGoal,
|
|
);
|
|
}
|
|
|
|
/// Requests exemption from OEM battery optimization (MIUI, some Samsung
|
|
/// configs), which can otherwise degrade the 15-minute background-check
|
|
/// reliability well past its accepted ±15 min target.
|
|
Future<void> _requestBatteryExemption() async {
|
|
final request =
|
|
widget.requestBatteryExemption ??
|
|
() => Permission.ignoreBatteryOptimizations.request();
|
|
try {
|
|
final status = await request();
|
|
if (status.isGranted) {
|
|
_showCue(CueKind.ok, 'Battery optimization exemption granted.');
|
|
} else {
|
|
_showCue(CueKind.warn, 'Exemption not granted.');
|
|
}
|
|
} on Exception catch (e) {
|
|
_showCue(CueKind.error, 'Could not request exemption: $e');
|
|
}
|
|
}
|
|
}
|