From 7f46b72c5139cd331a1303a030ec184c2965c93c Mon Sep 17 00:00:00 2001 From: Hank Grabowski Date: Mon, 30 Jan 2023 18:14:25 -0500 Subject: [PATCH] Replace ValueListenableBuilders throughout with new StatusAndRefreshButton --- lib/controls/status_and_refresh_button.dart | 46 +++++++++++++++++++++ lib/screens/gallery_browsers_screen.dart | 29 +++---------- lib/screens/gallery_screen.dart | 37 +++++------------ lib/screens/home.dart | 31 ++++---------- lib/screens/notifications_screen.dart | 28 +++---------- lib/screens/user_posts_screen.dart | 39 +++-------------- 6 files changed, 81 insertions(+), 129 deletions(-) create mode 100644 lib/controls/status_and_refresh_button.dart diff --git a/lib/controls/status_and_refresh_button.dart b/lib/controls/status_and_refresh_button.dart new file mode 100644 index 0000000..1dd3bbe --- /dev/null +++ b/lib/controls/status_and_refresh_button.dart @@ -0,0 +1,46 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; + +class StatusAndRefreshButton extends StatelessWidget { + final ValueListenable valueListenable; + final Future Function() refreshFunction; + final Color? busyColor; + final Color? buttonColor; + + const StatusAndRefreshButton({ + super.key, + required this.valueListenable, + required this.refreshFunction, + this.buttonColor, + this.busyColor, + }); + + @override + Widget build(BuildContext context) { + return ValueListenableBuilder( + valueListenable: valueListenable, + builder: (context2, executing, _) { + if (executing) { + final theme = Theme.of(context); + final size = theme.appBarTheme.actionsIconTheme?.size ?? + theme.iconTheme.size ?? + 24; + return Center( + child: SizedBox( + width: size, + height: size, + child: CircularProgressIndicator( + color: busyColor, + ), + ), + ); + } + return IconButton( + onPressed: refreshFunction, + icon: Icon( + Icons.refresh, + color: buttonColor, + )); + }); + } +} diff --git a/lib/screens/gallery_browsers_screen.dart b/lib/screens/gallery_browsers_screen.dart index 167adc1..2763fb0 100644 --- a/lib/screens/gallery_browsers_screen.dart +++ b/lib/screens/gallery_browsers_screen.dart @@ -5,6 +5,7 @@ import 'package:provider/provider.dart'; import '../controls/padding.dart'; import '../controls/standard_appbar.dart'; +import '../controls/status_and_refresh_button.dart'; import '../globals.dart'; import '../services/gallery_service.dart'; import '../services/network_status_service.dart'; @@ -19,29 +20,11 @@ class GalleryBrowsersScreen extends StatelessWidget { final nss = getIt(); return Scaffold( appBar: StandardAppBar.build(context, 'Galleries', actions: [ - ValueListenableBuilder( - valueListenable: nss.imageGalleryLoadingStatus, - builder: (context2, executing, _) { - if (executing) { - final theme = Theme.of(context); - final size = theme.appBarTheme.actionsIconTheme?.size ?? - theme.iconTheme.size ?? - 24; - return Center( - child: SizedBox( - width: size, - height: size, - child: CircularProgressIndicator( - color: Theme.of(context).canvasColor), - ), - ); - } - return IconButton( - onPressed: () async => await service.updateGalleries(), - icon: Icon( - Icons.refresh, - )); - }), + StatusAndRefreshButton( + valueListenable: nss.imageGalleryLoadingStatus, + refreshFunction: () async => await service.updateGalleries(), + busyColor: Theme.of(context).colorScheme.background, + ), ]), body: RefreshIndicator( onRefresh: () async { diff --git a/lib/screens/gallery_screen.dart b/lib/screens/gallery_screen.dart index 4232160..2c89136 100644 --- a/lib/screens/gallery_screen.dart +++ b/lib/screens/gallery_screen.dart @@ -4,6 +4,7 @@ import 'package:logging/logging.dart'; import 'package:provider/provider.dart'; import '../controls/standard_appbar.dart'; +import '../controls/status_and_refresh_button.dart'; import '../globals.dart'; import '../serializers/friendica/image_entry_friendica_extensions.dart'; import '../services/gallery_service.dart'; @@ -29,33 +30,15 @@ class GalleryScreen extends StatelessWidget { ); return Scaffold( appBar: StandardAppBar.build(context, galleryName, actions: [ - ValueListenableBuilder( - valueListenable: nss.imageGalleryLoadingStatus, - builder: (context2, executing, _) { - if (executing) { - final theme = Theme.of(context); - final size = theme.appBarTheme.actionsIconTheme?.size ?? - theme.iconTheme.size ?? - 24; - return Center( - child: SizedBox( - width: size, - height: size, - child: CircularProgressIndicator( - color: Theme.of(context).canvasColor), - ), - ); - } - return IconButton( - onPressed: () async => await service.updateGalleryImageList( - galleryName: galleryName, - withNextPage: false, - nextPageOnly: false, - ), - icon: Icon( - Icons.refresh, - )); - }), + StatusAndRefreshButton( + valueListenable: nss.imageGalleryLoadingStatus, + refreshFunction: () async => await service.updateGalleryImageList( + galleryName: galleryName, + withNextPage: false, + nextPageOnly: false, + ), + busyColor: Theme.of(context).appBarTheme.foregroundColor, + ), ]), body: body, ); diff --git a/lib/screens/home.dart b/lib/screens/home.dart index 1a2d9b5..47b2de4 100644 --- a/lib/screens/home.dart +++ b/lib/screens/home.dart @@ -5,6 +5,7 @@ import 'package:provider/provider.dart'; import '../controls/app_bottom_nav_bar.dart'; import '../controls/padding.dart'; +import '../controls/status_and_refresh_button.dart'; import '../controls/timeline/timeline_panel.dart'; import '../globals.dart'; import '../models/TimelineIdentifiers.dart'; @@ -81,30 +82,12 @@ class _HomeScreenState extends State { ], ), actions: [ - ValueListenableBuilder( - valueListenable: nss.timelineLoadingStatus, - builder: (context2, executing, _) { - if (executing) { - final theme = Theme.of(context); - final size = theme.appBarTheme.actionsIconTheme?.size ?? - theme.iconTheme.size ?? - 24; - return Center( - child: SizedBox( - width: size, - height: size, - child: const CircularProgressIndicator(), - ), - ); - } - return IconButton( - onPressed: () async => await manager.updateTimeline( - currentTimeline, TimelineRefreshType.refresh), - icon: Icon( - Icons.refresh, - color: Theme.of(context).textTheme.bodyLarge?.color, - )); - }), + StatusAndRefreshButton( + valueListenable: nss.timelineLoadingStatus, + refreshFunction: () async => await manager.updateTimeline( + currentTimeline, TimelineRefreshType.refresh), + buttonColor: Theme.of(context).textTheme.bodyLarge?.color, + ), IconButton( onPressed: () { context.push('/post/new'); diff --git a/lib/screens/notifications_screen.dart b/lib/screens/notifications_screen.dart index f87731b..58ec006 100644 --- a/lib/screens/notifications_screen.dart +++ b/lib/screens/notifications_screen.dart @@ -5,6 +5,7 @@ import 'package:provider/provider.dart'; import '../controls/app_bottom_nav_bar.dart'; import '../controls/notifications_control.dart'; import '../controls/standard_appbar.dart'; +import '../controls/status_and_refresh_button.dart'; import '../globals.dart'; import '../services/network_status_service.dart'; import '../services/notifications_manager.dart'; @@ -53,28 +54,11 @@ class NotificationsScreen extends StatelessWidget { context, title, actions: [ - ValueListenableBuilder( - valueListenable: nss.notificationsUpdateStatus, - builder: (context2, executing, _) { - if (executing) { - final theme = Theme.of(context); - final size = theme.appBarTheme.actionsIconTheme?.size ?? - theme.iconTheme.size ?? - 24; - return Center( - child: SizedBox( - width: size, - height: size, - child: CircularProgressIndicator( - color: theme.canvasColor, - ), - ), - ); - } - return IconButton( - onPressed: () async => await manager.updateNotifications(), - icon: const Icon(Icons.refresh)); - }), + StatusAndRefreshButton( + valueListenable: nss.notificationsUpdateStatus, + refreshFunction: () async => manager.updateNotifications(), + busyColor: Theme.of(context).colorScheme.background, + ), IconButton( onPressed: () async => _clearAllNotifications(context, manager), icon: const Icon(Icons.cleaning_services), diff --git a/lib/screens/user_posts_screen.dart b/lib/screens/user_posts_screen.dart index 681f314..4972d5e 100644 --- a/lib/screens/user_posts_screen.dart +++ b/lib/screens/user_posts_screen.dart @@ -2,10 +2,10 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../controls/standard_appbar.dart'; +import '../controls/status_and_refresh_button.dart'; import '../controls/timeline/timeline_panel.dart'; import '../globals.dart'; import '../models/TimelineIdentifiers.dart'; -import '../routes.dart'; import '../services/network_status_service.dart'; import '../services/timeline_manager.dart'; @@ -24,38 +24,11 @@ class UserPostsScreen extends StatelessWidget { context, 'User Posts', actions: [ - ValueListenableBuilder( - valueListenable: nss.timelineLoadingStatus, - builder: (context2, executing, _) { - if (executing) { - final theme = Theme.of(context); - final size = theme.appBarTheme.actionsIconTheme?.size ?? - theme.iconTheme.size ?? - 24; - return Center( - child: SizedBox( - width: size, - height: size, - child: CircularProgressIndicator( - color: Theme.of(context).canvasColor, - ), - ), - ); - } - return IconButton( - onPressed: () async => await manager.updateTimeline( - timeline, TimelineRefreshType.refresh), - icon: Icon( - Icons.refresh, - )); - }), - IconButton( - onPressed: () { - Navigator.of(context).popUntil((route) { - return route.settings.name == ScreenPaths.timelines; - }); - }, - icon: const Icon(Icons.home), + StatusAndRefreshButton( + valueListenable: nss.timelineLoadingStatus, + refreshFunction: () async => await manager.updateTimeline( + timeline, TimelineRefreshType.refresh), + busyColor: Theme.of(context).colorScheme.background, ), ], ),