|
|
@@ -1,6 +1,9 @@
|
|
|
import 'dart:convert';
|
|
|
|
|
|
import 'package:get/get.dart';
|
|
|
+import 'package:web_socket_channel/io.dart';
|
|
|
+
|
|
|
+import './actions.dart' as actions;
|
|
|
|
|
|
class Player {
|
|
|
double currentTime = 0;
|
|
|
@@ -9,6 +12,17 @@ class Player {
|
|
|
int songId;
|
|
|
bool playing = false;
|
|
|
List<int> queue = [];
|
|
|
+
|
|
|
+ Map<String, dynamic> stringify() {
|
|
|
+ return {
|
|
|
+ 'currentTime': this.currentTime,
|
|
|
+ 'seekTime': this.seekTime,
|
|
|
+ 'master': this.master,
|
|
|
+ 'songId': this.songId,
|
|
|
+ 'playing': this.playing,
|
|
|
+ 'queue': this.queue,
|
|
|
+ };
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
class Client {
|
|
|
@@ -22,11 +36,13 @@ class Client {
|
|
|
}
|
|
|
|
|
|
class Controller extends GetxController {
|
|
|
- var name = ''.obs;
|
|
|
- var uniqueName = ''.obs;
|
|
|
+ RxString name = ''.obs;
|
|
|
+ RxString uniqueName = ''.obs;
|
|
|
+
|
|
|
+ IOWebSocketChannel channel;
|
|
|
|
|
|
Rx<Player> player = new Player().obs;
|
|
|
- RxList<Client> clients = [].obs;
|
|
|
+ RxList<Client> clients;
|
|
|
|
|
|
setName(String newName) {
|
|
|
name.value = newName;
|
|
|
@@ -38,15 +54,15 @@ class Controller extends GetxController {
|
|
|
onRemoteMessage(String message) {
|
|
|
var action = jsonDecode(message);
|
|
|
switch (action['type']) {
|
|
|
- case 'CLIENT_LIST_UPDATED':
|
|
|
+ case actions.CLIENT_LIST_UPDATED:
|
|
|
print("Client list updated: ${action['payload']}");
|
|
|
var payload = action['payload'];
|
|
|
|
|
|
List<Client> clientList = [];
|
|
|
for (var i = 0; i < payload.length; i++) {
|
|
|
clientList.add(new Client(
|
|
|
- payload[i].name,
|
|
|
- payload[i].lastPing,
|
|
|
+ payload[i]['name'],
|
|
|
+ payload[i]['lastPing'],
|
|
|
));
|
|
|
}
|
|
|
|
|
|
@@ -54,12 +70,12 @@ class Controller extends GetxController {
|
|
|
|
|
|
break;
|
|
|
|
|
|
- case 'STATE_SET':
|
|
|
+ case actions.STATE_SET:
|
|
|
Player nextPlayer = new Player();
|
|
|
|
|
|
- nextPlayer.currentTime = action['payload']['currentTime'];
|
|
|
- nextPlayer.seekTime = action['payload']['seekTime'];
|
|
|
- nextPlayer.master = action['payload']['queue'];
|
|
|
+ nextPlayer.currentTime = action['payload']['currentTime'].toDouble();
|
|
|
+ nextPlayer.seekTime = action['payload']['seekTime'].toDouble();
|
|
|
+ nextPlayer.master = action['payload']['master'];
|
|
|
nextPlayer.songId = action['payload']['songId'];
|
|
|
nextPlayer.playing = action['payload']['playing'];
|
|
|
|
|
|
@@ -73,4 +89,25 @@ class Controller extends GetxController {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ void _remoteDispatch(String action) {
|
|
|
+ if (this.channel == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ this.channel.sink.add(action);
|
|
|
+ }
|
|
|
+
|
|
|
+ bool _isMaster() => this.uniqueName.value == this.player.value.master;
|
|
|
+
|
|
|
+ void playPause() {
|
|
|
+ this.player.value.playing = !this.player.value.playing;
|
|
|
+
|
|
|
+ if (!this._isMaster()) {
|
|
|
+ this._remoteDispatch(jsonEncode({
|
|
|
+ 'type': actions.STATE_SET,
|
|
|
+ 'payload': this.player.value.stringify(),
|
|
|
+ }));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|