controller.dart 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import 'dart:convert';
  2. import 'package:get/get.dart';
  3. class Player {
  4. double currentTime = 0;
  5. double seekTime = -1;
  6. String master;
  7. int songId;
  8. bool playing = false;
  9. List<int> queue = [];
  10. }
  11. class Client {
  12. String name;
  13. int lastPing;
  14. Client(String name, int lastPing) {
  15. this.name = name;
  16. this.lastPing = lastPing;
  17. }
  18. }
  19. class Controller extends GetxController {
  20. var name = ''.obs;
  21. var uniqueName = ''.obs;
  22. Rx<Player> player = new Player().obs;
  23. RxList<Client> clients = [].obs;
  24. setName(String newName) {
  25. name.value = newName;
  26. }
  27. setUniqueName(String newUniqueName) {
  28. uniqueName.value = newUniqueName;
  29. }
  30. onRemoteMessage(String message) {
  31. var action = jsonDecode(message);
  32. switch (action['type']) {
  33. case 'CLIENT_LIST_UPDATED':
  34. print("Client list updated: ${action['payload']}");
  35. var payload = action['payload'];
  36. List<Client> clientList = [];
  37. for (var i = 0; i < payload.length; i++) {
  38. clientList.add(new Client(
  39. payload[i].name,
  40. payload[i].lastPing,
  41. ));
  42. }
  43. this.clients = clientList.obs;
  44. break;
  45. case 'STATE_SET':
  46. Player nextPlayer = new Player();
  47. nextPlayer.currentTime = action['payload']['currentTime'];
  48. nextPlayer.seekTime = action['payload']['seekTime'];
  49. nextPlayer.master = action['payload']['queue'];
  50. nextPlayer.songId = action['payload']['songId'];
  51. nextPlayer.playing = action['payload']['playing'];
  52. nextPlayer.queue = [];
  53. for (var i = 0; i < action['payload']['queue'].length; i++) {
  54. nextPlayer.queue.add(action['payload']['queue'][i]);
  55. }
  56. this.player.value = nextPlayer;
  57. break;
  58. }
  59. }
  60. }