Commit e0fb3687 authored by 胡曲's avatar 胡曲

fix bugs

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