jellyfin_playback_bridge.dart 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import 'package:flutter/services.dart';
  2. import '../models/jellyfin_playback.dart';
  3. import 'jellyfin_playback_platform.dart';
  4. /// MethodChannel implementation of the Media3 service. The Dart side never
  5. /// reads the token back; [JellyfinPlaybackQueue.toMap] carries it into the
  6. /// service and the native reporter keeps it in memory.
  7. class NativeJellyfinPlayback implements JellyfinPlaybackPlatform {
  8. static const _channel = MethodChannel(
  9. 'com.wasp.cardash_launcher/jellyfin_playback',
  10. );
  11. Future<bool> _invokeBool(String method, [Object? arguments]) async {
  12. try {
  13. return await _channel.invokeMethod<bool>(method, arguments) ?? false;
  14. } on PlatformException {
  15. return false;
  16. } on MissingPluginException {
  17. return false;
  18. }
  19. }
  20. @override
  21. Future<bool> setQueue(JellyfinPlaybackQueue queue) {
  22. return _invokeBool('setQueue', queue.toMap());
  23. }
  24. @override
  25. Future<bool> play() => _invokeBool('play');
  26. @override
  27. Future<bool> pause() => _invokeBool('pause');
  28. @override
  29. Future<bool> next() => _invokeBool('next');
  30. @override
  31. Future<bool> previous() => _invokeBool('previous');
  32. @override
  33. Future<bool> seekToIndex(int index) {
  34. return _invokeBool('seekToIndex', {'index': index});
  35. }
  36. @override
  37. Future<bool> seek(Duration position) {
  38. return _invokeBool('seek', {'positionMs': position.inMilliseconds});
  39. }
  40. @override
  41. Future<bool> stop() => _invokeBool('stop');
  42. @override
  43. Future<bool> notificationPermissionGranted() {
  44. return _invokeBool('notificationPermissionGranted');
  45. }
  46. @override
  47. Future<bool> requestNotificationPermission() {
  48. return _invokeBool('requestNotificationPermission');
  49. }
  50. @override
  51. Future<JellyfinPlaybackSnapshot> getState() async {
  52. try {
  53. final raw = await _channel.invokeMethod<dynamic>('getState');
  54. if (raw is Map) {
  55. return JellyfinPlaybackSnapshot.fromMap(raw);
  56. }
  57. } on PlatformException {
  58. // The service may not be attached yet.
  59. } on MissingPluginException {
  60. // Widget tests and non-Android targets.
  61. }
  62. return JellyfinPlaybackSnapshot.empty;
  63. }
  64. @override
  65. void dispose() {}
  66. }