From 4ea30d0132a2d54082aeb70f8ecf78c1354495fe Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sat, 29 Feb 2020 17:06:47 +0800 Subject: [PATCH] feat(gl): commit screen --- lib/models/gitlab.dart | 10 +++++++++ lib/models/gitlab.g.dart | 14 ++++++++++++ lib/router.dart | 6 ++++++ lib/screens/gl_commit.dart | 44 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 lib/screens/gl_commit.dart diff --git a/lib/models/gitlab.dart b/lib/models/gitlab.dart index baff444..e8ccdaa 100644 --- a/lib/models/gitlab.dart +++ b/lib/models/gitlab.dart @@ -178,6 +178,16 @@ class GitlabCommit { _$GitlabCommitFromJson(json); } +@JsonSerializable(fieldRename: FieldRename.snake) +class GitlabDiff { + String diff; + String newPath; + String oldPath; + GitlabDiff(); + factory GitlabDiff.fromJson(Map json) => + _$GitlabDiffFromJson(json); +} + @JsonSerializable(fieldRename: FieldRename.snake) class GitlabIssue { String title; diff --git a/lib/models/gitlab.g.dart b/lib/models/gitlab.g.dart index 1b55158..d8a33c8 100644 --- a/lib/models/gitlab.g.dart +++ b/lib/models/gitlab.g.dart @@ -309,6 +309,20 @@ Map _$GitlabCommitToJson(GitlabCommit instance) => 'message': instance.message, }; +GitlabDiff _$GitlabDiffFromJson(Map json) { + return GitlabDiff() + ..diff = json['diff'] as String + ..newPath = json['new_path'] as String + ..oldPath = json['old_path'] as String; +} + +Map _$GitlabDiffToJson(GitlabDiff instance) => + { + 'diff': instance.diff, + 'new_path': instance.newPath, + 'old_path': instance.oldPath, + }; + GitlabIssue _$GitlabIssueFromJson(Map json) { return GitlabIssue() ..title = json['title'] as String diff --git a/lib/router.dart b/lib/router.dart index 3b70deb..ebab84c 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -6,6 +6,7 @@ import 'package:git_touch/screens/bb_user.dart'; import 'package:git_touch/screens/code_theme.dart'; import 'package:git_touch/screens/gh_commits.dart'; import 'package:git_touch/screens/gh_org_repos.dart'; +import 'package:git_touch/screens/gl_commit.dart'; import 'package:git_touch/screens/gt_commits.dart'; import 'package:git_touch/screens/gt_issues.dart'; import 'package:git_touch/screens/gt_object.dart'; @@ -146,6 +147,7 @@ class GitlabRouter { GitlabRouter.issues, GitlabRouter.mergeRequests, GitlabRouter.commits, + GitlabRouter.commit, GitlabRouter.projectMembers, GitlabRouter.groupMembers, GitlabRouter.issue, @@ -182,6 +184,10 @@ class GitlabRouter { '/projects/:id/commits', (context, params) => GlCommitsScreen(params['id'].first, prefix: params['prefix'].first)); + static final commit = RouterScreen( + '/projects/:id/commit/:sha', + (context, params) => + GlCommitScreen(params['id'].first, sha: params['sha'].first)); static final projectMembers = RouterScreen( '/projects/:id/members', (context, parameters) => diff --git a/lib/screens/gl_commit.dart b/lib/screens/gl_commit.dart new file mode 100644 index 0000000..3422912 --- /dev/null +++ b/lib/screens/gl_commit.dart @@ -0,0 +1,44 @@ +import 'package:flutter/material.dart'; +import 'package:git_touch/models/auth.dart'; +import 'package:git_touch/models/gitlab.dart'; +import 'package:git_touch/models/code.dart'; +import 'package:git_touch/scaffolds/refresh_stateful.dart'; +import 'package:git_touch/widgets/app_bar_title.dart'; +import 'package:provider/provider.dart'; + +class GlCommitScreen extends StatelessWidget { + final String id; + final String sha; + GlCommitScreen(this.id, {this.sha}); + + Future> _query(BuildContext context, [int page = 1]) async { + final auth = Provider.of(context); + final res = await auth + .fetchGitlabWithPage('/projects/$id/repository/commits/$sha/diff'); + return (res.data as List).map((v) => GitlabDiff.fromJson(v)).toList(); + } + + @override + Widget build(BuildContext context) { + final code = Provider.of(context); + + return RefreshStatefulScaffold>( + title: AppBarTitle('Commits'), + fetchData: () => _query(context), + bodyBuilder: (items, _) { + return Column( + children: [ + for (var item in items) + Text( + item.diff, + style: TextStyle( + fontFamily: code.fontFamilyUsed, + fontSize: 14, + ), + ) + ], + ); + }, + ); + } +}