2026-03-02 19:41:01 -05:00
|
|
|
|
import 'package:encrypter/encrypter/xor.dart';
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2026-05-27 11:38:16 -04:00
|
|
|
|
import 'package:provider/provider.dart';
|
2026-03-04 11:05:24 -05:00
|
|
|
|
|
2026-05-27 11:38:16 -04:00
|
|
|
|
import '../main.dart';
|
|
|
|
|
|
import '../theme/app_colors.dart';
|
2026-06-04 08:39:37 -04:00
|
|
|
|
import '../theme/dimens.dart';
|
2026-03-02 19:41:01 -05:00
|
|
|
|
import '../utils/oott_api.dart';
|
|
|
|
|
|
import '../utils/pref_utils.dart';
|
2026-06-08 12:28:03 -04:00
|
|
|
|
import '../utils/push_service.dart';
|
2026-03-03 12:20:57 -05:00
|
|
|
|
import '../utils/ui_snackbars.dart';
|
2026-03-02 19:41:01 -05:00
|
|
|
|
|
|
|
|
|
|
class Settings extends StatefulWidget {
|
2026-06-08 12:28:03 -04:00
|
|
|
|
const Settings({super.key, this.pushService});
|
|
|
|
|
|
|
|
|
|
|
|
/// Injectable so widget tests can supply a fake; defaults to the real
|
|
|
|
|
|
/// FCM-backed service.
|
|
|
|
|
|
final PushService? pushService;
|
2026-03-04 11:05:24 -05:00
|
|
|
|
|
2026-03-02 19:41:01 -05:00
|
|
|
|
@override
|
2026-05-27 13:57:03 -04:00
|
|
|
|
State<Settings> createState() => _SettingsState();
|
2026-03-02 19:41:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class _SettingsState extends State<Settings> {
|
|
|
|
|
|
final _baseUrlController = TextEditingController();
|
|
|
|
|
|
final _apiKeyController = TextEditingController();
|
|
|
|
|
|
bool _apiKeyVisible = false;
|
2026-03-03 12:20:57 -05:00
|
|
|
|
bool _testOk = false;
|
2026-05-27 11:38:16 -04:00
|
|
|
|
bool _connectionModified = false;
|
2026-06-04 08:39:37 -04:00
|
|
|
|
bool _isFirstRun = false;
|
2026-05-27 11:38:16 -04:00
|
|
|
|
late String _selectedTheme;
|
2026-06-08 12:28:03 -04:00
|
|
|
|
late final PushService _pushService;
|
|
|
|
|
|
bool _pushEnabled = false;
|
|
|
|
|
|
bool _pushBusy = false;
|
2026-03-03 12:20:57 -05:00
|
|
|
|
|
2026-03-02 19:41:01 -05:00
|
|
|
|
final _formKey = GlobalKey<FormState>();
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void initState() {
|
|
|
|
|
|
super.initState();
|
2026-06-04 08:39:37 -04:00
|
|
|
|
_isFirstRun = (PrefUtil.getValue('base_url', '') as String).isEmpty;
|
2026-05-28 11:14:29 -04:00
|
|
|
|
_baseUrlController.text = PrefUtil.getValue('base_url', '') as String;
|
|
|
|
|
|
_apiKeyController.text = XOR().xorDecode(
|
|
|
|
|
|
PrefUtil.getValue('api_key', '') as String,
|
|
|
|
|
|
);
|
|
|
|
|
|
_selectedTheme = context.read<AppState>().themeKey;
|
2026-06-08 12:28:03 -04:00
|
|
|
|
_pushService = widget.pushService ?? FirebasePushService();
|
|
|
|
|
|
_pushEnabled = PrefUtil.getValue('push_enabled', false) as bool;
|
2026-03-02 19:41:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void dispose() {
|
|
|
|
|
|
_baseUrlController.dispose();
|
|
|
|
|
|
_apiKeyController.dispose();
|
|
|
|
|
|
super.dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-28 11:14:29 -04:00
|
|
|
|
void _onConnectionChanged(String _) {
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
_testOk = false;
|
|
|
|
|
|
_connectionModified = true;
|
|
|
|
|
|
});
|
2026-03-02 19:41:01 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-28 11:14:29 -04:00
|
|
|
|
Future<void> _testConnection() async {
|
|
|
|
|
|
if (!_formKey.currentState!.validate()) return;
|
|
|
|
|
|
final result = await BackendAPI.test(
|
|
|
|
|
|
_baseUrlController.text,
|
|
|
|
|
|
_apiKeyController.text,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
_testOk = result == null;
|
|
|
|
|
|
if (result == null) _connectionModified = false;
|
|
|
|
|
|
});
|
|
|
|
|
|
if (result == null) {
|
|
|
|
|
|
UISnackbars.showSuccess(context, 'It works!');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
UISnackbars.showError(context, result);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-08 12:28:03 -04:00
|
|
|
|
// Enable or disable push on this specific device. The toggle reflects user
|
|
|
|
|
|
// intent (persisted), but enabling can still fail if the OS permission is
|
|
|
|
|
|
// declined, in which case the switch falls back to off.
|
|
|
|
|
|
Future<void> _togglePush(bool value) async {
|
|
|
|
|
|
setState(() => _pushBusy = true);
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (value) {
|
|
|
|
|
|
final enabled = await _pushService.enable();
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
if (enabled) {
|
|
|
|
|
|
await PrefUtil.setValue('push_enabled', true);
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
setState(() => _pushEnabled = true);
|
|
|
|
|
|
UISnackbars.showSuccess(
|
|
|
|
|
|
context,
|
|
|
|
|
|
'Push notifications enabled on this device',
|
|
|
|
|
|
);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setState(() => _pushEnabled = false);
|
|
|
|
|
|
UISnackbars.showError(
|
|
|
|
|
|
context,
|
|
|
|
|
|
'Could not enable push. Check notification permission for OOTT.',
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
await _pushService.disable();
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
await PrefUtil.setValue('push_enabled', false);
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
setState(() => _pushEnabled = false);
|
|
|
|
|
|
UISnackbars.showSuccess(
|
|
|
|
|
|
context,
|
|
|
|
|
|
'Push notifications disabled on this device',
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
debugPrint('Failed to update push settings: $e');
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
UISnackbars.showError(context, 'Failed to update push settings');
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
if (mounted) setState(() => _pushBusy = false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-31 18:43:46 -04:00
|
|
|
|
Future<void> _save() async {
|
2026-05-28 11:14:29 -04:00
|
|
|
|
if (!_formKey.currentState!.validate()) return;
|
2026-05-31 18:55:35 -04:00
|
|
|
|
try {
|
|
|
|
|
|
final urlOk = await PrefUtil.setValue(
|
|
|
|
|
|
'base_url',
|
|
|
|
|
|
_baseUrlController.text,
|
|
|
|
|
|
);
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
final keyOk = await PrefUtil.setValue(
|
|
|
|
|
|
'api_key',
|
|
|
|
|
|
XOR().xorEncode(_apiKeyController.text),
|
|
|
|
|
|
);
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
final themeOk = await context.read<AppState>().setTheme(_selectedTheme);
|
|
|
|
|
|
if (!mounted) return;
|
|
|
|
|
|
if (urlOk && keyOk && themeOk) {
|
|
|
|
|
|
BackendAPI.instance.reconfigureFromPrefs();
|
|
|
|
|
|
UISnackbars.showSuccess(context, 'Settings saved successfully');
|
|
|
|
|
|
} else {
|
|
|
|
|
|
UISnackbars.showError(context, 'Failed to save settings');
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (e) {
|
2026-05-31 19:00:38 -04:00
|
|
|
|
debugPrint('Failed to save settings: $e');
|
2026-05-31 18:55:35 -04:00
|
|
|
|
if (!mounted) return;
|
2026-05-31 19:00:38 -04:00
|
|
|
|
UISnackbars.showError(context, 'Failed to save settings');
|
2026-05-31 18:43:46 -04:00
|
|
|
|
}
|
2026-03-03 12:20:57 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-02 19:41:01 -05:00
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
2026-05-28 11:14:29 -04:00
|
|
|
|
final appColors = Theme.of(context).extension<AppColorExtension>()!;
|
|
|
|
|
|
final colorScheme = Theme.of(context).colorScheme;
|
2026-05-28 19:51:31 -04:00
|
|
|
|
final textTheme = Theme.of(context).textTheme;
|
2026-06-04 08:39:37 -04:00
|
|
|
|
final saveDisabled = _connectionModified && !_testOk;
|
2026-05-28 11:14:29 -04:00
|
|
|
|
|
2026-05-28 19:51:31 -04:00
|
|
|
|
return SingleChildScrollView(
|
|
|
|
|
|
child: Form(
|
2026-05-28 11:14:29 -04:00
|
|
|
|
key: _formKey,
|
|
|
|
|
|
child: Column(
|
2026-05-28 19:51:31 -04:00
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2026-05-28 11:14:29 -04:00
|
|
|
|
children: [
|
2026-06-04 08:39:37 -04:00
|
|
|
|
if (_isFirstRun) ...[
|
|
|
|
|
|
Card(
|
|
|
|
|
|
color: colorScheme.secondaryContainer,
|
|
|
|
|
|
child: Padding(
|
|
|
|
|
|
padding: const EdgeInsets.all(Insets.lg),
|
|
|
|
|
|
child: Row(
|
|
|
|
|
|
children: [
|
|
|
|
|
|
Icon(
|
|
|
|
|
|
Icons.waving_hand_outlined,
|
|
|
|
|
|
color: colorScheme.onSecondaryContainer,
|
|
|
|
|
|
),
|
|
|
|
|
|
const SizedBox(width: Insets.md),
|
|
|
|
|
|
Expanded(
|
|
|
|
|
|
child: Text(
|
|
|
|
|
|
'Welcome to OOTT! Point the app at your server’s API '
|
|
|
|
|
|
'below, then Test and Save to get started.',
|
|
|
|
|
|
style: textTheme.bodyMedium?.copyWith(
|
|
|
|
|
|
color: colorScheme.onSecondaryContainer,
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
const SizedBox(height: Insets.lg),
|
|
|
|
|
|
],
|
2026-05-28 11:14:29 -04:00
|
|
|
|
TextFormField(
|
|
|
|
|
|
controller: _baseUrlController,
|
|
|
|
|
|
onChanged: _onConnectionChanged,
|
|
|
|
|
|
validator: (value) => value == null || value.isEmpty
|
|
|
|
|
|
? 'The URL cannot be empty'
|
|
|
|
|
|
: null,
|
|
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
|
|
border: UnderlineInputBorder(),
|
|
|
|
|
|
labelText: "Base URL of your OOTT server's API",
|
|
|
|
|
|
hintText: 'For example http://192.168.0.1:3000/api',
|
2026-03-02 19:41:01 -05:00
|
|
|
|
),
|
|
|
|
|
|
),
|
2026-06-04 08:39:37 -04:00
|
|
|
|
const SizedBox(height: Insets.lg),
|
2026-05-28 11:14:29 -04:00
|
|
|
|
TextFormField(
|
|
|
|
|
|
controller: _apiKeyController,
|
|
|
|
|
|
onChanged: _onConnectionChanged,
|
|
|
|
|
|
validator: (value) => value == null || value.isEmpty
|
|
|
|
|
|
? 'The API key cannot be empty'
|
|
|
|
|
|
: null,
|
|
|
|
|
|
obscureText: !_apiKeyVisible,
|
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
|
border: const UnderlineInputBorder(),
|
|
|
|
|
|
labelText: 'API key',
|
|
|
|
|
|
suffixIcon: IconButton(
|
|
|
|
|
|
icon: Icon(
|
|
|
|
|
|
_apiKeyVisible ? Icons.visibility : Icons.visibility_off,
|
|
|
|
|
|
),
|
|
|
|
|
|
onPressed: () =>
|
|
|
|
|
|
setState(() => _apiKeyVisible = !_apiKeyVisible),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
2026-06-04 08:39:37 -04:00
|
|
|
|
const SizedBox(height: Insets.lg),
|
2026-05-28 11:14:29 -04:00
|
|
|
|
DropdownButtonFormField<String>(
|
|
|
|
|
|
initialValue: _selectedTheme,
|
|
|
|
|
|
decoration: const InputDecoration(
|
|
|
|
|
|
border: UnderlineInputBorder(),
|
|
|
|
|
|
labelText: 'Theme',
|
|
|
|
|
|
),
|
|
|
|
|
|
items: const [
|
|
|
|
|
|
DropdownMenuItem(
|
|
|
|
|
|
value: 'catppuccin_mocha',
|
|
|
|
|
|
child: Text('Catppuccin Mocha'),
|
|
|
|
|
|
),
|
|
|
|
|
|
DropdownMenuItem(
|
|
|
|
|
|
value: 'gruvbox_dark',
|
|
|
|
|
|
child: Text('Gruvbox Dark'),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
onChanged: (value) {
|
|
|
|
|
|
if (value != null) setState(() => _selectedTheme = value);
|
|
|
|
|
|
},
|
|
|
|
|
|
),
|
2026-06-08 12:28:03 -04:00
|
|
|
|
if (_pushService.isSupported) ...[
|
|
|
|
|
|
const SizedBox(height: Insets.sm),
|
|
|
|
|
|
SwitchListTile(
|
|
|
|
|
|
contentPadding: EdgeInsets.zero,
|
|
|
|
|
|
title: const Text('Push notifications on this device'),
|
|
|
|
|
|
subtitle: const Text(
|
|
|
|
|
|
'Receive alerts on this device even when the app is closed.',
|
|
|
|
|
|
),
|
|
|
|
|
|
value: _pushEnabled,
|
|
|
|
|
|
onChanged: _pushBusy ? null : _togglePush,
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
2026-06-04 08:39:37 -04:00
|
|
|
|
const SizedBox(height: Insets.lg),
|
2026-05-28 11:14:29 -04:00
|
|
|
|
Row(
|
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
|
|
|
|
children: [
|
2026-06-04 08:39:37 -04:00
|
|
|
|
FilledButton.icon(
|
2026-05-28 11:14:29 -04:00
|
|
|
|
onPressed: () {
|
|
|
|
|
|
_testConnection();
|
|
|
|
|
|
},
|
|
|
|
|
|
label: const Text('Test'),
|
|
|
|
|
|
icon: Icon(_testOk ? Icons.check : Icons.play_arrow),
|
2026-06-04 08:39:37 -04:00
|
|
|
|
style: FilledButton.styleFrom(
|
2026-05-28 11:14:29 -04:00
|
|
|
|
backgroundColor: _testOk
|
|
|
|
|
|
? appColors.success
|
|
|
|
|
|
: colorScheme.secondary,
|
|
|
|
|
|
foregroundColor: _testOk
|
|
|
|
|
|
? appColors.onSuccess
|
|
|
|
|
|
: colorScheme.onSecondary,
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
2026-06-04 08:39:37 -04:00
|
|
|
|
const SizedBox(width: Insets.sm),
|
|
|
|
|
|
FilledButton.icon(
|
|
|
|
|
|
onPressed: saveDisabled ? null : _save,
|
2026-05-28 11:14:29 -04:00
|
|
|
|
label: const Text('Save'),
|
|
|
|
|
|
icon: const Icon(Icons.save),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
|
|
|
|
|
),
|
2026-06-04 08:39:37 -04:00
|
|
|
|
if (saveDisabled) ...[
|
|
|
|
|
|
const SizedBox(height: Insets.sm),
|
|
|
|
|
|
Align(
|
|
|
|
|
|
alignment: Alignment.centerRight,
|
|
|
|
|
|
child: Text(
|
|
|
|
|
|
'Test the connection before saving your changes.',
|
|
|
|
|
|
style: textTheme.bodySmall?.copyWith(
|
|
|
|
|
|
color: colorScheme.onSurfaceVariant,
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
|
|
|
|
|
],
|
2026-05-28 11:14:29 -04:00
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
),
|
2026-03-02 19:41:01 -05:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|