Commit e0fb3687 authored by 胡曲's avatar 胡曲

fix bugs

parent 7ba2abc6
...@@ -27,17 +27,24 @@ class AbnormalDetailView extends StatelessWidget { ...@@ -27,17 +27,24 @@ class AbnormalDetailView extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider( return BlocProvider(
create: (_) => AbnormalCubit(alertId: alertId), create: (_) => AbnormalCubit(alertId: alertId),
child: Scaffold( child: Builder(
backgroundColor: const Color.fromRGBO(242, 243, 245, 1), builder: (context) => PopScope(
appBar: AppBar( canPop: false,
onPopInvokedWithResult: (didPop, _) {
if (!didPop) {
final cubit = context.read<AbnormalCubit>();
Navigator.of(context).pop(cubit.state.isHandled);
}
},
child: Scaffold(
backgroundColor: const Color.fromRGBO(242, 243, 245, 1),
appBar: AppBar(
backgroundColor: Colors.white, backgroundColor: Colors.white,
elevation: 0, elevation: 0,
leading: IconButton( leading: IconButton(
icon: const Icon(Icons.arrow_back_ios, icon: const Icon(Icons.arrow_back_ios,
color: Color.fromRGBO(100, 116, 139, 1)), color: Color.fromRGBO(100, 116, 139, 1)),
onPressed: () { onPressed: () => context.maybePop(),
context.popRoute();
},
), ),
title: Text( title: Text(
'查看告警详情', '查看告警详情',
...@@ -49,7 +56,7 @@ class AbnormalDetailView extends StatelessWidget { ...@@ -49,7 +56,7 @@ class AbnormalDetailView extends StatelessWidget {
), ),
centerTitle: true, centerTitle: true,
), ),
body: BlocBuilder<AbnormalCubit, AbnormalState>( body: BlocBuilder<AbnormalCubit, AbnormalState>(
builder: (context, state) { builder: (context, state) {
if (state.isLoading) { if (state.isLoading) {
return const Center(child: CircularProgressIndicator(color: Color.fromRGBO(66, 165, 245, 1.0))); return const Center(child: CircularProgressIndicator(color: Color.fromRGBO(66, 165, 245, 1.0)));
...@@ -90,6 +97,8 @@ class AbnormalDetailView extends StatelessWidget { ...@@ -90,6 +97,8 @@ class AbnormalDetailView extends StatelessWidget {
); );
}, },
), ),
),
),
), ),
); );
} }
......
...@@ -81,44 +81,13 @@ class AbnormalListCubit extends Cubit<AbnormalListState> { ...@@ -81,44 +81,13 @@ class AbnormalListCubit extends Cubit<AbnormalListState> {
Future<void> onAlarmItemTap(AlarmItem item, BuildContext context) async { Future<void> onAlarmItemTap(AlarmItem item, BuildContext context) async {
final alertId = int.tryParse(item.id) ?? 0; final alertId = int.tryParse(item.id) ?? 0;
final handled = await context.pushRoute( await context.pushRoute(
AbnormalDetailRoute(alertId: alertId, alarmStatus: item.status), AbnormalDetailRoute(alertId: alertId, alarmStatus: item.status),
); ).then((value) {
// if (handled != null && handled is bool && handled == true) { if (value != null && value is bool && value) {
// _updateItemStatus(item.id); // 详情页已处理,返回刷新数据
// } _pagingController?.refresh();
} }
});
/// 在分页列表中定位并更新单条记录的状态(不重新请求接口)
void _updateItemStatus(String alertId) {
final state = _pagingController?.value;
if (state == null) return;
final pages = state.pages;
if (pages == null) return;
var found = false;
final newPages = pages.map((page) {
if (found) return page;
final index = page.indexWhere((i) => i.id == alertId);
if (index == -1) return page;
found = true;
final newPage = List<AlarmItem>.from(page);
newPage[index] = AlarmItem(
id: newPage[index].id,
title: newPage[index].title,
deviceInfo: newPage[index].deviceInfo,
alarmTime: newPage[index].alarmTime,
processTime: newPage[index].processTime,
status: AlarmStatus.processed,
level: newPage[index].level,
alertIcon: newPage[index].alertIcon,
);
return newPage;
}).toList();
if (found) {
_pagingController?.value = state.copyWith(pages: newPages);
}
} }
} }
...@@ -12,6 +12,8 @@ class HomeIndexCubit extends Cubit<HomeIndexState> { ...@@ -12,6 +12,8 @@ class HomeIndexCubit extends Cubit<HomeIndexState> {
final AlertDashboardService _service; final AlertDashboardService _service;
final AlertDetailService _alertDetailService; final AlertDetailService _alertDetailService;
DateTime? _lastLoadTime;
HomeIndexCubit({ HomeIndexCubit({
AlertDashboardService? service, AlertDashboardService? service,
AlertDetailService? alertDetailService, AlertDetailService? alertDetailService,
...@@ -24,10 +26,11 @@ class HomeIndexCubit extends Cubit<HomeIndexState> { ...@@ -24,10 +26,11 @@ class HomeIndexCubit extends Cubit<HomeIndexState> {
repository: AlertDetailRepository(), repository: AlertDetailRepository(),
), ),
super(const HomeIndexState()) { super(const HomeIndexState()) {
loadDashboard(); loadData();
} }
Future<void> loadDashboard() async { Future<void> loadData() async {
_lastLoadTime = DateTime.now();
emit(state.copyWith(isLoading: true, error: null)); emit(state.copyWith(isLoading: true, error: null));
try { try {
final dashboard = await _service.getDashboard(); final dashboard = await _service.getDashboard();
...@@ -37,8 +40,13 @@ class HomeIndexCubit extends Cubit<HomeIndexState> { ...@@ -37,8 +40,13 @@ class HomeIndexCubit extends Cubit<HomeIndexState> {
} }
} }
Future<void> reloadDashboard() async { /// 切回页面时调用,距上次加载不足 30 秒则跳过
loadDashboard(); void reloadDataIfStale() {
if (_lastLoadTime != null &&
DateTime.now().difference(_lastLoadTime!) < const Duration(seconds: 30)) {
return;
}
loadData();
} }
/// 处理告警 /// 处理告警
...@@ -48,7 +56,7 @@ class HomeIndexCubit extends Cubit<HomeIndexState> { ...@@ -48,7 +56,7 @@ class HomeIndexCubit extends Cubit<HomeIndexState> {
try { try {
await _alertDetailService.handleAlert(alertId: alertId); await _alertDetailService.handleAlert(alertId: alertId);
emit(state.copyWith(isHandlingAlert: false)); emit(state.copyWith(isHandlingAlert: false));
await reloadDashboard(); await loadData();
} catch (e) { } catch (e) {
emit(state.copyWith(isHandlingAlert: false, error: e.toString())); emit(state.copyWith(isHandlingAlert: false, error: e.toString()));
rethrow; rethrow;
......
...@@ -45,7 +45,7 @@ class _HomeViewState extends State<HomeView> with AutoRouteAwareStateMixin<HomeV ...@@ -45,7 +45,7 @@ class _HomeViewState extends State<HomeView> with AutoRouteAwareStateMixin<HomeV
if (!mounted) return; if (!mounted) return;
if (_tabsRouter?.activeIndex == 0) { if (_tabsRouter?.activeIndex == 0) {
context.read<HomeIndexCubit>().reloadDashboard(); context.read<HomeIndexCubit>().reloadDataIfStale();
} }
} }
...@@ -77,7 +77,7 @@ class _HomeViewState extends State<HomeView> with AutoRouteAwareStateMixin<HomeV ...@@ -77,7 +77,7 @@ class _HomeViewState extends State<HomeView> with AutoRouteAwareStateMixin<HomeV
SizedBox(height: 16.h), SizedBox(height: 16.h),
ElevatedButton( ElevatedButton(
onPressed: () => onPressed: () =>
context.read<HomeIndexCubit>().reloadDashboard(), context.read<HomeIndexCubit>().loadData(),
child: const Text('重试'), child: const Text('重试'),
), ),
], ],
......
...@@ -135,8 +135,15 @@ class VoltageOperateBlock extends StatelessWidget { ...@@ -135,8 +135,15 @@ class VoltageOperateBlock extends StatelessWidget {
), ),
Expanded( Expanded(
child: GestureDetector( child: GestureDetector(
onTap: () { onTap: () async {
context.pushRoute(AbnormalDetailRoute(alertId: alertId, alarmStatus: AlarmStatus.pending)); await context.pushRoute(
AbnormalDetailRoute(alertId: alertId,
alarmStatus: AlarmStatus.pending)
).then((value) {
if (value != null && value is bool && value) {
context.read<HomeIndexCubit>().loadData();
}
});
}, },
child: Container( child: Container(
padding: EdgeInsets.symmetric(vertical: 20.h), padding: EdgeInsets.symmetric(vertical: 20.h),
......
...@@ -37,7 +37,7 @@ class InspectionTopologyCubit extends Cubit<InspectionTopologyState> { ...@@ -37,7 +37,7 @@ class InspectionTopologyCubit extends Cubit<InspectionTopologyState> {
void _emitFromTopology(DeviceTopologyBO topology) { void _emitFromTopology(DeviceTopologyBO topology) {
final info = topology.deviceInfo; final info = topology.deviceInfo;
final treeNodes = topology.treeNodes; final treeNodes = topology.treeNodes.take(11).toList();
if (treeNodes.isEmpty) { if (treeNodes.isEmpty) {
emit(InspectionTopologyState( emit(InspectionTopologyState(
......
...@@ -9,11 +9,11 @@ import 'package:smart_hotel_app/views/report/index/cubit/report_index_state.dart ...@@ -9,11 +9,11 @@ import 'package:smart_hotel_app/views/report/index/cubit/report_index_state.dart
class ReportIndexCubit extends Cubit<ReportIndexState> { class ReportIndexCubit extends Cubit<ReportIndexState> {
final DashboardService _service; final DashboardService _service;
DateTime? _lastLoadTime;
ReportIndexCubit({DashboardService? service}) ReportIndexCubit({DashboardService? service})
: _service = service ?? DashboardService(repository: DashboardRepository()), : _service = service ?? DashboardService(repository: DashboardRepository()),
super(const ReportIndexState()) { super(const ReportIndexState());
// loadData();
}
Future<void> loadData() async { Future<void> loadData() async {
emit(state.copyWith(isLoading: true, clearError: true)); emit(state.copyWith(isLoading: true, clearError: true));
...@@ -25,7 +25,13 @@ class ReportIndexCubit extends Cubit<ReportIndexState> { ...@@ -25,7 +25,13 @@ class ReportIndexCubit extends Cubit<ReportIndexState> {
} }
} }
Future<void> reloadData() async { /// 切回页面时调用,距上次加载不足 30 秒则跳过
void reloadDataIfStale() {
if (_lastLoadTime != null &&
DateTime.now().difference(_lastLoadTime!) < const Duration(seconds: 30)) {
return;
}
_lastLoadTime = DateTime.now();
loadData(); loadData();
} }
......
...@@ -43,7 +43,7 @@ class _ReportIndexViewState extends State<ReportIndexView> with AutoRouteAwareSt ...@@ -43,7 +43,7 @@ class _ReportIndexViewState extends State<ReportIndexView> with AutoRouteAwareSt
if (!mounted) return; if (!mounted) return;
if (_tabsRouter?.activeIndex == 2) { if (_tabsRouter?.activeIndex == 2) {
context.read<ReportIndexCubit>().reloadData(); context.read<ReportIndexCubit>().reloadDataIfStale();
} }
} }
......
...@@ -11,23 +11,26 @@ class ServiceIndexCubit extends Cubit<ServiceIndexState> { ...@@ -11,23 +11,26 @@ class ServiceIndexCubit extends Cubit<ServiceIndexState> {
final RoomStatisticsService _statisticsService; final RoomStatisticsService _statisticsService;
List<FloorAreaBO> _floorAreasCache = []; List<FloorAreaBO> _floorAreasCache = [];
DateTime? _lastLoadTime;
ServiceIndexCubit() ServiceIndexCubit()
: _roomService = RoomService(repository: RoomRepository()), : _roomService = RoomService(repository: RoomRepository()),
_statisticsService = RoomStatisticsService( _statisticsService = RoomStatisticsService(
repository: RoomStatisticsRepository(), repository: RoomStatisticsRepository(),
), ),
super(const ServiceIndexState()) { super(const ServiceIndexState());
// _init();
}
// Future<void> _init() async { /// 切回页面时调用,距上次加载不足 30 秒则跳过
// await Future.wait([ void reloadDataIfStale() {
// loadStatistics(), if (_lastLoadTime != null &&
// loadFloorAndAreas(), DateTime.now().difference(_lastLoadTime!) < const Duration(seconds: 30)) {
// ]); return;
// } }
_lastLoadTime = DateTime.now();
loadData();
}
Future<void> reloadAll() async { Future<void> loadData() async {
emit(state.copyWith(isLoading: true, error: null)); emit(state.copyWith(isLoading: true, error: null));
try { try {
await Future.wait([ await Future.wait([
......
...@@ -40,7 +40,7 @@ class _ServiceIndexViewState extends State<ServiceIndexView> with AutoRouteAware ...@@ -40,7 +40,7 @@ class _ServiceIndexViewState extends State<ServiceIndexView> with AutoRouteAware
if (!mounted) return; if (!mounted) return;
if (_tabsRouter?.activeIndex == 1) { if (_tabsRouter?.activeIndex == 1) {
context.read<ServiceIndexCubit>().reloadAll(); context.read<ServiceIndexCubit>().reloadDataIfStale();
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment