Преглед на файлове

fix(flutter): resumeRecording now uses dispatcher for new room

wenhongquan преди 3 седмици
родител
ревизия
9831a26227
променени са 1 файла, в които са добавени 26 реда и са изтрити 34 реда
  1. 26 34
      flutter_asr_client/lib/pages/recording/notifiers/recording_notifier.dart

+ 26 - 34
flutter_asr_client/lib/pages/recording/notifiers/recording_notifier.dart

@@ -111,6 +111,26 @@ final class RecordingNotifier extends AutoDisposeAsyncNotifier<RecordingState> {
 
   LiveKitService get _liveKit => ref.read(liveKitServiceProvider);
 
+  Future<({String room, String url, String token})> _getRoomInfo(String identity) async {
+    final lkUrl = await ref.read(settingsServiceProvider).getLiveKitUrl();
+    final dispatcherUrl = await ref.read(settingsServiceProvider).getDispatcherUrl();
+
+    if (dispatcherUrl.isNotEmpty) {
+      try {
+        final resp = await http.post(
+          Uri.parse('$dispatcherUrl/connect'),
+          headers: {'Content-Type': 'application/json'},
+          body: jsonEncode({'identity': identity}),
+        ).timeout(const Duration(seconds: 5));
+        if (resp.statusCode == 200) {
+          final data = jsonDecode(resp.body) as Map<String, dynamic>;
+          return (room: data['room'] as String, url: data['url'] as String, token: data['token'] as String);
+        }
+      } catch (_) {}
+    }
+    return (room: 'asr-test', url: lkUrl, token: '');
+  }
+
   @override
   FutureOr<RecordingState> build() async {
     ref.onDispose(() {
@@ -119,9 +139,6 @@ final class RecordingNotifier extends AutoDisposeAsyncNotifier<RecordingState> {
     });
 
     final identity = 'user-${_uuid.v4().substring(0, 6)}';
-    final settingsService = ref.read(settingsServiceProvider);
-    final lkUrl = await settingsService.getLiveKitUrl();
-    final dispatcherUrl = await settingsService.getDispatcherUrl();
 
     final initialState = const RecordingState(
       projectName: 'G15 沈海高速改扩建 · K1120+300',
@@ -129,36 +146,11 @@ final class RecordingNotifier extends AutoDisposeAsyncNotifier<RecordingState> {
       templateName: '模板 · 普通混凝土坍落度 GB/T 50080',
     );
 
-    String roomName = 'asr-test';
-    String token = '';
-    String url = lkUrl;
-
-    // Try dispatcher for isolated room
-    if (dispatcherUrl.isNotEmpty) {
-      try {
-        final resp = await http.post(
-          Uri.parse('$dispatcherUrl/connect'),
-          headers: {'Content-Type': 'application/json'},
-          body: jsonEncode({'identity': identity}),
-        ).timeout(const Duration(seconds: 5));
-        if (resp.statusCode == 200) {
-          final data = jsonDecode(resp.body) as Map<String, dynamic>;
-          roomName = data['room'] as String;
-          token = data['token'] as String;
-          url = data['url'] as String;
-          debugPrint('[notifier] dispatcher: room=$roomName url=$url');
-        }
-      } catch (e) {
-        debugPrint('[notifier] dispatcher failed: $e, using fallback');
-      }
-    }
-
+    final info = await _getRoomInfo(identity);
     try {
-      await _liveKit.connect(url: url, room: roomName, identity: identity, token: token);
+      await _liveKit.connect(url: info.url, room: info.room, identity: identity, token: info.token);
     } on Exception catch (e) {
-      return initialState.copyWith(
-        errorMessage: '无法连接 LiveKit: $e',
-      );
+      return initialState.copyWith(errorMessage: '无法连接 LiveKit: $e');
     }
 
     _listenToMessages();
@@ -242,9 +234,9 @@ final class RecordingNotifier extends AutoDisposeAsyncNotifier<RecordingState> {
   Future<void> resumeRecording() async {
     if (state.value?.status != RecordingStatus.paused) return;
     try {
-      final roomName = 'asr-test';
-      final lkUrl = await ref.read(settingsServiceProvider).getLiveKitUrl();
-      await _liveKit.connect(url: lkUrl, room: roomName, identity: 'user-${_uuid.v4().substring(0, 6)}');
+      final identity = 'user-${_uuid.v4().substring(0, 6)}';
+      final info = await _getRoomInfo(identity);
+      await _liveKit.connect(url: info.url, room: info.room, identity: identity, token: info.token);
       _updateState((s) => _startedState(s));
     } on Exception catch (e) {
       _updateState((s) => s.copyWith(errorMessage: '无法恢复录音: $e'));