102 lines
3.5 KiB
Dart
102 lines
3.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:star_lock/mine/message/messageList/messageList_entity.dart';
|
|
import 'package:star_lock/network/api_repository.dart';
|
|
import 'package:star_lock/network/start_chart_api.dart';
|
|
import 'package:star_lock/talk/starChart/constant/talk_status.dart';
|
|
import 'package:star_lock/talk/starChart/star_chart_manage.dart';
|
|
import 'package:star_lock/tools/baseGetXController.dart';
|
|
import 'package:star_lock/tools/eventBusEventManage.dart';
|
|
import 'package:star_lock/tools/storage.dart';
|
|
|
|
class AppLifecycleObserver extends WidgetsBindingObserver {
|
|
// 刷新消息列表
|
|
StreamSubscription? _readMessageRefreshUIEvent;
|
|
|
|
void _readMessageRefreshUIAction() {
|
|
// 蓝牙协议通知传输跟蓝牙之外的数据传输类不一样 eventBus
|
|
_readMessageRefreshUIEvent =
|
|
eventBus.on<ReadTalkMessageRefreshUI>().listen((event) async {
|
|
// 查询第一条消息
|
|
final MessageListEntity entity = await ApiRepository.to
|
|
.messageListLoadData(pageNo: '1', pageSize: '1');
|
|
if (entity.errorCode!.codeIsSuccessful) {
|
|
final lockName = event.lockName;
|
|
if (lockName != null && lockName.isNotEmpty) {
|
|
final readAt = entity.data?.list?.first.readAt == 0;
|
|
final data = entity.data?.list?.first.data;
|
|
if (readAt && data != null && data.contains(lockName)) {
|
|
// 如果是未读且饱含了锁名,就将这个消息设置为已读
|
|
final entity2 = await ApiRepository.to
|
|
.readMessageLoadData(messageId: entity.data!.list!.first.id!);
|
|
if (entity2.errorCode!.codeIsSuccessful) {
|
|
eventBus.fire(ReadMessageRefreshUI());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
super.didChangeAppLifecycleState(state);
|
|
|
|
// 打印应用程序生命周期状态
|
|
print('AppLifecycleState: $state');
|
|
|
|
// 根据应用程序状态执行相应的操作
|
|
if (state == AppLifecycleState.paused) {
|
|
// 应用程序进入后台
|
|
onAppPaused();
|
|
} else if (state == AppLifecycleState.resumed) {
|
|
// 应用程序恢复到前台
|
|
onAppResumed();
|
|
} else if (state == AppLifecycleState.detached) {
|
|
// 应用程序被杀死
|
|
onAppDetached();
|
|
}
|
|
}
|
|
|
|
void onAppPaused() {
|
|
// 处理应用程序进入后台的逻辑
|
|
|
|
final status = StartChartManage().talkStatus.status;
|
|
if (status == TalkStatus.passiveCallWaitingAnswer ||
|
|
status == TalkStatus.proactivelyCallWaitingAnswer ||
|
|
status == TalkStatus.answeredSuccessfully ||
|
|
status == TalkStatus.uninitialized) {
|
|
Get.back();
|
|
}
|
|
StartChartManage().destruction();
|
|
_readMessageRefreshUIEvent?.cancel();
|
|
}
|
|
|
|
void onAppResumed() async {
|
|
// 处理应用程序恢复到前台的逻辑
|
|
StartChartManage().init();
|
|
final loginData = await Storage.getLoginData();
|
|
|
|
// 获取星图url
|
|
if (loginData != null &&
|
|
loginData?.starchart != null &&
|
|
loginData?.starchart?.scdUrl != null &&
|
|
loginData?.starchart?.scdUrl != '') {
|
|
StartChartApi.to.startChartHost =
|
|
loginData!.starchart!.scdUrl ?? StartChartApi.to.startChartHost;
|
|
}
|
|
|
|
// 监听对讲消息处理角标已读
|
|
_readMessageRefreshUIAction();
|
|
print('App has resumed to the foreground.');
|
|
}
|
|
|
|
void onAppDetached() {
|
|
// 处理应用程序被杀死的逻辑
|
|
StartChartManage().destruction();
|
|
print('App has been detached.');
|
|
}
|
|
}
|