From 4946247812bd1bf43ee64d87dd7e96a69086d5b3 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Wed, 11 Jun 2025 12:04:01 +0200 Subject: [PATCH] Misc + clean code logic future for configurations and sections --- lib/Screens/Home/home_3.0.dart | 17 +++++--- lib/Screens/Visit/components/body.dart | 59 +++++++++----------------- lib/Screens/Visit/visit.dart | 2 +- 3 files changed, 30 insertions(+), 48 deletions(-) diff --git a/lib/Screens/Home/home_3.0.dart b/lib/Screens/Home/home_3.0.dart index 178c5d4..3a273c7 100644 --- a/lib/Screens/Home/home_3.0.dart +++ b/lib/Screens/Home/home_3.0.dart @@ -43,11 +43,14 @@ class _HomePage3State extends State with WidgetsBindingObserver { List alreadyDownloaded = []; late VisitAppContext visitAppContext; + late Future?> _futureConfigurations; @override void initState() { SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge); super.initState(); + final appContext = Provider.of(context, listen: false); + _futureConfigurations = getConfigurationsCall(appContext); } @override @@ -59,7 +62,7 @@ class _HomePage3State extends State with WidgetsBindingObserver { return Scaffold( extendBody: true, body: FutureBuilder( - future: getConfigurationsCall(visitAppContext.clientAPI, appContext), + future: _futureConfigurations,//getConfigurationsCall(visitAppContext.clientAPI, appContext), builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { configurations = List.from(snapshot.data).where((configuration) => configuration.isMobile!).toList(); @@ -287,9 +290,9 @@ class _HomePage3State extends State 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 with WidgetsBindingObserver { width: size.width, height: size.height, child: FutureBuilder( - future: getConfigurationsCall(visitAppContext.clientAPI, appContext), + future: getConfigurationsCall(appContext), builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { configurations = List.from(snapshot.data).where((configuration) => configuration.isMobile!).toList(); @@ -392,7 +395,7 @@ class _HomePage3State extends State with WidgetsBindingObserver { ); } - Future?> getConfigurationsCall(Client client, AppContext appContext) async { + Future?> getConfigurationsCall(AppContext appContext) async { bool isOnline = await hasNetwork(); VisitAppContext visitAppContext = appContext.getContext(); @@ -424,7 +427,7 @@ class _HomePage3State extends State with WidgetsBindingObserver { } if(visitAppContext.beaconSections == null) { - List? sections = await ApiService.getAllBeacons(client, visitAppContext.instanceId!); + List? sections = await ApiService.getAllBeacons(visitAppContext.clientAPI, visitAppContext.instanceId!); if(sections != null && sections.isNotEmpty) { List 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 with WidgetsBindingObserver { } } - return await ApiService.getConfigurations(client, visitAppContext); + return await ApiService.getConfigurations(visitAppContext.clientAPI, visitAppContext); } } \ No newline at end of file diff --git a/lib/Screens/Visit/components/body.dart b/lib/Screens/Visit/components/body.dart index 6ba867c..b80c5cd 100644 --- a/lib/Screens/Visit/components/body.dart +++ b/lib/Screens/Visit/components/body.dart @@ -31,12 +31,20 @@ class _BodyState extends State { late List sections; late List _allSections; late List rawSections; - List sectionsToDisplay = []; String? searchValue; int? searchNumberValue; final ValueNotifier> filteredSections = ValueNotifier([]); + late Future> _futureSections; + + @override + void initState() { + super.initState(); + final appContext = Provider.of(context, listen: false); + _futureSections = getSections(appContext); + } + @override Widget build(BuildContext context) { final appContext = Provider.of(context); @@ -88,9 +96,10 @@ class _BodyState extends State { 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 { 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 { 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 { ), ), FutureBuilder( - future: getSections(appContext), + future: _futureSections, builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { /*print("SECTIONTODISPA"); @@ -179,8 +172,9 @@ class _BodyState extends State { child: RefreshIndicator( onRefresh: () async { if(!widget.configuration.isOffline!) { - // Force refresh if online - setState(() {}); + setState(() { + _futureSections = getSections(appContext); + }); } }, child: ValueListenableBuilder>( valueListenable: filteredSections, @@ -231,7 +225,7 @@ class _BodyState extends State { ); } - getSections(AppContext appContext) async { + Future> getSections(AppContext appContext) async { VisitAppContext visitAppContext = appContext.getContext(); if(widget.configuration.isOffline!) { @@ -255,26 +249,11 @@ class _BodyState extends State { 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; + _allSections = sections; + applyFilters(visitAppContext); - 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) { diff --git a/lib/Screens/Visit/visit.dart b/lib/Screens/Visit/visit.dart index e6ed6f4..12def0f 100644 --- a/lib/Screens/Visit/visit.dart +++ b/lib/Screens/Visit/visit.dart @@ -341,7 +341,7 @@ class _VisitPageState extends State with WidgetsBindingObserver { }); return PopScope( - canPop: false, + canPop: true, child: Scaffold( /*appBar: CustomAppBar( title: TranslationHelper.get(configuration!.title, visitAppContext),