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: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))