refactor: repository item

This commit is contained in:
Rongjian Zhang 2019-11-02 17:50:04 +08:00
parent c6c5465ad3
commit bbb0bab1bf
2 changed files with 58 additions and 46 deletions

View File

@ -43,22 +43,7 @@ class GitlabUserScreen extends StatelessWidget {
BorderView(height: 10), BorderView(height: 10),
Column( Column(
children: projects.map((project) { children: projects.map((project) {
return RepositoryItem({ return RepositoryItem.gitlab(project);
'owner': {
'__typename': 'user',
'login': project['owner']['name'],
'avatarUrl': project['owner']['avatar_url'],
},
'name': project['name'],
'description': project['description'],
'isPrivate': project['visibility'] == 'private',
'isFork': false,
'stargazers': {'totalCount': project['star_count']},
'forks': {
'totalCount': project['forks_count'],
},
'primaryLanguage': null
});
}).toList(), }).toList(),
) )
], ],

View File

@ -30,12 +30,54 @@ primaryLanguage {
'''; ''';
class RepositoryItem extends StatelessWidget { class RepositoryItem extends StatelessWidget {
final Map<String, dynamic> payload; final String owner;
final String avatarUrl;
final String name;
final String description;
final IconData iconData;
final int starCount;
final int forkCount;
final String primaryLanguageName;
final String primaryLanguageColor;
final Widget Function(BuildContext context) screenBuilder;
final bool inRepoScreen; final bool inRepoScreen;
final List topics;
RepositoryItem(this.payload, {this.inRepoScreen = false}); RepositoryItem(payload, {this.inRepoScreen = false})
: this.owner = payload['owner']['login'],
this.avatarUrl = payload['owner']['avatarUrl'],
this.name = payload['name'],
this.description = payload['description'],
this.iconData = _buildIconData(payload),
this.starCount = payload['stargazers']['totalCount'],
this.forkCount = payload['forks']['totalCount'],
this.primaryLanguageName = payload['primaryLanguage'] == null
? null
: payload['primaryLanguage']['name'],
this.primaryLanguageColor = payload['primaryLanguage'] == null
? null
: payload['primaryLanguage']['color'],
this.screenBuilder = ((_) =>
RepositoryScreen(payload['owner']['login'], payload['name'])),
this.topics = payload['repositoryTopics'] == null
? []
: payload['repositoryTopics']['nodes'];
IconData _buildIconData() { RepositoryItem.gitlab(payload, {this.inRepoScreen = false})
: this.owner = payload['namespace']['name'],
this.avatarUrl = payload['owner']['avatar_url'],
this.name = payload['name'],
this.description = payload['description'],
this.iconData = Octicons.repo,
this.starCount = payload['star_count'],
this.forkCount = payload['forks_count'],
this.primaryLanguageName = null,
this.primaryLanguageColor = null,
this.screenBuilder = ((_) =>
RepositoryScreen(payload['owner']['login'], payload['name'])),
this.topics = [];
static IconData _buildIconData(payload) {
if (payload['isPrivate']) { if (payload['isPrivate']) {
return Octicons.lock; return Octicons.lock;
} }
@ -60,16 +102,12 @@ class RepositoryItem extends StatelessWidget {
width: 10, width: 10,
height: 10, height: 10,
decoration: BoxDecoration( decoration: BoxDecoration(
color: convertColor(payload['primaryLanguage'] == null color: convertColor(primaryLanguageColor),
? null
: payload['primaryLanguage']['color']),
shape: BoxShape.circle, shape: BoxShape.circle,
), ),
), ),
SizedBox(width: 4), SizedBox(width: 4),
Text(payload['primaryLanguage'] == null Text(primaryLanguageName ?? 'Unknown'),
? 'Unknown'
: payload['primaryLanguage']['name']),
]), ]),
), ),
Expanded( Expanded(
@ -77,7 +115,7 @@ class RepositoryItem extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[
Icon(Octicons.star, size: 14, color: PrimerColors.gray600), Icon(Octicons.star, size: 14, color: PrimerColors.gray600),
Text(numberFormat.format(payload['stargazers']['totalCount'])), Text(numberFormat.format(starCount)),
], ],
), ),
), ),
@ -87,7 +125,7 @@ class RepositoryItem extends StatelessWidget {
children: <Widget>[ children: <Widget>[
Icon(Octicons.repo_forked, Icon(Octicons.repo_forked,
size: 14, color: PrimerColors.gray600), size: 14, color: PrimerColors.gray600),
Text(numberFormat.format(payload['forks']['totalCount'])), Text(numberFormat.format(forkCount)),
], ],
), ),
), ),
@ -101,7 +139,7 @@ class RepositoryItem extends StatelessWidget {
return Wrap( return Wrap(
spacing: 4, spacing: 4,
runSpacing: 4, runSpacing: 4,
children: (payload['repositoryTopics']['nodes'] as List).map((node) { children: topics.map((node) {
return Container( return Container(
padding: EdgeInsets.symmetric(vertical: 4, horizontal: 8), padding: EdgeInsets.symmetric(vertical: 4, horizontal: 8),
decoration: BoxDecoration( decoration: BoxDecoration(
@ -124,22 +162,13 @@ class RepositoryItem extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
// TODO: text style // TODO: text style
return Link( return Link(
screenBuilder: inRepoScreen screenBuilder: inRepoScreen ? null : screenBuilder,
? null
: (_) => RepositoryScreen(payload['owner']['login'], payload['name']),
child: Container( child: Container(
padding: CommonStyle.padding, padding: CommonStyle.padding,
child: Row( child: Row(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[
Link( Avatar.small(url: avatarUrl),
child: Avatar.small(url: payload['owner']['avatarUrl']),
screenBuilder: (_) => UserScreen(
payload['owner']['login'],
isOrganization:
payload['owner']['__typename'] == 'Organization',
),
),
SizedBox(width: 8), SizedBox(width: 8),
Expanded( Expanded(
child: Column( child: Column(
@ -149,7 +178,7 @@ class RepositoryItem extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[
Text( Text(
payload['owner']['login'] + ' / ', owner + ' / ',
style: TextStyle( style: TextStyle(
fontSize: inRepoScreen ? 18 : 16, fontSize: inRepoScreen ? 18 : 16,
color: PrimerColors.blue500, color: PrimerColors.blue500,
@ -157,7 +186,7 @@ class RepositoryItem extends StatelessWidget {
), ),
), ),
Text( Text(
payload['name'], name,
style: TextStyle( style: TextStyle(
fontSize: inRepoScreen ? 18 : 16, fontSize: inRepoScreen ? 18 : 16,
color: PrimerColors.blue500, color: PrimerColors.blue500,
@ -165,14 +194,12 @@ class RepositoryItem extends StatelessWidget {
), ),
), ),
Expanded(child: Container()), Expanded(child: Container()),
Icon(_buildIconData(), Icon(iconData, size: 18, color: PrimerColors.gray600),
size: 18, color: PrimerColors.gray600),
], ],
), ),
if (payload['description'] != null && if (description != null && description.isNotEmpty)
(payload['description'] as String).isNotEmpty)
Text( Text(
payload['description'], description,
style: TextStyle( style: TextStyle(
color: PrimerColors.gray700, color: PrimerColors.gray700,
fontSize: inRepoScreen ? 15 : 14), fontSize: inRepoScreen ? 15 : 14),