import 'dart:async'; import 'dart:io'; import 'package:connectivity_plus/connectivity_plus.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/main/lockMian/lockList/lockList_logic.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(); Future getStarLockInfo( {bool isUnShowLoading = false}) async { final LockListInfoEntity entity = await ApiRepository.to.getStarLockListInfo( pageNo: pageNo, pageSize: 50, isUnShowLoading: isUnShowLoading, ); if (entity.errorCode!.codeIsSuccessful) { loadMainDataLogic(entity.data!); Storage.saveMainListData(entity.data!); // return entity.data!; } // else { // // showToast("数据请求失败"); // // state.dataLength.value = 0; // LockListInfoGroupEntity? lockListInfoGroupEntity = await Storage.getLockMainListData(); // loadMainDataLogic(lockListInfoGroupEntity!); // } return entity; } void loadMainDataLogic(LockListInfoGroupEntity entity) { if (entity.groupList!.isEmpty) { 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; //检测控制器是否存在 if (Get.isRegistered()) { //设置控制器数据并刷新 Get.find().setLockListInfoGroupEntity(entity); } } /// 获取联网类型 void 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 ConnectivityResult connectResult = await Connectivity().checkConnectivity(); return connectResult != 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(); } }); } /// 检测推送是否开启 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( '为了让您及时收到重要通知和更新,我们需要获取通知权限。请点击“确定”按钮,然后在设置页面中启用通知权限。'.tr, () async { openAppSettings(); }); } } @override void onReady() { super.onReady(); // 开启UDP UdpHelp().openUDP(); BlueManage(); } @override void onInit() { super.onInit(); checkWhetherPushIsEnabled(); } @override void onClose() { super.onClose(); } }