2024-01-19 11:09:46 +08:00

190 lines
5.2 KiB
Dart

import 'dart:async';
import 'dart:typed_data';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:star_lock/talk/udp/udp_senderManage.dart';
import '../../../blue/io_tool/manager_event_bus.dart';
import '../../../network/api_repository.dart';
import '../../../talk/udp/udp_help.dart';
import '../../../tools/baseGetXController.dart';
import '../../../tools/eventBusEventManage.dart';
import '../entity/lockListInfo_entity.dart';
import 'lockMain_state.dart';
class LockMainLogic extends BaseGetXController {
final LockMainState state = LockMainState();
var page = 1;
// final RefreshController refreshController = RefreshController(
// initialRefresh: false
// );
/// 下拉刷新方法
// void onRefresh() {
// page = 1;
// getLockInfo();
// }
//
// void onLoading() {
// getLockInfo();
// }
// void getLockInfo() async{
// var entity = await ApiRepository.to.getLockInfo(
// lastUpdateDate:DateTime.now().millisecondsSinceEpoch.toString(),
// pageNo:page.toString(),
// );
// if(entity.errorCode!.codeIsSuccessful){
// // if (page == 0) {
// // refreshController.refreshCompleted();
// // } else {
// // if (entity.data!.keyInfos!.isEmpty) {
// // refreshController.loadNoData();
// // } else {
// // refreshController.loadComplete();
// // }
// // }
// // page++;
//
// if(entity.data!.keyInfos!.isEmpty){
// state.dataLength.value = 0;
// }else if(entity.data!.keyInfos!.length == 1){
// state.dataLength.value = 1;
// }else{
// state.dataLength.value = 2;
// }
// state.lockMainEntity.value = entity;
// }else{
// // refreshController.loadFailed();
// }
// // refreshController.refreshCompleted();
// }
// Future<LockListInfoGroupEntity>
void getStarLockInfo() async{
var entity = await ApiRepository.to.getStarLockListInfo(
pageNo:page,
pageSize:20,
);
if(entity.errorCode!.codeIsSuccessful){
// if (page == 0) {
// refreshController.refreshCompleted();
// } else {
// if (entity.data!.keyInfos!.isEmpty) {
// refreshController.loadNoData();
// } else {
// refreshController.loadComplete();
// }
// }
// page++;
// Future.delayed(const Duration(milliseconds: 200));
if(entity.data!.groupList!.isEmpty){
state.dataLength.value = 0;
}else if(entity.data!.groupList!.length == 1){
GroupList groupList = entity.data!.groupList![0];
if(groupList.lockList!.length > 1){
state.dataLength.value = 2;
}else{
state.dataLength.value = 1;
}
}else{
state.dataLength.value = 2;
}
state.lockListInfoEntity.value = entity;
// return entity.data!;
}else{
print("首页锁列表请求失败");
state.dataLength.value = 0;
// refreshController.loadFailed();
// return entity.data!;
}
// refreshController.refreshCompleted();
}
late StreamSubscription _teamEvent;
void _initLoadDataAction() {
_teamEvent = eventBus.on<RefreshLockListInfoDataEvent>().listen((event) {
// getLockInfo();
getStarLockInfo();
print("收到消息");
});
}
/// 获取联网类型
void getConnectType() async {
var connectResult = await (Connectivity().checkConnectivity());
if (connectResult == ConnectivityResult.mobile) {
// _netType = "4G";
state.networkConnectionStatus.value = 1;
// print("4G 4G 4G 4G 4G");
} else if (connectResult == ConnectivityResult.wifi) {
// _netType = "wifi";
state.networkConnectionStatus.value = 1;
// print("wifi wifi wifi wifi wifi");
} else {
// _netType = "未连接";
state.networkConnectionStatus.value = 0;
// print("未连接 未连接 未连接 未连接 未连接");
}
}
/// 判断网络是否连接
Future<bool> isConnected() async {
var connectResult = await (Connectivity().checkConnectivity());
return connectResult != ConnectivityResult.none;
}
/// 设置网络切换监听
connectListener() async {
Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
print("设置网络切换监听:$result");
if(state.networkConnectionStatus.value == 0 && result != ConnectivityResult.none){
// 从无网络到有网络
state.networkConnectionStatus.value = 1;
getStarLockInfo();
}
});
}
@override
void onReady() {
// TODO: implement onReady
super.onReady();
print("onReady()");
_initLoadDataAction();
// 开启UDP
UdpHelp().openUDP();
}
@override
void onInit() {
// TODO: implement onInit
super.onInit();
print("onInit()");
// getLockInfo();
// 设置网络变化监听
connectListener();
// 获取网络连接状态
getConnectType();
getStarLockInfo();
}
@override
void onClose() {
// TODO: implement onClose
super.onClose();
// refreshController.dispose();
_teamEvent.cancel();
// state.timer.cancel();
}
}