166 lines
5.5 KiB
Dart
166 lines
5.5 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||
import 'package:get_it/get_it.dart';
|
||
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
||
import 'package:maibu_satabot_v2/core/consts/tcp_consts.dart';
|
||
import 'package:maibu_satabot_v2/core/network/tcp/tcp_client.dart';
|
||
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
|
||
|
||
import '../../../devices/domain/entities/device_entity.dart';
|
||
import '../widgets/ImmersionHeader.dart';
|
||
import '../widgets/quick_actions_grid.dart';
|
||
import '../widgets/work_params_card.dart';
|
||
|
||
class HomePage extends StatefulWidget {
|
||
const HomePage({super.key});
|
||
|
||
@override
|
||
State<HomePage> createState() => _HomePageState();
|
||
}
|
||
|
||
class _HomePageState extends State<HomePage> {
|
||
bool _isInitializing = false; // 标记是否正在初始化
|
||
bool _hasLoadedDevices = false; // 标记是否已加载过设备列表
|
||
|
||
/// 🔥 核心方法:页面显示前的初始化操作
|
||
Future<void> _initializePage() async {
|
||
if (_isInitializing) {
|
||
debugPrint('[HomePage] 正在初始化,跳过重复调用');
|
||
return;
|
||
}
|
||
|
||
_isInitializing = true;
|
||
|
||
try {
|
||
final userState = context.read<AppUserCubit>().state;
|
||
final String? username = userState.user?.username;
|
||
|
||
if (username == null || username.isEmpty) {
|
||
debugPrint('[HomePage] 用户未登录,跳过初始化');
|
||
return;
|
||
}
|
||
|
||
final devicesCubit = context.read<DevicesCubit>();
|
||
|
||
// 🔥 步骤 1: 先检查设备列表是否需要刷新
|
||
if (!_hasLoadedDevices && (devicesCubit.state.devices == null || devicesCubit.state.devices!.isEmpty)) {
|
||
debugPrint('[HomePage] 设备列表为空,触发获取...');
|
||
|
||
// ❌ 不要 await,让它后台执行
|
||
devicesCubit.fetchAllDevices(username);
|
||
|
||
// 🔥 等待一小段时间让数据过来
|
||
// await Future.delayed(const Duration(milliseconds: 500));
|
||
|
||
_hasLoadedDevices = true;
|
||
}
|
||
|
||
// 🔥 步骤 2: 获取最新设备列表
|
||
final deviceState = devicesCubit.state;
|
||
final List<DeviceEntity> deviceList = deviceState.devices ?? [];
|
||
|
||
if (deviceList.isNotEmpty) {
|
||
// 🔥 步骤 3: TCP 连接(异步执行,不阻塞 UI)
|
||
final firstDevice = deviceState.selectedDevice ?? deviceList.first;
|
||
debugPrint('🔌 [HomePage] 准备连接设备:${firstDevice.deviceName}');
|
||
|
||
final tcpClient = GetIt.instance<TcpClient>();
|
||
var bool = tcpClient.isConnected;
|
||
debugPrint('tcpClient,$bool');
|
||
if (!tcpClient.isConnected) {
|
||
// ❌ 异步执行,不阻塞
|
||
tcpClient.connectBySwitch(
|
||
host: TCPConsts.TCP_IP,
|
||
port: TCPConsts.TCP_PORT,
|
||
deviceName: firstDevice.deviceName
|
||
).then((_) {
|
||
debugPrint('✅ [HomePage] TCP 连接成功');
|
||
tcpClient.startHeartbeat(interval: const Duration(seconds: 4));
|
||
}).catchError((e) {
|
||
debugPrint('❌ [HomePage] TCP 连接失败:$e');
|
||
});
|
||
} else {
|
||
debugPrint('✅ [HomePage] TCP 已连接');
|
||
}
|
||
} else {
|
||
debugPrint('⚠️ [HomePage] 设备列表仍为空');
|
||
}
|
||
|
||
} catch (e) {
|
||
debugPrint('❌ [HomePage] 初始化异常:$e');
|
||
} finally {
|
||
_isInitializing = false;
|
||
}
|
||
}
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
debugPrint('📄 [HomePage] initState - 页面开始初始化');
|
||
|
||
// 🔥 关键:在页面显示之前执行初始化
|
||
// 使用 addPostFrameCallback 确保 build 完成后立即执行
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
_initializePage();
|
||
});
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
debugPrint('🎨 [HomePage] build - 渲染 UI');
|
||
|
||
// 同时监听用户和设备状态
|
||
final userState = context.watch<AppUserCubit>().state;
|
||
final deviceState = context.watch<DevicesCubit>().state;
|
||
final currentDevice = deviceState.selectedDevice;
|
||
|
||
final defaultDevice = DeviceEntity(
|
||
deviceName: '暂未绑定设备',
|
||
productId: 00000,
|
||
productName: '',
|
||
tenantId: 00000,
|
||
tenantName: ''
|
||
);
|
||
|
||
// 如果没有绑定设备,展示未绑定设备的提示页面
|
||
if (currentDevice == null) {
|
||
debugPrint('ℹ️ [HomePage] 无选中设备,显示默认设备');
|
||
|
||
return Scaffold(
|
||
backgroundColor: const Color(0xFFF7F7F7),
|
||
body: CustomScrollView(
|
||
physics: const BouncingScrollPhysics(),
|
||
slivers: [
|
||
SliverToBoxAdapter(child: ImmersionHeader(device: defaultDevice)),
|
||
const SliverToBoxAdapter(child: QuickActionsGrid()),
|
||
const SliverToBoxAdapter(child: WorkParamsCard()),
|
||
const SliverToBoxAdapter(child: SizedBox(height: 120)),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
// 如果有绑定设备,正常展示页面
|
||
debugPrint('✅ [HomePage] 有选中设备:${currentDevice.deviceName}');
|
||
|
||
return Scaffold(
|
||
backgroundColor: const Color(0xFFF7F7F7),
|
||
body: CustomScrollView(
|
||
physics: const BouncingScrollPhysics(),
|
||
slivers: [
|
||
SliverToBoxAdapter(child: ImmersionHeader(device: currentDevice)),
|
||
const SliverToBoxAdapter(child: QuickActionsGrid()),
|
||
SliverToBoxAdapter(child: WorkParamsCard()),
|
||
const SliverToBoxAdapter(child: SizedBox(height: 120)),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
debugPrint('🗑️ [HomePage] dispose - 页面销毁');
|
||
super.dispose();
|
||
}
|
||
}
|