Added configuration redirect and partial settings form

This commit is contained in:
rzuasti
2026-03-02 19:41:01 -05:00
parent e6b5594226
commit 1d029c8c5f
3 changed files with 119 additions and 4 deletions
+12 -1
View File
@@ -1,9 +1,12 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:frontend/settings/settings.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import 'notifications/notification_list.dart'; import 'notifications/notification_list.dart';
import 'utils/pref_utils.dart';
// Routes definitions // Routes definitions
final GoRouter router = GoRouter( final GoRouter router = GoRouter(
// If there is no API base URL send the user to settings
initialLocation: '/notifications', initialLocation: '/notifications',
routes: [ routes: [
ShellRoute( ShellRoute(
@@ -15,16 +18,18 @@ final GoRouter router = GoRouter(
path: '/notifications', path: '/notifications',
name: 'notifications', name: 'notifications',
builder: (context, state) => NotificationList(), builder: (context, state) => NotificationList(),
redirect: (context, state) => _redirectToSettings(),
), ),
GoRoute( GoRoute(
path: '/devices', path: '/devices',
name: 'devices', name: 'devices',
builder: (context, state) => const Placeholder(), builder: (context, state) => const Placeholder(),
redirect: (context, state) => _redirectToSettings(),
), ),
GoRoute( GoRoute(
path: '/settings', path: '/settings',
name: 'settings', name: 'settings',
builder: (context, state) => const Placeholder(), builder: (context, state) => Settings(),
), ),
GoRoute( GoRoute(
path: '/about', path: '/about',
@@ -103,6 +108,12 @@ class MainShell extends StatelessWidget {
} }
} }
String? _redirectToSettings() {
return (PrefUtil.getValue("base_url", "") as String == "")
? '/settings'
: null;
}
int _calculateSelectedIndex(BuildContext context) { int _calculateSelectedIndex(BuildContext context) {
final location = GoRouterState.of(context).uri.path; final location = GoRouterState.of(context).uri.path;
if (location.startsWith('/notifications')) return 0; if (location.startsWith('/notifications')) return 0;
+104
View File
@@ -0,0 +1,104 @@
import 'package:encrypter/encrypter/xor.dart';
import 'package:flutter/material.dart';
import '../utils/oott_api.dart';
import '../utils/pref_utils.dart';
class Settings extends StatefulWidget {
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
bool _isLoading = true;
final _baseUrlController = TextEditingController();
final _apiKeyController = TextEditingController();
bool _apiKeyVisible = false;
final _formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
_getData();
}
@override
void dispose() {
_baseUrlController.dispose();
_apiKeyController.dispose();
super.dispose();
}
void _getData() async {
_baseUrlController.text = PrefUtil.getValue("base_url", "") as String;
_apiKeyController.text = XOR().xorDecode(
PrefUtil.getValue("api_key", "") as String,
);
_isLoading = false;
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: _isLoading
? const Center(child: CircularProgressIndicator())
: Form(
key: _formKey,
child: Column(
children: <Widget>[
SizedBox(height: 16),
TextFormField(
controller: _baseUrlController,
decoration: const InputDecoration(
border: UnderlineInputBorder(),
labelText: 'Base URL of your OOTT server\'s API',
hintText: "For example http://192.168.0.1:3000/api",
),
),
SizedBox(height: 16),
TextFormField(
controller: _apiKeyController,
obscureText: !_apiKeyVisible,
decoration: InputDecoration(
border: UnderlineInputBorder(),
labelText: 'API Key',
suffixIcon: IconButton(
icon: Icon(
_apiKeyVisible
? Icons.visibility
: Icons.visibility_off,
),
onPressed: () {
setState(() {
_apiKeyVisible = !_apiKeyVisible;
});
},
),
),
),
SizedBox(height: 16),
ElevatedButton.icon(
onPressed: () {
showDialog(
context: context,
builder: (context) {
return AlertDialog(
content: Text(
_baseUrlController.text +
' - ' +
_apiKeyController.text,
),
);
},
);
},
label: Text('Test'),
icon: const Icon(Icons.check),
),
],
),
),
);
}
}
+3 -3
View File
@@ -28,11 +28,11 @@ class PrefUtil {
static Object getValue(String key, Object defaultValue) { static Object getValue(String key, Object defaultValue) {
switch (defaultValue.runtimeType) { switch (defaultValue.runtimeType) {
case String: case String:
return preferences.getString(key) ?? ""; return preferences.getString(key) ?? defaultValue;
case bool: case bool:
return preferences.getBool(key) ?? false; return preferences.getBool(key) ?? defaultValue;
case int: case int:
return preferences.getInt(key) ?? 0; return preferences.getInt(key) ?? defaultValue;
default: default:
return defaultValue; return defaultValue;
} }