feat: refactor input source handling and add audio input service
This commit is contained in:
@@ -13,15 +13,15 @@ import 'package:lbjconsole/services/database_service.dart';
|
||||
import 'package:lbjconsole/services/notification_service.dart';
|
||||
import 'package:lbjconsole/services/background_service.dart';
|
||||
import 'package:lbjconsole/services/rtl_tcp_service.dart';
|
||||
import 'package:lbjconsole/services/audio_input_service.dart';
|
||||
import 'package:lbjconsole/themes/app_theme.dart';
|
||||
import 'dart:convert';
|
||||
|
||||
class _ConnectionStatusWidget extends StatefulWidget {
|
||||
final BLEService bleService;
|
||||
final RtlTcpService rtlTcpService;
|
||||
final DateTime? lastReceivedTime;
|
||||
final DateTime? rtlTcpLastReceivedTime;
|
||||
final bool rtlTcpEnabled;
|
||||
final InputSource inputSource;
|
||||
final bool rtlTcpConnected;
|
||||
|
||||
const _ConnectionStatusWidget({
|
||||
@@ -29,7 +29,7 @@ class _ConnectionStatusWidget extends StatefulWidget {
|
||||
required this.rtlTcpService,
|
||||
required this.lastReceivedTime,
|
||||
required this.rtlTcpLastReceivedTime,
|
||||
required this.rtlTcpEnabled,
|
||||
required this.inputSource,
|
||||
required this.rtlTcpConnected,
|
||||
});
|
||||
|
||||
@@ -59,6 +59,15 @@ class _ConnectionStatusWidgetState extends State<_ConnectionStatusWidget> {
|
||||
_deviceStatus = widget.bleService.deviceStatus;
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant _ConnectionStatusWidget oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.inputSource != widget.inputSource ||
|
||||
oldWidget.rtlTcpConnected != widget.rtlTcpConnected) {
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_connectionSubscription?.cancel();
|
||||
@@ -67,18 +76,31 @@ class _ConnectionStatusWidgetState extends State<_ConnectionStatusWidget> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isRtlTcpMode = widget.rtlTcpEnabled;
|
||||
final rtlTcpConnected = widget.rtlTcpConnected;
|
||||
|
||||
final isConnected = isRtlTcpMode ? rtlTcpConnected : _isConnected;
|
||||
final statusColor = isRtlTcpMode
|
||||
? (rtlTcpConnected ? Colors.green : Colors.red)
|
||||
: (_isConnected ? Colors.green : Colors.red);
|
||||
final statusText = isRtlTcpMode
|
||||
? (rtlTcpConnected ? '已连接' : '未连接')
|
||||
: _deviceStatus;
|
||||
|
||||
final lastReceivedTime = isRtlTcpMode ? widget.rtlTcpLastReceivedTime : widget.lastReceivedTime;
|
||||
bool isConnected;
|
||||
Color statusColor;
|
||||
String statusText;
|
||||
DateTime? displayTime;
|
||||
|
||||
switch (widget.inputSource) {
|
||||
case InputSource.rtlTcp:
|
||||
isConnected = widget.rtlTcpConnected;
|
||||
statusColor = isConnected ? Colors.green : Colors.red;
|
||||
statusText = isConnected ? '已连接' : '未连接';
|
||||
displayTime = widget.rtlTcpLastReceivedTime;
|
||||
break;
|
||||
case InputSource.audioInput:
|
||||
isConnected = AudioInputService().isListening;
|
||||
statusColor = isConnected ? Colors.green : Colors.red;
|
||||
statusText = isConnected ? '监听中' : '已停止';
|
||||
displayTime = widget.rtlTcpLastReceivedTime ?? widget.lastReceivedTime;
|
||||
break;
|
||||
case InputSource.bluetooth:
|
||||
isConnected = _isConnected;
|
||||
statusColor = isConnected ? Colors.green : Colors.red;
|
||||
statusText = _deviceStatus;
|
||||
displayTime = widget.lastReceivedTime;
|
||||
break;
|
||||
}
|
||||
|
||||
return Row(
|
||||
children: [
|
||||
@@ -86,12 +108,12 @@ class _ConnectionStatusWidgetState extends State<_ConnectionStatusWidget> {
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (lastReceivedTime == null || !isConnected) ...[
|
||||
if (displayTime == null || !isConnected) ...[
|
||||
Text(statusText,
|
||||
style: const TextStyle(color: Colors.white70, fontSize: 12)),
|
||||
],
|
||||
_LastReceivedTimeWidget(
|
||||
lastReceivedTime: lastReceivedTime,
|
||||
lastReceivedTime: displayTime,
|
||||
isConnected: isConnected,
|
||||
),
|
||||
],
|
||||
@@ -215,7 +237,9 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
||||
DateTime? _lastReceivedTime;
|
||||
DateTime? _rtlTcpLastReceivedTime;
|
||||
bool _isHistoryEditMode = false;
|
||||
bool _rtlTcpEnabled = false;
|
||||
|
||||
InputSource _inputSource = InputSource.bluetooth;
|
||||
|
||||
bool _rtlTcpConnected = false;
|
||||
bool _isConnected = false;
|
||||
final GlobalKey<HistoryScreenState> _historyScreenKey =
|
||||
@@ -230,7 +254,7 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
||||
_bleService = BLEService();
|
||||
_rtlTcpService = RtlTcpService();
|
||||
_bleService.initialize();
|
||||
_loadRtlTcpSettings();
|
||||
_loadInputSettings();
|
||||
_initializeServices();
|
||||
_checkAndStartBackgroundService();
|
||||
_setupConnectionListener();
|
||||
@@ -248,23 +272,26 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
||||
}
|
||||
}
|
||||
|
||||
void _loadRtlTcpSettings() async {
|
||||
developer.log('rtl_tcp: load_settings');
|
||||
void _loadInputSettings() async {
|
||||
final settings = await _databaseService.getAllSettings();
|
||||
developer.log('rtl_tcp: settings_loaded: enabled=${(settings?['rtlTcpEnabled'] ?? 0) == 1}, host=${settings?['rtlTcpHost']?.toString() ?? '127.0.0.1'}, port=${settings?['rtlTcpPort']?.toString() ?? '14423'}');
|
||||
final sourceStr = settings?['inputSource'] as String? ?? 'bluetooth';
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_rtlTcpEnabled = (settings?['rtlTcpEnabled'] ?? 0) == 1;
|
||||
_inputSource = InputSource.values.firstWhere(
|
||||
(e) => e.name == sourceStr,
|
||||
orElse: () => InputSource.bluetooth,
|
||||
);
|
||||
_rtlTcpConnected = _rtlTcpService.isConnected;
|
||||
});
|
||||
|
||||
if (_rtlTcpEnabled && !_rtlTcpConnected) {
|
||||
if (_inputSource == InputSource.rtlTcp && !_rtlTcpConnected) {
|
||||
final host = settings?['rtlTcpHost']?.toString() ?? '127.0.0.1';
|
||||
final port = settings?['rtlTcpPort']?.toString() ?? '14423';
|
||||
developer.log('rtl_tcp: auto_connect');
|
||||
_connectToRtlTcp(host, port);
|
||||
} else {
|
||||
developer.log('rtl_tcp: skip_connect: enabled=$_rtlTcpEnabled, connected=$_rtlTcpConnected');
|
||||
} else if (_inputSource == InputSource.audioInput) {
|
||||
await AudioInputService().startListening();
|
||||
setState(() {});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -292,41 +319,47 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
||||
_rtlTcpLastReceivedTimeSubscription =
|
||||
_rtlTcpService.lastReceivedTimeStream.listen((time) {
|
||||
if (mounted) {
|
||||
if (_rtlTcpEnabled) {
|
||||
setState(() {
|
||||
_rtlTcpLastReceivedTime = time;
|
||||
});
|
||||
}
|
||||
setState(() {
|
||||
_rtlTcpLastReceivedTime = time;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _setupSettingsListener() {
|
||||
developer.log('rtl_tcp: setup_listener');
|
||||
_settingsSubscription =
|
||||
DatabaseService.instance.onSettingsChanged((settings) {
|
||||
developer.log('rtl_tcp: settings_changed: enabled=${(settings['rtlTcpEnabled'] ?? 0) == 1}, host=${settings['rtlTcpHost']?.toString() ?? '127.0.0.1'}, port=${settings['rtlTcpPort']?.toString() ?? '14423'}');
|
||||
if (mounted) {
|
||||
final rtlTcpEnabled = (settings['rtlTcpEnabled'] ?? 0) == 1;
|
||||
if (rtlTcpEnabled != _rtlTcpEnabled) {
|
||||
setState(() {
|
||||
_rtlTcpEnabled = rtlTcpEnabled;
|
||||
});
|
||||
|
||||
if (rtlTcpEnabled) {
|
||||
final host = settings['rtlTcpHost']?.toString() ?? '127.0.0.1';
|
||||
final port = settings['rtlTcpPort']?.toString() ?? '14423';
|
||||
_connectToRtlTcp(host, port);
|
||||
} else {
|
||||
_rtlTcpConnectionSubscription?.cancel();
|
||||
_rtlTcpDataSubscription?.cancel();
|
||||
_rtlTcpLastReceivedTimeSubscription?.cancel();
|
||||
_rtlTcpService.disconnect();
|
||||
setState(() {
|
||||
_rtlTcpConnected = false;
|
||||
_rtlTcpLastReceivedTime = null;
|
||||
});
|
||||
}
|
||||
final sourceStr = settings['inputSource'] as String? ?? 'bluetooth';
|
||||
print('[MainScreen] Settings changed: inputSource=$sourceStr');
|
||||
final newInputSource = InputSource.values.firstWhere(
|
||||
(e) => e.name == sourceStr,
|
||||
orElse: () => InputSource.bluetooth,
|
||||
);
|
||||
|
||||
print('[MainScreen] Current: $_inputSource, New: $newInputSource');
|
||||
|
||||
setState(() {
|
||||
_inputSource = newInputSource;
|
||||
});
|
||||
|
||||
switch (newInputSource) {
|
||||
case InputSource.rtlTcp:
|
||||
setState(() {
|
||||
_rtlTcpConnected = _rtlTcpService.isConnected;
|
||||
});
|
||||
print('[MainScreen] RTL-TCP mode, connected: $_rtlTcpConnected');
|
||||
break;
|
||||
case InputSource.audioInput:
|
||||
setState(() {});
|
||||
break;
|
||||
case InputSource.bluetooth:
|
||||
_rtlTcpService.disconnect();
|
||||
setState(() {
|
||||
_rtlTcpConnected = false;
|
||||
_rtlTcpLastReceivedTime = null;
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
if (_currentIndex == 1) {
|
||||
@@ -347,20 +380,16 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
||||
|
||||
_rtlTcpConnectionSubscription = _rtlTcpService.connectionStream.listen((connected) {
|
||||
if (mounted) {
|
||||
if (_rtlTcpEnabled) {
|
||||
setState(() {
|
||||
_rtlTcpConnected = connected;
|
||||
});
|
||||
}
|
||||
setState(() {
|
||||
_rtlTcpConnected = connected;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _connectToRtlTcp(String host, String port) async {
|
||||
developer.log('rtl_tcp: connect: $host:$port');
|
||||
try {
|
||||
await _rtlTcpService.connect(host: host, port: port);
|
||||
developer.log('rtl_tcp: connect_req_sent');
|
||||
} catch (e) {
|
||||
developer.log('rtl_tcp: connect_fail: $e');
|
||||
}
|
||||
@@ -391,38 +420,37 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
||||
await _notificationService.initialize();
|
||||
|
||||
_dataSubscription = _bleService.dataStream.listen((record) {
|
||||
_notificationService.showTrainNotification(record);
|
||||
if (_historyScreenKey.currentState != null) {
|
||||
_historyScreenKey.currentState!.addNewRecord(record);
|
||||
}
|
||||
if (_realtimeScreenKey.currentState != null) {
|
||||
_realtimeScreenKey.currentState!.addNewRecord(record);
|
||||
if (_inputSource == InputSource.bluetooth) {
|
||||
_processRecord(record);
|
||||
}
|
||||
});
|
||||
|
||||
_rtlTcpDataSubscription = _rtlTcpService.dataStream.listen((record) {
|
||||
developer.log('rtl_tcp: recv_data: train=${record.train}');
|
||||
developer.log('rtl_tcp: recv_json: ${jsonEncode(record.toJson())}');
|
||||
_notificationService.showTrainNotification(record);
|
||||
if (_historyScreenKey.currentState != null) {
|
||||
_historyScreenKey.currentState!.addNewRecord(record);
|
||||
}
|
||||
if (_realtimeScreenKey.currentState != null) {
|
||||
_realtimeScreenKey.currentState!.addNewRecord(record);
|
||||
if (_inputSource != InputSource.bluetooth) {
|
||||
_processRecord(record);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void _processRecord(record) {
|
||||
_notificationService.showTrainNotification(record);
|
||||
_historyScreenKey.currentState?.addNewRecord(record);
|
||||
_realtimeScreenKey.currentState?.addNewRecord(record);
|
||||
}
|
||||
|
||||
void _showConnectionDialog() {
|
||||
_bleService.setAutoConnectBlocked(true);
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: true,
|
||||
builder: (context) =>
|
||||
_PixelPerfectBluetoothDialog(bleService: _bleService, rtlTcpEnabled: _rtlTcpEnabled),
|
||||
_PixelPerfectBluetoothDialog(
|
||||
bleService: _bleService,
|
||||
inputSource: _inputSource
|
||||
),
|
||||
).then((_) {
|
||||
_bleService.setAutoConnectBlocked(false);
|
||||
if (!_bleService.isManualDisconnect) {
|
||||
if (_inputSource == InputSource.bluetooth && !_bleService.isManualDisconnect) {
|
||||
_bleService.ensureConnection();
|
||||
}
|
||||
});
|
||||
@@ -452,6 +480,12 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
||||
);
|
||||
}
|
||||
|
||||
final IconData statusIcon = switch (_inputSource) {
|
||||
InputSource.rtlTcp => Icons.wifi,
|
||||
InputSource.audioInput => Icons.mic,
|
||||
InputSource.bluetooth => Icons.bluetooth,
|
||||
};
|
||||
|
||||
return AppBar(
|
||||
backgroundColor: AppTheme.primaryBlack,
|
||||
elevation: 0,
|
||||
@@ -469,12 +503,12 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
||||
rtlTcpService: _rtlTcpService,
|
||||
lastReceivedTime: _lastReceivedTime,
|
||||
rtlTcpLastReceivedTime: _rtlTcpLastReceivedTime,
|
||||
rtlTcpEnabled: _rtlTcpEnabled,
|
||||
inputSource: _inputSource,
|
||||
rtlTcpConnected: _rtlTcpConnected,
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
_rtlTcpEnabled ? Icons.wifi : Icons.bluetooth,
|
||||
statusIcon,
|
||||
color: Colors.white,
|
||||
),
|
||||
onPressed: _showConnectionDialog,
|
||||
@@ -562,7 +596,6 @@ class _MainScreenState extends State<MainScreen> with WidgetsBindingObserver {
|
||||
SettingsScreen(
|
||||
onSettingsChanged: () {
|
||||
_loadMapType();
|
||||
_loadRtlTcpSettings();
|
||||
},
|
||||
),
|
||||
];
|
||||
@@ -609,8 +642,8 @@ enum _ScanState { initial, scanning, finished }
|
||||
|
||||
class _PixelPerfectBluetoothDialog extends StatefulWidget {
|
||||
final BLEService bleService;
|
||||
final bool rtlTcpEnabled;
|
||||
const _PixelPerfectBluetoothDialog({required this.bleService, required this.rtlTcpEnabled});
|
||||
final InputSource inputSource;
|
||||
const _PixelPerfectBluetoothDialog({required this.bleService, required this.inputSource});
|
||||
@override
|
||||
State<_PixelPerfectBluetoothDialog> createState() =>
|
||||
_PixelPerfectBluetoothDialogState();
|
||||
@@ -625,6 +658,7 @@ class _PixelPerfectBluetoothDialogState
|
||||
DateTime? _lastReceivedTime;
|
||||
StreamSubscription? _rtlTcpConnectionSubscription;
|
||||
bool _rtlTcpConnected = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -640,11 +674,11 @@ class _PixelPerfectBluetoothDialogState
|
||||
}
|
||||
});
|
||||
|
||||
if (widget.rtlTcpEnabled && widget.bleService.rtlTcpService != null) {
|
||||
if (widget.inputSource == InputSource.rtlTcp && widget.bleService.rtlTcpService != null) {
|
||||
_rtlTcpConnected = widget.bleService.rtlTcpService!.isConnected;
|
||||
}
|
||||
|
||||
if (!widget.bleService.isConnected && !widget.rtlTcpEnabled) {
|
||||
if (!widget.bleService.isConnected && widget.inputSource == InputSource.bluetooth) {
|
||||
_startScan();
|
||||
}
|
||||
}
|
||||
@@ -684,31 +718,24 @@ class _PixelPerfectBluetoothDialogState
|
||||
await widget.bleService.disconnect();
|
||||
}
|
||||
|
||||
void _setupLastReceivedTimeListener() {
|
||||
_lastReceivedTimeSubscription =
|
||||
widget.bleService.lastReceivedTimeStream.listen((timestamp) {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_lastReceivedTime = timestamp;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isConnected = widget.bleService.isConnected;
|
||||
final (String title, Widget content) = switch (widget.inputSource) {
|
||||
InputSource.rtlTcp => ('RTL-TCP 服务器', _buildRtlTcpView(context)),
|
||||
InputSource.audioInput => ('音频输入', _buildAudioInputView(context)),
|
||||
InputSource.bluetooth => (
|
||||
'蓝牙设备',
|
||||
widget.bleService.isConnected
|
||||
? _buildConnectedView(context, widget.bleService.connectedDevice)
|
||||
: _buildDisconnectedView(context)
|
||||
),
|
||||
};
|
||||
|
||||
return AlertDialog(
|
||||
title: Text(widget.rtlTcpEnabled ? 'RTL-TCP 服务器' : '蓝牙设备'),
|
||||
title: Text(title),
|
||||
content: SizedBox(
|
||||
width: double.maxFinite,
|
||||
child: SingleChildScrollView(
|
||||
child: widget.rtlTcpEnabled
|
||||
? _buildRtlTcpView(context)
|
||||
: (isConnected
|
||||
? _buildConnectedView(context, widget.bleService.connectedDevice)
|
||||
: _buildDisconnectedView(context)),
|
||||
),
|
||||
child: SingleChildScrollView(child: content),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
@@ -733,13 +760,6 @@ class _PixelPerfectBluetoothDialogState
|
||||
Text(device?.remoteId.str ?? '',
|
||||
style: Theme.of(context).textTheme.bodySmall,
|
||||
textAlign: TextAlign.center),
|
||||
if (_lastReceivedTime != null) ...[
|
||||
const SizedBox(height: 8),
|
||||
_LastReceivedTimeWidget(
|
||||
lastReceivedTime: _lastReceivedTime,
|
||||
isConnected: widget.bleService.isConnected,
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _disconnect,
|
||||
@@ -785,13 +805,21 @@ class _PixelPerfectBluetoothDialogState
|
||||
const SizedBox(height: 8),
|
||||
Text(currentAddress,
|
||||
style: TextStyle(color: isConnected ? Colors.green : Colors.grey)),
|
||||
]);
|
||||
}
|
||||
|
||||
Widget _buildAudioInputView(BuildContext context) {
|
||||
return Column(mainAxisSize: MainAxisSize.min, children: [
|
||||
const Icon(Icons.mic, size: 48, color: Colors.blue),
|
||||
const SizedBox(height: 16),
|
||||
if (_lastReceivedTime != null && isConnected) ...[
|
||||
_LastReceivedTimeWidget(
|
||||
lastReceivedTime: _lastReceivedTime,
|
||||
isConnected: isConnected,
|
||||
),
|
||||
],
|
||||
Text('监听中',
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(fontWeight: FontWeight.bold)),
|
||||
const SizedBox(height: 8),
|
||||
const Text("请使用音频线连接设备",
|
||||
style: TextStyle(color: Colors.grey)),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -818,4 +846,4 @@ class _PixelPerfectBluetoothDialogState
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ import 'dart:io';
|
||||
import 'package:lbjconsole/models/merged_record.dart';
|
||||
import 'package:lbjconsole/services/database_service.dart';
|
||||
import 'package:lbjconsole/services/background_service.dart';
|
||||
import 'package:lbjconsole/services/audio_input_service.dart';
|
||||
import 'package:lbjconsole/services/rtl_tcp_service.dart';
|
||||
import 'package:lbjconsole/themes/app_theme.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
@@ -37,7 +39,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
GroupBy _groupBy = GroupBy.trainAndLoco;
|
||||
TimeWindow _timeWindow = TimeWindow.unlimited;
|
||||
String _mapType = 'map';
|
||||
bool _rtlTcpEnabled = false;
|
||||
|
||||
InputSource _inputSource = InputSource.bluetooth;
|
||||
|
||||
String _rtlTcpHost = '127.0.0.1';
|
||||
String _rtlTcpPort = '14423';
|
||||
|
||||
@@ -52,131 +56,6 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
_loadRecordCount();
|
||||
}
|
||||
|
||||
Widget _buildRtlTcpSettings() {
|
||||
return Card(
|
||||
color: AppTheme.tertiaryBlack,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(16.0),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.wifi,
|
||||
color: Theme.of(context).colorScheme.primary),
|
||||
const SizedBox(width: 12),
|
||||
const Text('RTL-TCP 源', style: AppTheme.titleMedium),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('启用 RTL-TCP 源', style: AppTheme.bodyLarge),
|
||||
],
|
||||
),
|
||||
Switch(
|
||||
value: _rtlTcpEnabled,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_rtlTcpEnabled = value;
|
||||
});
|
||||
_saveSettings();
|
||||
},
|
||||
activeThumbColor: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
],
|
||||
),
|
||||
Visibility(
|
||||
visible: _rtlTcpEnabled,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
decoration: InputDecoration(
|
||||
labelText: '服务器地址',
|
||||
hintText: '输入RTL-TCP服务器地址',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
hintStyle: const TextStyle(color: Colors.white54),
|
||||
border: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderSide:
|
||||
BorderSide(color: Theme.of(context).colorScheme.primary),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
controller: _rtlTcpHostController,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_rtlTcpHost = value;
|
||||
});
|
||||
_saveSettings();
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
decoration: InputDecoration(
|
||||
labelText: '服务器端口',
|
||||
hintText: '输入RTL-TCP服务器端口',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
hintStyle: const TextStyle(color: Colors.white54),
|
||||
border: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderSide:
|
||||
BorderSide(color: Theme.of(context).colorScheme.primary),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
controller: _rtlTcpPortController,
|
||||
keyboardType: TextInputType.number,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_rtlTcpPort = value;
|
||||
});
|
||||
_saveSettings();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_deviceNameController.dispose();
|
||||
_rtlTcpHostController.dispose();
|
||||
_rtlTcpPortController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _loadSettings() async {
|
||||
final settingsMap = await _databaseService.getAllSettings() ?? {};
|
||||
final settings = MergeSettings.fromMap(settingsMap);
|
||||
@@ -193,20 +72,17 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
_groupBy = settings.groupBy;
|
||||
_timeWindow = settings.timeWindow;
|
||||
_mapType = settingsMap['mapType']?.toString() ?? 'webview';
|
||||
_rtlTcpEnabled = (settingsMap['rtlTcpEnabled'] ?? 0) == 1;
|
||||
|
||||
_rtlTcpHost = settingsMap['rtlTcpHost']?.toString() ?? '127.0.0.1';
|
||||
_rtlTcpPort = settingsMap['rtlTcpPort']?.toString() ?? '14423';
|
||||
_rtlTcpHostController.text = _rtlTcpHost;
|
||||
_rtlTcpPortController.text = _rtlTcpPort;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _loadRecordCount() async {
|
||||
final count = await _databaseService.getRecordCount();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_recordCount = count;
|
||||
final sourceStr = settingsMap['inputSource'] as String? ?? 'bluetooth';
|
||||
_inputSource = InputSource.values.firstWhere(
|
||||
(e) => e.name == sourceStr,
|
||||
orElse: () => InputSource.bluetooth,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -222,37 +98,43 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
'groupBy': _groupBy.name,
|
||||
'timeWindow': _timeWindow.name,
|
||||
'mapType': _mapType,
|
||||
'rtlTcpEnabled': _rtlTcpEnabled ? 1 : 0,
|
||||
'inputSource': _inputSource.name,
|
||||
'rtlTcpHost': _rtlTcpHost,
|
||||
'rtlTcpPort': _rtlTcpPort,
|
||||
});
|
||||
widget.onSettingsChanged?.call();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildBluetoothSettings(),
|
||||
const SizedBox(height: 20),
|
||||
_buildAppSettings(),
|
||||
const SizedBox(height: 20),
|
||||
_buildRtlTcpSettings(),
|
||||
const SizedBox(height: 20),
|
||||
_buildMergeSettings(),
|
||||
const SizedBox(height: 20),
|
||||
_buildDataManagement(),
|
||||
const SizedBox(height: 20),
|
||||
_buildAboutSection(),
|
||||
],
|
||||
),
|
||||
);
|
||||
Future<void> _switchInputSource(InputSource newSource) async {
|
||||
await AudioInputService().stopListening();
|
||||
await RtlTcpService().disconnect();
|
||||
setState(() {
|
||||
_inputSource = newSource;
|
||||
});
|
||||
|
||||
switch (newSource) {
|
||||
case InputSource.audioInput:
|
||||
await AudioInputService().startListening();
|
||||
break;
|
||||
case InputSource.rtlTcp:
|
||||
RtlTcpService().connect(host: _rtlTcpHost, port: _rtlTcpPort);
|
||||
break;
|
||||
case InputSource.bluetooth:
|
||||
break;
|
||||
}
|
||||
|
||||
_saveSettings();
|
||||
}
|
||||
|
||||
Widget _buildBluetoothSettings() {
|
||||
@override
|
||||
void dispose() {
|
||||
_deviceNameController.dispose();
|
||||
_rtlTcpHostController.dispose();
|
||||
_rtlTcpPortController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Widget _buildInputSourceSettings() {
|
||||
return Card(
|
||||
color: AppTheme.tertiaryBlack,
|
||||
elevation: 0,
|
||||
@@ -266,48 +148,213 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
children: [
|
||||
Row(
|
||||
children: [
|
||||
Icon(Icons.bluetooth,
|
||||
color: Theme.of(context).colorScheme.primary),
|
||||
Icon(Icons.input, color: Theme.of(context).colorScheme.primary),
|
||||
const SizedBox(width: 12),
|
||||
const Text('蓝牙设备', style: AppTheme.titleMedium),
|
||||
const Text('信号源设置', style: AppTheme.titleMedium),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _deviceNameController,
|
||||
decoration: InputDecoration(
|
||||
labelText: '设备名称',
|
||||
hintText: '输入设备名称',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
hintStyle: const TextStyle(color: Colors.white54),
|
||||
border: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
|
||||
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text('信号源', style: AppTheme.bodyLarge),
|
||||
DropdownButton<InputSource>(
|
||||
value: _inputSource,
|
||||
items: const [
|
||||
DropdownMenuItem(
|
||||
value: InputSource.bluetooth,
|
||||
child: Text('蓝牙设备', style: AppTheme.bodyMedium),
|
||||
),
|
||||
DropdownMenuItem(
|
||||
value: InputSource.rtlTcp,
|
||||
child: Text('RTL-TCP', style: AppTheme.bodyMedium),
|
||||
),
|
||||
DropdownMenuItem(
|
||||
value: InputSource.audioInput,
|
||||
child: Text('音频输入', style: AppTheme.bodyMedium),
|
||||
),
|
||||
],
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
_switchInputSource(value);
|
||||
}
|
||||
},
|
||||
dropdownColor: AppTheme.secondaryBlack,
|
||||
style: AppTheme.bodyMedium,
|
||||
underline: Container(height: 0),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
],
|
||||
),
|
||||
|
||||
if (_inputSource == InputSource.bluetooth) ...[
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: _deviceNameController,
|
||||
decoration: InputDecoration(
|
||||
labelText: '蓝牙设备名称',
|
||||
hintText: '输入设备名称',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
hintStyle: const TextStyle(color: Colors.white54),
|
||||
border: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Theme.of(context).colorScheme.primary),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderSide:
|
||||
BorderSide(color: Theme.of(context).colorScheme.primary),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_deviceName = value;
|
||||
});
|
||||
_saveSettings();
|
||||
},
|
||||
),
|
||||
],
|
||||
|
||||
if (_inputSource == InputSource.rtlTcp) ...[
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
decoration: InputDecoration(
|
||||
labelText: '服务器地址',
|
||||
hintText: '127.0.0.1',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
hintStyle: const TextStyle(color: Colors.white54),
|
||||
border: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Theme.of(context).colorScheme.primary),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
controller: _rtlTcpHostController,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_rtlTcpHost = value;
|
||||
});
|
||||
_saveSettings();
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
decoration: InputDecoration(
|
||||
labelText: '服务器端口',
|
||||
hintText: '14423',
|
||||
labelStyle: const TextStyle(color: Colors.white70),
|
||||
hintStyle: const TextStyle(color: Colors.white54),
|
||||
border: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
enabledBorder: OutlineInputBorder(
|
||||
borderSide: const BorderSide(color: Colors.white54),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
focusedBorder: OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Theme.of(context).colorScheme.primary),
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
),
|
||||
),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
controller: _rtlTcpPortController,
|
||||
keyboardType: TextInputType.number,
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_rtlTcpPort = value;
|
||||
});
|
||||
_saveSettings();
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () {
|
||||
RtlTcpService()
|
||||
.connect(host: _rtlTcpHost, port: _rtlTcpPort);
|
||||
},
|
||||
icon: const Icon(Icons.refresh),
|
||||
label: const Text("重新连接 RTL-TCP"),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppTheme.secondaryBlack,
|
||||
foregroundColor: Colors.white,
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
],
|
||||
|
||||
if (_inputSource == InputSource.audioInput) ...[
|
||||
const SizedBox(height: 16),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.blue.withOpacity(0.1),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
border: Border.all(color: Colors.blue.withOpacity(0.3)),
|
||||
),
|
||||
child: const Row(
|
||||
children: [
|
||||
Icon(Icons.mic, color: Colors.blue),
|
||||
SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Text(
|
||||
'音频解调已启用。请通过音频线 (Line-in) 或麦克风输入信号。',
|
||||
style: TextStyle(color: Colors.white70, fontSize: 13),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
style: const TextStyle(color: Colors.white),
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_deviceName = value;
|
||||
});
|
||||
_saveSettings();
|
||||
},
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(20.0),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildInputSourceSettings(),
|
||||
const SizedBox(height: 20),
|
||||
_buildAppSettings(),
|
||||
const SizedBox(height: 20),
|
||||
_buildMergeSettings(),
|
||||
const SizedBox(height: 20),
|
||||
_buildDataManagement(),
|
||||
const SizedBox(height: 20),
|
||||
_buildAboutSection(),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildAppSettings() {
|
||||
return Card(
|
||||
color: AppTheme.tertiaryBlack,
|
||||
@@ -490,86 +537,80 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const SizedBox(height: 16),
|
||||
const Text('分组方式', style: AppTheme.bodyLarge),
|
||||
const SizedBox(height: 8),
|
||||
DropdownButtonFormField<GroupBy>(
|
||||
initialValue: _groupBy,
|
||||
items: const [
|
||||
DropdownMenuItem(
|
||||
value: GroupBy.trainOnly,
|
||||
child: Text('仅车次号', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: GroupBy.locoOnly,
|
||||
child: Text('仅机车号', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: GroupBy.trainOrLoco,
|
||||
child: Text('车次号或机车号', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: GroupBy.trainAndLoco,
|
||||
child: Text('车次号与机车号', style: AppTheme.bodyMedium)),
|
||||
],
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_groupBy = value;
|
||||
});
|
||||
_saveSettings();
|
||||
}
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
filled: true,
|
||||
fillColor: AppTheme.secondaryBlack,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
borderSide: BorderSide.none,
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text('分组方式', style: AppTheme.bodyLarge),
|
||||
DropdownButton<GroupBy>(
|
||||
value: _groupBy,
|
||||
items: const [
|
||||
DropdownMenuItem(
|
||||
value: GroupBy.trainOnly,
|
||||
child: Text('仅车次号', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: GroupBy.locoOnly,
|
||||
child: Text('仅机车号', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: GroupBy.trainOrLoco,
|
||||
child: Text('车次号或机车号', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: GroupBy.trainAndLoco,
|
||||
child: Text('车次号与机车号', style: AppTheme.bodyMedium)),
|
||||
],
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_groupBy = value;
|
||||
});
|
||||
_saveSettings();
|
||||
}
|
||||
},
|
||||
dropdownColor: AppTheme.secondaryBlack,
|
||||
style: AppTheme.bodyMedium,
|
||||
underline: Container(height: 0),
|
||||
),
|
||||
),
|
||||
dropdownColor: AppTheme.secondaryBlack,
|
||||
style: AppTheme.bodyMedium,
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text('时间窗口', style: AppTheme.bodyLarge),
|
||||
const SizedBox(height: 8),
|
||||
DropdownButtonFormField<TimeWindow>(
|
||||
initialValue: _timeWindow,
|
||||
items: const [
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.oneHour,
|
||||
child: Text('1小时内', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.twoHours,
|
||||
child: Text('2小时内', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.sixHours,
|
||||
child: Text('6小时内', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.twelveHours,
|
||||
child: Text('12小时内', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.oneDay,
|
||||
child: Text('24小时内', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.unlimited,
|
||||
child: Text('不限时间', style: AppTheme.bodyMedium)),
|
||||
],
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_timeWindow = value;
|
||||
});
|
||||
_saveSettings();
|
||||
}
|
||||
},
|
||||
decoration: InputDecoration(
|
||||
filled: true,
|
||||
fillColor: AppTheme.secondaryBlack,
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12.0),
|
||||
borderSide: BorderSide.none,
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
const Text('时间窗口', style: AppTheme.bodyLarge),
|
||||
DropdownButton<TimeWindow>(
|
||||
value: _timeWindow,
|
||||
items: const [
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.oneHour,
|
||||
child: Text('1小时内', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.twoHours,
|
||||
child: Text('2小时内', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.sixHours,
|
||||
child: Text('6小时内', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.twelveHours,
|
||||
child: Text('12小时内', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.oneDay,
|
||||
child: Text('24小时内', style: AppTheme.bodyMedium)),
|
||||
DropdownMenuItem(
|
||||
value: TimeWindow.unlimited,
|
||||
child: Text('不限时间', style: AppTheme.bodyMedium)),
|
||||
],
|
||||
onChanged: (value) {
|
||||
if (value != null) {
|
||||
setState(() {
|
||||
_timeWindow = value;
|
||||
});
|
||||
_saveSettings();
|
||||
}
|
||||
},
|
||||
dropdownColor: AppTheme.secondaryBlack,
|
||||
style: AppTheme.bodyMedium,
|
||||
underline: Container(height: 0),
|
||||
),
|
||||
),
|
||||
dropdownColor: AppTheme.secondaryBlack,
|
||||
style: AppTheme.bodyMedium,
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
@@ -712,15 +753,13 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
String _formatFileSize(int bytes) {
|
||||
if (bytes < 1024) return '$bytes B';
|
||||
if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(1)} KB';
|
||||
return '${(bytes / 1024 / 1024).toStringAsFixed(1)} MB';
|
||||
}
|
||||
|
||||
String _formatDateTime(DateTime dateTime) {
|
||||
return '${dateTime.year}-${dateTime.month.toString().padLeft(2, '0')}-${dateTime.day.toString().padLeft(2, '0')} '
|
||||
'${dateTime.hour.toString().padLeft(2, '0')}:${dateTime.minute.toString().padLeft(2, '0')}';
|
||||
Future<void> _loadRecordCount() async {
|
||||
final count = await _databaseService.getRecordCount();
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_recordCount = count;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> _getAppVersion() async {
|
||||
@@ -910,6 +949,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
'mergeRecordsEnabled': 0,
|
||||
'groupBy': 'trainAndLoco',
|
||||
'timeWindow': 'unlimited',
|
||||
'inputSource': 'bluetooth',
|
||||
});
|
||||
|
||||
Navigator.pop(context);
|
||||
|
||||
49
lib/services/audio_input_service.dart
Normal file
49
lib/services/audio_input_service.dart
Normal file
@@ -0,0 +1,49 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'dart:developer' as developer;
|
||||
|
||||
class AudioInputService {
|
||||
static final AudioInputService _instance = AudioInputService._internal();
|
||||
factory AudioInputService() => _instance;
|
||||
AudioInputService._internal();
|
||||
|
||||
static const _methodChannel = MethodChannel('org.noxylva.lbjconsole/audio_input');
|
||||
|
||||
bool _isListening = false;
|
||||
bool get isListening => _isListening;
|
||||
|
||||
Future<bool> startListening() async {
|
||||
if (_isListening) return true;
|
||||
|
||||
var status = await Permission.microphone.status;
|
||||
if (!status.isGranted) {
|
||||
status = await Permission.microphone.request();
|
||||
if (!status.isGranted) {
|
||||
developer.log('Microphone permission denied', name: 'AudioInput');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
await _methodChannel.invokeMethod('start');
|
||||
_isListening = true;
|
||||
developer.log('Audio input started', name: 'AudioInput');
|
||||
return true;
|
||||
} on PlatformException catch (e) {
|
||||
developer.log('Failed to start audio input: ${e.message}', name: 'AudioInput');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> stopListening() async {
|
||||
if (!_isListening) return;
|
||||
|
||||
try {
|
||||
await _methodChannel.invokeMethod('stop');
|
||||
_isListening = false;
|
||||
developer.log('Audio input stopped', name: 'AudioInput');
|
||||
} catch (e) {
|
||||
developer.log('Error stopping audio input: $e', name: 'AudioInput');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,16 +4,23 @@ import 'package:path/path.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
import 'dart:developer' as developer;
|
||||
|
||||
import 'package:lbjconsole/models/train_record.dart';
|
||||
|
||||
enum InputSource {
|
||||
bluetooth,
|
||||
rtlTcp,
|
||||
audioInput
|
||||
}
|
||||
|
||||
class DatabaseService {
|
||||
static final DatabaseService instance = DatabaseService._internal();
|
||||
factory DatabaseService() => instance;
|
||||
DatabaseService._internal();
|
||||
|
||||
static const String _databaseName = 'train_database';
|
||||
static const _databaseVersion = 8;
|
||||
static const _databaseVersion = 9;
|
||||
|
||||
static const String trainRecordsTable = 'train_records';
|
||||
static const String appSettingsTable = 'app_settings';
|
||||
@@ -63,6 +70,8 @@ class DatabaseService {
|
||||
}
|
||||
|
||||
Future<void> _onUpgrade(Database db, int oldVersion, int newVersion) async {
|
||||
developer.log('Database upgrading from $oldVersion to $newVersion', name: 'Database');
|
||||
|
||||
if (oldVersion < 2) {
|
||||
await db.execute(
|
||||
'ALTER TABLE $appSettingsTable ADD COLUMN hideTimeOnlyRecords INTEGER NOT NULL DEFAULT 0');
|
||||
@@ -97,6 +106,27 @@ class DatabaseService {
|
||||
await db.execute(
|
||||
'ALTER TABLE $appSettingsTable ADD COLUMN rtlTcpPort TEXT NOT NULL DEFAULT "14423"');
|
||||
}
|
||||
if (oldVersion < 9) {
|
||||
await db.execute(
|
||||
'ALTER TABLE $appSettingsTable ADD COLUMN inputSource TEXT NOT NULL DEFAULT "bluetooth"');
|
||||
|
||||
try {
|
||||
final List<Map<String, dynamic>> results = await db.query(appSettingsTable, columns: ['rtlTcpEnabled'], where: 'id = 1');
|
||||
if (results.isNotEmpty) {
|
||||
final int rtlTcpEnabled = results.first['rtlTcpEnabled'] as int? ?? 0;
|
||||
if (rtlTcpEnabled == 1) {
|
||||
await db.update(
|
||||
appSettingsTable,
|
||||
{'inputSource': 'rtlTcp'},
|
||||
where: 'id = 1'
|
||||
);
|
||||
developer.log('Migrated V8 settings: inputSource set to rtlTcp', name: 'Database');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
developer.log('Migration V8->V9 data update failed: $e', name: 'Database');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onCreate(Database db, int version) async {
|
||||
@@ -150,7 +180,8 @@ class DatabaseService {
|
||||
mapSettingsTimestamp INTEGER,
|
||||
rtlTcpEnabled INTEGER NOT NULL DEFAULT 0,
|
||||
rtlTcpHost TEXT NOT NULL DEFAULT '127.0.0.1',
|
||||
rtlTcpPort TEXT NOT NULL DEFAULT '14423'
|
||||
rtlTcpPort TEXT NOT NULL DEFAULT '14423',
|
||||
inputSource TEXT NOT NULL DEFAULT 'bluetooth'
|
||||
)
|
||||
''');
|
||||
|
||||
@@ -177,11 +208,12 @@ class DatabaseService {
|
||||
'groupBy': 'trainAndLoco',
|
||||
'timeWindow': 'unlimited',
|
||||
'mapTimeFilter': 'unlimited',
|
||||
'hideUngroupableRecords': 0,
|
||||
'mapSettingsTimestamp': null,
|
||||
'rtlTcpEnabled': 0,
|
||||
'rtlTcpHost': '127.0.0.1',
|
||||
'rtlTcpPort': '14423',
|
||||
'hideUngroupableRecords': 0,
|
||||
'mapSettingsTimestamp': null,
|
||||
'rtlTcpEnabled': 0,
|
||||
'rtlTcpHost': '127.0.0.1',
|
||||
'rtlTcpPort': '14423',
|
||||
'inputSource': 'bluetooth',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -409,14 +441,13 @@ class DatabaseService {
|
||||
StreamSubscription<void> onSettingsChanged(
|
||||
Function(Map<String, dynamic>) listener) {
|
||||
_settingsListeners.add(listener);
|
||||
return Stream.value(null).listen((_) {})
|
||||
..onData((_) {})
|
||||
..onDone(() {
|
||||
_settingsListeners.remove(listener);
|
||||
});
|
||||
return _SettingsListenerSubscription(() {
|
||||
_settingsListeners.remove(listener);
|
||||
});
|
||||
}
|
||||
|
||||
void _notifySettingsChanged(Map<String, dynamic> settings) {
|
||||
print('[Database] Notifying ${_settingsListeners.length} settings listeners');
|
||||
for (final listener in _settingsListeners) {
|
||||
listener(settings);
|
||||
}
|
||||
@@ -499,3 +530,39 @@ class DatabaseService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _SettingsListenerSubscription implements StreamSubscription<void> {
|
||||
final void Function() _onCancel;
|
||||
bool _isCanceled = false;
|
||||
|
||||
_SettingsListenerSubscription(this._onCancel);
|
||||
|
||||
@override
|
||||
Future<void> cancel() async {
|
||||
if (!_isCanceled) {
|
||||
_isCanceled = true;
|
||||
_onCancel();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onData(void Function(void data)? handleData) {}
|
||||
|
||||
@override
|
||||
void onDone(void Function()? handleDone) {}
|
||||
|
||||
@override
|
||||
void onError(Function? handleError) {}
|
||||
|
||||
@override
|
||||
void pause([Future<void>? resumeSignal]) {}
|
||||
|
||||
@override
|
||||
void resume() {}
|
||||
|
||||
@override
|
||||
bool get isPaused => false;
|
||||
|
||||
@override
|
||||
Future<E> asFuture<E>([E? futureValue]) => Future.value(futureValue);
|
||||
}
|
||||
Reference in New Issue
Block a user