一次大的提交
This commit is contained in:
@@ -4,23 +4,29 @@ 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';
|
||||
import '../../../home/domain/usecases/get_site_list_usecase.dart';
|
||||
import '../../../../../core/app/app_user_cubit.dart';
|
||||
|
||||
class SiteState {
|
||||
final List<SiteEntity> sites;
|
||||
final SiteEntity? selectedSite;
|
||||
final bool isLoading;
|
||||
|
||||
const SiteState({
|
||||
this.sites = const [],
|
||||
this.selectedSite,
|
||||
this.isLoading = false,
|
||||
});
|
||||
|
||||
SiteState copyWith({
|
||||
List<SiteEntity>? sites,
|
||||
SiteEntity? selectedSite,
|
||||
bool? isLoading,
|
||||
}) {
|
||||
return SiteState(
|
||||
sites: sites ?? this.sites,
|
||||
selectedSite: selectedSite ?? this.selectedSite,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -28,9 +34,47 @@ class SiteState {
|
||||
class SiteCubit extends Cubit<SiteState> {
|
||||
final SharedPreferences sharedPreferences;
|
||||
static const String _selectedSiteIdKey = 'selected_site_id';
|
||||
bool _hasLoadedSites = false;
|
||||
|
||||
SiteCubit(this.sharedPreferences) : super(const SiteState());
|
||||
|
||||
/// 加载场站列表(首次调用时请求接口,之后不重复请求)
|
||||
Future<void> loadSites() async {
|
||||
if (_hasLoadedSites) return;
|
||||
_hasLoadedSites = true;
|
||||
|
||||
emit(state.copyWith(isLoading: true));
|
||||
|
||||
try {
|
||||
final userId = GetIt.I<AppUserCubit>().state.user?.userId;
|
||||
if (userId == null) {
|
||||
emit(state.copyWith(isLoading: false));
|
||||
return;
|
||||
}
|
||||
|
||||
final result = await GetIt.I<GetSiteListUseCase>().call(userId);
|
||||
result.fold(
|
||||
(failure) {
|
||||
debugPrint('❌ [SiteCubit] 加载场站列表失败: ${failure.message}');
|
||||
emit(state.copyWith(isLoading: false));
|
||||
},
|
||||
(sites) {
|
||||
updateSites(sites);
|
||||
emit(state.copyWith(isLoading: false));
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('❌ [SiteCubit] 加载场站列表异常: $e');
|
||||
emit(state.copyWith(isLoading: false));
|
||||
}
|
||||
}
|
||||
|
||||
/// 强制重新加载场站列表
|
||||
Future<void> reloadSites() async {
|
||||
_hasLoadedSites = false;
|
||||
await loadSites();
|
||||
}
|
||||
|
||||
/// 更新场站列表
|
||||
void updateSites(List<SiteEntity> sites) {
|
||||
// 尝试恢复之前选中的场站
|
||||
@@ -52,8 +96,10 @@ class SiteCubit extends Cubit<SiteState> {
|
||||
/// 选择场站(持久化)
|
||||
void selectSite(SiteEntity site) {
|
||||
debugPrint('🏭 [SiteCubit] ========== 切换场站 ==========');
|
||||
debugPrint('🏭 [SiteCubit] 从 ${state.selectedSite?.siteName ?? "无"} 切换到 ${site.siteName}');
|
||||
|
||||
debugPrint(
|
||||
'🏭 [SiteCubit] 从 ${state.selectedSite?.siteName ?? "无"} 切换到 ${site.siteName}',
|
||||
);
|
||||
|
||||
// 🔥 关键修复:只有切换到不同场站时才断开TCP
|
||||
if (state.selectedSite?.id != site.id) {
|
||||
final tcpClient = GetIt.I<TcpClient>();
|
||||
@@ -67,7 +113,7 @@ class SiteCubit extends Cubit<SiteState> {
|
||||
} else {
|
||||
debugPrint('✅ [SiteCubit] 相同场站,保持TCP连接状态');
|
||||
}
|
||||
|
||||
|
||||
sharedPreferences.setInt(_selectedSiteIdKey, site.id);
|
||||
emit(state.copyWith(selectedSite: site));
|
||||
debugPrint('✅ [SiteCubit] 场站切换完成');
|
||||
@@ -81,6 +127,7 @@ class SiteCubit extends Cubit<SiteState> {
|
||||
|
||||
/// 清空所有场站数据(退出登录时调用)
|
||||
void clearAll() {
|
||||
_hasLoadedSites = false;
|
||||
sharedPreferences.remove(_selectedSiteIdKey);
|
||||
emit(const SiteState());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user