From 7ca3fe03c58afc1f0b74b5682ead8df31fbc96da Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sat, 8 Oct 2022 00:46:34 +0800 Subject: [PATCH] refactor: use list extension --- lib/screens/gh_trending.dart | 98 ++++++++++++++++++------------------ lib/utils/utils.dart | 35 ++++--------- lib/widgets/event_item.dart | 8 +-- lib/widgets/issue_item.dart | 4 +- lib/widgets/repo_header.dart | 4 +- 5 files changed, 66 insertions(+), 83 deletions(-) diff --git a/lib/screens/gh_trending.dart b/lib/screens/gh_trending.dart index 25bc577..fb13af1 100644 --- a/lib/screens/gh_trending.dart +++ b/lib/screens/gh_trending.dart @@ -30,60 +30,58 @@ class GhTrendingScreen extends StatelessWidget { bodyBuilder: (payload, activeTab) { return Column( crossAxisAlignment: CrossAxisAlignment.stretch, - children: join( - CommonStyle.border, - activeTab == 0 - ? [ - for (var v in payload.cast()) - RepoItem.gh( - owner: v.author, - avatarUrl: v.avatar, - name: v.name, - description: v.description, - starCount: v.stars ?? 0, - forkCount: v.forks ?? 0, - primaryLanguageName: v.language, - primaryLanguageColor: v.languageColor, - note: '${v.currentPeriodStars} stars today', - isPrivate: false, - isFork: false, // TODO: - ) - ] - : [ - for (var v in payload.cast()) - UserItem.github( - login: v.username, - name: v.name, - avatarUrl: v.avatar, - bio: v.repo == null - ? null - : LinkWidget( - url: '/github/${v.username}/${v.repo!.name}', - child: Row( - children: [ - Icon( - Octicons.repo, - size: 17, - color: AntTheme.of(context) - .colorTextSecondary, - ), - const SizedBox(width: 4), - Expanded( - child: Text( - '${v.username} / ${v.repo!.name}', - style: TextStyle( - fontSize: 17, + children: (activeTab == 0 + ? [ + for (var v in payload.cast()) + RepoItem.gh( + owner: v.author, + avatarUrl: v.avatar, + name: v.name, + description: v.description, + starCount: v.stars ?? 0, + forkCount: v.forks ?? 0, + primaryLanguageName: v.language, + primaryLanguageColor: v.languageColor, + note: '${v.currentPeriodStars} stars today', + isPrivate: false, + isFork: false, // TODO: + ) + ] + : [ + for (var v in payload.cast()) + UserItem.github( + login: v.username, + name: v.name, + avatarUrl: v.avatar, + bio: v.repo == null + ? null + : LinkWidget( + url: '/github/${v.username}/${v.repo!.name}', + child: Row( + children: [ + Icon( + Octicons.repo, + size: 17, color: AntTheme.of(context) .colorTextSecondary, ), - overflow: TextOverflow.ellipsis, - )) - ], + const SizedBox(width: 4), + Expanded( + child: Text( + '${v.username} / ${v.repo!.name}', + style: TextStyle( + fontSize: 17, + color: AntTheme.of(context) + .colorTextSecondary, + ), + overflow: TextOverflow.ellipsis, + )) + ], + ), ), - ), - ) - ], - ), + ) + ]) + .withSeparator(CommonStyle.border), ); }, ); diff --git a/lib/utils/utils.dart b/lib/utils/utils.dart index 4a02a8e..d8091fb 100644 --- a/lib/utils/utils.dart +++ b/lib/utils/utils.dart @@ -77,32 +77,17 @@ class GithubPalette { // final pageSize = 5; const kPageSize = 30; -List join(T seperator, List xs) { - final result = []; - xs.asMap().forEach((index, x) { - if (x == null) return; - - result.add(x); - if (index < xs.length - 1) { - result.add(seperator); +extension MyList on List { + List withSeparator(T separator) { + final result = []; + for (var i = 0; i < length; i++) { + if (i != 0) { + result.add(separator); + } + result.add(this[i]); } - }); - - return result; -} - -List joinAll(T seperator, List> xss) { - final result = []; - xss.asMap().forEach((index, x) { - if (x.isEmpty) return; - - result.addAll(x); - if (index < xss.length - 1) { - result.add(seperator); - } - }); - - return result; + return result; + } } final numberFormat = NumberFormat(); diff --git a/lib/widgets/event_item.dart b/lib/widgets/event_item.dart index f2ace08..f535b08 100644 --- a/lib/widgets/event_item.dart +++ b/lib/widgets/event_item.dart @@ -56,7 +56,7 @@ class EventItem extends StatelessWidget { Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, - children: join(const SizedBox(height: 6), [ + children: [ Text.rich( TextSpan( style: TextStyle( @@ -81,7 +81,7 @@ class EventItem extends StatelessWidget { ], ), if (card != null) card - ]), + ].withSeparator(const SizedBox(height: 6)), ), ), ], @@ -233,7 +233,7 @@ class EventItem extends StatelessWidget { borderRadius: const BorderRadius.all(Radius.circular(4))), child: Column( crossAxisAlignment: CrossAxisAlignment.start, - children: join(const SizedBox(height: 6), [ + children: [ Row( children: [ IssueIcon(state, size: 20), @@ -285,7 +285,7 @@ class EventItem extends StatelessWidget { ] ], ) - ]), + ].withSeparator(const SizedBox(height: 6)), ), ), ); diff --git a/lib/widgets/issue_item.dart b/lib/widgets/issue_item.dart index 48696bb..9687f09 100644 --- a/lib/widgets/issue_item.dart +++ b/lib/widgets/issue_item.dart @@ -68,7 +68,7 @@ class IssueItem extends StatelessWidget { Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, - children: join(const SizedBox(height: 8), [ + children: [ Text.rich( TextSpan( children: [ @@ -129,7 +129,7 @@ class IssueItem extends StatelessWidget { ], ), ) - ]), + ].withSeparator(const SizedBox(height: 8)), ), ), // Column( diff --git a/lib/widgets/repo_header.dart b/lib/widgets/repo_header.dart index 755e552..250c813 100644 --- a/lib/widgets/repo_header.dart +++ b/lib/widgets/repo_header.dart @@ -30,7 +30,7 @@ class RepoHeader extends StatelessWidget { padding: CommonStyle.padding, child: Column( crossAxisAlignment: CrossAxisAlignment.start, - children: join(const SizedBox(height: 12), [ + children: [ Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ @@ -73,7 +73,7 @@ class RepoHeader extends StatelessWidget { ), ), if (trailings != null) ...trailings! - ]), + ].withSeparator(const SizedBox(height: 12)), ), ); }