2026-03-17 11:05:32 +08:00
|
|
|
|
import 'dart:async';
|
2026-01-11 19:42:22 +08:00
|
|
|
|
import 'package:flutter/material.dart';
|
2026-01-18 20:21:14 +08:00
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2026-03-09 08:38:29 +08:00
|
|
|
|
import 'package:get_it/get_it.dart';
|
2026-01-18 20:21:14 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/core/app/app_user_cubit.dart';
|
2026-03-09 13:21:18 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/core/consts/tcp_consts.dart';
|
2026-04-24 15:24:43 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/core/localization/app_localizations.dart';
|
2026-03-09 13:21:18 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/core/network/tcp/tcp_client.dart';
|
2026-01-18 20:21:14 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_cubit.dart';
|
2026-03-17 11:05:32 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/features/devices/presentation/bloc/devices_state.dart'; // 👈 必须加
|
2026-04-29 14:25:46 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/features/remote_control/domain/repositories/remote_control_repository.dart';
|
2026-06-17 13:46:30 +08:00
|
|
|
|
import 'package:maibu_satabot_v2/features/remote_control/presentation/bloc/remote_control_cubit.dart';
|
2026-01-11 19:42:22 +08:00
|
|
|
|
|
2026-02-25 14:53:32 +08:00
|
|
|
|
import '../../../devices/domain/entities/device_entity.dart';
|
2026-04-29 14:25:46 +08:00
|
|
|
|
import '../bloc/permission_request_bloc.dart';
|
|
|
|
|
|
import '../bloc/permission_request_state.dart';
|
|
|
|
|
|
import '../bloc/permission_request_event.dart';
|
2026-01-18 20:21:14 +08:00
|
|
|
|
import '../widgets/ImmersionHeader.dart';
|
|
|
|
|
|
import '../widgets/quick_actions_grid.dart';
|
|
|
|
|
|
import '../widgets/work_params_card.dart';
|
2026-01-11 19:42:22 +08:00
|
|
|
|
|
2026-03-09 13:21:18 +08:00
|
|
|
|
class HomePage extends StatefulWidget {
|
2026-01-18 20:21:14 +08:00
|
|
|
|
const HomePage({super.key});
|
2026-01-11 19:42:22 +08:00
|
|
|
|
|
2026-03-09 13:21:18 +08:00
|
|
|
|
@override
|
|
|
|
|
|
State<HomePage> createState() => _HomePageState();
|
|
|
|
|
|
}
|
2026-03-05 16:34:06 +08:00
|
|
|
|
|
2026-03-09 13:21:18 +08:00
|
|
|
|
class _HomePageState extends State<HomePage> {
|
2026-03-17 11:05:32 +08:00
|
|
|
|
bool _isInitializing = false;
|
|
|
|
|
|
bool _isDevicesLoading = true; // 👈 新加:设备加载中状态
|
|
|
|
|
|
bool _hasLoadedDevices = false;
|
2026-03-05 16:34:06 +08:00
|
|
|
|
|
2026-03-17 11:05:32 +08:00
|
|
|
|
/// 🔥 修复:等待设备加载完成,再执行后续逻辑
|
2026-03-09 13:21:18 +08:00
|
|
|
|
Future<void> _initializePage() async {
|
2026-03-17 11:05:32 +08:00
|
|
|
|
if (_isInitializing) return;
|
2026-03-09 13:21:18 +08:00
|
|
|
|
_isInitializing = true;
|
2026-03-05 16:34:06 +08:00
|
|
|
|
|
2026-03-09 13:21:18 +08:00
|
|
|
|
try {
|
2026-03-17 11:05:32 +08:00
|
|
|
|
final username = context.read<AppUserCubit>().state.user?.username;
|
2026-03-09 13:21:18 +08:00
|
|
|
|
if (username == null || username.isEmpty) {
|
2026-03-17 11:05:32 +08:00
|
|
|
|
setState(() => _isDevicesLoading = false);
|
2026-03-09 13:21:18 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
final devicesCubit = context.read<DevicesCubit>();
|
|
|
|
|
|
|
2026-06-17 13:46:30 +08:00
|
|
|
|
// 🔥 修复:检查是否已有 targetDevice,如果有则不需要加载设备列表
|
|
|
|
|
|
final remoteControlState = context.read<RemoteControlCubit>().state;
|
|
|
|
|
|
if (remoteControlState.targetDevice != null) {
|
|
|
|
|
|
debugPrint('✅ [HomePage] 已有 targetDevice: ${remoteControlState.targetDevice!.deviceName},跳过设备列表加载');
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
_isDevicesLoading = false;
|
|
|
|
|
|
_hasLoadedDevices = true;
|
|
|
|
|
|
});
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 11:05:32 +08:00
|
|
|
|
// 🔥 关键:等待设备加载完成
|
|
|
|
|
|
await devicesCubit.fetchAllDevices(username);
|
2026-03-09 13:21:18 +08:00
|
|
|
|
|
2026-03-17 11:05:32 +08:00
|
|
|
|
// 🔥 加载完成!关闭 loading
|
|
|
|
|
|
setState(() {
|
|
|
|
|
|
_isDevicesLoading = false;
|
2026-03-09 13:21:18 +08:00
|
|
|
|
_hasLoadedDevices = true;
|
2026-03-17 11:05:32 +08:00
|
|
|
|
});
|
2026-03-09 13:21:18 +08:00
|
|
|
|
|
2026-03-17 11:05:32 +08:00
|
|
|
|
// 🔥 现在再拿设备列表 = 最新的,绝对不是旧的!
|
|
|
|
|
|
final deviceList = devicesCubit.state.devices ?? [];
|
2026-03-09 13:21:18 +08:00
|
|
|
|
if (deviceList.isNotEmpty) {
|
2026-03-17 11:05:32 +08:00
|
|
|
|
final firstDevice = deviceList.first;
|
|
|
|
|
|
|
|
|
|
|
|
// 🔥 自动选中第一个设备(保证是最新的)
|
|
|
|
|
|
devicesCubit.selectDevice(firstDevice);
|
2026-03-09 13:21:18 +08:00
|
|
|
|
|
2026-06-16 17:34:00 +08:00
|
|
|
|
// 🔥 关键修复:首页不再自动连接TCP,改为选择设备时才连接
|
2026-03-17 11:05:32 +08:00
|
|
|
|
// 连接 TCP
|
2026-06-16 17:34:00 +08:00
|
|
|
|
// final tcpClient = GetIt.instance<TcpClient>();
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
//
|
|
|
|
|
|
// // 🔥 关键:TCP 连接成功后,主动请求权限以激活服务器的推送机制
|
|
|
|
|
|
// final remoteRepo = GetIt.instance<RemoteControlRepository>();
|
|
|
|
|
|
// remoteRepo.requestControlPermission(firstDevice.deviceName, "app");
|
|
|
|
|
|
// debugPrint('✅ [HomePage] 已发送权限请求,激活服务器推送');
|
|
|
|
|
|
// });
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// // 🔥 TCP 已连接,也要发送权限请求
|
|
|
|
|
|
// final remoteRepo = GetIt.instance<RemoteControlRepository>();
|
|
|
|
|
|
// remoteRepo.requestControlPermission(firstDevice.deviceName, "app");
|
|
|
|
|
|
// debugPrint('✅ [HomePage] TCP 已连接,已发送权限请求');
|
|
|
|
|
|
// }
|
|
|
|
|
|
debugPrint('ℹ️ [HomePage] TCP 连接已移至选择设备时执行');
|
2026-03-05 16:34:06 +08:00
|
|
|
|
}
|
2026-03-09 13:21:18 +08:00
|
|
|
|
} catch (e) {
|
2026-03-17 11:05:32 +08:00
|
|
|
|
setState(() => _isDevicesLoading = false);
|
|
|
|
|
|
debugPrint('初始化异常:$e');
|
2026-03-09 13:21:18 +08:00
|
|
|
|
} finally {
|
|
|
|
|
|
_isInitializing = false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
void initState() {
|
|
|
|
|
|
super.initState();
|
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
|
_initializePage();
|
2026-03-05 16:34:06 +08:00
|
|
|
|
});
|
2026-03-09 13:21:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
|
final userState = context.watch<AppUserCubit>().state;
|
|
|
|
|
|
final deviceState = context.watch<DevicesCubit>().state;
|
2026-02-25 14:53:32 +08:00
|
|
|
|
|
2026-03-17 11:05:32 +08:00
|
|
|
|
// 🔥 加载中 → 显示 Loading
|
|
|
|
|
|
if (_isDevicesLoading) {
|
|
|
|
|
|
return const Scaffold(body: Center(child: CircularProgressIndicator()));
|
2026-03-09 13:21:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-17 11:05:32 +08:00
|
|
|
|
// 🔥 现在取的设备 = 100% 最新加载完成的
|
|
|
|
|
|
final currentDevice = deviceState.selectedDevice;
|
|
|
|
|
|
|
2026-04-24 15:24:43 +08:00
|
|
|
|
final defaultDevice = DeviceEntity(
|
|
|
|
|
|
deviceName: AppLocalizations.of(context).translate('home.no_device_bound'),
|
|
|
|
|
|
productId: 00000,
|
|
|
|
|
|
productName: '',
|
|
|
|
|
|
tenantId: 00000,
|
|
|
|
|
|
tenantName: '',
|
|
|
|
|
|
);
|
2026-03-09 13:21:18 +08:00
|
|
|
|
|
2026-06-08 13:35:56 +08:00
|
|
|
|
// 🔥 移除 PermissionRequestBloc 监听,统一由 RemoteControlCubit 处理权限弹窗
|
|
|
|
|
|
return Scaffold(
|
|
|
|
|
|
backgroundColor: const Color(0xFFF7F7F7),
|
|
|
|
|
|
body: CustomScrollView(
|
|
|
|
|
|
physics: const BouncingScrollPhysics(),
|
|
|
|
|
|
slivers: [
|
|
|
|
|
|
SliverToBoxAdapter(child: ImmersionHeader(device: currentDevice ?? defaultDevice)),
|
|
|
|
|
|
const SliverToBoxAdapter(child: QuickActionsGrid()),
|
|
|
|
|
|
SliverToBoxAdapter(child: WorkParamsCard()),
|
|
|
|
|
|
const SliverToBoxAdapter(child: SizedBox(height: 120)),
|
2026-03-09 13:21:18 +08:00
|
|
|
|
],
|
|
|
|
|
|
),
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-08 13:35:56 +08:00
|
|
|
|
// 🔥 移除首页的权限弹窗方法,统一由 RemoteControlCubit 处理
|
|
|
|
|
|
// Widget _buildPermissionDialog(BuildContext context, PermissionRequestDialogVisible state) {
|
|
|
|
|
|
// ...
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
2026-03-09 13:21:18 +08:00
|
|
|
|
@override
|
|
|
|
|
|
void dispose() {
|
|
|
|
|
|
super.dispose();
|
2026-01-11 19:42:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|