Skip to content

Commit

Permalink
Merge pull request #56 from bdlukaa/updates
Browse files Browse the repository at this point in the history
Correctly dispose current players on close
  • Loading branch information
curtishall authored Jan 31, 2023
2 parents de2d527 + 3458331 commit b33a2a0
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 21 deletions.
5 changes: 5 additions & 0 deletions lib/providers/downloads.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class DownloadsManager extends ChangeNotifier {
}
}

/// Whether there are no events downloaded, nor downloading events
bool get isEmpty {
return downloadedEvents.isEmpty && downloading.isEmpty;
}

/// Whether the given event is downloaded
bool isEventDownloaded(int eventId) {
return downloadedEvents.any((de) => de.event.id == eventId);
Expand Down
38 changes: 37 additions & 1 deletion lib/widgets/desktop_buttons.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import 'dart:async';
import 'dart:io';

import 'package:bluecherry_client/main.dart';
import 'package:bluecherry_client/models/device.dart';
Expand All @@ -27,6 +28,7 @@ import 'package:bluecherry_client/widgets/home.dart';
import 'package:bluecherry_client/widgets/misc.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:unity_video_player/unity_video_player.dart';
import 'package:window_manager/window_manager.dart';

final navigationStream = StreamController.broadcast();
Expand Down Expand Up @@ -83,7 +85,26 @@ class WindowButtons extends StatefulWidget {
State<WindowButtons> createState() => _WindowButtonsState();
}

class _WindowButtonsState extends State<WindowButtons> {
class _WindowButtonsState extends State<WindowButtons> with WindowListener {
@override
void initState() {
windowManager.addListener(this);
_init();
super.initState();
}

@override
void dispose() {
windowManager.removeListener(this);
super.dispose();
}

Future<void> _init() async {
// Add this line to override the default close handler
await windowManager.setPreventClose(true);
setState(() {});
}

@override
Widget build(BuildContext context) {
if (!isDesktop) return const SizedBox.shrink();
Expand Down Expand Up @@ -230,4 +251,19 @@ class _WindowButtonsState extends State<WindowButtons> {
},
);
}

@override
Future<void> onWindowClose() async {
final isPreventClose = await windowManager.isPreventClose();
// We ensure all the players are disposed in order to not keep the app alive
// in background, wasting unecessary resources!
if (isPreventClose) {
for (final player in UnityVideoPlayerInterface.players) {
debugPrint('Disposing player ${player.hashCode}');
player.dispose();
}
windowManager.destroy();
exit(0);
}
}
}
20 changes: 10 additions & 10 deletions lib/widgets/device_grid/layout_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,25 +208,25 @@ class LayoutTile extends StatelessWidget {
}
}

