一次大的提交
This commit is contained in:
@@ -14,7 +14,12 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
final AppUserCubit appUserCubit;
|
||||
final SiteCubit siteCubit;
|
||||
|
||||
HomeV2Bloc(this.getHomeDataUseCase, this.getSiteListUseCase, this.appUserCubit, this.siteCubit) : super(const HomeV2Initial()) {
|
||||
HomeV2Bloc(
|
||||
this.getHomeDataUseCase,
|
||||
this.getSiteListUseCase,
|
||||
this.appUserCubit,
|
||||
this.siteCubit,
|
||||
) : super(const HomeV2Initial()) {
|
||||
on<HomeV2LoadData>(_onLoadData);
|
||||
on<HomeV2Refresh>(_onRefresh);
|
||||
on<HomeV2ToggleTrendType>(_onToggleTrendType);
|
||||
@@ -28,26 +33,25 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
|
||||
final user = appUserCubit.state.user;
|
||||
if (user == null) {
|
||||
emit(const HomeV2Error(
|
||||
message: '用户未登录',
|
||||
shouldShowError: true,
|
||||
));
|
||||
emit(const HomeV2Error(message: '用户未登录', shouldShowError: true));
|
||||
return;
|
||||
}
|
||||
|
||||
// 并行加载首页数据和场站列表
|
||||
final homeResult = await getHomeDataUseCase(const NoParams());
|
||||
final siteResult = await getSiteListUseCase(user.orgId); // 使用用户的 orgId
|
||||
final siteResult = await getSiteListUseCase(user.userId); // 使用用户的 userId
|
||||
|
||||
homeResult.fold(
|
||||
(failure) => emit(HomeV2Error(
|
||||
message: ErrorHandler.getErrorMessage(failure.message),
|
||||
shouldShowError: true, // 🔥 标记需要显示弹窗
|
||||
)),
|
||||
(failure) => emit(
|
||||
HomeV2Error(
|
||||
message: ErrorHandler.getErrorMessage(failure.message),
|
||||
shouldShowError: true, // 🔥 标记需要显示弹窗
|
||||
),
|
||||
),
|
||||
(homeData) {
|
||||
List<SiteEntity> sites = [];
|
||||
SiteEntity? selectedSite;
|
||||
|
||||
|
||||
siteResult.fold(
|
||||
(failure) {
|
||||
print('加载场站列表失败: ${failure.message}');
|
||||
@@ -56,7 +60,7 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
sites = siteList;
|
||||
// 从全局 SiteCubit 获取之前选中的场站
|
||||
final savedSelectedSite = siteCubit.state.selectedSite;
|
||||
|
||||
|
||||
// 尝试找到之前选中的场站
|
||||
if (savedSelectedSite != null && siteList.isNotEmpty) {
|
||||
selectedSite = siteList.firstWhere(
|
||||
@@ -67,19 +71,21 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
// 没有选中过,默认选中第一个
|
||||
selectedSite = siteList.isNotEmpty ? siteList.first : null;
|
||||
}
|
||||
|
||||
|
||||
// 更新全局 SiteCubit 的选中状态
|
||||
if (selectedSite != null) {
|
||||
siteCubit.selectSite(selectedSite!);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
emit(HomeV2Loaded(
|
||||
homeData: homeData,
|
||||
sites: sites,
|
||||
selectedSite: selectedSite,
|
||||
));
|
||||
|
||||
emit(
|
||||
HomeV2Loaded(
|
||||
homeData: homeData,
|
||||
sites: sites,
|
||||
selectedSite: selectedSite,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -90,29 +96,28 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
) async {
|
||||
if (state is HomeV2Loaded) {
|
||||
final currentState = state as HomeV2Loaded;
|
||||
|
||||
|
||||
final user = appUserCubit.state.user;
|
||||
if (user == null) {
|
||||
emit(const HomeV2Error(
|
||||
message: '用户未登录',
|
||||
shouldShowError: true,
|
||||
));
|
||||
emit(const HomeV2Error(message: '用户未登录', shouldShowError: true));
|
||||
return;
|
||||
}
|
||||
|
||||
// 并行刷新首页数据和电站列表
|
||||
final homeResult = await getHomeDataUseCase(const NoParams());
|
||||
final siteResult = await getSiteListUseCase(user.orgId);
|
||||
final siteResult = await getSiteListUseCase(user.userId);
|
||||
|
||||
homeResult.fold(
|
||||
(failure) => emit(HomeV2Error(
|
||||
message: ErrorHandler.getErrorMessage(failure.message),
|
||||
shouldShowError: true, // 🔥 标记需要显示弹窗
|
||||
)),
|
||||
(failure) => emit(
|
||||
HomeV2Error(
|
||||
message: ErrorHandler.getErrorMessage(failure.message),
|
||||
shouldShowError: true, // 🔥 标记需要显示弹窗
|
||||
),
|
||||
),
|
||||
(homeData) {
|
||||
List<SiteEntity> sites = currentState.sites;
|
||||
SiteEntity? selectedSite = currentState.selectedSite;
|
||||
|
||||
|
||||
siteResult.fold(
|
||||
(failure) {
|
||||
print('刷新场站列表失败: ${failure.message}');
|
||||
@@ -129,20 +134,22 @@ class HomeV2Bloc extends Bloc<HomeV2Event, HomeV2State> {
|
||||
} else if (siteList.isNotEmpty) {
|
||||
selectedSite = siteList.first;
|
||||
}
|
||||
|
||||
|
||||
// 更新全局 SiteCubit 的选中状态
|
||||
if (selectedSite != null) {
|
||||
siteCubit.selectSite(selectedSite!);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
emit(HomeV2Loaded(
|
||||
homeData: homeData,
|
||||
trendType: currentState.trendType,
|
||||
sites: sites,
|
||||
selectedSite: selectedSite,
|
||||
));
|
||||
|
||||
emit(
|
||||
HomeV2Loaded(
|
||||
homeData: homeData,
|
||||
trendType: currentState.trendType,
|
||||
sites: sites,
|
||||
selectedSite: selectedSite,
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import 'package:maibu_satabot_v2/features/v2/home/presentation/bloc/home_v2_bloc
|
||||
import 'package:maibu_satabot_v2/features/v2/home/presentation/bloc/home_v2_event.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/presentation/bloc/home_v2_state.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/site/presentation/cubit/site_cubit.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/site/presentation/widgets/site_selector_widget.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/presentation/widgets/power_card.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/presentation/widgets/stats_grid.dart';
|
||||
import 'package:maibu_satabot_v2/features/v2/home/presentation/widgets/work_order_card.dart';
|
||||
@@ -60,9 +61,16 @@ class _HomeV2PageState extends State<HomeV2Page> {
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(Icons.error_outline, size: 48, color: Colors.orange),
|
||||
const Icon(
|
||||
Icons.error_outline,
|
||||
size: 48,
|
||||
color: Colors.orange,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(state.message, style: const TextStyle(fontSize: 14, color: Colors.grey)),
|
||||
Text(
|
||||
state.message,
|
||||
style: const TextStyle(fontSize: 14, color: Colors.grey),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
ElevatedButton(
|
||||
onPressed: () => _bloc.add(const HomeV2LoadData()),
|
||||
@@ -91,47 +99,8 @@ class _HomeV2PageState extends State<HomeV2Page> {
|
||||
color: Colors.white,
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: InkWell(
|
||||
onTap: _showPlantSelector,
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Flexible(
|
||||
child: StreamBuilder<SiteState>(
|
||||
stream: sl<SiteCubit>().stream,
|
||||
builder: (context, snapshot) {
|
||||
final siteState =
|
||||
snapshot.data ??
|
||||
sl<SiteCubit>().state;
|
||||
return Text(
|
||||
siteState.selectedSite?.siteName ??
|
||||
AppLocalizations.of(
|
||||
context,
|
||||
).translate(
|
||||
'home_v2.select_site',
|
||||
),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
const Icon(
|
||||
Icons.arrow_drop_down,
|
||||
size: 20,
|
||||
color: Color(0xFF1D2129),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
Expanded(child: SiteSelectorWidget()),
|
||||
const SizedBox(width: 12),
|
||||
TcpStatusIndicator(
|
||||
onTap: () => _showDeviceStatusModal(context),
|
||||
),
|
||||
@@ -252,7 +221,9 @@ class _HomeV2PageState extends State<HomeV2Page> {
|
||||
}
|
||||
|
||||
// 🔥 Initial/Loading 状态显示加载指示器
|
||||
return const Center(child: CircularProgressIndicator(color: Color(0xFF165DFF)));
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(color: Color(0xFF165DFF)),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user