From 810e1eb4def80bc5e6c10ff0de23691373966319 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sat, 2 Nov 2019 01:12:17 +0800 Subject: [PATCH] feat: add gitlab todos and issue --- lib/models/auth.dart | 7 +++ lib/screens/gitlab/issue.dart | 85 +++++++++++++++++++++++++++++++++++ lib/screens/gitlab/todos.dart | 69 ++++++++++++++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 lib/screens/gitlab/issue.dart create mode 100644 lib/screens/gitlab/todos.dart diff --git a/lib/models/auth.dart b/lib/models/auth.dart index 97f887d..757240b 100644 --- a/lib/models/auth.dart +++ b/lib/models/auth.dart @@ -131,6 +131,13 @@ class AuthModel with ChangeNotifier { } } + Future fetchGitlab(String p) async { + final res = await http.get(activeAccount.domain + '/api/v4' + p, + headers: {'Private-Token': token}); + final info = json.decode(utf8.decode(res.bodyBytes)); + return info; + } + void init() async { // Listen scheme _sub = getUriLinksStream().listen(_onSchemeDetected, onError: (err) { diff --git a/lib/screens/gitlab/issue.dart b/lib/screens/gitlab/issue.dart new file mode 100644 index 0000000..aa24370 --- /dev/null +++ b/lib/screens/gitlab/issue.dart @@ -0,0 +1,85 @@ +import 'package:flutter/material.dart'; +import 'package:git_touch/scaffolds/refresh_stateful.dart'; +import 'package:git_touch/utils/utils.dart'; +import 'package:git_touch/widgets/avatar.dart'; +import 'package:git_touch/widgets/markdown_view.dart'; +import 'package:provider/provider.dart'; +import 'package:git_touch/models/auth.dart'; +import 'package:timeago/timeago.dart' as timeago; + +class GitlabIssueScreen extends StatelessWidget { + final int projectId; + final int issueIid; + final bool isMr; + + GitlabIssueScreen(this.projectId, this.issueIid, {this.isMr}); + + @override + Widget build(BuildContext context) { + return RefreshStatefulScaffold( + title: Text('Issue #$issueIid'), + fetchData: () async { + final type = isMr ? 'merge_requests' : 'issues'; + final items = await Future.wait([ + Provider.of(context) + .fetchGitlab('/projects/$projectId/$type/$issueIid'), + Provider.of(context) + .fetchGitlab('/projects/$projectId/$type/$issueIid/notes'), + Provider.of(context) + .fetchGitlab('/projects/$projectId/$type/$issueIid/award_emoji'), + ]); + return items; + }, + bodyBuilder: (payload) { + final data = payload.data[0]; + final notes = payload.data[1] as List; + final emoji = payload.data[2]; + + return Column( + children: [ + Container( + padding: CommonStyle.padding, + child: Column( + children: [ + Text(data['title']), + Row( + children: [ + Avatar.medium(url: data['author']['avatar_url']), + Expanded( + child: Text(data['description']), + ), + ], + ), + Text(timeago.format(DateTime.parse(data['created_at']))) + ], + ), + ), + CommonStyle.border, + Column( + children: notes.map((note) { + return Container( + padding: CommonStyle.padding, + child: Column( + children: [ + Row( + children: [ + Avatar.medium(url: note['author']['avatar_url']), + Expanded( + child: Column( + children: [Text(note['author']['name'])], + ), + ) + ], + ), + MarkdownView(note['body']), + ], + ), + ); + }).toList(), + ) + ], + ); + }, + ); + } +} diff --git a/lib/screens/gitlab/todos.dart b/lib/screens/gitlab/todos.dart new file mode 100644 index 0000000..95417af --- /dev/null +++ b/lib/screens/gitlab/todos.dart @@ -0,0 +1,69 @@ +import 'package:flutter/material.dart'; +import 'package:git_touch/models/auth.dart'; +import 'package:git_touch/scaffolds/refresh_stateful.dart'; +import 'package:git_touch/screens/gitlab/issue.dart'; +import 'package:git_touch/utils/utils.dart'; +import 'package:git_touch/widgets/avatar.dart'; +import 'package:git_touch/widgets/link.dart'; +import 'package:primer/primer.dart'; +import 'package:provider/provider.dart'; + +class GitlabTodosScreen extends StatelessWidget { + @override + Widget build(BuildContext context) { + return RefreshStatefulScaffold( + title: Text('Todos'), + fetchData: () { + return Provider.of(context).fetchGitlab('/todos'); + }, + bodyBuilder: (payload) { + return Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: (payload.data as List).map((item) { + return Link( + screenBuilder: (_) => GitlabIssueScreen( + item['target']['project_id'], item['target']['iid'], + isMr: item['target_type'] == 'MergeRequest'), + child: Container( + padding: CommonStyle.padding, + child: Row( + children: [ + Avatar.medium( + url: item['author']['avatar_url'], + ), + SizedBox(width: 12), + Expanded( + child: Text.rich( + TextSpan( + children: [ + TextSpan( + text: item['author']['name'], + style: TextStyle( + color: PrimerColors.blue500, + fontWeight: FontWeight.w500, + ), + ), + TextSpan( + text: ' ' + + item['action_name'] + + ' you ' + + item['target_type'] + + ' ' + + item['project']['path_with_namespace'] + + ' ' + + item['target']['iid'].toString(), + ), + ], + ), + ), + ), + ], + ), + ), + ); + }).toList(), + ); + }, + ); + } +}