瀏覽代碼

feat: player state controller

Fela Maslen 4 年之前
父節點
當前提交
efea48054a
共有 1 個文件被更改,包括 49 次插入1 次删除
  1. 49 1
      gmus-mobile/lib/controller.dart

+ 49 - 1
gmus-mobile/lib/controller.dart

@@ -2,10 +2,32 @@ import 'dart:convert';
 
 import 'package:get/get.dart';
 
+class Player {
+  double currentTime = 0;
+  double seekTime = -1;
+  String master;
+  int songId;
+  bool playing = false;
+  List<int> queue = [];
+}
+
+class Client {
+  String name;
+  int lastPing;
+
+  Client(String name, int lastPing) {
+    this.name = name;
+    this.lastPing = lastPing;
+  }
+}
+
 class Controller extends GetxController {
   var name = ''.obs;
   var uniqueName = ''.obs;
 
+  Rx<Player> player = new Player().obs;
+  RxList<Client> clients = [].obs;
+
   setName(String newName) {
     name.value = newName;
   }
@@ -18,10 +40,36 @@ class Controller extends GetxController {
     switch (action['type']) {
       case '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,
+          ));
+        }
+
+        this.clients = clientList.obs;
+
         break;
 
       case 'STATE_SET':
-        print("State set (remote): ${action['payload']}");
+        Player nextPlayer = new Player();
+
+        nextPlayer.currentTime = action['payload']['currentTime'];
+        nextPlayer.seekTime = action['payload']['seekTime'];
+        nextPlayer.master = action['payload']['queue'];
+        nextPlayer.songId = action['payload']['songId'];
+        nextPlayer.playing = action['payload']['playing'];
+
+        nextPlayer.queue = [];
+        for (var i = 0; i < action['payload']['queue'].length; i++) {
+          nextPlayer.queue.add(action['payload']['queue'][i]);
+        }
+
+        this.player.value = nextPlayer;
+
         break;
     }
   }