From 3c23106d32f6c7e21ecb4f5ff06f0dc119d25ec6 Mon Sep 17 00:00:00 2001 From: Mark Johnson Date: Fri, 21 Jun 2024 15:26:15 +0100 Subject: [PATCH] Improve OS loading Rather than waiting for the OS list to load, when the app starts, the loads them in the background, and only waits if we reach the list of available downloads without them loaded yet. In this case, we use a FutureBuilder to display a loading spinner until the list is ready. --- lib/main.dart | 2 +- lib/src/model/operating_system.dart | 2 +- lib/src/pages/operating_system_selection.dart | 78 ++++++++++++------- pubspec.yaml | 2 +- 4 files changed, 53 insertions(+), 31 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 251d685..6a08e8e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -80,7 +80,7 @@ void main() async { setWindowMaxSize(const Size(692, 580)); final foundQuickGet = await Process.run('which', ['quickget']); if (foundQuickGet.exitCode == 0) { - gOperatingSystems = await loadOperatingSystems(false); + gOperatingSystems = loadOperatingSystems(false); getIcons(); AppVersion.packageInfo = await PackageInfo.fromPlatform(); } diff --git a/lib/src/model/operating_system.dart b/lib/src/model/operating_system.dart index a3c913d..f870138 100644 --- a/lib/src/model/operating_system.dart +++ b/lib/src/model/operating_system.dart @@ -8,4 +8,4 @@ class OperatingSystem { List versions; } -var gOperatingSystems = []; +Future>? gOperatingSystems; diff --git a/lib/src/pages/operating_system_selection.dart b/lib/src/pages/operating_system_selection.dart index 622fc2c..2645a87 100644 --- a/lib/src/pages/operating_system_selection.dart +++ b/lib/src/pages/operating_system_selection.dart @@ -25,9 +25,6 @@ class _OperatingSystemSelectionState extends State { @override Widget build(BuildContext context) { - var list = gOperatingSystems - .where((os) => os.name.toLowerCase().contains(term.toLowerCase())) - .toList(); return Scaffold( appBar: AppBar( title: Text(context.t('Select operating system')), @@ -69,36 +66,61 @@ class _OperatingSystemSelectionState extends State { body: SingleChildScrollView( child: Column( children: [ - ListView.builder( - padding: const EdgeInsets.only(top: 4), - shrinkWrap: true, - itemCount: list.length, - itemBuilder: (context, index) { - var item = list[index]; - Widget icon; + FutureBuilder( + future: gOperatingSystems, + builder: (BuildContext context, AsyncSnapshot snapshot) { + if (snapshot.hasData) { + List list = snapshot.data! + .where((os) => os.name.toLowerCase().contains(term.toLowerCase())) + .toList(); + return ListView.builder( + padding: const EdgeInsets.only(top: 4), + shrinkWrap: true, + itemCount: list.length, + itemBuilder: (context, index) { + var item = list[index]; + Widget icon; - if (osIcons.containsKey(item.code)) { - icon = SvgPicture.asset( - osIcons[item.code]!, - width: 32, - height: 32, + if (osIcons.containsKey(item.code)) { + icon = SvgPicture.asset( + osIcons[item.code]!, + width: 32, + height: 32, + ); + } else { + // Replace with generic icon + icon = const Icon(Icons.computer, size: 32); + } + return Card( + child: ListTile( + title: Text(item.name), + leading: icon, + onTap: () { + Navigator.of(context).pop(item); + }, + ), + ); + }, ); } else { - // Replace with generic icon - icon = const Icon(Icons.computer, size: 32); + return Row( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Column( + children: [ + const Padding( + padding: EdgeInsets.all(16.0), + child: CircularProgressIndicator() + ), + Text(context.t('Loading available downloads')), + ], + ) + ], + ); } - return Card( - child: ListTile( - title: Text(item.name), - leading: icon, - onTap: () { - Navigator.of(context).pop(item); - }, - ), - ); - }, + } ), - ], + ] ), ), ); diff --git a/pubspec.yaml b/pubspec.yaml index 96277fd..bacbd40 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,7 +4,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.2.8-1 environment: - sdk: ">=2.14.4 <3.0.0" + sdk: ">=2.15.0 <3.0.0" dependencies: flutter: