style: refactored ESenseConnectDialog into own Widget

This commit is contained in:
Orangerot 2025-01-07 03:35:33 +01:00
parent 36e859d2fe
commit 0178eb5ffc
2 changed files with 73 additions and 45 deletions

View file

@ -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'),
),
],
);
}
}

View file

@ -4,6 +4,7 @@ import 'package:esense_flutter/esense.dart';
import 'package:file_picker/file_picker.dart'; import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.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 'package:shared_preferences/shared_preferences.dart';
import 'level.dart'; import 'level.dart';
@ -19,15 +20,17 @@ class _LevelSelectionState extends State<LevelSelection> {
String? stepmaniaCoursesPath; String? stepmaniaCoursesPath;
List<FileSystemEntity> stepmaniaCoursesFolders = []; List<FileSystemEntity> stepmaniaCoursesFolders = [];
ESenseManager? eSenseManager;
String _deviceStatus = '';
String eSenseDeviceName = ''; String eSenseDeviceName = '';
ESenseManager? eSenseManager;
ValueNotifier<String> _deviceStatus = ValueNotifier('');
// String _deviceStatus = '';
bool connected = false; bool connected = false;
bool sampling = false; bool sampling = false;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_listenToESense();
loadFolderPath(); loadFolderPath();
} }
@ -61,40 +64,42 @@ class _LevelSelectionState extends State<LevelSelection> {
connected = false; connected = false;
switch (event.type) { switch (event.type) {
case ConnectionType.connected: case ConnectionType.connected:
_deviceStatus = 'connected'; _deviceStatus.value = 'connected';
connected = true; connected = true;
break; break;
case ConnectionType.unknown: case ConnectionType.unknown:
_deviceStatus = 'unknown'; _deviceStatus.value = 'unknown';
break; break;
case ConnectionType.disconnected: case ConnectionType.disconnected:
_deviceStatus = 'disconnected'; _deviceStatus.value = 'disconnected';
sampling = false; sampling = false;
break; break;
case ConnectionType.device_found: case ConnectionType.device_found:
_deviceStatus = 'device_found'; _deviceStatus.value = 'device_found';
break; break;
case ConnectionType.device_not_found: case ConnectionType.device_not_found:
_deviceStatus = 'device_not_found'; _deviceStatus.value = 'device_not_found';
break; break;
} }
}); });
}); });
} }
Future<void> _connectToESense() async { Future<void> _connectToESense(String deviceName) async {
if (!connected) { if (!connected) {
await _askForPermissions(); await _askForPermissions();
print('Trying to connect to eSense device...'); print('Trying to connect to eSense device...');
setState(() {
eSenseDeviceName = deviceName;
});
print(eSenseDeviceName); print(eSenseDeviceName);
eSenseManager = ESenseManager(eSenseDeviceName); eSenseManager = ESenseManager(eSenseDeviceName);
connected = await eSenseManager!.connect(); connected = await eSenseManager!.connect();
print('success!'); print('success!');
setState(() { setState(() {
_deviceStatus = connected ? 'connecting...' : 'connection failed'; _deviceStatus.value = connected ? 'connecting...' : 'connection failed';
}); });
_listenToESense();
} }
} }
@ -147,44 +152,15 @@ class _LevelSelectionState extends State<LevelSelection> {
title: const Text('Sense the Rhythm'), title: const Text('Sense the Rhythm'),
actions: [ actions: [
IconButton( IconButton(
onPressed: () => showDialog<String>( onPressed: () => showDialog(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
return AlertDialog( return ESenseConnectDialog(
title: const Text('Connect to ESense'), deviceStatus: _deviceStatus,
content: StatefulBuilder(builder: connect: (String name) {
(BuildContext context, StateSetter setState) { _connectToESense(name);
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'),
),
],
);
},
), ),
icon: const Icon(Icons.bluetooth)) icon: const Icon(Icons.bluetooth))
], ],