实现推送消息触发的远程开锁请求流程,包括: 1. 新增RemoteUnlockRequestEvent和PushExtraEvent事件类型 2. 在锁详情页添加倒计时弹窗和动画效果 3. 实现Native层推送数据缓存和转发机制 4. 添加RemoteUnlockCoordinator处理应用未启动时的推送导航
119 lines
4.0 KiB
Dart
119 lines
4.0 KiB
Dart
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<RemoteUnlockRequestEvent> _pending = <RemoteUnlockRequestEvent>[];
|
|
|
|
static void init() {
|
|
_instance._init();
|
|
}
|
|
|
|
void _init() {
|
|
if (_inited) return;
|
|
_inited = true;
|
|
_sub = eventBus.on<PushExtraEvent>().listen((PushExtraEvent evt) async {
|
|
final Map<String, dynamic> 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<LockDetailLogic>()) {
|
|
final int currentLockId = Get.find<LockDetailLogic>().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<RemoteUnlockRequestEvent>.from(_pending)) {
|
|
await _navigateToLockDetailAndRefire(e);
|
|
print('导航到指定界面成功');
|
|
}
|
|
_pending.clear();
|
|
}
|
|
});
|
|
}
|
|
|
|
Future<void> _navigateToLockDetailAndRefire(RemoteUnlockRequestEvent event) async {
|
|
final LockListInfoItemEntity? item = await _findLockItem(event.lockId);
|
|
print('导航item: $item');
|
|
if (item == null) return;
|
|
|
|
Get.toNamed(Routers.lockDetailMainPage, arguments: <String, Object?>{
|
|
'keyInfo': item,
|
|
'isOnlyOneData': false,
|
|
});
|
|
print('导航到了指定界面:${event.lockId}, ${event.timeoutSeconds}');
|
|
|
|
int tries = 0;
|
|
while (tries < 40) {
|
|
tries++;
|
|
if (Get.isRegistered<LockDetailLogic>()) {
|
|
final int currentLockId = Get.find<LockDetailLogic>().state.keyInfos.value.lockId ?? 0;
|
|
if (currentLockId == event.lockId) {
|
|
break;
|
|
}
|
|
}
|
|
await Future<void>.delayed(const Duration(milliseconds: 50));
|
|
}
|
|
|
|
eventBus.fire(RemoteUnlockRequestEvent(lockId: event.lockId, timeoutSeconds: event.timeoutSeconds));
|
|
}
|
|
|
|
Future<LockListInfoItemEntity?> _findLockItem(int lockId) async {
|
|
final LockListInfoGroupEntity? stored = await Storage.getLockMainListData();
|
|
LockListInfoItemEntity? item;
|
|
if (stored != null) {
|
|
for (final GroupList g in stored.groupList ?? <GroupList>[]) {
|
|
for (final LockListInfoItemEntity l in g.lockList ?? <LockListInfoItemEntity>[]) {
|
|
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 ?? <GroupList>[]) {
|
|
for (final LockListInfoItemEntity l in g.lockList ?? <LockListInfoItemEntity>[]) {
|
|
if ((l.lockId ?? 0) == lockId) {
|
|
return l;
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|