一次大的提交
This commit is contained in:
@@ -7,6 +7,7 @@ import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_patcher/flutter_patcher.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/core/infrastructure/logging/app_bloc_observer.dart';
|
||||
import 'package:maibu_satabot_v2/core/logging/i_logger_service.dart';
|
||||
@@ -80,16 +81,18 @@ class MyApp extends StatelessWidget {
|
||||
create: (_) {
|
||||
final user = sl<AppUserCubit>().state.user;
|
||||
final devicesCubit = sl<DevicesCubit>();
|
||||
|
||||
|
||||
// 🔥 修复:检查是否已有 targetDevice,如果有则不需要加载设备列表
|
||||
final remoteControlCubit = sl<RemoteControlCubit>();
|
||||
if (user != null && remoteControlCubit.state.targetDevice == null) {
|
||||
debugPrint('📱 [Main] 无 targetDevice,开始加载设备列表');
|
||||
devicesCubit.fetchAllDevices(user.username);
|
||||
} else if (user != null) {
|
||||
debugPrint('✅ [Main] 已有 targetDevice: ${remoteControlCubit.state.targetDevice!.deviceName},跳过设备列表加载');
|
||||
debugPrint(
|
||||
'✅ [Main] 已有 targetDevice: ${remoteControlCubit.state.targetDevice!.deviceName},跳过设备列表加载',
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
return devicesCubit;
|
||||
},
|
||||
),
|
||||
@@ -105,9 +108,7 @@ class MyApp extends StatelessWidget {
|
||||
BlocProvider<RemoteControlCubit>(
|
||||
create: (_) => sl<RemoteControlCubit>(),
|
||||
),
|
||||
BlocProvider<DeviceTaskCubit>(
|
||||
create: (_) => sl<DeviceTaskCubit>(),
|
||||
),
|
||||
BlocProvider<DeviceTaskCubit>(create: (_) => sl<DeviceTaskCubit>()),
|
||||
],
|
||||
child: BlocBuilder<LocaleCubit, Locale>(
|
||||
bloc: localeCubit,
|
||||
@@ -143,10 +144,24 @@ class _LifecycleListener extends StatefulWidget {
|
||||
|
||||
class _LifecycleListenerState extends State<_LifecycleListener>
|
||||
with WidgetsBindingObserver {
|
||||
String? _currentSessionId;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_initSession();
|
||||
}
|
||||
|
||||
Future<void> _initSession() async {
|
||||
final prefs = sl<SharedPreferences>();
|
||||
final savedCurrentId = prefs.getString('current_session_id');
|
||||
if (savedCurrentId != null) {
|
||||
_currentSessionId = savedCurrentId;
|
||||
debugPrint('📱 [生命周期] 恢复会话ID: $savedCurrentId');
|
||||
} else {
|
||||
debugPrint('📱 [生命周期] 首次启动,等待 appStarted 初始化会话');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -157,9 +172,59 @@ class _LifecycleListenerState extends State<_LifecycleListener>
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
debugPrint('📱 [生命周期] 应用恢复到前台');
|
||||
_handleResume();
|
||||
if (state == AppLifecycleState.paused) {
|
||||
_handleAppPaused();
|
||||
} else if (state == AppLifecycleState.resumed) {
|
||||
_handleAppResumed();
|
||||
} else if (state == AppLifecycleState.detached) {
|
||||
_handleAppDetached();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _handleAppPaused() async {
|
||||
final prefs = sl<SharedPreferences>();
|
||||
await prefs.setBool('pending_kill_logout', true);
|
||||
if (_currentSessionId != null) {
|
||||
await prefs.setString('saved_session_id', _currentSessionId!);
|
||||
}
|
||||
debugPrint('📱 [生命周期] 应用进入后台,已保存会话ID和杀后台标记');
|
||||
}
|
||||
|
||||
Future<void> _handleAppResumed() async {
|
||||
final prefs = sl<SharedPreferences>();
|
||||
final savedSessionId = prefs.getString('saved_session_id');
|
||||
final currentSessionId =
|
||||
prefs.getString('current_session_id') ?? _currentSessionId;
|
||||
|
||||
if (currentSessionId != null && savedSessionId == currentSessionId) {
|
||||
debugPrint('📱 [生命周期] 应用正常恢复(会话ID匹配),清除杀后台标记');
|
||||
await prefs.setBool('pending_kill_logout', false);
|
||||
await prefs.remove('saved_session_id');
|
||||
} else if (savedSessionId != null && savedSessionId != currentSessionId) {
|
||||
debugPrint('📱 [生命周期] 应用从后台恢复但会话ID不匹配,设置杀后台标记');
|
||||
await prefs.setBool('pending_kill_logout', true);
|
||||
} else {
|
||||
debugPrint('📱 [生命周期] 应用恢复(未进入过后台),清除杀后台标记');
|
||||
await prefs.setBool('pending_kill_logout', false);
|
||||
}
|
||||
|
||||
_handleResume();
|
||||
}
|
||||
|
||||
Future<void> _handleAppDetached() async {
|
||||
final prefs = sl<SharedPreferences>();
|
||||
final savedSessionId = prefs.getString('saved_session_id');
|
||||
final currentSessionId =
|
||||
prefs.getString('current_session_id') ?? _currentSessionId;
|
||||
|
||||
if (savedSessionId != null &&
|
||||
currentSessionId != null &&
|
||||
savedSessionId == currentSessionId) {
|
||||
debugPrint('📱 [生命周期] 应用在后台被杀(detached),保留杀后台标记');
|
||||
} else {
|
||||
debugPrint('📱 [生命周期] 应用在前台被杀(detached),清除杀后台标记');
|
||||
await prefs.setBool('pending_kill_logout', false);
|
||||
await prefs.remove('saved_session_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,16 +232,13 @@ class _LifecycleListenerState extends State<_LifecycleListener>
|
||||
try {
|
||||
final authCubit = context.read<AuthCubit>();
|
||||
if (authCubit.state is! AuthAuthenticated) return;
|
||||
|
||||
// 🔥 关键修复:只有在用户已选择设备时才重连 TCP
|
||||
// 否则创建一个无用的 TCP 连接,服务端会残留 session,
|
||||
// 导致后续 connectBySwitch() 复用该连接时触发 have_logged_in
|
||||
|
||||
final remoteControlCubit = sl<RemoteControlCubit>();
|
||||
if (remoteControlCubit.state.targetDevice == null) {
|
||||
debugPrint('📱 [生命周期] 未选择设备,跳过 TCP 重连(避免创建无用连接)');
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
await authCubit.reconnectAfterResume();
|
||||
} catch (e) {
|
||||
|
||||
Reference in New Issue
Block a user