From 59e7d53e7d26c078420810bdac1caeef413499a2 Mon Sep 17 00:00:00 2001 From: csf Date: Tue, 19 Apr 2022 21:15:19 +0800 Subject: [PATCH] android ensure clients sync;new client scrollToBottom --- lib/models/server_model.dart | 22 +++++++++++-- lib/pages/server_page.dart | 61 ++++++++++++++++++++---------------- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/lib/models/server_model.dart b/lib/models/server_model.dart index b45d0236c..9668758e2 100644 --- a/lib/models/server_model.dart +++ b/lib/models/server_model.dart @@ -41,6 +41,8 @@ class ServerModel with ChangeNotifier { Map get clients => _clients; + final controller = ScrollController(); + ServerModel() { () async { /** @@ -95,6 +97,12 @@ class ServerModel with ChangeNotifier { _connectStatus = status; notifyListeners(); } + final res = + FFI.getByName('check_clients_length', _clients.length.toString()); + if (res.isNotEmpty) { + debugPrint("clients not match!"); + updateClientState(res); + } }); } @@ -294,8 +302,8 @@ class ServerModel with ChangeNotifier { notifyListeners(); } - updateClientState() { - var res = FFI.getByName("clients_state"); + updateClientState([String? json]) { + var res = json ?? FFI.getByName("clients_state"); try { final List clientsJson = jsonDecode(res); for (var clientJson in clientsJson) { @@ -315,6 +323,7 @@ class ServerModel with ChangeNotifier { return; } _clients[client.id] = client; + scrollToBottom(); notifyListeners(); showLoginDialog(client); } catch (e) { @@ -368,6 +377,14 @@ class ServerModel with ChangeNotifier { tag: getLoginDialogTag(client.id)); } + scrollToBottom() { + Future.delayed(Duration(milliseconds: 200), () { + controller.animateTo(controller.position.maxScrollExtent, + duration: Duration(milliseconds: 200), + curve: Curves.fastLinearToSlowEaseIn); + }); + } + void sendLoginResponse(Client client, bool res) { final Map response = Map(); response["id"] = client.id; @@ -392,6 +409,7 @@ class ServerModel with ChangeNotifier { final client = Client.fromJson(jsonDecode(evt['client'])); DialogManager.dismissByTag(getLoginDialogTag(client.id)); _clients[client.id] = client; + scrollToBottom(); notifyListeners(); } catch (e) {} } diff --git a/lib/pages/server_page.dart b/lib/pages/server_page.dart index 900d6b29e..ed3832473 100644 --- a/lib/pages/server_page.dart +++ b/lib/pages/server_page.dart @@ -65,6 +65,7 @@ class ServerPage extends StatelessWidget implements PageShape { value: FFI.serverModel, child: Consumer( builder: (context, serverModel, child) => SingleChildScrollView( + controller: FFI.serverModel.controller, child: Center( child: Column( mainAxisAlignment: MainAxisAlignment.start, @@ -336,33 +337,39 @@ class ConnectionManager extends StatelessWidget { )) ], ), - entry.value.authorized?SizedBox.shrink():Text( - translate("android_new_connection_tip"), - style: TextStyle(color: Colors.black54), - ), - entry.value.authorized? ElevatedButton.icon( - style: ButtonStyle( - backgroundColor: - MaterialStateProperty.all(Colors.red)), - icon: Icon(Icons.close), - onPressed: () { - FFI.setByName("close_conn", entry.key.toString()); - FFI.invokeMethod("cancel_notification", entry.key); - }, - label: Text(translate("Close"))): - Row(children: [ - TextButton( - child: Text(translate("Dismiss")), - onPressed: () { - serverModel.sendLoginResponse(entry.value,false); - }), - SizedBox(width: 20), - ElevatedButton( - child: Text(translate("Accept")), - onPressed: () { - serverModel.sendLoginResponse(entry.value,true); - }), - ]), + entry.value.authorized + ? SizedBox.shrink() + : Text( + translate("android_new_connection_tip"), + style: TextStyle(color: Colors.black54), + ), + entry.value.authorized + ? ElevatedButton.icon( + style: ButtonStyle( + backgroundColor: + MaterialStateProperty.all(Colors.red)), + icon: Icon(Icons.close), + onPressed: () { + FFI.setByName("close_conn", entry.key.toString()); + FFI.invokeMethod( + "cancel_notification", entry.key); + }, + label: Text(translate("Close"))) + : Row(children: [ + TextButton( + child: Text(translate("Dismiss")), + onPressed: () { + serverModel.sendLoginResponse( + entry.value, false); + }), + SizedBox(width: 20), + ElevatedButton( + child: Text(translate("Accept")), + onPressed: () { + serverModel.sendLoginResponse( + entry.value, true); + }), + ]), ], ))) .toList());