controller.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:get/get.dart';
  4. import './actions.dart' as actions;
  5. import './preferences.dart' as Preferences;
  6. import './socket.dart';
  7. class Player {
  8. double currentTime = 0;
  9. double seekTime = -1;
  10. String master;
  11. List<String> activeClients = [];
  12. int songId;
  13. bool playing = false;
  14. List<int> queue = [];
  15. bool shuffleMode = false;
  16. Map<String, dynamic> stringify() {
  17. return {
  18. 'songId': this.songId,
  19. 'playing': this.playing,
  20. 'currentTime': this.currentTime,
  21. 'seekTime': this.seekTime,
  22. 'master': this.master,
  23. 'activeClients': this.activeClients,
  24. 'queue': this.queue,
  25. 'shuffleMode': this.shuffleMode,
  26. };
  27. }
  28. }
  29. class Client {
  30. String name;
  31. int lastPing;
  32. Client(String name, int lastPing) {
  33. this.name = name;
  34. this.lastPing = lastPing;
  35. }
  36. }
  37. class Controller extends GetxController {
  38. RxString apiUrl = ''.obs;
  39. RxString name = ''.obs;
  40. RxString uniqueName = ''.obs;
  41. Rx<Player> player = new Player().obs;
  42. RxList<Client> clients = <Client>[].obs;
  43. final Socket socket = Socket();
  44. Controller({
  45. this.apiUrl,
  46. this.name,
  47. });
  48. Future<void> connect() async {
  49. uniqueName.value = await socket.connect(
  50. apiUrl.value,
  51. name.value,
  52. (message) => onRemoteMessage(this, message),
  53. );
  54. }
  55. void disconnect() {
  56. socket.disconnect();
  57. uniqueName.value = '';
  58. }
  59. setApiUrl(String apiUrl) {
  60. this.disconnect();
  61. this.apiUrl = apiUrl.obs;
  62. this.connect();
  63. Preferences.setApiUrl(apiUrl);
  64. }
  65. setName(String newName) {
  66. name.value = newName;
  67. Preferences.setClientName(newName);
  68. }
  69. setClients(List<Client> newClients) {
  70. this.clients.assignAll(newClients);
  71. }
  72. bool _isMaster() => this.uniqueName.value == this.player.value.master;
  73. void playPause() {
  74. this.player.value.playing = !this.player.value.playing;
  75. if (!this._isMaster()) {
  76. this.socket.dispatch(jsonEncode({
  77. 'type': actions.STATE_SET,
  78. 'payload': this.player.value.stringify(),
  79. }));
  80. }
  81. }
  82. void playSong(int songId) {
  83. if (this._isMaster()) {
  84. throw Exception('Playing songs on mobile not yet implemented!');
  85. }
  86. this.player.value.songId = songId;
  87. this.player.value.currentTime = 0;
  88. this.player.value.seekTime = -1;
  89. this.player.value.playing = true;
  90. this.socket.dispatch(jsonEncode({
  91. 'type': actions.STATE_SET,
  92. 'payload': this.player.value.stringify(),
  93. }));
  94. }
  95. }