Files
flutterApp/lib/features/v2/site/presentation/cubit/site_cubit.dart
Songzex 71aea48d9f 集成接口 开始执行任务的领域层和数据层和UI层的开发啊(待测试)
集成功能 暂停 取消功能 恢复功能的接口的领域层和数据层的开发 下一步待集成到页面上
优化功能,优化了接口异常和未知异常对页面渲染的影响对用户的体验的不好情况。具体通过弹窗友好提示!
优化更新了tcp指示灯点击出现机器状态中添加选中设备的编号 方便用户使用的明白明了!。
优化更新关闭了tcp重连操作内链条中的获取用户设备和切换设备操作项。
调整了获取无人机状态信息的更新频率 为14秒一次
2026-06-16 17:34:00 +08:00

88 lines
2.7 KiB
Dart
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../../../core/network/tcp/tcp_client.dart';
import '../../../home/domain/entities/site_entity.dart';
class SiteState {
final List<SiteEntity> sites;
final SiteEntity? selectedSite;
const SiteState({
this.sites = const [],
this.selectedSite,
});
SiteState copyWith({
List<SiteEntity>? sites,
SiteEntity? selectedSite,
}) {
return SiteState(
sites: sites ?? this.sites,
selectedSite: selectedSite ?? this.selectedSite,
);
}
}
class SiteCubit extends Cubit<SiteState> {
final SharedPreferences sharedPreferences;
static const String _selectedSiteIdKey = 'selected_site_id';
SiteCubit(this.sharedPreferences) : super(const SiteState());
/// 更新场站列表
void updateSites(List<SiteEntity> sites) {
// 尝试恢复之前选中的场站
final savedSiteId = sharedPreferences.getInt(_selectedSiteIdKey);
SiteEntity? selectedSite;
if (savedSiteId != null && sites.isNotEmpty) {
selectedSite = sites.firstWhere(
(site) => site.id == savedSiteId,
orElse: () => sites.first,
);
} else if (sites.isNotEmpty) {
selectedSite = sites.first;
}
emit(state.copyWith(sites: sites, selectedSite: selectedSite));
}
/// 选择场站(持久化)
void selectSite(SiteEntity site) {
debugPrint('🏭 [SiteCubit] ========== 切换场站 ==========');
debugPrint('🏭 [SiteCubit] 从 ${state.selectedSite?.siteName ?? "无"} 切换到 ${site.siteName}');
// 🔥 关键修复:只有切换到不同场站时才断开TCP
if (state.selectedSite?.id != site.id) {
final tcpClient = GetIt.I<TcpClient>();
if (tcpClient.isConnected) {
debugPrint('🛑 [SiteCubit] 检测到TCP已连接,正在断开...');
tcpClient.disconnect();
debugPrint('✅ [SiteCubit] TCP已断开,防止场站间设备数据混淆');
} else {
debugPrint('ℹ️ [SiteCubit] TCP未连接,无需断开');
}
} else {
debugPrint('✅ [SiteCubit] 相同场站,保持TCP连接状态');
}
sharedPreferences.setInt(_selectedSiteIdKey, site.id);
emit(state.copyWith(selectedSite: site));
debugPrint('✅ [SiteCubit] 场站切换完成');
}
/// 清除选中场站
void clearSelectedSite() {
sharedPreferences.remove(_selectedSiteIdKey);
emit(state.copyWith(selectedSite: null));
}
/// 清空所有场站数据(退出登录时调用)
void clearAll() {
sharedPreferences.remove(_selectedSiteIdKey);
emit(const SiteState());
}
}