controller.dart 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import 'dart:convert';
  2. import 'package:get/get.dart';
  3. import 'package:web_socket_channel/io.dart';
  4. import './actions.dart' as actions;
  5. class Player {
  6. double currentTime = 0;
  7. double seekTime = -1;
  8. String master;
  9. int songId;
  10. bool playing = false;
  11. List<int> queue = [];
  12. Map<String, dynamic> stringify() {
  13. return {
  14. 'currentTime': this.currentTime,
  15. 'seekTime': this.seekTime,
  16. 'master': this.master,
  17. 'songId': this.songId,
  18. 'playing': this.playing,
  19. 'queue': this.queue,
  20. };
  21. }
  22. }
  23. class Client {
  24. String name;
  25. int lastPing;
  26. Client(String name, int lastPing) {
  27. this.name = name;
  28. this.lastPing = lastPing;
  29. }
  30. }
  31. class Controller extends GetxController {
  32. RxString name = ''.obs;
  33. RxString uniqueName = ''.obs;
  34. IOWebSocketChannel channel;
  35. Rx<Player> player = new Player().obs;
  36. RxList<Client> clients;
  37. setName(String newName) {
  38. name.value = newName;
  39. }
  40. setUniqueName(String newUniqueName) {
  41. uniqueName.value = newUniqueName;
  42. }
  43. onRemoteMessage(String message) {
  44. var action = jsonDecode(message);
  45. switch (action['type']) {
  46. case actions.CLIENT_LIST_UPDATED:
  47. var payload = action['payload'];
  48. List<Client> clientList = [];
  49. for (var i = 0; i < payload.length; i++) {
  50. clientList.add(new Client(
  51. payload[i]['name'],
  52. payload[i]['lastPing'],
  53. ));
  54. }
  55. this.clients = clientList.obs;
  56. break;
  57. case actions.STATE_SET:
  58. Player nextPlayer = new Player();
  59. nextPlayer.currentTime = action['payload']['currentTime'].toDouble();
  60. nextPlayer.seekTime = action['payload']['seekTime'].toDouble();
  61. nextPlayer.master = action['payload']['master'];
  62. nextPlayer.songId = action['payload']['songId'];
  63. nextPlayer.playing = action['payload']['playing'];
  64. nextPlayer.queue = [];
  65. for (var i = 0; i < action['payload']['queue'].length; i++) {
  66. nextPlayer.queue.add(action['payload']['queue'][i]);
  67. }
  68. this.player.value = nextPlayer;
  69. break;
  70. }
  71. }
  72. void _remoteDispatch(String action) {
  73. if (this.channel == null) {
  74. return;
  75. }
  76. this.channel.sink.add(action);
  77. }
  78. bool _isMaster() => this.uniqueName.value == this.player.value.master;
  79. void playPause() {
  80. this.player.value.playing = !this.player.value.playing;
  81. if (!this._isMaster()) {
  82. this._remoteDispatch(jsonEncode({
  83. 'type': actions.STATE_SET,
  84. 'payload': this.player.value.stringify(),
  85. }));
  86. }
  87. }
  88. void playSong(int songId) {
  89. if (this._isMaster()) {
  90. throw Exception('Playing songs on mobile not yet implemented!');
  91. }
  92. this.player.value.songId = songId;
  93. this.player.value.currentTime = 0;
  94. this.player.value.seekTime = -1;
  95. this.player.value.playing = true;
  96. this._remoteDispatch(jsonEncode({
  97. 'type': actions.STATE_SET,
  98. 'payload': this.player.value.stringify(),
  99. }));
  100. }
  101. }