diff --git a/lib/pages/apps.dart b/lib/pages/apps.dart index 2621303..3796794 100644 --- a/lib/pages/apps.dart +++ b/lib/pages/apps.dart @@ -15,6 +15,7 @@ class _AppsPageState extends State { @override Widget build(BuildContext context) { var appsProvider = context.watch(); + appsProvider.getUpdates(); return Scaffold( appBar: AppBar( @@ -28,35 +29,70 @@ class _AppsPageState extends State { 'No Apps', style: Theme.of(context).textTheme.headline4, ) - : ListView( - children: appsProvider.apps.values - .map( - (e) => ListTile( - title: Text(e.name), - subtitle: Text(e.author), - trailing: - Text(e.installedVersion ?? 'Not Installed'), - onTap: () { - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => AppPage(appId: e.id)), - ); - }, - ), - ) - .toList(), + : RefreshIndicator( + onRefresh: appsProvider.getUpdates, + child: ListView( + children: appsProvider.apps.values + .map( + (e) => ListTile( + title: Text('${e.author}/${e.name}'), + subtitle: + Text(e.installedVersion ?? 'Not Installed'), + trailing: e.installedVersion != null && + e.installedVersion != e.latestVersion + ? const Text('Update Available') + : null, + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => + AppPage(appId: e.id)), + ); + }, + ), + ) + .toList(), + ), ), ), - floatingActionButton: FloatingActionButton( - onPressed: () { - Navigator.push( - context, - MaterialPageRoute(builder: (context) => const AddAppPage()), - ); - }, - tooltip: 'Add App', - child: const Icon(Icons.add), + bottomSheet: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Padding( + padding: + const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0), + child: Row( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + Expanded( + child: appsProvider.apps.values.toList().where((e) { + return (e.installedVersion != null && + e.installedVersion != e.latestVersion); + }).isNotEmpty + ? OutlinedButton( + onPressed: () { + appsProvider.installUpdates().catchError((e) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar(content: Text(e.toString())), + ); + }); + }, + child: const Text('Update All')) + : Container()), + const SizedBox(width: 16.0), + OutlinedButton( + onPressed: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => const AddAppPage()), + ); + }, + child: const Text('Add App'), + ), + ])), + ], ), ); } diff --git a/lib/services/apps_provider.dart b/lib/services/apps_provider.dart index 95cd356..ca34737 100644 --- a/lib/services/apps_provider.dart +++ b/lib/services/apps_provider.dart @@ -17,6 +17,7 @@ class AppsProvider with ChangeNotifier { // In memory App state (should always be kept in sync with local storage versions) Map apps = {}; bool loadingApps = false; + bool gettingUpdates = false; AppsProvider() { initializeDownloader(); @@ -213,12 +214,17 @@ class AppsProvider with ChangeNotifier { Future> getUpdates() async { List updates = []; - List appIds = apps.keys.toList(); - for (int i = 0; i < appIds.length; i++) { - App? newApp = await getUpdate(appIds[i]); - if (newApp != null) { - updates.add(newApp); + if (!gettingUpdates) { + gettingUpdates = true; + + List appIds = apps.keys.toList(); + for (int i = 0; i < appIds.length; i++) { + App? newApp = await getUpdate(appIds[i]); + if (newApp != null) { + updates.add(newApp); + } } + gettingUpdates = false; } return updates; }