import 'dart:async'; import 'dart:io'; import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:flutter/material.dart'; import 'package:flutter_easyloading/flutter_easyloading.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:get/get.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:star_lock/appRouters.dart'; import 'package:star_lock/flavors.dart'; import 'package:star_lock/login/login/entity/LoginEntity.dart'; import 'package:star_lock/main/lockDetail/lockDetail/lockDetail_logic.dart'; import 'package:star_lock/main/lockMian/lockList/lockList_logic.dart'; import 'package:star_lock/talk/startChart/start_chart_manage.dart'; import 'package:star_lock/tools/eventBusEventManage.dart'; import 'package:star_lock/tools/push/xs_jPhush.dart'; import 'package:star_lock/tools/showTipView.dart'; import '../../../app_settings/app_settings.dart'; import '../../../blue/blue_manage.dart'; import '../../../network/api_repository.dart'; import '../../../talk/udp/udp_help.dart'; import '../../../tools/baseGetXController.dart'; import '../../../tools/storage.dart'; import '../entity/lockListInfo_entity.dart'; import 'lockMain_state.dart'; class LockMainLogic extends BaseGetXController { final LockMainState state = LockMainState(); final List> _subscriptions = >[]; Future getStarLockInfo( {bool isUnShowLoading = false}) async { final LockListInfoEntity entity = await ApiRepository.to.getStarLockListInfo( pageNo: pageNo, pageSize: 50, isUnShowLoading: isUnShowLoading, ); if (entity.errorCode!.codeIsSuccessful) { // AppLog.log('请求列表调用 loadMainDataLogic'); await loadMainDataLogic(entity.data!); // updateZoneOffsetsAndLanguages(); } return entity; } Future updateZoneOffsetsAndLanguages() async { final LoginEntity entity = await ApiRepository.to.updateZoneOffsetsAndLanguages( timezoneOffset: DateTime.now().timeZoneOffset.inSeconds, // language: '' ); if (entity.errorCode!.codeIsSuccessful) {} return entity; } Future loadMainDataLogic(LockListInfoGroupEntity entity) async { final bool every = entity.groupList?.every((GroupList list) => (list.lockList ?? []).isEmpty) ?? true; if ((entity.groupList ?? []).isEmpty || every) { state.dataLength.value = 0; } else if (entity.groupList!.length == 1) { final GroupList groupList = entity.groupList![0]; if (groupList.lockList!.length > 1) { state.dataLength.value = 2; } else { state.dataLength.value = 1; } } else { state.dataLength.value = 2; } state.lockListInfoGroupEntity.value = entity; // AppLog.log('entity:$entity state.lockListInfoGroupEntity.value.groupList!.length:${state.lockListInfoGroupEntity.value.groupList![0].lockList!.length}'); //检测控制器是否存在 if (Get.isRegistered()) { //设置控制器数据并刷新 // AppLog.log('检测控制器是否存 调用了 setLockListInfoGroupEntity'); Get.find().setLockListInfoGroupEntity(entity); } else { //延迟加载 Future.delayed(200.milliseconds, () { if (Get.isRegistered()) { //设置控制器数据并刷新 // AppLog.log('检测控制器是否存 延迟调用了 setLockListInfoGroupEntity'); Get.find().setLockListInfoGroupEntity(entity); } }); } if (state.dataLength.value == 1) { if (Get.isRegistered()) { //设置控制器数据并刷新 // AppLog.log('检测控制器是否存 调用了 setLockListInfoGroupEntity'); Get.find().loadData( lockListInfoItemEntity: entity.groupList![0].lockList![0], isOnlyOneData: true); } else { //延迟加载 Future.delayed(200.milliseconds, () { if (Get.isRegistered()) { //设置控制器数据并刷新 // AppLog.log('检测控制器是否存 延迟调用了 setLockListInfoGroupEntity'); Get.find().loadData( lockListInfoItemEntity: entity.groupList![0].lockList![0], isOnlyOneData: true); } }); } } await Storage.saveMainListData(entity); } /// 获取联网类型 // Future _getConnectType() async { // final ConnectivityResult connectResult = // await Connectivity().checkConnectivity(); // if (connectResult == ConnectivityResult.mobile) { // // _netType = "4G"; // state.networkConnectionStatus.value = 1; // AppLog.log('网络连接: 4G 4G 4G 4G 4G'); // } else if (connectResult == ConnectivityResult.wifi) { // // _netType = "wifi"; // state.networkConnectionStatus.value = 1; // AppLog.log('网络连接: wifi wifi wifi wifi wifi'); // } else { // // _netType = "未连接"; // state.networkConnectionStatus.value = 0; // AppLog.log('网络连接: 未连接 未连接 未连接 未连接 未连接'); // // showToast("网络访问失败,请检查网络是否正常"); // } // } /// 判断网络是否连接 Future isConnected() async { final List connectResults = await Connectivity().checkConnectivity(); return !connectResults.contains(ConnectivityResult.none); } /// 设置网络切换监听 // Future connectListener() async { // Connectivity().onConnectivityChanged.listen((ConnectivityResult result) { // AppLog.log('设置网络切换监听:$result'); // if (state.networkConnectionStatus.value == 0 && result != ConnectivityResult.none) { // // 从无网络到有网络 // state.networkConnectionStatus.value = 1; // getStarLockInfo(isUnShowLoading: true); // } else if (result == ConnectivityResult.none) { // state.networkConnectionStatus.value = 0; // } // }); // } /// 判断是否有网络 Future judgeTheNetwork() async { final bool isNetwork = await isConnected(); if (!isNetwork) { EasyLoading.showToast('网络访问失败,请检查网络是否正常'.tr); } return isNetwork; } /// 检测推送是否开启 Future checkWhetherPushIsEnabled() async { bool notificationEnabled = false; if (Platform.isAndroid) { notificationEnabled = await FlutterLocalNotificationsPlugin() .resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>() ?.areNotificationsEnabled() ?? false; } else if (Platform.isIOS) { notificationEnabled = await FlutterLocalNotificationsPlugin() .resolvePlatformSpecificImplementation< IOSFlutterLocalNotificationsPlugin>() ?.requestPermissions( alert: true, badge: false, sound: true, ) ?? false; } if (!notificationEnabled) { //推送未开启 ShowTipView().showIosTipWithContentDialog( '为了让您及时收到重要通知和更新,我们需要获取通知权限。请点击“确定”按钮,然后在设置页面中启用通知权限。', () async { openAppSettings(); }); } } //刷新锁设备 Future flushedStarLockInfo(int keyId, int lockId) async { AppLog.log('刷新锁设备: keyId:$keyId lockId:$lockId'); final LockListInfoEntity entity = await ApiRepository.to.getStarLockInfo( keyId: keyId, ); if (entity.errorCode!.codeIsSuccessful && (entity.data?.groupList ?? []).isNotEmpty) { state.lockListInfoGroupEntity.value.groupList ??= []; final GroupList list = entity.data!.groupList!.first; final LockListInfoItemEntity listItem = entity.data!.groupList!.first.lockList!.first; final int groupIndex = state.lockListInfoGroupEntity.value.groupList ?.indexWhere( (GroupList group) => group.groupId == list.groupId) ?? -1; if (groupIndex != -1) { //已存在 state.lockListInfoGroupEntity.value.groupList![groupIndex].lockList ??= []; final List lockList = state .lockListInfoGroupEntity.value.groupList![groupIndex].lockList!; list.lockList ??= []; final int keyIndex = lockList.indexWhere( (LockListInfoItemEntity lock) => lock.lockId == lockId) ?? -1; if (keyIndex != -1) { //已存在 lockList.removeAt(keyIndex); lockList.insert(keyIndex, listItem); if (Get.isRegistered()) { final bool isOnlyOneData = F.sw(xhjCall: () => false, skyCall: () => lockList.length > 1); Get.find().loadData( lockListInfoItemEntity: listItem, isOnlyOneData: isOnlyOneData); } } else { //新增 lockList.insert(0, listItem); } } else { //新增 state.lockListInfoGroupEntity.value.groupList!.insert(0, list); } state.lockListInfoGroupEntity.refresh(); AppLog.log('刷新调用 loadMainDataLogic'); loadMainDataLogic(state.lockListInfoGroupEntity.value); } update(); } //删除锁设备 Future deleteStarLockInfo(int keyId, int lockId) async { state.lockListInfoGroupEntity.value.groupList ?.forEach((GroupList groupList) { groupList.lockList ?.removeWhere((LockListInfoItemEntity lock) => lock.keyId == keyId); }); state.lockListInfoGroupEntity.refresh(); AppLog.log('删除调用 loadMainDataLogic'); loadMainDataLogic(state.lockListInfoGroupEntity.value); if (Get.isRegistered()) { final bool isKey = Get.find().state.keyInfos.value.keyId == keyId; if (isKey) { //判断当前是否为当前锁 EasyLoading.showToast('该已锁被删除'.tr, duration: 3.seconds); Get.until((Route route) => route.settings.name == Routers.starLockMain); } } update(); } //初始化订阅 void _initSubscription() { _subscriptions.add(eventBus .on() .listen((RefreshLockInfoDataEvent event) { //锁设备刷新 flushedStarLockInfo( event.keyId, event.lockId, ); })); _subscriptions.add(eventBus .on() .listen((DeleteLockInfoDataEvent event) { //锁设备删除 deleteStarLockInfo( event.keyId, event.lockId, ); })); _subscriptions.add(eventBus .on() .listen((RogerThatLockInfoDataEvent event) { //锁设备新增 flushedStarLockInfo( event.keyId, event.lockId, ); })); } @override Future onReady() async { super.onReady(); //初始化星图服务并绑定配置 await StartChartManage().init(); // 初始化JPush服务并绑定设备ID final XSJPushProvider jpushProvider = XSJPushProvider(); await jpushProvider.initJPushService(); await jpushProvider.initLocalNotification(isCancelLocalPush: false); // 开启UDP // AppLog.log('onReady() LockMainLogic'); UdpHelp().openUDP(); BlueManage(); } @override void onInit() { super.onInit(); checkWhetherPushIsEnabled(); _initSubscription(); // connectListener(); // AppLog.log('onInit() updateZoneOffsetsAndLanguages'); // updateZoneOffsetsAndLanguages(); } @override void onClose() { _subscriptions.forEach((StreamSubscription subscription) { subscription.cancel(); }); super.onClose(); } static LockMainLogic? to() { if (Get.isRegistered()) { return Get.find(); } return null; } }