From c6796df7de805df09e13d70ddc30e7457c8c4e80 Mon Sep 17 00:00:00 2001 From: pulipakaa24 Date: Sat, 21 Mar 2026 00:47:21 -0500 Subject: [PATCH] device name quirk fixed --- .../individualControl/devices_menu.dart | 1 + .../individualControl/peripheral_screen.dart | 46 +++++++++++++++---- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/lib/BlindMasterScreens/individualControl/devices_menu.dart b/lib/BlindMasterScreens/individualControl/devices_menu.dart index ed43031..3bdb88d 100644 --- a/lib/BlindMasterScreens/individualControl/devices_menu.dart +++ b/lib/BlindMasterScreens/individualControl/devices_menu.dart @@ -126,6 +126,7 @@ class _DevicesMenuState extends State { deviceId: device['id'], peripheralNum: portNums[0], deviceName: device['name'], + isSinglePort: true, ), ), ).then((_) { getDevices(); }); diff --git a/lib/BlindMasterScreens/individualControl/peripheral_screen.dart b/lib/BlindMasterScreens/individualControl/peripheral_screen.dart index ca0c002..9bba940 100644 --- a/lib/BlindMasterScreens/individualControl/peripheral_screen.dart +++ b/lib/BlindMasterScreens/individualControl/peripheral_screen.dart @@ -11,11 +11,12 @@ import 'package:google_fonts/google_fonts.dart'; import 'package:socket_io_client/socket_io_client.dart' as IO; class PeripheralScreen extends StatefulWidget { - const PeripheralScreen({super.key, required this.peripheralId, required this.deviceId, required this.peripheralNum, required this.deviceName}); + const PeripheralScreen({super.key, required this.peripheralId, required this.deviceId, required this.peripheralNum, required this.deviceName, this.isSinglePort = false}); final int peripheralId; final int peripheralNum; final int deviceId; final String deviceName; + final bool isSinglePort; @override State createState() => _PeripheralScreenState(); } @@ -23,6 +24,7 @@ class PeripheralScreen extends StatefulWidget { class _PeripheralScreenState extends State { IO.Socket? socket; String imagePath = ""; + late String _deviceName; String peripheralName = "..."; bool loaded = false; bool calibrated = false; @@ -55,6 +57,7 @@ class _PeripheralScreenState extends State { @override void initState() { super.initState(); + _deviceName = widget.deviceName; initAll(); initSocket(); } @@ -444,10 +447,13 @@ class _PeripheralScreenState extends State { builder: (BuildContext dialogContext) { return AlertDialog( title: Text( - "Rename Peripheral", + widget.isSinglePort ? "Rename Device" : "Rename Peripheral", style: GoogleFonts.aBeeZee(), ), - content: BlindMasterMainInput("New Peripheral Name", controller: _peripheralRenameController,), + content: BlindMasterMainInput( + widget.isSinglePort ? "New Device Name" : "New Peripheral Name", + controller: _peripheralRenameController, + ), actions: [ Row( mainAxisAlignment: MainAxisAlignment.spaceAround, @@ -458,14 +464,16 @@ class _PeripheralScreenState extends State { }, child: const Text( "Cancel", - style: TextStyle( - color: Colors.red - ), + style: TextStyle(color: Colors.red), ) ), ElevatedButton( onPressed: () { - updatePeriphName(_peripheralRenameController.text, widget.peripheralId); + if (widget.isSinglePort) { + updateDeviceName(_peripheralRenameController.text); + } else { + updatePeriphName(_peripheralRenameController.text, widget.peripheralId); + } Navigator.of(dialogContext).pop(); }, child: const Text("Confirm") @@ -478,6 +486,27 @@ class _PeripheralScreenState extends State { ); } + Future updateDeviceName(String name) async { + try { + if (name.isEmpty) throw Exception("New name cannot be empty!"); + final payload = { + 'deviceId': widget.deviceId, + 'newName': name, + }; + final response = await securePost(payload, 'rename_device'); + if (response == null) throw Exception("Auth Error"); + if (response.statusCode != 204) { + if (response.statusCode == 409) throw Exception("Choose a unique name!"); + throw Exception("Server Error"); + } + if (!mounted) return; + setState(() => _deviceName = name); + } catch (e) { + if (!mounted) return; + ScaffoldMessenger.of(context).showSnackBar(errorSnackbar(e)); + } + } + Future updatePeriphName(String name, int id) async { try { if (name.isEmpty) throw Exception("New name cannot be empty!"); @@ -566,9 +595,10 @@ class _PeripheralScreenState extends State { @override Widget build(BuildContext context) { return Scaffold( + resizeToAvoidBottomInset: false, appBar: AppBar( title: Text( - "${widget.deviceName} - $peripheralName", + widget.isSinglePort ? _deviceName : "$_deviceName - $peripheralName", style: GoogleFonts.aBeeZee(), ), backgroundColor: Theme.of(context).primaryColorLight,