diet-guard/app/lib/screens/settings_kcal_goal.dart
Krzysztof kuhy Rudnicki c77e7d9acf Split the last four Dart lib files; file-length gate now reports zero
Final batch of the 250-line sweep. `check_file_length.sh --all` exits 0: every
file in the repo is at or under 250 lines.

  github_mirror_screen.dart  391 -> 205  (+ actions, client_id_dialog parts)
  foodbank_service.dart      314 -> 209  (+ foodbank_service_manual part)
  settings_screen.dart       286 -> 237  (+ settings_kcal_goal part)
  firebase_backend.dart      282 -> 100  (+ firebase_client.dart)

Three of the four are `part` files rather than separate libraries, and that is
forced rather than stylistic: the moved code drives private state
(`_busy`, `_status`, the text controllers) and calls private helpers
(`_readBank`, `_normalize`), none of which are visible across a library
boundary in Dart. Widening them to public just to split a file would leak
internals the rest of the app has no business calling.

Within those parts, mixins rather than extensions, for a concrete reason: an
extension is not a subclass of `State`, so calling `setState` from one is an
`invalid_use_of_protected_member`. Fields the State must own (the controllers
it creates and disposes) stay declared there and are reached through abstract
getters.

`firebase_client.dart` is a real library split -- opening an authenticated
client has no private coupling to the keystore-backed account record, only to
its public API. The one exception, dropping the account marker without setting
the opt-out flag, became `forgetAccountMarker()` rather than exposing the
secure store.

Verified: flutter test 555 (unchanged), flutter analyze clean, 707 Python
tests / 100% branch coverage, check_patch_targets 0, pre-commit clean,
check_file_length.sh --all exit 0.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RA9ynjqXPnexRQnR5oAuCC
2026-08-15 21:07:24 +02:00

63 lines
2.0 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> {
String? _status;
void _showMessage(String message) {
if (!mounted) return;
setState(() => _status = message);
}
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();
_showMessage(
status.isGranted
? 'Battery optimization exemption granted.'
: 'Exemption not granted -- notifications may be delayed.',
);
} on Exception catch (e) {
_showMessage('Could not request exemption: $e');
}
}
}