mirror of
https://github.com/kuhyx/diet-guard.git
synced 2026-11-19 07:54:33 +01:00
Flutter 3.50 deprecates package:flutter/material.dart; 73 deprecation infos failed flutter analyze --fatal-infos, and four Dart pins were behind (crdt_sync_flutter 0.8.2, flutter_local_notifications 23.0.2, github_device_auth 0.5.0, sync_settings_ui 0.8.2). Those UI packages now sit on material_ui, so the app has to as well or their widgets lose MaterialLocalizations. Imports swap one-for-one and dart fix re-sorted the directives; no behaviour change. analyze is clean and all six test shards pass. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WzxdWWfb63jncj9Lca2jeX
251 lines
9.8 KiB
Dart
251 lines
9.8 KiB
Dart
/// App settings: kcal goal, notifications, data export/import, and links to
|
|
/// the shared Firebase "Sync settings" and the app-local GitHub mirror (its
|
|
/// connect also runs a real `runSync`, which the shared screen does not).
|
|
library;
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:crdt_sync/crdt_sync.dart';
|
|
import 'package:crdt_sync_flutter/crdt_sync_flutter.dart'
|
|
show E2ePassphraseButton;
|
|
import 'package:diet_guard_app/models/meal_schedule.dart';
|
|
import 'package:diet_guard_app/screens/github_mirror_screen.dart';
|
|
import 'package:diet_guard_app/screens/settings_kuchnia.dart';
|
|
import 'package:diet_guard_app/services/app_settings_service.dart';
|
|
import 'package:diet_guard_app/services/due_slot_check.dart';
|
|
import 'package:diet_guard_app/services/firebase_backend.dart';
|
|
import 'package:diet_guard_app/services/firebase_client.dart';
|
|
import 'package:diet_guard_app/services/google_sign_in_backend.dart';
|
|
import 'package:diet_guard_app/services/meal_schedule_service.dart';
|
|
import 'package:diet_guard_app/services/sync_e2e.dart';
|
|
import 'package:diet_guard_app/ui/cues.dart';
|
|
import 'package:diet_guard_app/ui/macro_kind.dart';
|
|
import 'package:diet_guard_app/ui/theme.dart';
|
|
import 'package:diet_guard_app/widgets/data_backup_row.dart';
|
|
import 'package:diet_guard_app/widgets/log_sound_tile.dart';
|
|
import 'package:diet_guard_app/widgets/meal_schedule_editor.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:material_ui/material_ui.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'package:sync_settings_ui/sync_settings_ui.dart';
|
|
|
|
part 'settings_kcal_goal.dart';
|
|
part 'settings_meal_schedule.dart';
|
|
|
|
/// Screen for app-specific settings and links to sync configuration.
|
|
class SettingsScreen extends StatefulWidget {
|
|
/// Creates a [SettingsScreen].
|
|
const SettingsScreen({
|
|
super.key,
|
|
this.httpClient,
|
|
this.requestBatteryExemption,
|
|
this.googleFirebaseFactory,
|
|
this.googleAvailable,
|
|
this.accountLoader,
|
|
this.accountSaver,
|
|
this.accountClearer,
|
|
this.sessionProbe,
|
|
this.firebaseFactory,
|
|
});
|
|
|
|
/// Injectable HTTP client for the linked [GitHubMirrorScreen]; tests pass
|
|
/// a [MockClient].
|
|
final http.Client? httpClient;
|
|
|
|
/// Injectable battery-optimization-exemption request; tests pass a fake.
|
|
/// Production defaults to
|
|
/// `Permission.ignoreBatteryOptimizations.request()`.
|
|
final Future<PermissionStatus> Function()? requestBatteryExemption;
|
|
|
|
/// Builds the Firebase backend via Google sign-in, for the shared Sync
|
|
/// settings screen. Injected so tests need no platform channel -- the
|
|
/// plugin reaches the OS account picker.
|
|
final Future<FirebaseRestClient?> Function()? googleFirebaseFactory;
|
|
|
|
/// Whether to offer the Google button. Defaults to what the platform
|
|
/// supports; injected by tests, whose host reports unsupported.
|
|
final bool? googleAvailable;
|
|
|
|
/// Reads the stored Firebase account. Injected for the same reason as
|
|
/// [googleFirebaseFactory]: the keystore is a platform channel.
|
|
///
|
|
/// Deliberately backed by [storedAccount], not [loadAccount]: the shared
|
|
/// `SyncSettingsScreen` uses this single closure both to display the
|
|
/// status and to read back the account right after a Google sign-in.
|
|
/// [loadAccount]'s desktop-wrapper fallback resolves to `file:///` on
|
|
/// Android and throws -- verified on the phone, where it turned a
|
|
/// successful sign-in into "Google sign-in failed". `storedAccount` is the
|
|
/// keystore-only read [firebase_backend.dart] documents for exactly this
|
|
/// read-back case.
|
|
final Future<FirebaseAccount?> Function()? accountLoader;
|
|
|
|
/// Persists the account. See [accountLoader].
|
|
final Future<void> Function(FirebaseAccount)? accountSaver;
|
|
|
|
/// Forgets the account and any cached session. See [accountLoader].
|
|
final Future<void> Function()? accountClearer;
|
|
|
|
/// Whether a Firebase session is stored. See [accountLoader].
|
|
final Future<bool> Function()? sessionProbe;
|
|
|
|
/// Builds the Firebase backend from the stored account. Injected so tests
|
|
/// can supply a fake.
|
|
final Future<FirebaseRestClient?> Function()? firebaseFactory;
|
|
|
|
@override
|
|
State<SettingsScreen> createState() => _SettingsScreenState();
|
|
}
|
|
|
|
class _SettingsScreenState extends State<SettingsScreen>
|
|
with _SettingsKcalGoal, _SettingsMealSchedule {
|
|
final _kcalGoalController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_kcalGoalController.text = AppSettingsService.dailyKcalGoal.toString();
|
|
_loadSchedule();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_kcalGoalDebounce?.cancel();
|
|
// Leaving the screen inside the debounce window must not silently drop
|
|
// the edit the user just typed.
|
|
_flushKcalGoal();
|
|
_kcalGoalController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
/// True on the one platform with OEM battery optimization to exempt from.
|
|
///
|
|
/// Uses [defaultTargetPlatform] rather than `Platform.isAndroid` because
|
|
/// `dart:io` is a throwing stub in the desktop web build.
|
|
bool get _isAndroid =>
|
|
!kIsWeb && defaultTargetPlatform == TargetPlatform.android;
|
|
|
|
Future<void> _openSyncSettings() async {
|
|
await Navigator.of(context).push<void>(
|
|
MaterialPageRoute(
|
|
builder: (_) => SyncSettingsScreen(
|
|
accountLoader: widget.accountLoader ?? storedAccount,
|
|
accountSaver: widget.accountSaver ?? saveAccount,
|
|
accountClearer: widget.accountClearer ?? clearAccount,
|
|
sessionProbe: widget.sessionProbe ?? isFirebaseConfigured,
|
|
firebaseFactory: widget.firebaseFactory ?? openFirebase,
|
|
googleFirebaseFactory:
|
|
widget.googleFirebaseFactory ?? openFirebaseWithGoogle,
|
|
googleAvailable: widget.googleAvailable ?? googleSignInSupported,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Future<void> _openGitHubMirror() async {
|
|
await Navigator.of(context).push<void>(
|
|
MaterialPageRoute(
|
|
builder: (_) => GitHubMirrorScreen(httpClient: widget.httpClient),
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
// No title: the gear that opened this screen already names it.
|
|
appBar: AppBar(),
|
|
// Capped at the prose width so fields do not span a ~1366px window.
|
|
body: Center(
|
|
child: ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: AppWidth.prose),
|
|
child: ListView(
|
|
padding: const EdgeInsets.all(16),
|
|
children: [
|
|
// A four-digit number: capped so the field does not read as a
|
|
// layout error by spanning the whole column.
|
|
ConstrainedBox(
|
|
constraints: const BoxConstraints(maxWidth: AppWidth.field),
|
|
child: IconField(
|
|
icon: MacroKind.kcal.icon,
|
|
iconColor: MacroKind.kcal.color,
|
|
semanticLabel: 'Daily kcal goal',
|
|
controller: _kcalGoalController,
|
|
keyboardType: TextInputType.number,
|
|
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
|
|
onChanged: _onKcalGoalChanged,
|
|
),
|
|
),
|
|
const LogSoundTile(),
|
|
const SizedBox(height: 16),
|
|
const Divider(),
|
|
const SizedBox(height: 8),
|
|
..._mealScheduleSection(context),
|
|
const KuchniaSettingsSection(),
|
|
const SizedBox(height: 24),
|
|
const Divider(),
|
|
ListTile(
|
|
contentPadding: EdgeInsets.zero,
|
|
leading: const Icon(
|
|
Icons.cloud_sync_outlined,
|
|
semanticLabel: 'Sync settings',
|
|
),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: () => unawaited(_openSyncSettings()),
|
|
),
|
|
// End-to-end encryption passphrase: closed lock while the last
|
|
// sync found no usable key, so nothing syncs until it is set.
|
|
ValueListenableBuilder<bool>(
|
|
valueListenable: syncLocked,
|
|
builder: (_, locked, _) => ListTile(
|
|
contentPadding: EdgeInsets.zero,
|
|
leading: E2ePassphraseButton(locked: locked),
|
|
),
|
|
),
|
|
// Legacy mirror: muted tint marks it as the secondary path.
|
|
ListTile(
|
|
contentPadding: EdgeInsets.zero,
|
|
leading: Icon(
|
|
Icons.code,
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
|
semanticLabel: 'Advanced sync (GitHub)',
|
|
),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: () => unawaited(_openGitHubMirror()),
|
|
),
|
|
DataBackupRow(
|
|
onImported: () => _kcalGoalController.text = AppSettingsService
|
|
.dailyKcalGoal
|
|
.toString(),
|
|
),
|
|
// Battery-optimization exemption is Android-only, and
|
|
// `permission_handler` has no web implementation at all;
|
|
// the row would throw rather than degrade in the browser.
|
|
if (_isAndroid) ...[
|
|
const SizedBox(height: 24),
|
|
const Divider(),
|
|
const SizedBox(height: 8),
|
|
Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: IconButton.outlined(
|
|
onPressed: _requestBatteryExemption,
|
|
icon: const Icon(
|
|
Icons.battery_alert,
|
|
semanticLabel: 'Disable battery optimization',
|
|
),
|
|
),
|
|
),
|
|
],
|
|
if (_status != null) ...[
|
|
const SizedBox(height: 16),
|
|
Align(alignment: Alignment.centerLeft, child: _status),
|
|
],
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|