summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOrangerot <purple@orangerot.dev>2025-01-07 03:35:33 +0100
committerOrangerot <purple@orangerot.dev>2025-01-07 03:35:33 +0100
commit0178eb5ffcc6ba798ab5f30c055d4be35610c655 (patch)
treea1aadf7b34190941e7077e86c829e096fb11f5b4
parent36e859d2fe431a27b388ac9d8a13ade921a375a7 (diff)
style: refactored ESenseConnectDialog into own Widget
-rw-r--r--lib/esense_connect_dialog.dart52
-rw-r--r--lib/level_selection.dart66
2 files changed, 73 insertions, 45 deletions
diff --git a/lib/esense_connect_dialog.dart b/lib/esense_connect_dialog.dart
new file mode 100644
index 0000000..b598174
--- /dev/null
+++ b/lib/esense_connect_dialog.dart
@@ -0,0 +1,52 @@
+import 'package:flutter/material.dart';
+
+class ESenseConnectDialog extends StatefulWidget {
+ final void Function(String) connect;
+ final ValueNotifier<String> deviceStatus;
+ const ESenseConnectDialog(
+ {super.key, required this.deviceStatus, required this.connect});
+
+ @override
+ State<ESenseConnectDialog> createState() => _ESenseConnectDialogState();
+}
+
+class _ESenseConnectDialogState extends State<ESenseConnectDialog> {
+ String eSenseDeviceName = '';
+
+ @override
+ Widget build(BuildContext context) {
+ return AlertDialog(
+ title: const Text('Connect to ESense'),
+ content: Column(mainAxisSize: MainAxisSize.min, children: [
+ TextField(
+ onChanged: (input) {
+ setState(() {
+ eSenseDeviceName = input;
+ });
+ },
+ decoration: InputDecoration(
+ border: OutlineInputBorder(),
+ hintText: 'eSense-xxxx',
+ labelText: 'Device name',
+ ),
+ ),
+ // Text(eSenseDeviceName),
+ ValueListenableBuilder(
+ valueListenable: widget.deviceStatus,
+ builder: (BuildContext context, String value, Widget? child) {
+ return Text(value);
+ }),
+ ]),
+ actions: <Widget>[
+ TextButton(
+ onPressed: () => Navigator.pop(context, 'Cancel'),
+ child: const Text('Discard'),
+ ),
+ TextButton(
+ onPressed: () => widget.connect(eSenseDeviceName),
+ child: const Text('Connect'),
+ ),
+ ],
+ );
+ }
+}
diff --git a/lib/level_selection.dart b/lib/level_selection.dart
index 5ba7a21..e902304 100644
--- a/lib/level_selection.dart
+++ b/lib/level_selection.dart
@@ -4,6 +4,7 @@ import 'package:esense_flutter/esense.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
+import 'package:sense_the_rhythm/esense_connect_dialog.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'level.dart';
@@ -19,15 +20,17 @@ class _LevelSelectionState extends State<LevelSelection> {
String? stepmaniaCoursesPath;
List<FileSystemEntity> stepmaniaCoursesFolders = [];
- ESenseManager? eSenseManager;
- String _deviceStatus = '';
String eSenseDeviceName = '';
+ ESenseManager? eSenseManager;
+ ValueNotifier<String> _deviceStatus = ValueNotifier('');
+ // String _deviceStatus = '';
bool connected = false;
bool sampling = false;
@override
void initState() {
super.initState();
+ _listenToESense();
loadFolderPath();
}
@@ -61,40 +64,42 @@ class _LevelSelectionState extends State<LevelSelection> {
connected = false;
switch (event.type) {
case ConnectionType.connected:
- _deviceStatus = 'connected';
+ _deviceStatus.value = 'connected';
connected = true;
break;
case ConnectionType.unknown:
- _deviceStatus = 'unknown';
+ _deviceStatus.value = 'unknown';
break;
case ConnectionType.disconnected:
- _deviceStatus = 'disconnected';
+ _deviceStatus.value = 'disconnected';
sampling = false;
break;
case ConnectionType.device_found:
- _deviceStatus = 'device_found';
+ _deviceStatus.value = 'device_found';
break;
case ConnectionType.device_not_found:
- _deviceStatus = 'device_not_found';
+ _deviceStatus.value = 'device_not_found';
break;
}
});
});
}
- Future<void> _connectToESense() async {
+ Future<void> _connectToESense(String deviceName) async {
if (!connected) {
await _askForPermissions();
print('Trying to connect to eSense device...');
+ setState(() {
+ eSenseDeviceName = deviceName;
+ });
print(eSenseDeviceName);
eSenseManager = ESenseManager(eSenseDeviceName);
connected = await eSenseManager!.connect();
print('success!');
setState(() {
- _deviceStatus = connected ? 'connecting...' : 'connection failed';
+ _deviceStatus.value = connected ? 'connecting...' : 'connection failed';
});
- _listenToESense();
}
}
@@ -147,43 +152,14 @@ class _LevelSelectionState extends State<LevelSelection> {
title: const Text('Sense the Rhythm'),
actions: [
IconButton(
- onPressed: () => showDialog<String>(
+ onPressed: () => showDialog(
context: context,
builder: (BuildContext context) {
- return AlertDialog(
- title: const Text('Connect to ESense'),
- content: StatefulBuilder(builder:
- (BuildContext context, StateSetter setState) {
- return Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- TextField(
- onChanged: (input) {
- setState(() {
- eSenseDeviceName = input;
- });
- },
- decoration: InputDecoration(
- border: OutlineInputBorder(),
- hintText: 'eSense-xxxx',
- labelText: 'Device name',
- ),
- ),
- Text(eSenseDeviceName),
- Text(_deviceStatus)
- ]);
- }),
- actions: <Widget>[
- TextButton(
- onPressed: () => Navigator.pop(context, 'Cancel'),
- child: const Text('Discard'),
- ),
- TextButton(
- onPressed: () => _connectToESense(),
- child: const Text('Connect'),
- ),
- ],
- );
+ return ESenseConnectDialog(
+ deviceStatus: _deviceStatus,
+ connect: (String name) {
+ _connectToESense(name);
+ });
},
),
icon: const Icon(Icons.bluetooth))