trcr/lib/settings_screen.dart

244 lines
9 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-06-30 23:00:08 -07:00
import 'package:traccar_client/main.dart';
2025-06-28 14:57:56 -07:00
import 'package:traccar_client/password_service.dart';
2025-06-23 21:32:17 -07:00
import 'package:traccar_client/qr_code_screen.dart';
2025-06-14 13:26:13 -07:00
import 'package:wakelock_partial_android/wakelock_partial_android.dart';
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-06-13 09:57:18 -07:00
bool advanced = false;
2025-05-05 08:02:02 -07:00
2025-05-08 21:51:43 -07:00
String _getAccuracyLabel(String? key) {
return switch (key) {
2025-06-16 21:15:38 -07:00
'highest' => AppLocalizations.of(context)!.highestAccuracyLabel,
2025-05-08 21:51:43 -07:00
'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 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(
2025-07-10 20:16:21 -07:00
scrollable: true,
2025-05-07 07:46:20 -07:00
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')) {
2025-06-30 23:00:08 -07:00
messengerKey.currentState?.showSnackBar(SnackBar(content: Text(errorMessage)));
2025-06-01 12:04:41 -07:00
return;
}
}
2025-05-07 07:46:20 -07:00
if (isInt) {
2025-06-13 15:09:59 -07:00
int? intValue = int.tryParse(result);
2025-05-07 07:46:20 -07:00
if (intValue != null) {
2025-06-13 15:09:59 -07:00
if (key == Preferences.heartbeat && intValue > 0 && intValue < 60) {
intValue = 60; // minimum heartbeat is 60 seconds
}
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-06-28 14:57:56 -07:00
Future<void> _changePassword() async {
final controller = TextEditingController();
final result = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
2025-07-10 20:16:21 -07:00
scrollable: true,
2025-06-28 14:57:56 -07:00
content: TextField(
controller: controller,
decoration: InputDecoration(labelText: AppLocalizations.of(context)!.passwordLabel),
obscureText: true,
),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: Text(AppLocalizations.of(context)!.cancelButton),
),
TextButton(
onPressed: () => Navigator.pop(context, true),
child: Text(AppLocalizations.of(context)!.saveButton),
),
],
),
);
if (result == true) {
await PasswordService.setPassword(controller.text);
}
}
2025-05-07 07:46:20 -07:00
Widget _buildListTile(String title, String key, bool isInt) {
2025-06-13 15:40:47 -07:00
String? value;
if (isInt) {
final intValue = Preferences.instance.getInt(key);
if (intValue != null && intValue > 0) {
value = intValue.toString();
} else {
value = AppLocalizations.of(context)!.disabledValue;
}
} else {
value = 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() {
2025-06-16 21:15:38 -07:00
final accuracyOptions = ['highest', 'high', 'medium', 'low'];
2025-05-08 21:51:43 -07:00
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) {
2025-06-16 21:15:38 -07:00
final isHighestAccuracy = Preferences.instance.getString(Preferences.accuracy) == 'highest';
final distance = Preferences.instance.getInt(Preferences.distance);
2025-05-05 08:02:02 -07:00
return Scaffold(
2025-06-23 21:32:17 -07:00
appBar: AppBar(
title: Text(AppLocalizations.of(context)!.settingsTitle),
actions: [
IconButton(
icon: const Icon(Icons.qr_code_scanner),
onPressed: () async {
await Navigator.push(context, MaterialPageRoute(builder: (_) => const QrCodeScreen()));
setState(() {});
},
),
],
),
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-06-16 21:15:38 -07:00
if (isHighestAccuracy || Platform.isAndroid && distance == 0)
2025-05-21 07:36:04 -07:00
_buildListTile(AppLocalizations.of(context)!.intervalLabel, Preferences.interval, true),
2025-06-16 21:15:38 -07:00
if (isHighestAccuracy)
_buildListTile(AppLocalizations.of(context)!.angleLabel, Preferences.angle, true),
2025-06-11 17:47:43 -07:00
_buildListTile(AppLocalizations.of(context)!.heartbeatLabel, Preferences.heartbeat, true),
2025-05-05 08:02:02 -07:00
SwitchListTile(
2025-06-13 10:02:53 -07:00
title: Text(AppLocalizations.of(context)!.advancedLabel),
value: advanced,
onChanged: (value) {
setState(() => advanced = value);
2025-05-05 08:02:02 -07:00
},
),
2025-06-16 21:15:38 -07:00
if (advanced)
2025-06-13 15:36:07 -07:00
_buildListTile(AppLocalizations.of(context)!.fastestIntervalLabel, Preferences.fastestInterval, true),
2025-06-13 10:02:53 -07:00
if (advanced)
SwitchListTile(
title: Text(AppLocalizations.of(context)!.bufferLabel),
2025-06-23 21:32:17 -07:00
value: Preferences.instance.getBool(Preferences.buffer) ?? true,
2025-06-13 10:02:53 -07:00
onChanged: (value) async {
await Preferences.instance.setBool(Preferences.buffer, value);
await bg.BackgroundGeolocation.setConfig(Preferences.geolocationConfig());
2025-06-23 21:32:17 -07:00
setState(() {});
2025-06-13 10:02:53 -07:00
},
2025-06-13 09:57:18 -07:00
),
2025-06-14 12:54:33 -07:00
if (advanced && Platform.isAndroid)
SwitchListTile(
title: Text(AppLocalizations.of(context)!.wakelockLabel),
2025-06-23 21:32:17 -07:00
value: Preferences.instance.getBool(Preferences.wakelock) ?? false,
2025-06-14 12:54:33 -07:00
onChanged: (value) async {
await Preferences.instance.setBool(Preferences.wakelock, value);
2025-06-14 13:26:13 -07:00
if (value) {
final state = await bg.BackgroundGeolocation.state;
if (state.isMoving == true) {
WakelockPartialAndroid.acquire();
}
} else {
WakelockPartialAndroid.release();
}
2025-06-23 21:32:17 -07:00
setState(() {});
2025-06-14 12:54:33 -07:00
},
),
2025-06-13 09:57:18 -07:00
if (advanced)
SwitchListTile(
title: Text(AppLocalizations.of(context)!.stopDetectionLabel),
2025-06-23 21:32:17 -07:00
value: Preferences.instance.getBool(Preferences.stopDetection) ?? true,
2025-06-13 09:57:18 -07:00
onChanged: (value) async {
await Preferences.instance.setBool(Preferences.stopDetection, value);
await bg.BackgroundGeolocation.setConfig(Preferences.geolocationConfig());
2025-06-23 21:32:17 -07:00
setState(() {});
2025-06-13 09:57:18 -07:00
},
),
2025-06-28 14:57:56 -07:00
if (advanced)
ListTile(
title: Text(AppLocalizations.of(context)!.passwordLabel),
onTap: _changePassword,
),
2025-05-05 08:02:02 -07:00
],
),
);
}
}