mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Extract DeviceType as an enum with icon and label getters
Mirrors the NotificationType pattern. Removes duplicated _deviceTypes list and _deviceIcon() helper from both device screens, centralising all type-to-icon logic in the model. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
2da339a2b0
commit
1b470fc28f
@@ -2,22 +2,12 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
import '../model/device.dart';
|
import '../model/device.dart';
|
||||||
|
import '../model/device_type.dart';
|
||||||
import '../utils/friendly_date_formatter.dart';
|
import '../utils/friendly_date_formatter.dart';
|
||||||
import '../utils/oott_api.dart';
|
import '../utils/oott_api.dart';
|
||||||
import '../utils/ui_snackbars.dart';
|
import '../utils/ui_snackbars.dart';
|
||||||
import '../widgets/status_badge.dart';
|
import '../widgets/status_badge.dart';
|
||||||
|
|
||||||
const _deviceTypes = [
|
|
||||||
'phone',
|
|
||||||
'laptop',
|
|
||||||
'tablet',
|
|
||||||
'server',
|
|
||||||
'router',
|
|
||||||
'tv',
|
|
||||||
'printer',
|
|
||||||
'unknown',
|
|
||||||
];
|
|
||||||
|
|
||||||
class DeviceDetail extends StatefulWidget {
|
class DeviceDetail extends StatefulWidget {
|
||||||
final String macAddress;
|
final String macAddress;
|
||||||
|
|
||||||
@@ -99,9 +89,7 @@ class _DeviceDetailState extends State<DeviceDetail> {
|
|||||||
Future<void> _showRegisterDialog(Device device) async {
|
Future<void> _showRegisterDialog(Device device) async {
|
||||||
final formKey = GlobalKey<FormState>();
|
final formKey = GlobalKey<FormState>();
|
||||||
String owner = '';
|
String owner = '';
|
||||||
String deviceType = _deviceTypes.contains(device.deviceType)
|
DeviceType deviceType = device.deviceType;
|
||||||
? device.deviceType
|
|
||||||
: 'unknown';
|
|
||||||
|
|
||||||
final saved = await showDialog<bool>(
|
final saved = await showDialog<bool>(
|
||||||
context: context,
|
context: context,
|
||||||
@@ -123,12 +111,15 @@ class _DeviceDetailState extends State<DeviceDetail> {
|
|||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
InputDecorator(
|
InputDecorator(
|
||||||
decoration: const InputDecoration(labelText: 'Device Type'),
|
decoration: const InputDecoration(labelText: 'Device Type'),
|
||||||
child: DropdownButton<String>(
|
child: DropdownButton<DeviceType>(
|
||||||
value: deviceType,
|
value: deviceType,
|
||||||
isExpanded: true,
|
isExpanded: true,
|
||||||
underline: const SizedBox(),
|
underline: const SizedBox(),
|
||||||
items: _deviceTypes
|
items: DeviceType.values
|
||||||
.map((t) => DropdownMenuItem(value: t, child: Text(t)))
|
.map(
|
||||||
|
(t) =>
|
||||||
|
DropdownMenuItem(value: t, child: Text(t.label)),
|
||||||
|
)
|
||||||
.toList(),
|
.toList(),
|
||||||
onChanged: (value) =>
|
onChanged: (value) =>
|
||||||
setDialogState(() => deviceType = value ?? deviceType),
|
setDialogState(() => deviceType = value ?? deviceType),
|
||||||
@@ -162,7 +153,7 @@ class _DeviceDetailState extends State<DeviceDetail> {
|
|||||||
await BackendAPI.instance.registerDevice(
|
await BackendAPI.instance.registerDevice(
|
||||||
device.macAddress,
|
device.macAddress,
|
||||||
owner,
|
owner,
|
||||||
deviceType,
|
deviceType.name,
|
||||||
);
|
);
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
UISnackbars.showSuccess(context, 'Device registered');
|
UISnackbars.showSuccess(context, 'Device registered');
|
||||||
@@ -173,27 +164,6 @@ class _DeviceDetailState extends State<DeviceDetail> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IconData _deviceIcon(String deviceType) {
|
|
||||||
switch (deviceType.toLowerCase()) {
|
|
||||||
case 'phone':
|
|
||||||
return Icons.phone_android;
|
|
||||||
case 'laptop':
|
|
||||||
return Icons.laptop;
|
|
||||||
case 'tablet':
|
|
||||||
return Icons.tablet_android;
|
|
||||||
case 'server':
|
|
||||||
return Icons.dns;
|
|
||||||
case 'router':
|
|
||||||
return Icons.router;
|
|
||||||
case 'tv':
|
|
||||||
return Icons.tv;
|
|
||||||
case 'printer':
|
|
||||||
return Icons.print;
|
|
||||||
default:
|
|
||||||
return Icons.device_unknown;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final device = _device;
|
final device = _device;
|
||||||
@@ -248,10 +218,7 @@ class _DeviceDetailState extends State<DeviceDetail> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
_DeviceHeader(
|
_DeviceHeader(device: device),
|
||||||
device: device,
|
|
||||||
deviceIcon: _deviceIcon(device.deviceType),
|
|
||||||
),
|
|
||||||
const SizedBox(height: 24),
|
const SizedBox(height: 24),
|
||||||
_DeviceInfoCard(device: device),
|
_DeviceInfoCard(device: device),
|
||||||
],
|
],
|
||||||
@@ -263,16 +230,15 @@ class _DeviceDetailState extends State<DeviceDetail> {
|
|||||||
|
|
||||||
class _DeviceHeader extends StatelessWidget {
|
class _DeviceHeader extends StatelessWidget {
|
||||||
final Device device;
|
final Device device;
|
||||||
final IconData deviceIcon;
|
|
||||||
|
|
||||||
const _DeviceHeader({required this.device, required this.deviceIcon});
|
const _DeviceHeader({required this.device});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final theme = Theme.of(context);
|
final theme = Theme.of(context);
|
||||||
return Row(
|
return Row(
|
||||||
children: [
|
children: [
|
||||||
Icon(deviceIcon, size: 48),
|
Icon(device.deviceType.icon, size: 48),
|
||||||
const SizedBox(width: 16),
|
const SizedBox(width: 16),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Column(
|
child: Column(
|
||||||
@@ -321,12 +287,7 @@ class _DeviceInfoCard extends StatelessWidget {
|
|||||||
value: formatter.format(device.lastSeen),
|
value: formatter.format(device.lastSeen),
|
||||||
),
|
),
|
||||||
const Divider(height: 1),
|
const Divider(height: 1),
|
||||||
_InfoRow(
|
_InfoRow(label: 'Device Type', value: device.deviceType.label),
|
||||||
label: 'Device Type',
|
|
||||||
value: device.deviceType.isEmpty || device.deviceType == 'unknown'
|
|
||||||
? 'Unknown'
|
|
||||||
: device.deviceType,
|
|
||||||
),
|
|
||||||
const Divider(height: 1),
|
const Divider(height: 1),
|
||||||
_InfoRow(
|
_InfoRow(
|
||||||
label: 'Owner',
|
label: 'Owner',
|
||||||
|
|||||||
@@ -2,22 +2,12 @@ import 'package:flutter/material.dart';
|
|||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
import '../model/device.dart';
|
import '../model/device.dart';
|
||||||
|
import '../model/device_type.dart';
|
||||||
import '../utils/friendly_date_formatter.dart';
|
import '../utils/friendly_date_formatter.dart';
|
||||||
import '../utils/oott_api.dart';
|
import '../utils/oott_api.dart';
|
||||||
import '../utils/ui_snackbars.dart';
|
import '../utils/ui_snackbars.dart';
|
||||||
import '../widgets/status_badge.dart';
|
import '../widgets/status_badge.dart';
|
||||||
|
|
||||||
const _deviceTypes = [
|
|
||||||
'phone',
|
|
||||||
'laptop',
|
|
||||||
'tablet',
|
|
||||||
'server',
|
|
||||||
'router',
|
|
||||||
'tv',
|
|
||||||
'printer',
|
|
||||||
'unknown',
|
|
||||||
];
|
|
||||||
|
|
||||||
enum _DeviceFilter { newDevices, registered, all }
|
enum _DeviceFilter { newDevices, registered, all }
|
||||||
|
|
||||||
class DeviceList extends StatefulWidget {
|
class DeviceList extends StatefulWidget {
|
||||||
@@ -111,9 +101,7 @@ class _DeviceListState extends State<DeviceList> {
|
|||||||
Future<void> _showRegisterDialog(Device device) async {
|
Future<void> _showRegisterDialog(Device device) async {
|
||||||
final formKey = GlobalKey<FormState>();
|
final formKey = GlobalKey<FormState>();
|
||||||
String owner = '';
|
String owner = '';
|
||||||
String deviceType = _deviceTypes.contains(device.deviceType)
|
DeviceType deviceType = device.deviceType;
|
||||||
? device.deviceType
|
|
||||||
: 'unknown';
|
|
||||||
|
|
||||||
final saved = await showDialog<bool>(
|
final saved = await showDialog<bool>(
|
||||||
context: context,
|
context: context,
|
||||||
@@ -135,12 +123,15 @@ class _DeviceListState extends State<DeviceList> {
|
|||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
InputDecorator(
|
InputDecorator(
|
||||||
decoration: const InputDecoration(labelText: 'Device Type'),
|
decoration: const InputDecoration(labelText: 'Device Type'),
|
||||||
child: DropdownButton<String>(
|
child: DropdownButton<DeviceType>(
|
||||||
value: deviceType,
|
value: deviceType,
|
||||||
isExpanded: true,
|
isExpanded: true,
|
||||||
underline: const SizedBox(),
|
underline: const SizedBox(),
|
||||||
items: _deviceTypes
|
items: DeviceType.values
|
||||||
.map((t) => DropdownMenuItem(value: t, child: Text(t)))
|
.map(
|
||||||
|
(t) =>
|
||||||
|
DropdownMenuItem(value: t, child: Text(t.label)),
|
||||||
|
)
|
||||||
.toList(),
|
.toList(),
|
||||||
onChanged: (value) =>
|
onChanged: (value) =>
|
||||||
setDialogState(() => deviceType = value ?? deviceType),
|
setDialogState(() => deviceType = value ?? deviceType),
|
||||||
@@ -174,7 +165,7 @@ class _DeviceListState extends State<DeviceList> {
|
|||||||
await BackendAPI.instance.registerDevice(
|
await BackendAPI.instance.registerDevice(
|
||||||
device.macAddress,
|
device.macAddress,
|
||||||
owner,
|
owner,
|
||||||
deviceType,
|
deviceType.name,
|
||||||
);
|
);
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
UISnackbars.showSuccess(context, 'Device registered');
|
UISnackbars.showSuccess(context, 'Device registered');
|
||||||
@@ -185,27 +176,6 @@ class _DeviceListState extends State<DeviceList> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
IconData _deviceIcon(String deviceType) {
|
|
||||||
switch (deviceType.toLowerCase()) {
|
|
||||||
case 'phone':
|
|
||||||
return Icons.phone_android;
|
|
||||||
case 'laptop':
|
|
||||||
return Icons.laptop;
|
|
||||||
case 'tablet':
|
|
||||||
return Icons.tablet_android;
|
|
||||||
case 'server':
|
|
||||||
return Icons.dns;
|
|
||||||
case 'router':
|
|
||||||
return Icons.router;
|
|
||||||
case 'tv':
|
|
||||||
return Icons.tv;
|
|
||||||
case 'printer':
|
|
||||||
return Icons.print;
|
|
||||||
default:
|
|
||||||
return Icons.device_unknown;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
String _emptyMessage() {
|
String _emptyMessage() {
|
||||||
switch (_filter) {
|
switch (_filter) {
|
||||||
case _DeviceFilter.newDevices:
|
case _DeviceFilter.newDevices:
|
||||||
@@ -281,12 +251,10 @@ class _DeviceListState extends State<DeviceList> {
|
|||||||
onTap: () =>
|
onTap: () =>
|
||||||
context.push('/devices/${device.macAddress}'),
|
context.push('/devices/${device.macAddress}'),
|
||||||
leading: Tooltip(
|
leading: Tooltip(
|
||||||
message:
|
message: device.deviceType == DeviceType.unknown
|
||||||
device.deviceType.isEmpty ||
|
|
||||||
device.deviceType == 'unknown'
|
|
||||||
? 'Device type unknown'
|
? 'Device type unknown'
|
||||||
: device.deviceType,
|
: device.deviceType.label,
|
||||||
child: Icon(_deviceIcon(device.deviceType)),
|
child: Icon(device.deviceType.icon),
|
||||||
),
|
),
|
||||||
title: Row(
|
title: Row(
|
||||||
children: [
|
children: [
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import 'device_type.dart';
|
||||||
|
|
||||||
class Device {
|
class Device {
|
||||||
final String macAddress;
|
final String macAddress;
|
||||||
final String ipv4Address;
|
final String ipv4Address;
|
||||||
@@ -5,7 +7,7 @@ class Device {
|
|||||||
final DateTime lastSeen;
|
final DateTime lastSeen;
|
||||||
final bool isRegistered;
|
final bool isRegistered;
|
||||||
final String owner;
|
final String owner;
|
||||||
final String deviceType;
|
final DeviceType deviceType;
|
||||||
|
|
||||||
Device({
|
Device({
|
||||||
required this.macAddress,
|
required this.macAddress,
|
||||||
@@ -24,5 +26,5 @@ class Device {
|
|||||||
lastSeen = DateTime.parse(json['last_seen'] as String),
|
lastSeen = DateTime.parse(json['last_seen'] as String),
|
||||||
isRegistered = json['is_registered'] as bool,
|
isRegistered = json['is_registered'] as bool,
|
||||||
owner = json['owner'] as String,
|
owner = json['owner'] as String,
|
||||||
deviceType = json['device_type'] as String;
|
deviceType = DeviceType.fromString(json['device_type'] as String);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
enum DeviceType {
|
||||||
|
phone,
|
||||||
|
laptop,
|
||||||
|
tablet,
|
||||||
|
server,
|
||||||
|
router,
|
||||||
|
tv,
|
||||||
|
printer,
|
||||||
|
unknown;
|
||||||
|
|
||||||
|
IconData get icon => switch (this) {
|
||||||
|
phone => Icons.phone_android,
|
||||||
|
laptop => Icons.laptop,
|
||||||
|
tablet => Icons.tablet_android,
|
||||||
|
server => Icons.dns,
|
||||||
|
router => Icons.router,
|
||||||
|
tv => Icons.tv,
|
||||||
|
printer => Icons.print,
|
||||||
|
unknown => Icons.device_unknown,
|
||||||
|
};
|
||||||
|
|
||||||
|
String get label =>
|
||||||
|
this == unknown ? 'Unknown' : name[0].toUpperCase() + name.substring(1);
|
||||||
|
|
||||||
|
static DeviceType fromString(String value) => switch (value.toLowerCase()) {
|
||||||
|
'phone' => phone,
|
||||||
|
'laptop' => laptop,
|
||||||
|
'tablet' => tablet,
|
||||||
|
'server' => server,
|
||||||
|
'router' => router,
|
||||||
|
'tv' => tv,
|
||||||
|
'printer' => printer,
|
||||||
|
_ => unknown,
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user