controller.dart 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. print("Client list updated: ${action['payload']}");
  48. var payload = action['payload'];
  49. List<Client> clientList = [];
  50. for (var i = 0; i < payload.length; i++) {
  51. clientList.add(new Client(
  52. payload[i]['name'],
  53. payload[i]['lastPing'],
  54. ));
  55. }
  56. this.clients = clientList.obs;
  57. break;
  58. case actions.STATE_SET:
  59. Player nextPlayer = new Player();
  60. nextPlayer.currentTime = action['payload']['currentTime'].toDouble();
  61. nextPlayer.seekTime = action['payload']['seekTime'].toDouble();
  62. nextPlayer.master = action['payload']['master'];
  63. nextPlayer.songId = action['payload']['songId'];
  64. nextPlayer.playing = action['payload']['playing'];
  65. nextPlayer.queue = [];
  66. for (var i = 0; i < action['payload']['queue'].length; i++) {
  67. nextPlayer.queue.add(action['payload']['queue'][i]);
  68. }
  69. this.player.value = nextPlayer;
  70. break;
  71. }
  72. }
  73. void _remoteDispatch(String action) {
  74. if (this.channel == null) {
  75. return;
  76. }
  77. this.channel.sink.add(action);
  78. }
  79. bool _isMaster() => this.uniqueName.value == this.player.value.master;
  80. void playPause() {
  81. this.player.value.playing = !this.player.value.playing;
  82. if (!this._isMaster()) {
  83. this._remoteDispatch(jsonEncode({
  84. 'type': actions.STATE_SET,
  85. 'payload': this.player.value.stringify(),
  86. }));
  87. }
  88. }
  89. }