134 lines
4.5 KiB
Dart
134 lines
4.5 KiB
Dart
import 'dart:convert';
|
|
import 'dart:math';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:gomix_flutter/connection_editor.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
class ConnectionSelector extends StatefulWidget {
|
|
final SharedPreferences prefs;
|
|
final Function initWs;
|
|
final Map<String, dynamic> connections;
|
|
const ConnectionSelector(
|
|
{super.key,
|
|
required this.prefs,
|
|
required this.initWs,
|
|
required this.connections});
|
|
|
|
@override
|
|
State<ConnectionSelector> createState() => _ConnectionSelectorState();
|
|
}
|
|
|
|
class _ConnectionSelectorState extends State<ConnectionSelector> {
|
|
String _updateConnectionName = "";
|
|
dynamic _updateConnection;
|
|
bool _updateValid = false;
|
|
|
|
void updateData(String connectionName, dynamic connection, bool isValid) {
|
|
_updateConnectionName = connectionName;
|
|
_updateConnection = connection;
|
|
_updateValid = isValid;
|
|
}
|
|
|
|
void openEditDialog(bool showDelete, void Function()? onDelete,
|
|
void Function()? onSubmit, String submitButtonText, String headerText) {
|
|
showDialog<String>(
|
|
context: context,
|
|
builder: (BuildContext context) => AlertDialog(
|
|
title: Text(headerText),
|
|
content: ConnectionEditor(
|
|
connectionName: _updateConnectionName,
|
|
connection: _updateConnection,
|
|
prefs: widget.prefs,
|
|
updateData: updateData,
|
|
),
|
|
actions: <Widget>[
|
|
if (showDelete)
|
|
TextButton(
|
|
onPressed: onDelete,
|
|
child: const Text('Delete', style: TextStyle(color: Colors.red)),
|
|
),
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, 'Cancel'),
|
|
child: const Text('Cancel'),
|
|
),
|
|
TextButton(
|
|
onPressed: onSubmit,
|
|
child: Text(submitButtonText),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return SizedBox(
|
|
width: min(MediaQuery.of(context).size.width, 400),
|
|
child: ListView(
|
|
shrinkWrap: true,
|
|
children: [
|
|
...widget.connections.keys.map(
|
|
(key) => ListTile(
|
|
contentPadding: const EdgeInsets.only(left: 5, right: 5),
|
|
title: Text(key),
|
|
trailing: IconButton(
|
|
icon: const Icon(Icons.edit_outlined),
|
|
onPressed: () {
|
|
_updateValid = false;
|
|
_updateConnectionName = key;
|
|
_updateConnection = widget.connections[key];
|
|
|
|
openEditDialog(true, () {
|
|
setState(() {
|
|
widget.connections.removeWhere((k, _) => k == key);
|
|
widget.prefs.setString(
|
|
"connections", jsonEncode(widget.connections));
|
|
});
|
|
Navigator.pop(context, 'Cancel');
|
|
}, () {
|
|
if (!_updateValid) return;
|
|
setState(() {
|
|
widget.connections.removeWhere((k, _) => k == key);
|
|
widget.connections[_updateConnectionName] =
|
|
_updateConnection;
|
|
widget.prefs.setString(
|
|
"connections", jsonEncode(widget.connections));
|
|
});
|
|
Navigator.pop(context, 'Save');
|
|
}, "Save", "Edit Instance");
|
|
},
|
|
),
|
|
onTap: () {
|
|
widget.prefs.setString("selectedConnection", key);
|
|
Navigator.pop(context, 'Select');
|
|
widget.initWs();
|
|
},
|
|
),
|
|
),
|
|
const SizedBox(height: 5),
|
|
TextButton.icon(
|
|
onPressed: () {
|
|
_updateValid = false;
|
|
_updateConnection = {"url": ""};
|
|
_updateConnectionName =
|
|
"Connection #${DateTime.now().millisecondsSinceEpoch.remainder(100000)}";
|
|
openEditDialog(false, () {}, () {
|
|
if (!_updateValid) return;
|
|
setState(() {
|
|
widget.connections[_updateConnectionName] =
|
|
_updateConnection;
|
|
widget.prefs.setString(
|
|
"connections", jsonEncode(widget.connections));
|
|
});
|
|
Navigator.pop(context, 'Save');
|
|
}, "Create", "Create Connection");
|
|
},
|
|
icon: const Icon(Icons.add),
|
|
label: const Text("New connection"))
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|