Просмотр исходного кода

feat: working play / pause button

Fela Maslen 4 лет назад
Родитель
Сommit
e40d6931dc

+ 2 - 0
gmus-mobile/lib/actions.dart

@@ -0,0 +1,2 @@
+const STATE_SET = 'STATE_SET';
+const CLIENT_LIST_UPDATED = 'CLIENT_LIST_UPDATED';

+ 4 - 2
gmus-mobile/lib/components/content.dart

@@ -1,9 +1,11 @@
 import 'package:flutter/material.dart';
 import 'package:get/get.dart';
-import 'package:gmus_mobile/components/identify.dart';
 
 import '../controller.dart';
 
+import './identify.dart';
+import './ui.dart';
+
 class Content extends StatelessWidget {
   final Controller controller = Get.find();
   @override
@@ -14,7 +16,7 @@ class Content extends StatelessWidget {
         return Identify();
       }
 
-      return Text("Artists and stuff here");
+      return UI();
     });
   }
 }

+ 26 - 0
gmus-mobile/lib/components/player.dart

@@ -0,0 +1,26 @@
+import 'package:flutter/material.dart';
+import 'package:get/get.dart';
+
+import '../controller.dart';
+
+class GmusPlayer extends StatelessWidget {
+  final Controller controller = Get.find();
+  @override
+  Widget build(BuildContext context) {
+    return Obx(() {
+      bool isPlaying = controller.player.value.playing;
+
+      String playPauseButtonText = isPlaying ? 'Pause' : 'Play';
+
+      return Row(
+        children: [
+          TextButton(
+              child: Text(playPauseButtonText),
+              onPressed: controller.playPause,
+          ),
+        ],
+      );
+    });
+  }
+}
+

+ 20 - 0
gmus-mobile/lib/components/ui.dart

@@ -0,0 +1,20 @@
+import 'package:flutter/material.dart';
+import 'package:get/get.dart';
+
+import '../controller.dart';
+
+import './player.dart';
+
+// Main UI once identified
+class UI extends StatelessWidget {
+  final Controller controller = Get.find();
+  @override
+  Widget build(BuildContext context) {
+    return Column(
+      children: [
+        GmusPlayer(),
+      ],
+    );
+  }
+}
+

+ 47 - 10
gmus-mobile/lib/controller.dart

@@ -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(),
+      }));
+    }
+  }
 }

+ 1 - 0
gmus-mobile/lib/socket.dart

@@ -50,6 +50,7 @@ void connect(Controller controller) async {
   print("Connecting to socket: $pubsubUrl");
 
   var channel = IOWebSocketChannel.connect(Uri.parse(pubsubUrl));
+  controller.channel = channel;
 
   channel.stream.listen((message) {
     controller.onRemoteMessage(message);