Skip to content

Commit

Permalink
Move mediakit Class instantiation and init out of tree (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feichtmeier authored Aug 14, 2023
1 parent fd9052b commit 19b5707
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
20 changes: 12 additions & 8 deletions lib/app/app.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:flutter/material.dart';
import 'package:media_kit_video/media_kit_video.dart';
import 'package:mpris_service/mpris_service.dart';
import 'package:musicpod/app/app_model.dart';
import 'package:musicpod/app/connectivity_notifier.dart';
Expand Down Expand Up @@ -41,6 +42,7 @@ class App extends StatefulWidget {
),
ChangeNotifierProvider(
create: (_) => PlayerModel(
videoController: getService<VideoController>(),
mpris: getService<MPRIS>(),
libraryService: getService<LibraryService>(),
),
Expand All @@ -62,7 +64,7 @@ class App extends StatefulWidget {
ChangeNotifierProvider(
create: (_) => ConnectivityNotifier(
getService<Connectivity>(),
)..init(),
),
)
],
child: const App(),
Expand Down Expand Up @@ -95,15 +97,17 @@ class _AppState extends State<App> {
},
);

WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
if (mounted) {
context.read<PlayerModel>().init();
final lm = context.read<LibraryModel>();
lm.init().then((value) {
if (lm.recentPatchNotesDisposed == false) {
_showPatchNotes(lm.disposePatchNotes);
}
});
final pm = context.read<PlayerModel>();
final c = context.read<ConnectivityNotifier>();
await lm.init();
await pm.init();
await c.init();
if (lm.recentPatchNotesDisposed == false) {
_showPatchNotes(lm.disposePatchNotes);
}
}
});
}
Expand Down
2 changes: 0 additions & 2 deletions lib/app/library_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class LibraryModel extends SafeChangeNotifier {
await _service.setNeverShowFailedImports();

Future<void> init() async {
await _service.init();

if (_service.appIndex != null &&
totalListAmount - 1 >= _service.appIndex!) {
_index = _service.appIndex;
Expand Down
18 changes: 7 additions & 11 deletions lib/app/player/player_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import 'package:palette_generator/palette_generator.dart';
import 'package:safe_change_notifier/safe_change_notifier.dart';

class PlayerModel extends SafeChangeNotifier {
PlayerModel({required MPRIS mpris, required LibraryService libraryService})
: _player = Player(
configuration: const PlayerConfiguration(
title: 'MusicPod',
),
),
PlayerModel({
required VideoController videoController,
required MPRIS mpris,
required LibraryService libraryService,
}) : controller = videoController,
_player = videoController.player,
_mpris = mpris,
_libraryService = libraryService;

final Player _player;
late final VideoController controller;
final VideoController controller;
final MPRIS _mpris;
final LibraryService _libraryService;
StreamSubscription<bool>? _playerSub;
Expand Down Expand Up @@ -273,8 +273,6 @@ class PlayerModel extends SafeChangeNotifier {
notifyListeners();
});

controller = VideoController(_player);

notifyListeners();
}

Expand Down Expand Up @@ -395,13 +393,11 @@ class PlayerModel extends SafeChangeNotifier {

@override
Future<void> dispose() async {
await _mpris.dispose();
await _playerSub?.cancel();
await _positionSub?.cancel();
await _durationSub?.cancel();
await _isCompletedSub?.cancel();
await _volumeSub?.cancel();
await _player.dispose();
super.dispose();
}
}
Expand Down
17 changes: 16 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:desktop_notifications/desktop_notifications.dart';
import 'package:flutter/material.dart';
import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart';
import 'package:mpris_service/mpris_service.dart';
import 'package:musicpod/musicpod.dart';
import 'package:musicpod/service/library_service.dart';
Expand All @@ -16,6 +17,19 @@ Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
MediaKit.ensureInitialized();

final libraryService = LibraryService();
await libraryService.init();

final player = Player(
configuration: const PlayerConfiguration(title: 'MusicPod'),
);
registerService<Player>(
() => player,
dispose: (p) => p.dispose(),
);

registerService<VideoController>(() => VideoController(player));

final mpris = await MPRIS.create(
busName: 'org.mpris.MediaPlayer2.musicpod',
identity: 'Musicpod',
Expand All @@ -26,8 +40,9 @@ Future<void> main() async {
() => mpris,
dispose: (s) async => await s.dispose(),
);

registerService<LibraryService>(
LibraryService.new,
() => libraryService,
dispose: (s) async => await s.dispose(),
);
registerService<LocalAudioService>(
Expand Down

0 comments on commit 19b5707

Please sign in to comment.