Skip to content

Commit

Permalink
ui for device cards
Browse files Browse the repository at this point in the history
  • Loading branch information
herzhenr committed Aug 20, 2023
1 parent 6fd9753 commit e909a25
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
2 changes: 2 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"homeDeviceListTitle": "Devices",
"homeDeviceCardWakeButton": "Wake Up",
"homeDeviceCardEditButton": "Edit",
"homeDeviceCardOnline": "Device is online",
"homeDeviceCardOffline": "Device is offline",
"homeWolCardTitle": "Waking up...",
"@DISCOVER": {},
"discoverTitle": "Discover Devices",
Expand Down
39 changes: 31 additions & 8 deletions lib/screens/home/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class _HomePageState extends State<HomePage> {

late SortingOrder selectedMenu = widget.selectedMenu;

Timer? _timer;
Timer? _pingDevicesTimer;

@override
void initState() {
Expand All @@ -62,8 +62,8 @@ class _HomePageState extends State<HomePage> {

@override
void dispose() {
_timer?.cancel();
print('---------timer canceled----------');
_pingDevicesTimer?.cancel();
// print('---------timer canceled----------');
super.dispose();
}

Expand Down Expand Up @@ -136,8 +136,8 @@ class _HomePageState extends State<HomePage> {
/// of the devices and update the ui accordingly
void _pingDevices() {
checkAllDevicesStatus();
print('---------timer started----------');
_timer = Timer.periodic(const Duration(seconds: 10), (timer) {
// print('---------timer started----------');
_pingDevicesTimer = Timer.periodic(const Duration(seconds: 16), (timer) {
checkAllDevicesStatus();
});
}
Expand All @@ -153,7 +153,7 @@ class _HomePageState extends State<HomePage> {
/// [device] is the device to ping
/// if the widget is not mounted anymore, the function will stop
Future<void> checkDeviceStatus(StorageDevice device) async {
print('pinging device: ${device.hostName}');
// print('pinging device: ${device.hostName}');
bool isOnline = await pingDevice(ipAddress: device.ipAddress);
if (mounted) {
setState(() {
Expand Down Expand Up @@ -246,7 +246,15 @@ class _HomePageState extends State<HomePage> {

Widget buildListview() {
return RefreshIndicator(
onRefresh: () async {},
onRefresh: () async {
_pingDevicesTimer?.cancel();
// print('---------timer canceled----------');
// set online state for all devices to null because online state is not known yet
for (StorageDevice device in _devices) {
device.isOnline = null;
}
_pingDevices();
},
child: ListView(
padding: AppConstants.screenPaddingScrollView,
children: [
Expand Down Expand Up @@ -419,15 +427,30 @@ class _HomePageState extends State<HomePage> {
required String subtitle2}) {
return customDualChoiceAlertdialog(
title: title != "" ? title : null,
child: (subtitle1 != "" || subtitle2 != "")
child: (subtitle1 != "" || subtitle2 != "" || device.isOnline != null)
? Column(
children: [
if (device.isOnline != null)
Text(
device.isOnline!
? AppLocalizations.of(context)!.homeDeviceCardOnline
: AppLocalizations.of(context)!
.homeDeviceCardOffline,
style: TextStyle(
color: device.isOnline!
? AppConstants.successMessageColor
: Theme.of(context).colorScheme.error)),
if (subtitle1 != "") Text(subtitle1),
if (subtitle2 != "") Text(subtitle2),
],
)
: null,
icon: getIcon(device.deviceType),
iconColor: device.isOnline != null
? device.isOnline!
? AppConstants.successMessageColor
: Theme.of(context).colorScheme.error
: null,
leftText: AppLocalizations.of(context)!.homeDeviceCardWakeButton,
rightText: AppLocalizations.of(context)!.homeDeviceCardEditButton,
leftIcon: AppConstants.wakeUp,
Expand Down
2 changes: 1 addition & 1 deletion lib/services/network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ Stream<List<Message>> sendWolAndGetMessages(

/// ping a list of devices and return their status
Future<bool> pingDevice({required String ipAddress}) async {
final ping = Ping(ipAddress, count: 1, timeout: 2);
final ping = Ping(ipAddress, count: 1, timeout: 3);

// Wait for the current ping to complete
await for (final response in ping.stream) {
Expand Down
7 changes: 4 additions & 3 deletions lib/widgets/layout_elements.dart
Original file line number Diff line number Diff line change
Expand Up @@ -173,17 +173,18 @@ class DeviceCard extends StatelessWidget {
? SizedBox(
height: double.infinity,
child: Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
Icon(
getIcon(deviceType!),
),
Positioned(
// draw a red marble
top: 15.0,
right: 0.0,
top: 12.0,
right: -4.0,
child: Icon(Icons.brightness_1,
size: 10.0,
size: 12.0,
color: isOnline == null
? Colors.grey
: isOnline!
Expand Down
4 changes: 2 additions & 2 deletions lib/widgets/universal_ui_components.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Widget customDualChoiceAlertdialog(
IconData? rightIcon,
Function()? leftOnPressed,
Function()? rightOnPressed}) {
return customAlertdialog(
return customAlertDialog(
title: title,
child: child,
icon: icon,
Expand Down Expand Up @@ -72,7 +72,7 @@ Widget customDualChoiceAlertdialog(
);
}

Widget customAlertdialog(
Widget customAlertDialog(
{String? title,
Widget? child,
IconData? icon,
Expand Down

0 comments on commit e909a25

Please sign in to comment.