Misc + clean code logic future for configurations and sections

This commit is contained in:
Thomas Fransolet 2025-06-11 12:04:01 +02:00
parent 9c5ae56549
commit 4946247812
3 changed files with 30 additions and 48 deletions

View File

@ -43,11 +43,14 @@ class _HomePage3State extends State<HomePage3> with WidgetsBindingObserver {
List<String?> alreadyDownloaded = [];
late VisitAppContext visitAppContext;
late Future<List<ConfigurationDTO>?> _futureConfigurations;
@override
void initState() {
SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
super.initState();
final appContext = Provider.of<AppContext>(context, listen: false);
_futureConfigurations = getConfigurationsCall(appContext);
}
@override
@ -59,7 +62,7 @@ class _HomePage3State extends State<HomePage3> with WidgetsBindingObserver {
return Scaffold(
extendBody: true,
body: FutureBuilder(
future: getConfigurationsCall(visitAppContext.clientAPI, appContext),
future: _futureConfigurations,//getConfigurationsCall(visitAppContext.clientAPI, appContext),
builder: (context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
configurations = List<ConfigurationDTO>.from(snapshot.data).where((configuration) => configuration.isMobile!).toList();
@ -287,9 +290,9 @@ class _HomePage3State extends State<HomePage3> with WidgetsBindingObserver {
return Text(TranslationHelper.getFromLocale("noData", appContext.getContext()));
} else {
return Center(
child: Container(
child: SizedBox(
height: size.height * 0.15,
child: LoadingCommon()
child: const LoadingCommon()
)
);
}
@ -309,7 +312,7 @@ class _HomePage3State extends State<HomePage3> with WidgetsBindingObserver {
width: size.width,
height: size.height,
child: FutureBuilder(
future: getConfigurationsCall(visitAppContext.clientAPI, appContext),
future: getConfigurationsCall(appContext),
builder: (context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
configurations = List<ConfigurationDTO>.from(snapshot.data).where((configuration) => configuration.isMobile!).toList();
@ -392,7 +395,7 @@ class _HomePage3State extends State<HomePage3> with WidgetsBindingObserver {
);
}
Future<List<ConfigurationDTO>?> getConfigurationsCall(Client client, AppContext appContext) async {
Future<List<ConfigurationDTO>?> getConfigurationsCall(AppContext appContext) async {
bool isOnline = await hasNetwork();
VisitAppContext visitAppContext = appContext.getContext();
@ -424,7 +427,7 @@ class _HomePage3State extends State<HomePage3> with WidgetsBindingObserver {
}
if(visitAppContext.beaconSections == null) {
List<SectionDTO>? sections = await ApiService.getAllBeacons(client, visitAppContext.instanceId!);
List<SectionDTO>? sections = await ApiService.getAllBeacons(visitAppContext.clientAPI, visitAppContext.instanceId!);
if(sections != null && sections.isNotEmpty) {
List<BeaconSection> beaconSections = sections.map((e) => BeaconSection(minorBeaconId: e.beaconId, orderInConfig: e.order, configurationId: e.configurationId, sectionId: e.id, sectionType: e.type)).toList();
visitAppContext.beaconSections = beaconSections;
@ -448,6 +451,6 @@ class _HomePage3State extends State<HomePage3> with WidgetsBindingObserver {
}
}
return await ApiService.getConfigurations(client, visitAppContext);
return await ApiService.getConfigurations(visitAppContext.clientAPI, visitAppContext);
}
}

View File

@ -31,12 +31,20 @@ class _BodyState extends State<Body> {
late List<SectionDTO> sections;
late List<SectionDTO> _allSections;
late List<dynamic> rawSections;
List<SectionDTO> sectionsToDisplay = [];
String? searchValue;
int? searchNumberValue;
final ValueNotifier<List<SectionDTO>> filteredSections = ValueNotifier([]);
late Future<List<SectionDTO>> _futureSections;
@override
void initState() {
super.initState();
final appContext = Provider.of<AppContext>(context, listen: false);
_futureSections = getSections(appContext);
}
@override
Widget build(BuildContext context) {
final appContext = Provider.of<AppContext>(context);
@ -88,9 +96,10 @@ class _BodyState extends State<Body> {
child: InkWell(
onTap: () {
//setState(() {
/**/
Navigator.of(context).pop();
visitAppContext.configuration = null;
visitAppContext.isScanningBeacons = false;
Navigator.of(context).pop();
/*Navigator.of(context).pushAndRemoveUntil(MaterialPageRoute(
builder: (context) => const HomePage3(),
),(route) => false);*/
@ -114,13 +123,6 @@ class _BodyState extends State<Body> {
SearchBox(onChanged: (value) {
searchValue = value?.trim();
applyFilters(visitAppContext);
/*setState(() {
if(value != null && value != "") {
searchValue = value;
} else {
searchValue = null;
}
});*/
}),
Expanded(
child: SearchNumberBox(onChanged: (value) {
@ -132,15 +134,6 @@ class _BodyState extends State<Body> {
FocusScope.of(context).unfocus();
applyFilters(visitAppContext);
}
/*setState(() {
if(value != null && value != "") {
searchNumberValue = int.parse(value);
} else {
searchNumberValue = null;
FocusScope.of(context).unfocus();
}
});
}*/
),
),
],
@ -169,7 +162,7 @@ class _BodyState extends State<Body> {
),
),
FutureBuilder(
future: getSections(appContext),
future: _futureSections,
builder: (context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
/*print("SECTIONTODISPA");
@ -179,8 +172,9 @@ class _BodyState extends State<Body> {
child: RefreshIndicator(
onRefresh: () async {
if(!widget.configuration.isOffline!) {
// Force refresh if online
setState(() {});
setState(() {
_futureSections = getSections(appContext);
});
} },
child: ValueListenableBuilder<List<SectionDTO>>(
valueListenable: filteredSections,
@ -231,7 +225,7 @@ class _BodyState extends State<Body> {
);
}
getSections(AppContext appContext) async {
Future<List<SectionDTO>> getSections(AppContext appContext) async {
VisitAppContext visitAppContext = appContext.getContext();
if(widget.configuration.isOffline!)
{
@ -255,26 +249,11 @@ class _BodyState extends State<Body> {
sections = sections.where((s) => s.configurationId == widget.configuration.id!).toList();
sections.sort((a,b) => a.order!.compareTo(b.order!));
//sectionsToDisplay = sections;
try {
_allSections = sections;
//visitAppContext.currentSections = sectionsToDisplay;
applyFilters(visitAppContext);
} catch(e) {
print(e);
}
/*if(searchValue != '' && searchValue != null) {
sectionsToDisplay = sections.where((s) => removeDiacritics(TranslationHelper.get(s.title, appContext.getContext()).toLowerCase()).contains(removeDiacritics(searchValue.toString().toLowerCase()))).toList();
} else {
if(searchNumberValue != null) {
sectionsToDisplay = sections.where((s) => s.order!+1 == searchNumberValue).toList();
} else {
sectionsToDisplay = sections;
}
}*/
return _allSections;
}
void applyFilters(VisitAppContext visitAppContext) {

View File

@ -341,7 +341,7 @@ class _VisitPageState extends State<VisitPage> with WidgetsBindingObserver {
});
return PopScope(
canPop: false,
canPop: true,
child: Scaffold(
/*appBar: CustomAppBar(
title: TranslationHelper.get(configuration!.title, visitAppContext),