From d45eea833938912baeea01f57ac287f0d30b8688 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 13 Jun 2023 17:19:18 +0200 Subject: [PATCH 1/4] neon_news: simplify feedsUnreadCountSum --- packages/neon/neon_news/lib/sort/folders.dart | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/neon/neon_news/lib/sort/folders.dart b/packages/neon/neon_news/lib/sort/folders.dart index 2797a079..b769ad8b 100644 --- a/packages/neon/neon_news/lib/sort/folders.dart +++ b/packages/neon/neon_news/lib/sort/folders.dart @@ -21,9 +21,4 @@ class FolderFeedsWrapper { final List feeds; } -int feedsUnreadCountSum(final List feeds) => [ - 0, // Fixes error when no feeds are in the folder - ...feeds.map((final f) => f.unreadCount!), - ].reduce( - (final a, final b) => a + b, - ); +int feedsUnreadCountSum(final List feeds) => feeds.fold(0, (final a, final b) => a + b.unreadCount!); From 7fa3a8e6f5db282b1840a98c96adf25c91b7f4ea Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 13 Jun 2023 17:19:18 +0200 Subject: [PATCH 2/4] neon_news: make feedsUnreadCountSum a property of FolderFeedsWrapper --- packages/neon/neon_news/lib/sort/folders.dart | 5 ++--- packages/neon/neon_news/lib/widgets/folders_view.dart | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/neon/neon_news/lib/sort/folders.dart b/packages/neon/neon_news/lib/sort/folders.dart index b769ad8b..05cf9813 100644 --- a/packages/neon/neon_news/lib/sort/folders.dart +++ b/packages/neon/neon_news/lib/sort/folders.dart @@ -3,7 +3,7 @@ part of '../neon_news.dart'; final foldersSortBox = SortBox( { FoldersSortProperty.alphabetical: (final folderFeedsWrapper) => folderFeedsWrapper.folder.name.toLowerCase(), - FoldersSortProperty.unreadCount: (final folderFeedsWrapper) => feedsUnreadCountSum(folderFeedsWrapper.feeds), + FoldersSortProperty.unreadCount: (final folderFeedsWrapper) => folderFeedsWrapper.feedsUnreadCountSum, }, { FoldersSortProperty.alphabetical: Box(FoldersSortProperty.unreadCount, SortBoxOrder.descending), @@ -19,6 +19,5 @@ class FolderFeedsWrapper { final NextcloudNewsFolder folder; final List feeds; + int get feedsUnreadCountSum => feeds.fold(0, (final a, final b) => a + b.unreadCount!); } - -int feedsUnreadCountSum(final List feeds) => feeds.fold(0, (final a, final b) => a + b.unreadCount!); diff --git a/packages/neon/neon_news/lib/widgets/folders_view.dart b/packages/neon/neon_news/lib/widgets/folders_view.dart index df8e457d..6b351aee 100644 --- a/packages/neon/neon_news/lib/widgets/folders_view.dart +++ b/packages/neon/neon_news/lib/widgets/folders_view.dart @@ -44,7 +44,7 @@ class NewsFoldersView extends StatelessWidget { final BuildContext context, final FolderFeedsWrapper folderFeedsWrapper, ) { - final unreadCount = feedsUnreadCountSum(folderFeedsWrapper.feeds); + final unreadCount = folderFeedsWrapper.feedsUnreadCountSum; return ListTile( title: Text( folderFeedsWrapper.folder.name, From ec862b7a1fa0819455496efd75851674bfc835b1 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 13 Jun 2023 17:19:18 +0200 Subject: [PATCH 3/4] neon_news: make FolderFeedsWrapper a record --- packages/neon/neon_news/lib/sort/folders.dart | 15 ++------- .../neon_news/lib/widgets/folders_view.dart | 32 +++++++++---------- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/packages/neon/neon_news/lib/sort/folders.dart b/packages/neon/neon_news/lib/sort/folders.dart index 05cf9813..50ff603d 100644 --- a/packages/neon/neon_news/lib/sort/folders.dart +++ b/packages/neon/neon_news/lib/sort/folders.dart @@ -2,8 +2,8 @@ part of '../neon_news.dart'; final foldersSortBox = SortBox( { - FoldersSortProperty.alphabetical: (final folderFeedsWrapper) => folderFeedsWrapper.folder.name.toLowerCase(), - FoldersSortProperty.unreadCount: (final folderFeedsWrapper) => folderFeedsWrapper.feedsUnreadCountSum, + FoldersSortProperty.alphabetical: (final folderFeedsWrapper) => folderFeedsWrapper.$1.name.toLowerCase(), + FoldersSortProperty.unreadCount: (final folderFeedsWrapper) => folderFeedsWrapper.$3, }, { FoldersSortProperty.alphabetical: Box(FoldersSortProperty.unreadCount, SortBoxOrder.descending), @@ -11,13 +11,4 @@ final foldersSortBox = SortBox( }, ); -class FolderFeedsWrapper { - FolderFeedsWrapper( - this.folder, - this.feeds, - ); - - final NextcloudNewsFolder folder; - final List feeds; - int get feedsUnreadCountSum => feeds.fold(0, (final a, final b) => a + b.unreadCount!); -} +typedef FolderFeedsWrapper = (NextcloudNewsFolder folder, List feeds, int unreadCount); diff --git a/packages/neon/neon_news/lib/widgets/folders_view.dart b/packages/neon/neon_news/lib/widgets/folders_view.dart index 6b351aee..2678ec71 100644 --- a/packages/neon/neon_news/lib/widgets/folders_view.dart +++ b/packages/neon/neon_news/lib/widgets/folders_view.dart @@ -19,14 +19,12 @@ class NewsFoldersView extends StatelessWidget { sortBoxOrderOption: bloc.options.foldersSortBoxOrderOption, input: feeds.data == null ? null - : folders.data - ?.map( - (final folder) => FolderFeedsWrapper( - folder, - feeds.data!.where((final feed) => feed.folderId == folder.id).toList(), - ), - ) - .toList(), + : folders.data?.map((final folder) { + final feedsInFolder = feeds.data!.where((final feed) => feed.folderId == folder.id).toList(); + final unreadCount = feedsInFolder.fold(0, (final a, final b) => a + b.unreadCount!); + + return (folder, feedsInFolder, unreadCount); + }).toList(), builder: (final context, final sorted) => NeonListView( scrollKey: 'news-folders', withFloatingActionButton: true, @@ -44,10 +42,10 @@ class NewsFoldersView extends StatelessWidget { final BuildContext context, final FolderFeedsWrapper folderFeedsWrapper, ) { - final unreadCount = folderFeedsWrapper.feedsUnreadCountSum; + final (folder, feeds, unreadCount) = folderFeedsWrapper; return ListTile( title: Text( - folderFeedsWrapper.folder.name, + folder.name, style: unreadCount == 0 ? Theme.of(context).textTheme.titleMedium!.copyWith(color: Theme.of(context).disabledColor) : null, @@ -68,7 +66,7 @@ class NewsFoldersView extends StatelessWidget { ), Center( child: Text( - folderFeedsWrapper.feeds.length.toString(), + feeds.length.toString(), style: TextStyle( color: Theme.of(context).colorScheme.onPrimary, ), @@ -95,19 +93,19 @@ class NewsFoldersView extends StatelessWidget { if (await showConfirmationDialog( context, // ignore: use_build_context_synchronously - AppLocalizations.of(context).folderDeleteConfirm(folderFeedsWrapper.folder.name), + AppLocalizations.of(context).folderDeleteConfirm(folder.name), )) { - bloc.deleteFolder(folderFeedsWrapper.folder.id); + bloc.deleteFolder(folder.id); } break; case NewsFolderAction.rename: final result = await showRenameDialog( context: context, title: AppLocalizations.of(context).folderRename, - value: folderFeedsWrapper.folder.name, + value: folder.name, ); if (result != null) { - bloc.renameFolder(folderFeedsWrapper.folder.id, result); + bloc.renameFolder(folder.id, result); } break; } @@ -115,7 +113,7 @@ class NewsFoldersView extends StatelessWidget { ), onLongPress: () { if (unreadCount > 0) { - bloc.markFolderAsRead(folderFeedsWrapper.folder.id); + bloc.markFolderAsRead(folder.id); } }, onTap: () async { @@ -123,7 +121,7 @@ class NewsFoldersView extends StatelessWidget { MaterialPageRoute( builder: (final context) => NewsFolderPage( bloc: bloc, - folder: folderFeedsWrapper.folder, + folder: folder, ), ), ); From be71e9ce43db53fd4e1a13ee7736462918dd0da6 Mon Sep 17 00:00:00 2001 From: Nikolas Rimikis Date: Tue, 13 Jun 2023 17:19:19 +0200 Subject: [PATCH 4/4] neon_news: simplify FolderFeedsWrapper interface --- packages/neon/neon_news/lib/sort/folders.dart | 2 +- packages/neon/neon_news/lib/widgets/folders_view.dart | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/packages/neon/neon_news/lib/sort/folders.dart b/packages/neon/neon_news/lib/sort/folders.dart index 50ff603d..435cc6a5 100644 --- a/packages/neon/neon_news/lib/sort/folders.dart +++ b/packages/neon/neon_news/lib/sort/folders.dart @@ -11,4 +11,4 @@ final foldersSortBox = SortBox( }, ); -typedef FolderFeedsWrapper = (NextcloudNewsFolder folder, List feeds, int unreadCount); +typedef FolderFeedsWrapper = (NextcloudNewsFolder folder, int feedCount, int unreadCount); diff --git a/packages/neon/neon_news/lib/widgets/folders_view.dart b/packages/neon/neon_news/lib/widgets/folders_view.dart index 2678ec71..6839df97 100644 --- a/packages/neon/neon_news/lib/widgets/folders_view.dart +++ b/packages/neon/neon_news/lib/widgets/folders_view.dart @@ -20,10 +20,11 @@ class NewsFoldersView extends StatelessWidget { input: feeds.data == null ? null : folders.data?.map((final folder) { - final feedsInFolder = feeds.data!.where((final feed) => feed.folderId == folder.id).toList(); + final feedsInFolder = feeds.data!.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, feedsInFolder, unreadCount); + return (folder, feedCount, unreadCount); }).toList(), builder: (final context, final sorted) => NeonListView( scrollKey: 'news-folders', @@ -42,7 +43,7 @@ class NewsFoldersView extends StatelessWidget { final BuildContext context, final FolderFeedsWrapper folderFeedsWrapper, ) { - final (folder, feeds, unreadCount) = folderFeedsWrapper; + final (folder, feedCount, unreadCount) = folderFeedsWrapper; return ListTile( title: Text( folder.name, @@ -66,7 +67,7 @@ class NewsFoldersView extends StatelessWidget { ), Center( child: Text( - feeds.length.toString(), + feedCount.toString(), style: TextStyle( color: Theme.of(context).colorScheme.onPrimary, ),