trcr/lib/settings_screen.dart

152 lines
5.2 KiB
Dart
Raw Normal View History

2025-05-21 07:36:04 -07:00
import 'dart:io';
2025-05-05 08:02:02 -07:00
import 'package:flutter/material.dart';
2025-05-07 07:46:20 -07:00
import 'package:flutter/services.dart';
2025-05-07 17:42:07 -07:00
import 'package:flutter_background_geolocation/flutter_background_geolocation.dart' as bg;
2025-05-07 07:46:20 -07:00
2025-06-05 23:15:05 -07:00
import 'l10n/app_localizations.dart';
2025-05-07 07:46:20 -07:00
import 'preferences.dart';
2025-05-05 08:02:02 -07:00
class SettingsScreen extends StatefulWidget {
const SettingsScreen({super.key});
2025-05-05 08:02:02 -07:00
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
2025-05-07 07:46:20 -07:00
bool buffering = true;
2025-05-05 08:02:02 -07:00
@override
void initState() {
super.initState();
2025-05-07 22:06:29 -07:00
_initState();
2025-05-05 08:02:02 -07:00
}
2025-05-07 22:06:29 -07:00
void _initState() async {
2025-05-05 08:02:02 -07:00
setState(() {
2025-05-10 09:51:51 -07:00
buffering = Preferences.instance.getBool(Preferences.buffer) ?? true;
2025-05-05 08:02:02 -07:00
});
}
2025-05-08 21:51:43 -07:00
String _getAccuracyLabel(String? key) {
return switch (key) {
'high' => AppLocalizations.of(context)!.highAccuracyLabel,
'low' => AppLocalizations.of(context)!.lowAccuracyLabel,
_ => AppLocalizations.of(context)!.mediumAccuracyLabel,
};
}
2025-05-07 07:46:20 -07:00
Future<void> _editSetting(String title, String key, bool isInt) async {
final initialValue = isInt
2025-05-10 09:51:51 -07:00
? Preferences.instance.getInt(key)?.toString() ?? '0'
: Preferences.instance.getString(key) ?? '';
2025-05-05 08:02:02 -07:00
2025-05-07 07:46:20 -07:00
final controller = TextEditingController(text: initialValue);
2025-06-01 12:44:16 -07:00
final scaffoldManager = ScaffoldMessenger.of(context);
final errorMessage = AppLocalizations.of(context)!.invalidValue;
2025-05-05 08:02:02 -07:00
2025-05-07 07:46:20 -07:00
final result = await showDialog<String>(
context: context,
builder: (context) => AlertDialog(
title: Text(title),
content: TextField(
controller: controller,
keyboardType: isInt ? TextInputType.number : TextInputType.text,
inputFormatters: isInt ? [FilteringTextInputFormatter.digitsOnly] : [],
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: Text(AppLocalizations.of(context)!.cancelButton),
),
TextButton(
onPressed: () => Navigator.pop(context, controller.text),
child: Text(AppLocalizations.of(context)!.saveButton),
),
],
),
);
if (result != null && result.isNotEmpty) {
2025-06-01 12:44:16 -07:00
if (key == Preferences.url) {
2025-06-01 12:04:41 -07:00
final uri = Uri.tryParse(result);
2025-06-01 12:44:16 -07:00
if (uri == null || uri.host.isEmpty || !(uri.scheme == 'http' || uri.scheme == 'https')) {
scaffoldManager.showSnackBar(SnackBar(content: Text(errorMessage)));
2025-06-01 12:04:41 -07:00
return;
}
}
2025-05-07 07:46:20 -07:00
if (isInt) {
final intValue = int.tryParse(result);
if (intValue != null) {
2025-05-10 09:51:51 -07:00
await Preferences.instance.setInt(key, intValue);
2025-05-07 07:46:20 -07:00
}
} else {
2025-05-10 09:51:51 -07:00
await Preferences.instance.setString(key, result);
2025-05-07 07:46:20 -07:00
}
2025-05-10 09:51:51 -07:00
await bg.BackgroundGeolocation.setConfig(Preferences.geolocationConfig());
2025-05-07 07:46:20 -07:00
setState(() {});
}
2025-05-05 08:02:02 -07:00
}
2025-05-07 07:46:20 -07:00
Widget _buildListTile(String title, String key, bool isInt) {
2025-05-10 09:51:51 -07:00
final value = isInt ? Preferences.instance.getInt(key)?.toString() : Preferences.instance.getString(key);
2025-05-07 07:46:20 -07:00
return ListTile(
title: Text(title),
subtitle: Text(value ?? ''),
onTap: () => _editSetting(title, key, isInt),
2025-05-05 08:02:02 -07:00
);
}
2025-05-08 21:51:43 -07:00
Widget _buildAccuracyListTile() {
final accuracyOptions = ['high', 'medium', 'low'];
return ListTile(
title: Text(AppLocalizations.of(context)!.accuracyLabel),
2025-05-10 09:51:51 -07:00
subtitle: Text(_getAccuracyLabel(Preferences.instance.getString(Preferences.accuracy))),
2025-05-08 21:51:43 -07:00
onTap: () async {
final selectedAccuracy = await showDialog<String>(
context: context,
builder: (context) => SimpleDialog(
title: Text(AppLocalizations.of(context)!.accuracyLabel),
children: accuracyOptions.map((option) => SimpleDialogOption(
child: Text(_getAccuracyLabel(option)),
onPressed: () => Navigator.pop(context, option),
)).toList(),
),
);
if (selectedAccuracy != null) {
2025-05-10 09:51:51 -07:00
await Preferences.instance.setString(Preferences.accuracy, selectedAccuracy);
await bg.BackgroundGeolocation.setConfig(Preferences.geolocationConfig());
2025-05-08 21:51:43 -07:00
setState(() {});
}
},
);
}
2025-05-05 08:02:02 -07:00
@override
Widget build(BuildContext context) {
return Scaffold(
2025-05-07 22:06:29 -07:00
appBar: AppBar(title: Text(AppLocalizations.of(context)!.settingsTitle)),
2025-05-05 08:02:02 -07:00
body: ListView(
children: [
2025-05-07 07:46:20 -07:00
_buildListTile(AppLocalizations.of(context)!.idLabel, Preferences.id, false),
_buildListTile(AppLocalizations.of(context)!.urlLabel, Preferences.url, false),
2025-05-08 21:51:43 -07:00
_buildAccuracyListTile(),
2025-05-07 07:46:20 -07:00
_buildListTile(AppLocalizations.of(context)!.distanceLabel, Preferences.distance, true),
2025-05-21 07:36:04 -07:00
if (Platform.isAndroid && Preferences.instance.getInt(Preferences.distance) == 0)
_buildListTile(AppLocalizations.of(context)!.intervalLabel, Preferences.interval, true),
2025-05-05 08:02:02 -07:00
SwitchListTile(
2025-05-07 07:46:20 -07:00
title: Text(AppLocalizations.of(context)!.bufferLabel),
value: buffering,
onChanged: (value) async {
2025-05-10 09:51:51 -07:00
await Preferences.instance.setBool(Preferences.buffer, value);
await bg.BackgroundGeolocation.setConfig(Preferences.geolocationConfig());
2025-05-07 07:46:20 -07:00
setState(() => buffering = value);
2025-05-05 08:02:02 -07:00
},
),
],
),
);
}
}