From 91ec09fcb9cfcf034ca92069d3a8b6cdd7d4ec71 Mon Sep 17 00:00:00 2001 From: Kiwix DevTeam Date: Wed, 22 Feb 2023 17:15:58 +0100 Subject: [PATCH] Finetune download prompt --- lib/Screens/Home/configurations_list.dart | 206 ++++++++++++++-------- 1 file changed, 137 insertions(+), 69 deletions(-) diff --git a/lib/Screens/Home/configurations_list.dart b/lib/Screens/Home/configurations_list.dart index 294295f..f5a45df 100644 --- a/lib/Screens/Home/configurations_list.dart +++ b/lib/Screens/Home/configurations_list.dart @@ -20,9 +20,13 @@ import 'package:mymuseum_visitapp/app_context.dart'; import 'package:mymuseum_visitapp/constants.dart'; import 'package:provider/provider.dart'; - class ConfigurationsList extends StatefulWidget { - const ConfigurationsList({Key? key, required this.configurations, required this.alreadyDownloaded, required this.requestRefresh}) : super(key: key); + const ConfigurationsList( + {Key? key, + required this.configurations, + required this.alreadyDownloaded, + required this.requestRefresh}) + : super(key: key); final List configurations; final List alreadyDownloaded; @@ -46,22 +50,28 @@ class _ConfigurationsListState extends State { @override Widget build(BuildContext context) { - Size size = MediaQuery.of(context).size; final appContext = Provider.of(context); visitAppContext = appContext.getContext(); return ListView.builder( - shrinkWrap: true, //I've set this as true here depending on what your listview content is + 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: () async { VisitAppContext visitAppContext = appContext.getContext(); - if(configurations[index].isOffline! && alreadyDownloaded.any((c) => c == configurations[index].id)) { - if(!configurations[index].languages!.contains(visitAppContext.language)) { + if (configurations[index].isOffline! && + alreadyDownloaded.any((c) => c == configurations[index].id)) { + if (!configurations[index] + .languages! + .contains(visitAppContext.language)) { ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text(TranslationHelper.getFromLocale("languageNotSupported", appContext.getContext())), backgroundColor: kBlue2), + SnackBar( + content: Text(TranslationHelper.getFromLocale( + "languageNotSupported", appContext.getContext())), + backgroundColor: kBlue2), ); } else { // Update context @@ -71,7 +81,8 @@ class _ConfigurationsListState extends State { appContext.setContext(visitAppContext); Navigator.of(context).pushReplacement(MaterialPageRoute( - builder: (context) => VisitPage(configurationId: configurations[index].id!), + builder: (context) => + VisitPage(configurationId: configurations[index].id!), )); /*Navigator.push( context, @@ -82,10 +93,13 @@ class _ConfigurationsListState extends State { /**/ } } else { - if(configurations[index].isOffline!) { + if (configurations[index].isOffline!) { // Not already downloaded ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text(TranslationHelper.getFromLocale("visitDownloadWarning", appContext.getContext())), backgroundColor: kBlue2), + SnackBar( + content: Text(TranslationHelper.getFromLocale( + "visitDownloadWarning", appContext.getContext())), + backgroundColor: kBlue2), ); } else { // Online mode @@ -101,77 +115,99 @@ class _ConfigurationsListState extends State { );*/ Navigator.of(context).pushReplacement(MaterialPageRoute( - builder: (context) => VisitPage(configurationId: configurations[index].id!), + builder: (context) => + VisitPage(configurationId: configurations[index].id!), )); } } }, child: Container( - height: size.height*0.15, + height: size.height * 0.15, decoration: boxDecoration(configurations[index], false), margin: const EdgeInsets.symmetric(vertical: 10, horizontal: 20), child: Stack( children: [ Row( children: [ - if(configurations[index].imageId != null) + if (configurations[index].imageId != null) Container( padding: const EdgeInsets.symmetric(horizontal: 0), height: 136, // image is square but we add extra 20 + 20 padding thats why width is 200 - width: size.width*0.3, + width: size.width * 0.3, child: FutureBuilder( - future: ApiService.getResource(appContext, configurations[index].imageId!), - builder: (context, AsyncSnapshot snapshot) { - if (snapshot.connectionState == ConnectionState.done) { - return snapshot.data != null ? ClipRRect( - borderRadius: const BorderRadius.only(topLeft: Radius.circular(20), bottomLeft: Radius.circular(20)), - child: snapshot.data.data != null ? Image.memory( - base64Decode(snapshot.data.data!), - fit: BoxFit.cover - ) : Image.network( - configurations[index].imageSource!, - fit: BoxFit.cover, - loadingBuilder: (BuildContext context, Widget child, - ImageChunkEvent? loadingProgress) { - if (loadingProgress == null) { - return child; - } - return Center( - child: CircularProgressIndicator( - color: kBlue1, - value: loadingProgress.expectedTotalBytes != null - ? loadingProgress.cumulativeBytesLoaded / - loadingProgress.expectedTotalBytes! - : null, - ), - ); - }, - ), - ) : const Text(""); - } else if (snapshot.connectionState == ConnectionState.none) { - return Text(TranslationHelper.getFromLocale("noData", appContext.getContext())); + future: ApiService.getResource( + appContext, configurations[index].imageId!), + builder: + (context, AsyncSnapshot snapshot) { + if (snapshot.connectionState == + ConnectionState.done) { + return snapshot.data != null + ? ClipRRect( + borderRadius: const BorderRadius.only( + topLeft: Radius.circular(20), + bottomLeft: Radius.circular(20)), + child: snapshot.data.data != null + ? Image.memory( + base64Decode( + snapshot.data.data!), + fit: BoxFit.cover) + : Image.network( + configurations[index] + .imageSource!, + fit: BoxFit.cover, + loadingBuilder: + (BuildContext context, + Widget child, + ImageChunkEvent? + loadingProgress) { + if (loadingProgress == + null) { + return child; + } + return Center( + child: + CircularProgressIndicator( + color: kBlue1, + value: loadingProgress + .expectedTotalBytes != + null + ? loadingProgress + .cumulativeBytesLoaded / + loadingProgress + .expectedTotalBytes! + : null, + ), + ); + }, + ), + ) + : const Text(""); + } else if (snapshot.connectionState == + ConnectionState.none) { + return Text(TranslationHelper.getFromLocale( + "noData", appContext.getContext())); } else { return Center( child: SizedBox( height: size.height * 0.15, - child: const Loading() - ) - ); + child: const Loading())); } - } - ), + }), ), Align( alignment: Alignment.topLeft, child: Container( - width: size.width*0.45, + width: size.width * 0.45, child: Center( child: Padding( padding: const EdgeInsets.only(left: 10), child: AutoSizeText( - TranslationHelper.get(configurations[index].title, appContext.getContext()), - style: const TextStyle(fontSize: kMenuTitleDetailSize), + TranslationHelper.get( + configurations[index].title, + appContext.getContext()), + style: const TextStyle( + fontSize: kMenuTitleDetailSize), maxFontSize: 18, maxLines: 2, ), @@ -181,7 +217,7 @@ class _ConfigurationsListState extends State { ), ], ), - if(configurations[index].isOffline!) + if (configurations[index].isOffline!) Positioned( bottom: 0, right: 0, @@ -196,24 +232,29 @@ class _ConfigurationsListState extends State { margin: const EdgeInsets.all(8), child: InkWell( onTap: () async { - await downloadClicked(appContext, configurations[index]); + await downloadClicked( + appContext, configurations[index]); widget.requestRefresh(); }, - 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), + 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 - ); + itemCount: configurations.length); } - Future downloadClicked(AppContext appContext, ConfigurationDTO configuration) async { + Future downloadClicked( + AppContext appContext, ConfigurationDTO configuration) async { bool isCancel = false; //if(!alreadyDownloaded.any((c) => c == configuration.id)) { await showDialog( @@ -226,9 +267,28 @@ class _ConfigurationsListState extends State { padding: const EdgeInsets.symmetric(vertical: 20), child: Column( mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.center, children: [ - Text(!alreadyDownloaded.any((c) => c == configuration.id) ? TranslationHelper.getFromLocale("downloadPrompt", appContext.getContext()) : TranslationHelper.getFromLocale("downloadPromptUpdate", appContext.getContext()), style: TextStyle(color: kMainColor)), - Text(TranslationHelper.getFromLocale("downloadLanguage", appContext.getContext()), style: TextStyle(color: kMainColor)), + Padding( + padding: const EdgeInsets.all(8.0), + child: Text( + !alreadyDownloaded.any((c) => c == configuration.id) + ? TranslationHelper.getFromLocale( + "downloadPrompt", appContext.getContext()) + : TranslationHelper.getFromLocale( + "downloadPromptUpdate", + appContext.getContext()), + style: TextStyle(color: kMainColor), + textAlign: TextAlign.center), + ), + const SizedBox( + height: 15, + ), + Text( + TranslationHelper.getFromLocale( + "downloadLanguage", appContext.getContext()), + style: TextStyle(color: kMainColor), + textAlign: TextAlign.center), const SizedBox( height: 25, ), @@ -238,14 +298,20 @@ class _ConfigurationsListState extends State { ), actions: [ TextButton( - child: Text(TranslationHelper.getFromLocale("close", appContext.getContext()), style: TextStyle(color: kMainColor)), + child: Text( + TranslationHelper.getFromLocale( + "close", appContext.getContext()), + style: TextStyle(color: kMainColor)), onPressed: () { isCancel = true; Navigator.of(context).pop(); }, ), TextButton( - child: Text(TranslationHelper.getFromLocale("download", appContext.getContext()), style: TextStyle(color: kMainColor)), + child: Text( + TranslationHelper.getFromLocale( + "download", appContext.getContext()), + style: TextStyle(color: kMainColor)), onPressed: () async { Navigator.of(context).pop(); }, @@ -257,8 +323,9 @@ class _ConfigurationsListState extends State { }); //} - if(!isCancel) { - String loadingText = TranslationHelper.getFromLocale("downloadConfiguration", appContext.getContext()); + if (!isCancel) { + String loadingText = TranslationHelper.getFromLocale( + "downloadConfiguration", appContext.getContext()); showDialog( barrierDismissible: false, context: context, @@ -281,7 +348,8 @@ class _ConfigurationsListState extends State { ); }); - var isFinish = await DownloadConfiguration.download(appContext, configuration); + var isFinish = + await DownloadConfiguration.download(appContext, configuration); print("C4EST FINIITO"); print(isFinish); @@ -308,4 +376,4 @@ boxDecoration(ConfigurationDTO configuration, bool isSelected) { ), ], ); -} \ No newline at end of file +}