Skip to content

Commit

Permalink
💾 Feat(DevicesStatus): Optimized user experience
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynesshely committed Jul 26, 2024
1 parent 81e7a73 commit edeac91
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 56 deletions.
4 changes: 1 addition & 3 deletions kitx_mobile/lib/instances.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ class Instances {

/// Restart [devicesService] and [devicesDiscoveryService]
void restartDevicesServer() {
devicesDiscoveryService.stop(sendExitPackage: false);
devicesService.stop();

devicesDiscoveryService.init();
Future.sync(devicesDiscoveryService.restart);
devicesService.init();
}

Expand Down
35 changes: 0 additions & 35 deletions kitx_mobile/lib/pages/controls/device_status_label.dart

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import 'package:kitx_mobile/instances.dart';
import 'package:kitx_mobile/services/public/service_status.dart';

/// Device Status Icon
class DeviceStatusIcon extends StatelessWidget {
class DevicesStatusIcon extends StatelessWidget {
/// Constructor for Device Status Icon
const DeviceStatusIcon({super.key});
const DevicesStatusIcon({super.key});

@override
Widget build(BuildContext context) {
Expand Down
58 changes: 58 additions & 0 deletions kitx_mobile/lib/pages/controls/devices_status_label.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kitx_mobile/instances.dart';
import 'package:kitx_mobile/services/public/service_status.dart';
import 'package:kitx_mobile/utils/handlers/vibration_handler.dart';

/// Device Status Label
class DevicesStatusLabel extends StatelessWidget {
/// Constructor for Device Status Label
const DevicesStatusLabel({required this.inHomePage, super.key});

/// If in home page
final bool inHomePage;

@override
Widget build(BuildContext context) {
return Obx(() {
var textStyle = Theme.of(context).textTheme.bodyMedium;
switch (instances.devicesDiscoveryService.serviceStatus.value) {
case ServiceStatus.running:
return Text(
'${instances.devicesService.length.obs} ${'HomePage_DevicesCount'.tr}',
style: textStyle,
);
case ServiceStatus.starting:
return Text('Public_Launching'.tr, style: textStyle);
case ServiceStatus.stopping:
return Text('Public_Stopping'.tr, style: textStyle);
case ServiceStatus.error:
return Row(
children: [
Padding(
padding: EdgeInsets.only(top: inHomePage ? 10 : 0),
child: ElevatedButton.icon(
onPressed: () {
VibrationHandler.tryVibrate();

Get.defaultDialog(
title: 'Public_Error'.tr,
titlePadding: EdgeInsets.only(top: 20),
middleText: instances.devicesDiscoveryService.serviceException.toString(),
textConfirm: 'Public_Close'.tr,
contentPadding: EdgeInsets.only(top: 10, bottom: 20),
onConfirm: () => Get.back.tryVibrate().call(),
);
},
icon: const Icon(Icons.error, color: Colors.redAccent),
label: Text("Public_View_Error".tr),
),
),
],
);
default:
return Text('Public_Closed'.tr, style: textStyle);
}
});
}
}
9 changes: 5 additions & 4 deletions kitx_mobile/lib/pages/devices_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kitx_mobile/instances.dart';
import 'package:kitx_mobile/pages/controls/device_card.dart';
import 'package:kitx_mobile/pages/controls/device_status_icon.dart';
import 'package:kitx_mobile/pages/controls/device_status_label.dart';
import 'package:kitx_mobile/pages/controls/devices_status_icon.dart';
import 'package:kitx_mobile/pages/controls/devices_status_label.dart';
import 'package:kitx_mobile/pages/pages.dart';
import 'package:kitx_mobile/pages/sub_pages/device_chat_page.dart';
import 'package:kitx_mobile/utils/extensions/device_info_ext.dart';
Expand Down Expand Up @@ -86,7 +86,7 @@ class _DevicesPage extends State<DevicesPage> {
PopupMenuButton(
tooltip: '',
padding: EdgeInsets.all(0),
icon: const DeviceStatusIcon(),
icon: const DevicesStatusIcon(),
position: PopupMenuPosition.under,
itemBuilder: (context) => [
PopupMenuItem(
Expand All @@ -98,6 +98,7 @@ class _DevicesPage extends State<DevicesPage> {
onTap: () => instances.shutdownDevicesServer.tryVibrate().call(),
),
],
onOpened: () => VibrationHandler.tryVibrate(),
),
const SizedBox(width: 10),
],
Expand Down Expand Up @@ -130,7 +131,7 @@ class _DevicesPage extends State<DevicesPage> {
padding: EdgeInsets.fromLTRB(30, 30, 30, 0),
child: const Hero(
tag: 'HeroTag_DevicesCount',
child: const DeviceStatusLabel(),
child: const DevicesStatusLabel(inHomePage: false),
),
),
const SizedBox(height: 25),
Expand Down
8 changes: 4 additions & 4 deletions kitx_mobile/lib/pages/home_page.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:kitx_mobile/instances.dart';
import 'package:kitx_mobile/pages/controls/device_status_label.dart';
import 'package:kitx_mobile/pages/controls/devices_status_label.dart';
import 'package:kitx_mobile/pages/controls/home_page_drawer.dart';
import 'package:kitx_mobile/pages/pages.dart';
import 'package:kitx_mobile/pages/plugins_page.dart';
Expand Down Expand Up @@ -110,7 +110,7 @@ class HomePage extends StatelessWidget implements ConstantPage {
title: Text('Drawer_Devices'.tr),
subtitle: const Hero(
tag: 'HeroTag_DevicesCount',
child: const DeviceStatusLabel(),
child: const DevicesStatusLabel(inHomePage: true),
),
trailing: const Icon(Icons.keyboard_arrow_right),
shape: tileRadius,
Expand All @@ -122,11 +122,11 @@ class HomePage extends StatelessWidget implements ConstantPage {
items: [
PopupMenuItem(
child: Text('Option_RestartDevicesServer'.tr),
onTap: instances.restartDevicesServer,
onTap: () => instances.restartDevicesServer.tryVibrate().call(),
),
PopupMenuItem(
child: Text('Option_ShutdownDevicesServer'.tr),
onTap: instances.shutdownDevicesServer,
onTap: () => instances.shutdownDevicesServer.tryVibrate().call(),
),
],
elevation: 8.0,
Expand Down
36 changes: 28 additions & 8 deletions kitx_mobile/lib/services/devices_discovery_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class DevicesDiscoveryService implements Service {
var connection = await instances.connectivity.checkConnectivity();

if (connection.contains(ConnectivityResult.wifi) == false) {
await Future.delayed(const Duration(milliseconds: 500));

warning('No wifi connection, WebService will not start.');
serviceException = Exception('No Wi-Fi connection, current is ${connection.toString()}');
serviceStatus.value = ServiceStatus.error;
Expand All @@ -61,8 +63,6 @@ class DevicesDiscoveryService implements Service {

_isExitPackageSent = false;

serviceStatus.value = ServiceStatus.running;

try {
var deviceInfo = DeviceInfo(
((b) => b
Expand Down Expand Up @@ -94,7 +94,7 @@ class DevicesDiscoveryService implements Service {
socket.broadcastEnabled = true;
socket.joinMulticast(InternetAddress(_udpBroadcastAddress));

sendTimer = Timer.periodic(Duration(seconds: config.webServiceUdpSendFrequency), (timer) {
sendTimer = Timer.periodic(Duration(seconds: config.webServiceUdpSendFrequency), (timer) async {
try {
deviceInfo = deviceInfo.rebuild(
(b) => b
Expand All @@ -119,6 +119,9 @@ class DevicesDiscoveryService implements Service {

timer.cancel();
socket.close();

await Future.delayed(const Duration(milliseconds: 500));

serviceException = e as Exception;
serviceStatus.value = ServiceStatus.error;

Expand Down Expand Up @@ -154,6 +157,8 @@ class DevicesDiscoveryService implements Service {
var _deviceInfo = DeviceInfo.fromString(_data);
if (_deviceInfo != null) await instances.devicesService.addDevice(_deviceInfo);
} catch (e, stack) {
await Future.delayed(const Duration(milliseconds: 500));

error('Can not deserialize device info pack: `$_data`', error: e, stackTrace: stack);

serviceException = e as Exception;
Expand All @@ -166,6 +171,8 @@ class DevicesDiscoveryService implements Service {

(() => serviceStatus.value = ServiceStatus.running).delay(milliseconds: 500).execute();
} catch (e, stack) {
await Future.delayed(const Duration(milliseconds: 500));

error('Unknown error', error: e, stackTrace: stack);

serviceException = e as Exception;
Expand All @@ -177,13 +184,22 @@ class DevicesDiscoveryService implements Service {

@override
Future<DevicesDiscoveryService> restart() async {
// TODO: implement restart
throw UnimplementedError();
var needStop = !(serviceStatus.value == ServiceStatus.error || serviceStatus.value == ServiceStatus.pending);

if (needStop) {
await stop(sendExitPackage: false);

await Future.delayed(const Duration(milliseconds: 1000));
}

await init();

return this;
}

@override
Future<DevicesDiscoveryService> stop({bool sendExitPackage = true}) async {
void stopAction() {
Future<void> stopAction() async {
sendTimer?.cancel();
sendTimer = null;

Expand All @@ -194,6 +210,8 @@ class DevicesDiscoveryService implements Service {
receiveSocket = null;
sendSocket = null;

await Future.delayed(const Duration(milliseconds: 500));

serviceStatus.value = ServiceStatus.pending;
}

Expand All @@ -205,13 +223,15 @@ class DevicesDiscoveryService implements Service {
serviceStatus.value = ServiceStatus.stopping;

if (inErrState) {
await Future.delayed(const Duration(milliseconds: 500));

serviceStatus.value = ServiceStatus.pending;
} else if (sendExitPackage) {
_isExitPackageSent = true;

stopAction.delay(milliseconds: 1500).execute();
await stopAction.delay(milliseconds: 1500).execute();
} else {
stopAction();
await stopAction();
}

return this;
Expand Down
5 changes: 5 additions & 0 deletions kitx_mobile/lib/services/devices_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ class DeviceService implements Service {
);
});

await Future.delayed(const Duration(milliseconds: 500));

serviceStatus.value = ServiceStatus.running;

return this;
Expand All @@ -160,11 +162,14 @@ class DeviceService implements Service {
@override
Future<DeviceService> stop() async {
serviceStatus.value = ServiceStatus.stopping;

deviceInfoList.clear();

localDeviceCardAdded = false;
mainDeviceCardAdded = false;

await Future.delayed(const Duration(milliseconds: 500));

serviceStatus.value = ServiceStatus.pending;

return this;
Expand Down

0 comments on commit edeac91

Please sign in to comment.