Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. /// Given some `dart:io` method that returns a PlatformType enum
  2. PlatformType get currentPlatform => PlatformType.windows;
  3. enum PlatformType { android, fuschia, iOS, linux, macOS, windows }
  4.  
  5. /// Intersection package, ie `video_player` exposes a common interface
  6. abstract class PlatformVideoController {
  7. Stream<double> get progress;
  8. Future<void> play();
  9. Future<void> pause();
  10. }
  11.  
  12. /// It also exposes a glorified switch block that allows people to load in their own implementations while
  13. /// consuming normally. ie, `video_player_ios`, `video_player_windows`, etc
  14. class VideoController implements PlatformVideoController {
  15. static final implementations = Map<PlatformType, PlatformVideoController>();
  16.  
  17. static addImplementations(Map<PlatformType, PlatformVideoController> i) =>
  18. implementations.addAll(i);
  19.  
  20. PlatformVideoController get _this {
  21. final implementation = implementations[currentPlatform];
  22.  
  23. /// Guard unimplemented
  24. if (implementation == null)
  25. throw UnimplementedError(
  26. "Platform $currentPlatform is not yet implemented.");
  27.  
  28. return implementation;
  29. }
  30.  
  31. @override
  32. Future<void> pause() => _this.pause();
  33.  
  34. @override
  35. Future<void> play() => _this.play();
  36.  
  37. @override
  38. Stream<double> get progress => _this.progress;
  39. }
  40.  
  41. /// An example adapter/implementation that might be on `video_player_windows`
  42. class WindowsVideoController implements PlatformVideoController {
  43. final controller = AwesomeWindowsVideoController();
  44.  
  45. @override
  46. Stream<double> get progress => controller.playheadLocation
  47. .map((l) => l.inMilliseconds / controller.videoLength.inMilliseconds);
  48.  
  49. @override
  50. Future<void> play() => controller.isPlaying(true);
  51.  
  52. @override
  53. Future<void> pause() => controller.isPlaying(false);
  54. }
  55.  
  56. /// The underlying platform specific controller that has way more methods than the intersection package
  57. /// because it provides a ton of unique Windows related tooling
  58. class AwesomeWindowsVideoController {
  59. Duration get videoLength => Duration(seconds: 15);
  60.  
  61. Stream<Duration> get playheadLocation => Stream.fromIterable(
  62. List.generate(15000, (i) => Duration(milliseconds: i)));
  63.  
  64. Future<void> isPlaying(bool isPlaying) => Future.value(null);
  65. }
  66.  
  67. /// Consumed by Flutter developers
  68. main() {
  69. VideoController.addImplementations({
  70. PlatformType.windows: WindowsVideoController(),
  71. });
  72.  
  73. /// You can use the intersection package to make your life easier
  74. final intersectionController = VideoController();
  75. intersectionController.play();
  76.  
  77. /// Or you can consume the platform specific package and get more features
  78. final platformController = AwesomeWindowsVideoController();
  79. platformController.isPlaying(true);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement