From 61214023fa3add402ae881ac7db8abf921ea3124 Mon Sep 17 00:00:00 2001 From: cloudwebrtc Date: Thu, 30 Nov 2023 12:48:50 +0800 Subject: [PATCH] move flutter_window_close handler from sdk to the example app. --- example/lib/main.dart | 6 ++++++ example/lib/pages/room.dart | 12 +++++++++++- example/lib/utils.dart | 3 +++ example/pubspec.yaml | 2 +- lib/src/core/room.dart | 12 ------------ lib/src/support/app_state.dart | 29 ----------------------------- pubspec.yaml | 1 - 7 files changed, 21 insertions(+), 44 deletions(-) create mode 100644 example/lib/utils.dart delete mode 100644 lib/src/support/app_state.dart diff --git a/example/lib/main.dart b/example/lib/main.dart index 10f71a31..2733d2e0 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -1,8 +1,10 @@ import 'package:flutter/material.dart'; +import 'package:flutter_window_close/flutter_window_close.dart'; import 'package:livekit_example/theme.dart'; import 'package:logging/logging.dart'; import 'package:intl/intl.dart'; import 'pages/connect.dart'; +import 'utils.dart'; void main() async { final format = DateFormat('HH:mm:ss'); @@ -14,6 +16,10 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); + FlutterWindowClose.setWindowShouldCloseHandler(() async { + await onWindowShouldClose?.call(); + return true; + }); runApp(const LiveKitExampleApp()); } diff --git a/example/lib/pages/room.dart b/example/lib/pages/room.dart index 39471d2d..433fd992 100644 --- a/example/lib/pages/room.dart +++ b/example/lib/pages/room.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import 'dart:math' as math; @@ -5,6 +6,7 @@ import 'package:flutter/material.dart'; import 'package:livekit_client/livekit_client.dart'; import '../exts.dart'; +import '../utils.dart'; import '../widgets/controls.dart'; import '../widgets/participant.dart'; import '../widgets/participant_info.dart'; @@ -25,7 +27,6 @@ class RoomPage extends StatefulWidget { } class _RoomPageState extends State { - // List participantTracks = []; EventsListener get _listener => widget.listener; bool get fastConnection => widget.room.engine.fastConnectOptions != null; @@ -46,6 +47,14 @@ class _RoomPageState extends State { if (lkPlatformIsMobile()) { Hardware.instance.setSpeakerphoneOn(true); } + + if (!lkPlatformIs(PlatformType.web) && !lkPlatformIsTest()) { + onWindowShouldClose = () async { + unawaited(widget.room.disconnect()); + await _listener.waitFor( + duration: const Duration(seconds: 5)); + }; + } } @override @@ -56,6 +65,7 @@ class _RoomPageState extends State { await _listener.dispose(); await widget.room.dispose(); })(); + onWindowShouldClose = null; super.dispose(); } diff --git a/example/lib/utils.dart b/example/lib/utils.dart new file mode 100644 index 00000000..fc74277d --- /dev/null +++ b/example/lib/utils.dart @@ -0,0 +1,3 @@ +import 'dart:async'; + +FutureOr Function()? onWindowShouldClose; diff --git a/example/pubspec.yaml b/example/pubspec.yaml index 7b72ccc0..11281b96 100644 --- a/example/pubspec.yaml +++ b/example/pubspec.yaml @@ -19,7 +19,7 @@ dependencies: shared_preferences: ^2.0.7 google_fonts: ^4.0.4 flutter_svg: ^2.0.5 - + flutter_window_close: ^0.2.2 livekit_client: path: ../ diff --git a/lib/src/core/room.dart b/lib/src/core/room.dart index 5b317e11..3799c4f9 100644 --- a/lib/src/core/room.dart +++ b/lib/src/core/room.dart @@ -34,7 +34,6 @@ import '../participant/participant.dart'; import '../participant/remote.dart'; import '../proto/livekit_models.pb.dart' as lk_models; import '../proto/livekit_rtc.pb.dart' as lk_rtc; -import '../support/app_state.dart'; import '../support/disposable.dart'; import '../support/platform.dart'; import '../track/local/audio.dart'; @@ -109,8 +108,6 @@ class Room extends DisposableChangeNotifier with EventsEmittable { // late EventsListener _signalListener; - StreamSubscription? _appCloseSubscription; - Room({ ConnectOptions connectOptions = const ConnectOptions(), RoomOptions roomOptions = const RoomOptions(), @@ -124,13 +121,6 @@ class Room extends DisposableChangeNotifier with EventsEmittable { _engineListener = this.engine.createListener(); _setUpEngineListeners(); - if (!kIsWeb && !lkPlatformIsTest()) { - _appCloseSubscription = - AppStateListener.instance.onWindowShouldClose.stream.listen((event) { - disconnect(); - }); - } - _signalListener = this.engine.signalClient.createListener(); _setUpSignalListeners(); @@ -153,8 +143,6 @@ class Room extends DisposableChangeNotifier with EventsEmittable { await _engineListener.dispose(); // dispose the engine await this.engine.dispose(); - // dispose the app state listener - await _appCloseSubscription?.cancel(); }); } diff --git a/lib/src/support/app_state.dart b/lib/src/support/app_state.dart deleted file mode 100644 index f855c369..00000000 --- a/lib/src/support/app_state.dart +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright 2023 LiveKit, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -import 'dart:async'; - -import 'package:flutter_window_close/flutter_window_close.dart'; - -class AppStateListener { - AppStateListener._internal() { - FlutterWindowClose.setWindowShouldCloseHandler(() async { - onWindowShouldClose.add('close'); - return true; - }); - } - static final AppStateListener instance = AppStateListener._internal(); - final StreamController onWindowShouldClose = - StreamController.broadcast(sync: true); -} diff --git a/pubspec.yaml b/pubspec.yaml index 22e72ef7..9d7b8921 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -39,7 +39,6 @@ dependencies: synchronized: ^3.0.0+3 protobuf: ^3.0.0 flutter_webrtc: 0.9.46 - flutter_window_close: ^0.2.2 device_info_plus: '>=8.0.0' js: ^0.6.4 platform_detect: ^2.0.7