144 lines
4.5 KiB
Dart
144 lines
4.5 KiB
Dart
import 'dart:async';
|
||
|
||
import 'package:flutter/material.dart';
|
||
import 'package:get/get.dart';
|
||
import 'package:get/get_rx/get_rx.dart';
|
||
import 'package:starcloud/entity/star_cloud_lock_list.dart';
|
||
import 'package:starcloud/sdk/sdk_device_operate_extension.dart';
|
||
import 'package:starcloud/sdk/starcloud.dart';
|
||
import 'package:starwork_flutter/base/app_logger.dart';
|
||
import 'package:starwork_flutter/base/base_controller.dart';
|
||
import 'package:starwork_flutter/common/constant/app_support_device_type.dart';
|
||
import 'package:starwork_flutter/common/events/refresh_device_list_event.dart';
|
||
import 'package:starwork_flutter/common/utils/event_bus_util.dart';
|
||
import 'package:starwork_flutter/views/main/main_controller.dart';
|
||
|
||
class DeviceManageController extends BaseController with GetSingleTickerProviderStateMixin {
|
||
final mainController = Get.find<MainController>();
|
||
|
||
// 选中状态 0:全部状态 1:在线 2:离线
|
||
final RxInt selectedStatusIndex = 0.obs;
|
||
|
||
// 选中设备类型
|
||
final selectedDeviceType = AppSupportDeviceType.lock.obs;
|
||
|
||
// 设备列表管理
|
||
final deviceList = <StarCloudLock>[].obs;
|
||
|
||
// 用于 UI 显示的过滤后列表
|
||
final filteredDeviceList = <StarCloudLock>[].obs;
|
||
|
||
// 在线设备数量
|
||
final onlineDeviceCount = 0.obs;
|
||
|
||
// 离线设备数量
|
||
final offlineDeviceCount = 0.obs;
|
||
|
||
// 刷新设备列表事件留监听
|
||
late StreamSubscription _refreshDeviceListSubscription;
|
||
|
||
// 搜索输入框
|
||
TextEditingController searchInputController = TextEditingController();
|
||
|
||
@override
|
||
void onInit() {
|
||
super.onInit();
|
||
initData();
|
||
|
||
// 监听刷新设备列表事件
|
||
_refreshDeviceListSubscription = EventBusUtil().instance.on<RefreshDeviceListEvent>().listen((event) async {
|
||
_requestTeamDeviceList();
|
||
});
|
||
|
||
// 监听搜索输入框变化
|
||
searchInputController.addListener(() {
|
||
_updateFilteredList(); // 输入变化时重新过滤
|
||
});
|
||
}
|
||
|
||
@override
|
||
void onClose() {
|
||
super.onClose();
|
||
_refreshDeviceListSubscription.cancel();
|
||
searchInputController.dispose();
|
||
}
|
||
|
||
void initData() async {
|
||
_requestTeamDeviceList();
|
||
}
|
||
|
||
/// 切换设备状态筛选
|
||
void changeStatusFilter(int index) {
|
||
selectedStatusIndex.value = index;
|
||
_updateFilteredList();
|
||
}
|
||
|
||
/// 根据当前 selectedStatusIndex 过滤设备列表
|
||
/// 根据当前 selectedStatusIndex 和 搜索关键词 过滤设备列表
|
||
void _updateFilteredList() {
|
||
List<StarCloudLock> filtered = deviceList;
|
||
|
||
// Step 1: 先按状态筛选
|
||
switch (selectedStatusIndex.value) {
|
||
case 0: // 全部
|
||
break;
|
||
case 1: // 在线
|
||
filtered = filtered.where((device) => device.network?.isOnline == 1).toList();
|
||
break;
|
||
case 2: // 离线
|
||
filtered = filtered.where((device) => device.network?.isOnline == 0).toList();
|
||
break;
|
||
}
|
||
|
||
// Step 2: 再按搜索关键词过滤(设备名称)
|
||
final String keyword = searchInputController.text.trim();
|
||
if (keyword.isNotEmpty) {
|
||
filtered = filtered.where((device) {
|
||
final name = device.lockName ?? '';
|
||
return name.toLowerCase().contains(keyword.toLowerCase());
|
||
}).toList();
|
||
}
|
||
|
||
// 更新 UI 列表
|
||
filteredDeviceList.assignAll(filtered);
|
||
}
|
||
|
||
void _requestTeamDeviceList() async {
|
||
showLoading();
|
||
await StarCloudSDK.instance.getDeviceList(
|
||
pageNo: 1,
|
||
pageSize: 999,
|
||
cloudUid: mainController.selectedTeam.value.teamCloudInfo!.uid!,
|
||
onSuccess: (StarCloudLockList list) {
|
||
filteredDeviceList.clear();
|
||
deviceList.clear();
|
||
for (var groupItem in list.groupList) {
|
||
for (var lock in groupItem.lockList) {
|
||
deviceList.add(lock);
|
||
if (lock.network?.isOnline == 1) {
|
||
onlineDeviceCount.value++;
|
||
} else {
|
||
offlineDeviceCount.value++;
|
||
}
|
||
}
|
||
}
|
||
// ✅ 在更新过滤前,先对 deviceList 按在线状态排序:在线在前,离线在后
|
||
deviceList.sort((a, b) {
|
||
final aOnline = a.network?.isOnline == 1;
|
||
final bOnline = b.network?.isOnline == 1;
|
||
if (aOnline == bOnline) return 0; // 状态相同,顺序不变
|
||
if (aOnline) return -1; // a 在线,排前面
|
||
return 1; // b 在线,或都离线,a 排后面
|
||
});
|
||
// ✅ 加载完原始数据后,更新过滤列表
|
||
_updateFilteredList();
|
||
hideLoading();
|
||
},
|
||
onError: (err) {
|
||
AppLogger.error('获取设备列表失败:${err}');
|
||
hideLoading();
|
||
},
|
||
);
|
||
}
|
||
}
|