import 'package:flutter/material.dart'; import 'dart:convert'; import 'dart:io'; import 'package:auto_size_text/auto_size_text.dart'; import 'package:manager_api/api.dart'; import 'package:mymuseum_visitapp/Components/CustomAppBar.dart'; import 'package:mymuseum_visitapp/Components/Loading.dart'; import 'package:mymuseum_visitapp/Components/ScannerBouton.dart'; import 'package:mymuseum_visitapp/Helpers/DatabaseHelper.dart'; import 'package:mymuseum_visitapp/Helpers/networkCheck.dart'; import 'package:mymuseum_visitapp/Helpers/translationHelper.dart'; import 'package:mymuseum_visitapp/Models/resourceModel.dart'; import 'package:mymuseum_visitapp/Models/visitContext.dart'; import 'package:mymuseum_visitapp/Screens/Visit/visit.dart'; import 'package:mymuseum_visitapp/app_context.dart'; import 'package:mymuseum_visitapp/client.dart'; import 'package:mymuseum_visitapp/constants.dart'; import 'package:provider/provider.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; class HomePage extends StatefulWidget { const HomePage({Key? key}) : super(key: key); @override State createState() => _HomePageState(); } class _HomePageState extends State { List configurations = []; List alreadyDownloaded = []; @override Widget build(BuildContext context) { Size size = MediaQuery.of(context).size; final appContext = Provider.of(context); VisitAppContext visitAppContext = appContext.getContext(); return Scaffold( appBar: CustomAppBar( title: TranslationHelper.getFromLocale("visitTitle", appContext), isHomeButton: false, ), body: SingleChildScrollView( child: SizedBox( width: size.width, height: size.height, child: FutureBuilder( future: getConfigurations(appContext.clientAPI), builder: (context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.done) { configurations = List.from(snapshot.data).where((configuration) => configuration.isMobile!).toList(); return RefreshIndicator ( onRefresh: () { setState(() {}); return Future(() => null); }, color: kSecondColor, child: ListView.builder( shrinkWrap: true, //I've set this as true here depending on what your listview content is //physics: NeverScrollableScrollPhysics(),//This prevents scrolling, but may inhibit refresh indicator, remove as you need itemBuilder: (BuildContext context, int index) { return InkWell( onTap: () { //print(configurations[index].label); // Update context VisitAppContext visitAppContext = appContext.getContext(); visitAppContext.configuration = configurations[index]; appContext.setContext(visitAppContext); if(configurations[index].isOffline! && alreadyDownloaded.any((c) => c == configurations[index].id)) { Navigator.of(context).pushReplacement(MaterialPageRoute( builder: (context) => VisitPage(configurationId: configurations[index].id!), )); } else { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text(TranslationHelper.getFromLocale("visitDownloadWarning", appContext)), backgroundColor: kBlue2), ); } }, child: Container( height: size.height*0.15, decoration: boxDecoration(configurations[index], false), margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 20), child: Stack( children: [ Align( alignment: Alignment.topLeft, child: Padding( padding: const EdgeInsets.only(top: 20, left: 10), child: AutoSizeText( configurations[index].label!, style: const TextStyle(fontSize: kMenuTitleDetailSize), maxLines: 1, ), ), ), if(configurations[index].isOffline!) Positioned( bottom: 0, right: 0, child: Container( width: 45, height: 45, decoration: BoxDecoration( shape: BoxShape.rectangle, color: kBlue1, borderRadius: BorderRadius.circular(20.0), ), margin: const EdgeInsets.all(8), child: InkWell( onTap: () async { downloadClicked(appContext, configurations[index]); }, child: configurations[index].isOffline! && !alreadyDownloaded.any((c) => c == configurations[index].id) ? const Icon(Icons.download, color: Colors.white) : const Icon(Icons.refresh, color: Colors.white), ), ) ) ], ), ), ); }, itemCount: configurations.length ), ); } else if (snapshot.connectionState == ConnectionState.none) { return Text(TranslationHelper.getFromLocale("noData", appContext)); } else { return Center( child: Container( height: size.height * 0.15, child: Loading() ) ); } } ), ), ), floatingActionButton: const ScannerBouton(isReplacement: false), //floatingActionButtonLocation: FloatingActionButtonLocation.miniCenterFloat, ); } Future?> getConfigurations(Client client) async { try { List? configurations; bool isOnline = await hasNetwork(); configurations = List.from(await DatabaseHelper.instance.getData(DatabaseTableType.configurations)); alreadyDownloaded = configurations.map((c) => c.id).toList(); if(isOnline) { configurations = await client.configurationApi!.configurationGet(); return configurations ?? []; } else { return configurations ?? []; } } catch (e) { print(e); print("IN CATCH"); return []; } } Future?> getAllSections(Client client, String configurationId) async { try { bool isOnline = await hasNetwork(); if(isOnline) { List? sections = await client.sectionApi!.sectionGetFromConfiguration(configurationId); return sections; } else { return []; // TODO return local list.. } } catch (e) { print(e); print("IN CATCH"); return []; } } Future getAndDownloadImage(Client client, ImageDTO imageDTO) async { try { bool isOnline = await hasNetwork(); if(isOnline) { var source = imageDTO.source_; if(imageDTO.source_!.contains("localhost:5000")) { source = imageDTO.source_!.replaceAll("http://localhost:5000", client.apiApi!.basePath); } HttpClient client2 = HttpClient(); var _downloadData = []; client2.getUrl(Uri.parse(source!)) .then((HttpClientRequest request) { return request.close(); }) .then((HttpClientResponse response) { response.listen((d) => _downloadData.addAll(d), onDone: () async { final base64Str = base64.encode(_downloadData); ResourceModel resourceModel = ResourceModel(id: imageDTO.resourceId, data: base64Str, type: ResourceType.Image); await DatabaseHelper.instance.insert(DatabaseTableType.resources, resourceModel.toMap()); //var test = base64.decode(base64Str); //return base64Str; /*setState(() { base64TEST = base64Str; });*/ } ); }); //return file; } else { //return null; // TODO return local list.. } } catch (e) { print(e); print("IN CATCH"); //return []; } } Future downloadClicked(AppContext appContext, ConfigurationDTO configuration) async { // Display config only downloaded - TODO // Update local DB - Configuration await DatabaseHelper.instance.insert(DatabaseTableType.configurations, configuration.toJson()); List? sections = await getAllSections(appContext.clientAPI, configuration.id!); if(sections!.isNotEmpty) { List sectionsToKeep = sections.where((s) => s.type == SectionType.Article).toList(); // TODO handle other type of section sectionsToKeep.sort((a,b) => a.order!.compareTo(b.order!)); int newOrder = 0; // Update local DB - Sections for(var section in sectionsToKeep) { section.order = newOrder; await DatabaseHelper.instance.insert(DatabaseTableType.sections, sectionToMap(section)); // Download all images.. ArticleDTO? articleDTO = ArticleDTO.fromJson(jsonDecode(section.data!)); if(articleDTO != null) { for(var image in articleDTO!.images!) { await getAndDownloadImage(appContext.clientAPI, image); } } newOrder = newOrder + 1; } } } Map sectionToMap(SectionDTO section) { return { 'id': section.id, 'label': section.label, 'title': jsonEncode(section.title), 'description': jsonEncode(section.description), 'imageId': section.imageId, 'imageSource': section.imageSource, 'configurationId': section.configurationId, 'isSubSection': section.isSubSection, 'parentId': section.parentId, 'type': section.type!.value, 'data': section.data, 'dateCreation': section.dateCreation!.toUtc().toIso8601String(), 'orderOfElement': section.order, //'token': token }; } } boxDecoration(ConfigurationDTO configuration, bool isSelected) { return BoxDecoration( color: Colors.white, shape: BoxShape.rectangle, borderRadius: BorderRadius.circular(20.0), border: Border.all( color: kBlue0.withOpacity(0.35), width: 0.2, ), boxShadow: [ BoxShadow( color: kBlue0.withOpacity(0.35), //spreadRadius: 0.15, blurRadius: 27, offset: const Offset(0, 15), // changes position of shadow ), ], ); }