import 'dart:async'; import 'package:get/get.dart'; import 'package:star_lock/appRouters.dart'; import 'package:star_lock/main/lockDetail/lockDetail/lockDetail_logic.dart'; import 'package:star_lock/main/lockMian/entity/lockListInfo_entity.dart'; import 'package:star_lock/network/api_repository.dart'; import 'package:star_lock/tools/eventBusEventManage.dart'; import 'package:star_lock/tools/push/message_constant.dart'; import 'package:star_lock/tools/storage.dart'; class RemoteUnlockCoordinator { factory RemoteUnlockCoordinator() => _instance; RemoteUnlockCoordinator._(); static final RemoteUnlockCoordinator _instance = RemoteUnlockCoordinator._(); StreamSubscription? _sub; bool _inited = false; final List _pending = []; static void init() { _instance._init(); } void _init() { if (_inited) return; _inited = true; _sub = eventBus.on().listen((PushExtraEvent evt) async { final Map data = evt.data; final int eventNo = int.tryParse(data['eventNo']?.toString() ?? '') ?? -1; if (eventNo != MessageConstant.talkPushBigImage) { return; } final int lockId = int.tryParse(data['lockId']?.toString() ?? '') ?? -1; final RemoteUnlockRequestEvent event = RemoteUnlockRequestEvent(lockId: lockId, timeoutSeconds: 60); if (Get.isRegistered()) { final int currentLockId = Get.find().state.keyInfos.value.lockId ?? 0; if (currentLockId == event.lockId) { return; } } if (Get.context == null) { _pending.add(event); _waitAppReadyAndFlush(); return; } await _navigateToLockDetailAndRefire(event); }); } void _waitAppReadyAndFlush() { Timer.periodic(const Duration(milliseconds: 100), (Timer t) async { print('等待app启动中...${Get.context}'); if (Get.context != null) { t.cancel(); for (final RemoteUnlockRequestEvent e in List.from(_pending)) { await _navigateToLockDetailAndRefire(e); print('导航到指定界面成功'); } _pending.clear(); } }); } Future _navigateToLockDetailAndRefire(RemoteUnlockRequestEvent event) async { final LockListInfoItemEntity? item = await _findLockItem(event.lockId); print('导航item: $item'); if (item == null) return; Get.toNamed(Routers.lockDetailMainPage, arguments: { 'keyInfo': item, 'isOnlyOneData': false, }); print('导航到了指定界面:${event.lockId}, ${event.timeoutSeconds}'); int tries = 0; while (tries < 40) { tries++; if (Get.isRegistered()) { final int currentLockId = Get.find().state.keyInfos.value.lockId ?? 0; if (currentLockId == event.lockId) { break; } } await Future.delayed(const Duration(milliseconds: 50)); } eventBus.fire(RemoteUnlockRequestEvent(lockId: event.lockId, timeoutSeconds: event.timeoutSeconds)); } Future _findLockItem(int lockId) async { final LockListInfoGroupEntity? stored = await Storage.getLockMainListData(); LockListInfoItemEntity? item; if (stored != null) { for (final GroupList g in stored.groupList ?? []) { for (final LockListInfoItemEntity l in g.lockList ?? []) { if ((l.lockId ?? 0) == lockId) { item = l; break; } } if (item != null) break; } } if (item != null) return item; final LockListInfoEntity res = await ApiRepository.to.getStarLockListInfo(pageNo: 1, pageSize: 50, isUnShowLoading: true); for (final GroupList g in res.data?.groupList ?? []) { for (final LockListInfoItemEntity l in g.lockList ?? []) { if ((l.lockId ?? 0) == lockId) { return l; } } } return null; } }