import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:ttstd_family_care/features/device/data/device_api.dart'; import 'package:ttstd_family_care/features/device/data/device_repository_impl.dart'; import 'package:ttstd_family_care/features/device/domain/device_models.dart'; import 'package:ttstd_family_care/features/device/domain/device_repository.dart'; import '../../../core/storage/token_storage.dart'; /// 设备仓储 Provider。 final deviceRepositoryProvider = Provider((ref) { const api = DeviceApi(); return DeviceRepositoryImpl(api); }); /// 当前用户已绑定设备列表。 final myDevicesProvider = FutureProvider>((ref) async { final repo = ref.watch(deviceRepositoryProvider); return repo.getMyDevices(); }); final _cachedMyDevicesProvider = FutureProvider>((ref) { return ref.watch(deviceRepositoryProvider).getCachedMyDevices(); }); final cachedDeviceLocationProvider = FutureProvider.autoDispose.family((ref, sn) { return ref.watch(deviceRepositoryProvider).getCachedLocation(sn); }); final cachedRecentScreenshotsProvider = FutureProvider.autoDispose.family, String>((ref, sn) { return ref.watch(deviceRepositoryProvider).getCachedRecentScreenshots(sn); }); /// 用户手动选择的设备 SN。 /// /// 为空或对应设备已解绑时,[currentDeviceProvider] 自动回退到绑定列表首个设备, /// 因此切换设备只需写入此状态。 final selectedDeviceSnOverrideProvider = NotifierProvider(SelectedDeviceSn.new); class SelectedDeviceSn extends Notifier { @override String? build() => TokenStorage.selectedDeviceSn; void select(String serialno) { TokenStorage.saveSelectedDeviceSn(serialno); state = serialno; } } /// 当前正在查看的设备(未绑定设备时为 null)。 /// /// 绑定多个设备时以用户选择为准;刷新列表期间保留上一次数据, /// 解绑当前设备后自动落到新列表首个设备。 final currentDeviceProvider = Provider((ref) { final devices = ref.watch(myDevicesProvider); final list = devices.valueOrNull ?? (devices.isLoading ? ref.watch(_cachedMyDevicesProvider).valueOrNull : null) ?? const []; if (list.isEmpty) return null; final selectedSn = ref.watch(selectedDeviceSnOverrideProvider); for (final device in list) { if (device.serialno == selectedSn) return device; } return list.first; }); /// 当前选中的设备序列号(默认取绑定列表首个设备)。 final selectedDeviceSnProvider = Provider( (ref) => ref.watch(currentDeviceProvider)?.serialno, ); /// 按 SN 获取设备基本信息。 final deviceInfoProvider = FutureProvider.family((ref, sn) async { if (sn.isEmpty) return null; final repo = ref.watch(deviceRepositoryProvider); return repo.getDeviceInfo(sn); }); /// 按 SN 获取设备最新定位信息。 final deviceLocationProvider = FutureProvider.family((ref, sn) async { if (sn.isEmpty) return null; final repo = ref.watch(deviceRepositoryProvider); return repo.getLocation(sn); }); /// 按 SN 获取设备已安装应用列表。 final deviceApksProvider = FutureProvider.family, String>((ref, sn) async { if (sn.isEmpty) return const []; final repo = ref.watch(deviceRepositoryProvider); return repo.getApks(sn); }); /// 按 SN 获取设备最近截图列表。 final recentScreenshotsProvider = FutureProvider.family, String>((ref, sn) async { if (sn.isEmpty) return const []; final repo = ref.watch(deviceRepositoryProvider); return repo.getRecentScreenshots(sn); }); /// 按 SN 获取设备策略信息(功能开关)。 final devicePolicyProvider = FutureProvider.family((ref, sn) async { if (sn.isEmpty) return null; final repo = ref.watch(deviceRepositoryProvider); return repo.getPolicy(sn); });