fix(bugs): prevent redundant API calls on PieChart interaction in DeviceManagerPage

This commit is contained in:
anhtunz
2025-01-13 10:11:26 +07:00
parent a17831d0ac
commit 408c92d843
5 changed files with 44 additions and 52 deletions

View File

@@ -9,7 +9,7 @@ import 'package:sfm_app/product/services/language_services.dart';
import '../constant/app/app_constants.dart';
class SharedPieChart extends StatelessWidget {
class SharedPieChart extends StatefulWidget {
const SharedPieChart(
{super.key,
required this.deviceByState,
@@ -18,24 +18,32 @@ class SharedPieChart extends StatelessWidget {
final Map<String, List<Device>> deviceByState;
final DevicesManagerBloc devicesManagerBloc;
@override
State<SharedPieChart> createState() => _SharedPieChartState();
}
class _SharedPieChartState extends State<SharedPieChart> {
int lastTouchedIndex = -1;
@override
Widget build(BuildContext context) {
int touchedIndex = -1;
TextStyle titleStyle = const TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
);
int offlineCount =
deviceByState[ApplicationConstants.OFFLINE_STATE]?.length ?? 0;
widget.deviceByState[ApplicationConstants.OFFLINE_STATE]?.length ?? 0;
int normalCount =
deviceByState[ApplicationConstants.NORMAL_STATE]?.length ?? 0;
widget.deviceByState[ApplicationConstants.NORMAL_STATE]?.length ?? 0;
int warningCount =
deviceByState[ApplicationConstants.WARNING_STATE]?.length ?? 0;
widget.deviceByState[ApplicationConstants.WARNING_STATE]?.length ?? 0;
int inProgressCount =
deviceByState[ApplicationConstants.INPROGRESS_STATE]?.length ?? 0;
widget.deviceByState[ApplicationConstants.INPROGRESS_STATE]?.length ??
0;
int errorCount =
deviceByState[ApplicationConstants.ERROR_STATE]?.length ?? 0;
widget.deviceByState[ApplicationConstants.ERROR_STATE]?.length ?? 0;
return AspectRatio(
aspectRatio: 1.5,
child: Row(
@@ -58,34 +66,7 @@ class SharedPieChart extends StatelessWidget {
}
int newTouchedIndex =
pieTouchResponse.touchedSection!.touchedSectionIndex;
// Chỉ gọi updateDevicesOnTapPieChart nếu touchedIndex thay đổi
if (newTouchedIndex != touchedIndex) {
touchedIndex = newTouchedIndex;
log("TouchedIndex: $touchedIndex");
log("Event: ${event.isInterestedForInteractions}");
Future.delayed(
context.lowDuration,
() => updateDevicesOnTapPieChart(newTouchedIndex),
);
}
// touchedIndex =
// pieTouchResponse.touchedSection!.touchedSectionIndex;
// // log("TouchedIndex: $touchedIndex");
// // log("Event: ${event.isInterestedForInteractions}");
// // int currentTouchedIndex = touchedIndex;
// if (currentTouchedIndex != touchedIndex) {
// touchedIndex = currentTouchedIndex;
// log("TouchedIndex: $touchedIndex");
// log("Event: ${event.isInterestedForInteractions}");
// Future.delayed(
// context.lowDuration,
// () => updateDevicesOnTapPieChart(touchedIndex),
// );
// }
updateDevicesOnTapPieChart(newTouchedIndex);
},
),
sections: [
@@ -204,18 +185,21 @@ class SharedPieChart extends StatelessWidget {
}
void updateDevicesOnTapPieChart(int touchedIndex) {
log("Update PieChart On Tap Function");
if (touchedIndex == 0) {
devicesManagerBloc.getDeviceByState(-1);
} else if (touchedIndex == 1) {
devicesManagerBloc.getDeviceByState(0);
} else if (touchedIndex == 2) {
devicesManagerBloc.getDeviceByState(1);
} else if (touchedIndex == 3) {
devicesManagerBloc.getDeviceByState(2);
} else {
devicesManagerBloc.getDeviceByState(-2);
}
if (touchedIndex != lastTouchedIndex) {
log("Update PieChart On Tap Function");
lastTouchedIndex = touchedIndex;
if (touchedIndex == 0) {
widget.devicesManagerBloc.getDeviceByState(-1);
} else if (touchedIndex == 1) {
widget.devicesManagerBloc.getDeviceByState(0);
} else if (touchedIndex == 2) {
widget.devicesManagerBloc.getDeviceByState(1);
} else if (touchedIndex == 3) {
widget.devicesManagerBloc.getDeviceByState(2);
} else {
widget.devicesManagerBloc.getDeviceByState(-2);
}
} else {}
}
}