From 42495461f01a044193ea198cbbfad49b7902de83 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Tue, 20 Jun 2023 17:46:49 +0200 Subject: [PATCH] neon,neon_files,neon_news,neon_notes,neon_notifications: Use Result helper methods --- packages/neon/neon/lib/src/blocs/apps.dart | 11 ++++++----- .../neon/lib/src/pages/account_settings.dart | 10 +++++----- packages/neon/neon/lib/src/pages/home.dart | 4 ++-- packages/neon/neon/lib/src/pages/login.dart | 2 +- packages/neon/neon/lib/src/pages/settings.dart | 10 +++++----- .../neon/neon/lib/src/utils/account_options.dart | 4 ++-- .../neon/neon/lib/src/utils/request_manager.dart | 2 +- packages/neon/neon/lib/src/utils/result.dart | 2 +- .../neon/neon/lib/src/widgets/account_tile.dart | 4 ++-- .../neon/neon/lib/src/widgets/cached_image.dart | 2 +- .../neon/lib/src/widgets/drawer_destination.dart | 10 +++++----- .../neon/neon/lib/src/widgets/user_avatar.dart | 2 +- .../neon_files/lib/dialogs/choose_folder.dart | 2 +- .../neon_files/lib/widgets/browser_view.dart | 16 ++++++++-------- packages/neon/neon_news/lib/blocs/news.dart | 5 +++-- .../neon/neon_news/lib/dialogs/add_feed.dart | 6 +++--- .../neon_news/lib/widgets/articles_view.dart | 4 ++-- .../neon/neon_news/lib/widgets/feeds_view.dart | 8 ++++---- .../neon/neon_news/lib/widgets/folders_view.dart | 10 +++++----- .../neon/neon_notes/lib/dialogs/create_note.dart | 4 ++-- .../neon_notes/lib/dialogs/select_category.dart | 4 ++-- .../neon_notes/lib/widgets/categories_view.dart | 2 +- .../lib/blocs/notifications.dart | 4 ++-- 23 files changed, 65 insertions(+), 63 deletions(-) diff --git a/packages/neon/neon/lib/src/blocs/apps.dart b/packages/neon/neon/lib/src/blocs/apps.dart index 1f859b5f..4c77a54a 100644 --- a/packages/neon/neon/lib/src/blocs/apps.dart +++ b/packages/neon/neon/lib/src/blocs/apps.dart @@ -34,17 +34,17 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates }); appImplementations.listen((final result) { - if (result.data != null) { + if (result.hasData) { final options = _accountsBloc.getOptionsFor(_account); unawaited( options.initialApp.stream.first.then((var initialApp) async { if (initialApp == null) { - if (result.data!.find('files') != null) { + if (result.requireData.find('files') != null) { initialApp = 'files'; - } else if (result.data!.isNotEmpty) { + } else if (result.requireData.isNotEmpty) { // This should never happen, because the files app is always installed and can not be removed, but just in // case this changes at a later point. - initialApp = result.data!.first.id; + initialApp = result.requireData.first.id; } } if (!activeAppID.hasValue) { @@ -176,7 +176,8 @@ class AppsBloc extends InteractiveBloc implements AppsBlocEvents, AppsBlocStates @override Future setActiveApp(final String? appID) async { - if (appID != null && (await appImplementations.firstWhere((final a) => a.data != null)).data!.find(appID) != null) { + if (appID != null && + (await appImplementations.firstWhere((final a) => a.hasData)).requireData.find(appID) != null) { if (activeAppID.valueOrNull != appID) { activeAppID.add(appID); } diff --git a/packages/neon/neon/lib/src/pages/account_settings.dart b/packages/neon/neon/lib/src/pages/account_settings.dart index b35deda0..134f6105 100644 --- a/packages/neon/neon/lib/src/pages/account_settings.dart +++ b/packages/neon/neon/lib/src/pages/account_settings.dart @@ -71,9 +71,9 @@ class AccountSettingsPage extends StatelessWidget { title: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ - if (userDetails.data != null) ...[ + if (userDetails.hasData) ...[ LinearProgressIndicator( - value: userDetails.data!.quota.relative / 100, + value: userDetails.requireData.quota.relative / 100, backgroundColor: Theme.of(context).colorScheme.primary.withOpacity(0.3), ), const SizedBox( @@ -81,9 +81,9 @@ class AccountSettingsPage extends StatelessWidget { ), Text( AppLocalizations.of(context).accountOptionsQuotaUsedOf( - filesize(userDetails.data!.quota.used, 1), - filesize(userDetails.data!.quota.total, 1), - userDetails.data!.quota.relative.toString(), + filesize(userDetails.requireData.quota.used, 1), + filesize(userDetails.requireData.quota.total, 1), + userDetails.requireData.quota.relative.toString(), ), ), ], diff --git a/packages/neon/neon/lib/src/pages/home.dart b/packages/neon/neon/lib/src/pages/home.dart index 56a937ad..60b95377 100644 --- a/packages/neon/neon/lib/src/pages/home.dart +++ b/packages/neon/neon/lib/src/pages/home.dart @@ -109,8 +109,8 @@ class _HomePageState extends State { Widget body = Builder( builder: (final context) => Column( children: [ - if (appImplementations.data != null) ...[ - if (appImplementations.data!.isEmpty) ...[ + if (appImplementations.hasData) ...[ + if (appImplementations.requireData.isEmpty) ...[ Expanded( child: Center( child: Text( diff --git a/packages/neon/neon/lib/src/pages/login.dart b/packages/neon/neon/lib/src/pages/login.dart index f842a411..dc7976d6 100644 --- a/packages/neon/neon/lib/src/pages/login.dart +++ b/packages/neon/neon/lib/src/pages/login.dart @@ -137,7 +137,7 @@ class _LoginPageState extends State { }, ), actions: [ - if (serverConnectionStateSnapshot.data != null) ...[ + if (serverConnectionStateSnapshot.hasData) ...[ IconButton( onPressed: _loginBloc.refresh, tooltip: AppLocalizations.of(context).loginRestart, diff --git a/packages/neon/neon/lib/src/pages/settings.dart b/packages/neon/neon/lib/src/pages/settings.dart index a0f5bfde..050cf8ce 100644 --- a/packages/neon/neon/lib/src/pages/settings.dart +++ b/packages/neon/neon/lib/src/pages/settings.dart @@ -51,7 +51,7 @@ class _SettingsPageState extends State { appImplementations: appImplementations, accountSpecificOptions: { if (accountsSnapshot.hasData) ...{ - for (final account in accountsSnapshot.data!) ...{ + for (final account in accountsSnapshot.requireData) ...{ account: accountsBloc.getOptionsFor(account).options, }, }, @@ -114,8 +114,8 @@ class _SettingsPageState extends State { SettingsCategory( title: Text(AppLocalizations.of(context).optionsCategoryPushNotifications), tiles: [ - if (pushNotificationsEnabledEnabledSnapshot.data != null && - !pushNotificationsEnabledEnabledSnapshot.data!) ...[ + if (pushNotificationsEnabledEnabledSnapshot.hasData && + !pushNotificationsEnabledEnabledSnapshot.requireData) ...[ NeonTextSettingsTile( text: AppLocalizations.of(context).globalOptionsPushNotificationsEnabledDisabledNotice, style: const TextStyle( @@ -164,7 +164,7 @@ class _SettingsPageState extends State { SettingsCategory( title: Text(AppLocalizations.of(context).optionsCategoryAccounts), tiles: [ - if (accountsSnapshot.data!.length > 1) ...[ + if (accountsSnapshot.requireData.length > 1) ...[ CheckBoxSettingsTile( option: globalOptions.rememberLastUsedAccount, ), @@ -172,7 +172,7 @@ class _SettingsPageState extends State { option: globalOptions.initialAccount, ), ], - for (final account in accountsSnapshot.data!) ...[ + for (final account in accountsSnapshot.requireData) ...[ NeonAccountSettingsTile( account: account, onTap: () { diff --git a/packages/neon/neon/lib/src/utils/account_options.dart b/packages/neon/neon/lib/src/utils/account_options.dart index d14d4eca..708b2bfe 100644 --- a/packages/neon/neon/lib/src/utils/account_options.dart +++ b/packages/neon/neon/lib/src/utils/account_options.dart @@ -6,10 +6,10 @@ class AccountSpecificOptions { this._appsBloc, ) { _appsBloc.appImplementations.listen((final result) { - if (result.data != null) { + if (result.hasData) { _appIDsSubject.add({ null: (final context) => AppLocalizations.of(context).accountOptionsAutomatic, - for (final app in result.data!) ...{ + for (final app in result.requireData) ...{ app.id: app.name, }, }); diff --git a/packages/neon/neon/lib/src/utils/request_manager.dart b/packages/neon/neon/lib/src/utils/request_manager.dart index 55f4c9e6..41c6f838 100644 --- a/packages/neon/neon/lib/src/utils/request_manager.dart +++ b/packages/neon/neon/lib/src/utils/request_manager.dart @@ -63,7 +63,7 @@ class RequestManager { final bool emitEmptyCache, final int retries, ) async { - if (subject.valueOrNull?.data != null) { + if (subject.valueOrNull?.hasData ?? false) { subject.add( Result( subject.value.data, diff --git a/packages/neon/neon/lib/src/utils/result.dart b/packages/neon/neon/lib/src/utils/result.dart index c7955ae9..180b11ee 100644 --- a/packages/neon/neon/lib/src/utils/result.dart +++ b/packages/neon/neon/lib/src/utils/result.dart @@ -36,7 +36,7 @@ class Result { final bool isCached; Result transform(final R? Function(T data) call) => Result( - data != null ? call(data as T) : null, + hasData ? call(data as T) : null, error, isLoading: isLoading, isCached: isCached, diff --git a/packages/neon/neon/lib/src/widgets/account_tile.dart b/packages/neon/neon/lib/src/widgets/account_tile.dart index 7fa8d211..e03d3846 100644 --- a/packages/neon/neon/lib/src/widgets/account_tile.dart +++ b/packages/neon/neon/lib/src/widgets/account_tile.dart @@ -40,10 +40,10 @@ class NeonAccountTile extends StatelessWidget { stream: userDetailsBloc.userDetails, builder: (final context, final userDetails) => Row( children: [ - if (userDetails.data != null) ...[ + if (userDetails.hasData) ...[ Flexible( child: Text( - userDetails.data!.displayname, + userDetails.requireData.displayname, style: Theme.of(context).textTheme.bodyLarge!.copyWith( color: textColor, ), diff --git a/packages/neon/neon/lib/src/widgets/cached_image.dart b/packages/neon/neon/lib/src/widgets/cached_image.dart index 285f8200..0c6a73cf 100644 --- a/packages/neon/neon/lib/src/widgets/cached_image.dart +++ b/packages/neon/neon/lib/src/widgets/cached_image.dart @@ -35,7 +35,7 @@ class _NeonCachedImageState extends State { future: future, builder: (final context, final fileSnapshot) { if (fileSnapshot.hasData) { - final content = fileSnapshot.data!.readAsBytesSync(); + final content = fileSnapshot.requireData.readAsBytesSync(); try { // TODO: Is this safe enough? diff --git a/packages/neon/neon/lib/src/widgets/drawer_destination.dart b/packages/neon/neon/lib/src/widgets/drawer_destination.dart index 8d0824e2..fdeac234 100644 --- a/packages/neon/neon/lib/src/widgets/drawer_destination.dart +++ b/packages/neon/neon/lib/src/widgets/drawer_destination.dart @@ -35,7 +35,7 @@ extension NavigationRailDestinationExtension on NavigationRailDestination { builder: (final context, final snapshot) { final colorScheme = Theme.of(context).colorScheme; - final color = snapshot.data! > 0 ? colorScheme.primary : colorScheme.onBackground; + final color = snapshot.requireData > 0 ? colorScheme.primary : colorScheme.onBackground; const size = Size.square(kAvatarSize * 2 / 3); final icon = Container( @@ -43,7 +43,7 @@ extension NavigationRailDestinationExtension on NavigationRailDestination { child: neonDestination.icon(size: size, color: color), ); - if (snapshot.data! <= 0) { + if (snapshot.requireData <= 0) { return icon; } @@ -55,7 +55,7 @@ extension NavigationRailDestinationExtension on NavigationRailDestination { ); return Text( - snapshot.data!.toString(), + snapshot.requireData.toString(), style: style, ); }, @@ -87,7 +87,7 @@ extension NavigationDrawerDestinationExtension on NavigationDrawerDestination { builder: (final context, final snapshot) { final label = Text(neonDestination.label); - if (snapshot.data! <= 0) { + if (snapshot.requireData <= 0) { return label; } @@ -102,7 +102,7 @@ extension NavigationDrawerDestinationExtension on NavigationDrawerDestination { ); return Text( - snapshot.data!.toString(), + snapshot.requireData.toString(), style: style, ); }, diff --git a/packages/neon/neon/lib/src/widgets/user_avatar.dart b/packages/neon/neon/lib/src/widgets/user_avatar.dart index e8d3a205..4909dd9d 100644 --- a/packages/neon/neon/lib/src/widgets/user_avatar.dart +++ b/packages/neon/neon/lib/src/widgets/user_avatar.dart @@ -107,7 +107,7 @@ class _UserAvatarState extends State { fontSize: 16, ), ); - } else if (result.data != null) { + } else if (result.hasData) { decoration = BoxDecoration( shape: BoxShape.circle, color: _userStatusToColor(result.data!.status), diff --git a/packages/neon/neon_files/lib/dialogs/choose_folder.dart b/packages/neon/neon_files/lib/dialogs/choose_folder.dart index f0a48a38..9ca288fd 100644 --- a/packages/neon/neon_files/lib/dialogs/choose_folder.dart +++ b/packages/neon/neon_files/lib/dialogs/choose_folder.dart @@ -44,7 +44,7 @@ class FilesChooseFolderDialog extends StatelessWidget { builder: (final context) => const FilesCreateFolderDialog(), ); if (result != null) { - bloc.createFolder([...pathSnapshot.data!, ...result]); + bloc.createFolder([...pathSnapshot.requireData, ...result]); } }, child: Text(AppLocalizations.of(context).folderCreate), diff --git a/packages/neon/neon_files/lib/widgets/browser_view.dart b/packages/neon/neon_files/lib/widgets/browser_view.dart index 49fbc2df..61a0f450 100644 --- a/packages/neon/neon_files/lib/widgets/browser_view.dart +++ b/packages/neon/neon_files/lib/widgets/browser_view.dart @@ -46,7 +46,7 @@ class _FilesBrowserViewState extends State { ? Container() : BackButtonListener( onBackButtonPressed: () async { - final path = pathSnapshot.data!; + final path = pathSnapshot.requireData; if (path.isNotEmpty) { widget.bloc.setPath(path.sublist(0, path.length - 1)); return true; @@ -59,12 +59,12 @@ class _FilesBrowserViewState extends State { sortBoxOrderOption: widget.bloc.options.filesSortBoxOrderOption, input: files.data, builder: (final context, final sorted) => NeonListView( - scrollKey: 'files-${pathSnapshot.data!.join('/')}', + scrollKey: 'files-${pathSnapshot.requireData.join('/')}', withFloatingActionButton: true, items: [ for (final uploadTask in sorted == null ? [] - : uploadTasksSnapshot.data!.where( + : uploadTasksSnapshot.requireData.where( (final task) => sorted.where((final file) => _pathMatchesFile(task.path, file.name)).isEmpty, )) ...[ @@ -95,9 +95,9 @@ class _FilesBrowserViewState extends State { if (!widget.onlyShowDirectories || file.isDirectory) ...[ Builder( builder: (final context) { - final matchingUploadTasks = uploadTasksSnapshot.data! + final matchingUploadTasks = uploadTasksSnapshot.requireData .where((final task) => _pathMatchesFile(task.path, file.name)); - final matchingDownloadTasks = downloadTasksSnapshot.data! + final matchingDownloadTasks = downloadTasksSnapshot.requireData .where((final task) => _pathMatchesFile(task.path, file.name)); return StreamBuilder( @@ -165,10 +165,10 @@ class _FilesBrowserViewState extends State { widget.bloc.setPath([]); }, ), - for (var i = 0; i < pathSnapshot.data!.length; i++) ...[ + for (var i = 0; i < pathSnapshot.requireData.length; i++) ...[ Builder( builder: (final context) { - final path = pathSnapshot.data!.sublist(0, i + 1); + final path = pathSnapshot.requireData.sublist(0, i + 1); return Tooltip( message: AppLocalizations.of(context).goToPath(path.join('/')), excludeFromSemantics: true, @@ -177,7 +177,7 @@ class _FilesBrowserViewState extends State { widget.bloc.setPath(path); }, child: Text( - pathSnapshot.data![i], + pathSnapshot.requireData[i], semanticsLabel: AppLocalizations.of(context).goToPath(path.join('/')), ), ), diff --git a/packages/neon/neon_news/lib/blocs/news.dart b/packages/neon/neon_news/lib/blocs/news.dart index 1ab52fb2..2ce6f5fe 100644 --- a/packages/neon/neon_news/lib/blocs/news.dart +++ b/packages/neon/neon_news/lib/blocs/news.dart @@ -35,9 +35,10 @@ class NewsBloc extends InteractiveBloc implements NewsBlocEvents, NewsBlocStates this.client, ) { mainArticlesBloc.articles.listen((final result) { - if (result.data != null) { + if (result.hasData) { final type = mainArticlesBloc.filterType.valueOrNull; - unreadCounter.add(result.data!.where((final a) => type == FilterType.starred ? a.starred : a.unread).length); + unreadCounter + .add(result.requireData.where((final a) => type == FilterType.starred ? a.starred : a.unread).length); } }); diff --git a/packages/neon/neon_news/lib/dialogs/add_feed.dart b/packages/neon/neon_news/lib/dialogs/add_feed.dart index 63c0cda7..413a0c57 100644 --- a/packages/neon/neon_news/lib/dialogs/add_feed.dart +++ b/packages/neon/neon_news/lib/dialogs/add_feed.dart @@ -77,9 +77,9 @@ class _NewsAddFeedDialogState extends State { visible: folders.isLoading, ), ), - if (folders.data != null) ...[ + if (folders.hasData) ...[ NewsFolderSelect( - folders: folders.data!, + folders: folders.requireData, value: folder, onChanged: (final f) { setState(() { @@ -90,7 +90,7 @@ class _NewsAddFeedDialogState extends State { ], ], ElevatedButton( - onPressed: folders.data != null ? submit : null, + onPressed: folders.hasData ? submit : null, child: Text(AppLocalizations.of(context).feedAdd), ), ], diff --git a/packages/neon/neon_news/lib/widgets/articles_view.dart b/packages/neon/neon_news/lib/widgets/articles_view.dart index 49047bfb..cc846e32 100644 --- a/packages/neon/neon_news/lib/widgets/articles_view.dart +++ b/packages/neon/neon_news/lib/widgets/articles_view.dart @@ -36,7 +36,7 @@ class _NewsArticlesViewState extends State { input: articles.data, builder: (final context, final sorted) => NeonListView( scrollKey: 'news-articles', - items: feeds.data == null ? null : sorted, + items: feeds.hasData ? sorted : null, isLoading: articles.isLoading || feeds.isLoading, error: articles.error ?? feeds.error, onRefresh: () async { @@ -48,7 +48,7 @@ class _NewsArticlesViewState extends State { builder: (final context, final article) => _buildArticle( context, article, - feeds.data!.singleWhere((final feed) => feed.id == article.feedId), + feeds.requireData.singleWhere((final feed) => feed.id == article.feedId), ), topFixedChildren: [ StreamBuilder( diff --git a/packages/neon/neon_news/lib/widgets/feeds_view.dart b/packages/neon/neon_news/lib/widgets/feeds_view.dart index bff83a61..a96301ef 100644 --- a/packages/neon/neon_news/lib/widgets/feeds_view.dart +++ b/packages/neon/neon_news/lib/widgets/feeds_view.dart @@ -19,9 +19,9 @@ class NewsFeedsView extends StatelessWidget { sortBox: feedsSortBox, sortPropertyOption: bloc.options.feedsSortPropertyOption, sortBoxOrderOption: bloc.options.feedsSortBoxOrderOption, - input: folders.data == null - ? null - : feeds.data?.where((final f) => folderID == null || f.folderId == folderID).toList(), + input: folders.hasData + ? feeds.data?.where((final f) => folderID == null || f.folderId == folderID).toList() + : null, builder: (final context, final sorted) => NeonListView( scrollKey: 'news-feeds', withFloatingActionButton: true, @@ -32,7 +32,7 @@ class NewsFeedsView extends StatelessWidget { builder: (final context, final feed) => _buildFeed( context, feed, - folders.data!, + folders.requireData, ), ), ), diff --git a/packages/neon/neon_news/lib/widgets/folders_view.dart b/packages/neon/neon_news/lib/widgets/folders_view.dart index 47418cdc..645f0e68 100644 --- a/packages/neon/neon_news/lib/widgets/folders_view.dart +++ b/packages/neon/neon_news/lib/widgets/folders_view.dart @@ -17,15 +17,15 @@ class NewsFoldersView extends StatelessWidget { sortBox: foldersSortBox, sortPropertyOption: bloc.options.foldersSortPropertyOption, sortBoxOrderOption: bloc.options.foldersSortBoxOrderOption, - input: feeds.data == null - ? null - : folders.data?.map((final folder) { - final feedsInFolder = feeds.data!.where((final feed) => feed.folderId == folder.id); + input: feeds.hasData + ? folders.data?.map((final folder) { + final feedsInFolder = feeds.requireData.where((final feed) => feed.folderId == folder.id); final feedCount = feedsInFolder.length; final unreadCount = feedsInFolder.fold(0, (final a, final b) => a + b.unreadCount!); return (folder, feedCount, unreadCount); - }).toList(), + }).toList() + : null, builder: (final context, final sorted) => NeonListView( scrollKey: 'news-folders', withFloatingActionButton: true, diff --git a/packages/neon/neon_notes/lib/dialogs/create_note.dart b/packages/neon/neon_notes/lib/dialogs/create_note.dart index 715d56e0..d30a1fc2 100644 --- a/packages/neon/neon_notes/lib/dialogs/create_note.dart +++ b/packages/neon/neon_notes/lib/dialogs/create_note.dart @@ -59,9 +59,9 @@ class _NotesCreateNoteDialogState extends State { visible: notes.isLoading, ), ), - if (notes.data != null) ...[ + if (notes.hasData) ...[ NotesCategorySelect( - categories: notes.data!.map((final note) => note.category).toSet().toList(), + categories: notes.requireData.map((final note) => note.category).toSet().toList(), onChanged: (final category) { selectedCategory = category; }, diff --git a/packages/neon/neon_notes/lib/dialogs/select_category.dart b/packages/neon/neon_notes/lib/dialogs/select_category.dart index 85623fda..09fea8a8 100644 --- a/packages/neon/neon_notes/lib/dialogs/select_category.dart +++ b/packages/neon/neon_notes/lib/dialogs/select_category.dart @@ -47,9 +47,9 @@ class _NotesSelectCategoryDialogState extends State { visible: notes.isLoading, ), ), - if (notes.data != null) ...[ + if (notes.hasData) ...[ NotesCategorySelect( - categories: notes.data!.map((final note) => note.category).toSet().toList(), + categories: notes.requireData.map((final note) => note.category).toSet().toList(), initialValue: widget.initialCategory, onChanged: (final category) { selectedCategory = category; diff --git a/packages/neon/neon_notes/lib/widgets/categories_view.dart b/packages/neon/neon_notes/lib/widgets/categories_view.dart index a8dfbfc1..177343d3 100644 --- a/packages/neon/neon_notes/lib/widgets/categories_view.dart +++ b/packages/neon/neon_notes/lib/widgets/categories_view.dart @@ -21,7 +21,7 @@ class NotesCategoriesView extends StatelessWidget { .map( (final category) => NoteCategory( category, - notes.data!.where((final note) => note.category == category).length, + notes.requireData.where((final note) => note.category == category).length, ), ) .toList(), diff --git a/packages/neon/neon_notifications/lib/blocs/notifications.dart b/packages/neon/neon_notifications/lib/blocs/notifications.dart index b52cf9ca..c47da43b 100644 --- a/packages/neon/neon_notifications/lib/blocs/notifications.dart +++ b/packages/neon/neon_notifications/lib/blocs/notifications.dart @@ -20,8 +20,8 @@ class NotificationsBloc extends InteractiveBloc this._client, ) { notifications.listen((final result) { - if (result.data != null) { - unreadCounter.add(result.data!.length); + if (result.hasData) { + unreadCounter.add(result.requireData.length); } });