// device.dart — 已登录设备(GET /v1/me/devices)。 /// 设备平台。线格式是字符串(与服务端 devices.platform 对齐),在 model 边界经 /// [fromWire] 解析为枚举,UI/逻辑层只碰枚举(穷尽 switch + 拼错编译期报错)。 enum DevicePlatform { ios, android, windows, macos, linux, unknown; static DevicePlatform fromWire(String s) => switch (s.toLowerCase().trim()) { 'ios' => DevicePlatform.ios, 'android' => DevicePlatform.android, 'windows' => DevicePlatform.windows, 'macos' => DevicePlatform.macos, 'linux' => DevicePlatform.linux, _ => DevicePlatform.unknown, }; /// 展示名(大小写规范化)。 String get label => switch (this) { DevicePlatform.ios => 'iOS', DevicePlatform.android => 'Android', DevicePlatform.windows => 'Windows', DevicePlatform.macos => 'macOS', DevicePlatform.linux => 'Linux', DevicePlatform.unknown => '—', }; bool get isMobile => this == DevicePlatform.ios || this == DevicePlatform.android; } class Device { const Device({ required this.uuid, required this.name, required this.platform, this.lastSeen, this.clientVersion = '', this.online = false, this.lastLogin, }); final String uuid; final String name; /// 平台线格式(原始字符串,保留以便回传/调试)。UI 用 [platformKind]。 final String platform; /// 平台枚举(由线格式解析)。 DevicePlatform get platformKind => DevicePlatform.fromWire(platform); /// 最近活跃(UTC);从未上线为 null。 final DateTime? lastSeen; /// 客户端版本(该设备最近上报),可能为空。 final String clientVersion; /// 在线(数据面活跃,last_seen 在阈值内,由服务端判定)。 final bool online; /// 最后登录时间(最近一次会话创建,UTC);无会话为 null。 final DateTime? lastLogin; factory Device.fromJson(Map m) { DateTime? parseTs(String key) { final s = m[key] as String?; return (s != null && s.isNotEmpty) ? DateTime.tryParse(s) : null; } return Device( uuid: m['uuid'] as String? ?? '', name: m['name'] as String? ?? '', platform: m['platform'] as String? ?? '', lastSeen: parseTs('last_seen'), clientVersion: m['client_version'] as String? ?? '', online: m['online'] as bool? ?? false, lastLogin: parseTs('last_login'), ); } }