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

@@ -3,7 +3,6 @@ import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../../product/extention/context_extention.dart'; import '../../product/extention/context_extention.dart';
import '../../product/services/language_services.dart'; import '../../product/services/language_services.dart';
import '../../product/constant/enums/app_theme_enums.dart';
import '../../bloc/bell_bloc.dart'; import '../../bloc/bell_bloc.dart';
import '../../product/base/bloc/base_bloc.dart'; import '../../product/base/bloc/base_bloc.dart';
import '../../product/services/api_services.dart'; import '../../product/services/api_services.dart';

View File

@@ -30,6 +30,7 @@ class _DevicesManagerScreenState extends State<DevicesManagerScreen> {
List<Device> devices = []; List<Device> devices = [];
Timer? getAllDevicesTimer; Timer? getAllDevicesTimer;
List<Widget> tags = []; List<Widget> tags = [];
int tagIndex = -2;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@@ -38,7 +39,7 @@ class _DevicesManagerScreenState extends State<DevicesManagerScreen> {
const duration = Duration(seconds: 10); const duration = Duration(seconds: 10);
getAllDevicesTimer = Timer.periodic( getAllDevicesTimer = Timer.periodic(
duration, duration,
(Timer t) => devicesManagerBloc.getDeviceByState(-2), (Timer t) => devicesManagerBloc.getDeviceByState(tagIndex),
); );
} }
@@ -65,7 +66,10 @@ class _DevicesManagerScreenState extends State<DevicesManagerScreen> {
.getDeviceByState(tagSnapshot.data?[0] ?? -2); .getDeviceByState(tagSnapshot.data?[0] ?? -2);
return const Center(child: CircularProgressIndicator()); return const Center(child: CircularProgressIndicator());
} else { } else {
if (tagSnapshot.data!.isNotEmpty) {} if (tagSnapshot.data!.isNotEmpty) {
tagIndex = tagSnapshot.data![0];
devicesManagerBloc.sinkTagStates.add([tagIndex]);
}
return SingleChildScrollView( return SingleChildScrollView(
child: Column( child: Column(
children: [ children: [
@@ -154,7 +158,7 @@ class _DevicesManagerScreenState extends State<DevicesManagerScreen> {
// log('Chuyen page: $pageIndex'); // log('Chuyen page: $pageIndex');
}, },
rowsPerPage: rowsPerPage:
(allDeviceSnapshot.data?.length ?? 0) < 6 (allDeviceSnapshot.data?.length ?? 1) < 6
? (allDeviceSnapshot.data?.length ?? ? (allDeviceSnapshot.data?.length ??
0) 0)
: 5, : 5,

View File

@@ -52,8 +52,10 @@ class _SettingsScreenState extends State<SettingsScreen> {
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: [ children: [
CircleAvatar( CircleAvatar(
backgroundColor: Theme.of(context).focusColor,
radius: 70, radius: 70,
child: CircleAvatar( child: CircleAvatar(
backgroundColor: Theme.of(context).highlightColor,
radius: 60, radius: 60,
child: CircleAvatar( child: CircleAvatar(
radius: 50, radius: 50,
@@ -61,7 +63,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
getAvatarContent( getAvatarContent(
userSnapshot.data?.username ?? ""), userSnapshot.data?.username ?? ""),
style: const TextStyle( style: const TextStyle(
fontSize: 35, fontWeight: FontWeight.bold), fontSize: 35,
fontWeight: FontWeight.bold,
),
), ),
), ),
), ),
@@ -121,6 +125,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
} }
}, },
child: Card( child: Card(
color: Theme.of(context).colorScheme.onInverseSurface,
margin: const EdgeInsets.only(left: 35, right: 35, bottom: 10), margin: const EdgeInsets.only(left: 35, right: 35, bottom: 10),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)), shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
child: ListTile( child: ListTile(

View File

@@ -1,11 +1,11 @@
import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'product/services/theme_services.dart'; import 'product/services/theme_services.dart';
import 'product/services/language_services.dart'; import 'product/services/language_services.dart';
import 'bloc/main_bloc.dart'; import 'bloc/main_bloc.dart';
import 'product/base/bloc/base_bloc.dart'; import 'product/base/bloc/base_bloc.dart';
import 'product/constant/navigation/navigation_router.dart'; import 'product/constant/navigation/navigation_router.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();

View File

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