Skip to content

Commit

Permalink
move flutter_window_close handler from sdk to the example app.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Nov 30, 2023
1 parent a0f2b9d commit 6121402
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 44 deletions.
6 changes: 6 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -14,6 +16,10 @@ void main() async {

WidgetsFlutterBinding.ensureInitialized();

FlutterWindowClose.setWindowShouldCloseHandler(() async {
await onWindowShouldClose?.call();
return true;
});
runApp(const LiveKitExampleApp());
}

Expand Down
12 changes: 11 additions & 1 deletion example/lib/pages/room.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'dart:async';
import 'dart:convert';
import 'dart:math' as math;

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';
Expand All @@ -25,7 +27,6 @@ class RoomPage extends StatefulWidget {
}

class _RoomPageState extends State<RoomPage> {
//
List<ParticipantTrack> participantTracks = [];
EventsListener<RoomEvent> get _listener => widget.listener;
bool get fastConnection => widget.room.engine.fastConnectOptions != null;
Expand All @@ -46,6 +47,14 @@ class _RoomPageState extends State<RoomPage> {
if (lkPlatformIsMobile()) {
Hardware.instance.setSpeakerphoneOn(true);
}

if (!lkPlatformIs(PlatformType.web) && !lkPlatformIsTest()) {
onWindowShouldClose = () async {
unawaited(widget.room.disconnect());
await _listener.waitFor<RoomDisconnectedEvent>(
duration: const Duration(seconds: 5));
};
}
}

@override
Expand All @@ -56,6 +65,7 @@ class _RoomPageState extends State<RoomPage> {
await _listener.dispose();
await widget.room.dispose();
})();
onWindowShouldClose = null;
super.dispose();
}

Expand Down
3 changes: 3 additions & 0 deletions example/lib/utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import 'dart:async';

FutureOr<void> Function()? onWindowShouldClose;
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ../

Expand Down
12 changes: 0 additions & 12 deletions lib/src/core/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -109,8 +108,6 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
//
late EventsListener<SignalEvent> _signalListener;

StreamSubscription<String>? _appCloseSubscription;

Room({
ConnectOptions connectOptions = const ConnectOptions(),
RoomOptions roomOptions = const RoomOptions(),
Expand All @@ -124,13 +121,6 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
_engineListener = this.engine.createListener();
_setUpEngineListeners();

if (!kIsWeb && !lkPlatformIsTest()) {
_appCloseSubscription =
AppStateListener.instance.onWindowShouldClose.stream.listen((event) {
disconnect();
});
}

_signalListener = this.engine.signalClient.createListener();
_setUpSignalListeners();

Expand All @@ -153,8 +143,6 @@ class Room extends DisposableChangeNotifier with EventsEmittable<RoomEvent> {
await _engineListener.dispose();
// dispose the engine
await this.engine.dispose();
// dispose the app state listener
await _appCloseSubscription?.cancel();
});
}

Expand Down
29 changes: 0 additions & 29 deletions lib/src/support/app_state.dart

This file was deleted.

1 change: 0 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6121402

Please sign in to comment.