IconData iconForLayout(DesktopLayoutType type) {
String textForLayout(BuildContext context, DesktopLayoutType type) {
switch (type) {
case DesktopLayoutType.singleView:
return Icons.crop_square;
return AppLocalizations.of(context).singleView;
case DesktopLayoutType.multipleView:
return Icons.view_comfy_outlined;
return AppLocalizations.of(context).multipleView;
case DesktopLayoutType.compactView:
return Icons.view_compact_outlined;
return AppLocalizations.of(context).compactView;
}
}

String textForLayout(BuildContext context, DesktopLayoutType type) {
IconData iconForLayout(DesktopLayoutType type) {
switch (type) {
case DesktopLayoutType.singleView:
return AppLocalizations.of(context).singleView;
return Icons.crop_square;
case DesktopLayoutType.multipleView:
return AppLocalizations.of(context).multipleView;
return Icons.view_compact_outlined;
case DesktopLayoutType.compactView:
return AppLocalizations.of(context).compactView;
return Icons.view_comfy_outlined;
}
}

Expand All @@ -235,9 +235,9 @@ IconData selectedIconForLayout(DesktopLayoutType type) {
case DesktopLayoutType.singleView:
return Icons.square_rounded;
case DesktopLayoutType.multipleView:
return Icons.view_comfy;
case DesktopLayoutType.compactView:
return Icons.view_compact;
case DesktopLayoutType.compactView:
return Icons.view_comfy;
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/widgets/downloads_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ class DownloadsManagerScreen extends StatelessWidget {
),
),
body: LayoutBuilder(builder: (context, consts) {
if (downloads.isEmpty) {
return Center(
child: Text(AppLocalizations.of(context).noDownloads),
);
}

final size = consts.biggest;
return CustomScrollView(
slivers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ class UnityVideoPlayerDesktopInterface extends UnityVideoPlayerInterface {

@override
UnityVideoPlayer createPlayer({int? width, int? height}) {
return UnityVideoPlayerDesktop();
final player = UnityVideoPlayerDesktop(width: width, height: height);
UnityVideoPlayerInterface.registerPlayer(player);
return player;
}

@override
Expand Down Expand Up @@ -138,5 +140,6 @@ class UnityVideoPlayerDesktop extends UnityVideoPlayer {
@override
void dispose() {
vlcPlayer.dispose();
UnityVideoPlayerInterface.unregisterPlayer(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,3 @@ flutter:
dartPluginClass: UnityVideoPlayerDesktopInterface
macos:
dartPluginClass: UnityVideoPlayerDesktopInterface
windows:
dartPluginClass: UnityVideoPlayerDesktopInterface
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class UnityVideoPlayerMediaKitInterface extends UnityVideoPlayerInterface {

@override
UnityVideoPlayer createPlayer({int? width, int? height}) {
return UnityVideoPlayerMediaKit();
final player = UnityVideoPlayerMediaKit(width: width, height: height);
UnityVideoPlayerInterface.registerPlayer(player);
return player;
}

@override
Expand Down Expand Up @@ -78,7 +80,8 @@ class __MKVideoState extends State<_MKVideo> {
super.initState();

widget.videoController.then((value) {
setState(() => videoController = value);
videoController = value;
if (mounted) setState(() {});
});
}

Expand Down Expand Up @@ -106,8 +109,8 @@ class UnityVideoPlayerMediaKit extends UnityVideoPlayer {
UnityVideoPlayerMediaKit({int? width, int? height}) {
mkVideoController = VideoController.create(
mkPlayer.handle,
height: height,
width: width,
// height: height,
// width: width,
);
}

Expand All @@ -118,8 +121,14 @@ class UnityVideoPlayerMediaKit extends UnityVideoPlayer {
}

@override
String? get dataSource =>
mkPlayer.state.playlist.medias[mkPlayer.state.playlist.index].uri;
String? get dataSource {
if (mkPlayer.state.playlist.medias.isEmpty) return null;

var index = mkPlayer.state.playlist.index;
if (index.isNegative) return null;

return mkPlayer.state.playlist.medias[index].uri;
}

@override
String? get error {
Expand Down Expand Up @@ -188,5 +197,6 @@ class UnityVideoPlayerMediaKit extends UnityVideoPlayer {
void dispose() async {
await (await mkVideoController).dispose();
await mkPlayer.dispose();
UnityVideoPlayerInterface.unregisterPlayer(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class UnityVideoPlayerMobileInterface extends UnityVideoPlayerInterface {

@override
UnityVideoPlayer createPlayer({int? width, int? height}) {
return UnityVideoPlayerMobile();
final player = UnityVideoPlayerMobile();
UnityVideoPlayerInterface.registerPlayer(player);
return player;
}

/// Creates a video view
Expand Down Expand Up @@ -120,5 +122,6 @@ class UnityVideoPlayerMobile extends UnityVideoPlayer {
@override
void dispose() {
ijkPlayer.dispose();
UnityVideoPlayerInterface.unregisterPlayer(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ abstract class UnityVideoPlayerInterface extends PlatformInterface {
UnityVideoPaneBuilder? paneBuilder,
Color color = const Color(0xFF000000),
});

static final _appPlayers = <UnityVideoPlayer>[];
static List<UnityVideoPlayer> get players => _appPlayers;

static void registerPlayer(UnityVideoPlayer player) {
_appPlayers.add(player);
}

static void unregisterPlayer(UnityVideoPlayer player) {
_appPlayers.remove(player);
}
}

// ignore: non_constant_identifier_names
Expand Down

0 comments on commit b33a2a0

Please sign in to comment.