73 lines
2.9 KiB
Dart
73 lines
2.9 KiB
Dart
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';
|
|
|
|
/// 设备仓储 Provider。
|
|
final deviceRepositoryProvider = Provider<DeviceRepository>((ref) {
|
|
const api = DeviceApi();
|
|
return DeviceRepositoryImpl(api);
|
|
});
|
|
|
|
/// 当前用户已绑定设备列表。
|
|
final myDevicesProvider = FutureProvider<List<DeviceBrief>>((ref) async {
|
|
final repo = ref.watch(deviceRepositoryProvider);
|
|
return repo.getMyDevices();
|
|
});
|
|
|
|
/// 当前选中的设备序列号(默认取绑定列表首个设备)。
|
|
///
|
|
/// 使用普通 Provider(而非 StateProvider)以便随 [myDevicesProvider] 的加载完成
|
|
/// 自动推导出首个已绑定设备的 SN。若后续需要手动切换设备,可改为 StateProvider。
|
|
///
|
|
/// 解绑/绑定后 [myDevicesProvider] 被 invalidate 重新请求时,用 [AsyncValue.value]
|
|
/// 在刷新期间保留上一次的列表,避免默认 SN 短暂丢失;刷新完成后自动取新列表
|
|
/// 首个设备作为新默认 SN(解绑的是默认设备时默认 SN 会随之更新)。
|
|
final selectedDeviceSnProvider = Provider<String?>((ref) {
|
|
final devices = ref.watch(myDevicesProvider);
|
|
// loading/error 期间保留上一次成功数据,避免默认 SN 闪失。
|
|
final list = devices.value ?? const <DeviceBrief>[];
|
|
return list.isEmpty ? null : list.first.serialno;
|
|
});
|
|
|
|
/// 按 SN 获取设备基本信息。
|
|
final deviceInfoProvider =
|
|
FutureProvider.family<DeviceSystemInfo?, String>((ref, sn) async {
|
|
if (sn.isEmpty) return null;
|
|
final repo = ref.watch(deviceRepositoryProvider);
|
|
return repo.getDeviceInfo(sn);
|
|
});
|
|
|
|
/// 按 SN 获取设备最新定位信息。
|
|
final deviceLocationProvider =
|
|
FutureProvider.family<DeviceLocation?, String>((ref, sn) async {
|
|
if (sn.isEmpty) return null;
|
|
final repo = ref.watch(deviceRepositoryProvider);
|
|
return repo.getLocation(sn);
|
|
});
|
|
|
|
/// 按 SN 获取设备已安装应用列表。
|
|
final deviceApksProvider =
|
|
FutureProvider.family<List<DeviceApkInfo>, String>((ref, sn) async {
|
|
if (sn.isEmpty) return const [];
|
|
final repo = ref.watch(deviceRepositoryProvider);
|
|
return repo.getApks(sn);
|
|
});
|
|
|
|
/// 按 SN 获取设备最近截图列表。
|
|
final recentScreenshotsProvider =
|
|
FutureProvider.family<List<ScreenshotVO>, String>((ref, sn) async {
|
|
if (sn.isEmpty) return const [];
|
|
final repo = ref.watch(deviceRepositoryProvider);
|
|
return repo.getRecentScreenshots(sn);
|
|
});
|
|
|
|
/// 按 SN 获取设备策略信息(功能开关)。
|
|
final devicePolicyProvider =
|
|
FutureProvider.family<DevicePolicy?, String>((ref, sn) async {
|
|
if (sn.isEmpty) return null;
|
|
final repo = ref.watch(deviceRepositoryProvider);
|
|
return repo.getPolicy(sn);
|
|
});
|