diff --git a/lib/models/github.dart b/lib/models/github.dart index 48432d6..445853c 100644 --- a/lib/models/github.dart +++ b/lib/models/github.dart @@ -350,3 +350,14 @@ class GithubFilesItem { factory GithubFilesItem.fromJson(Map json) => _$GithubFilesItemFromJson(json); } + +@JsonSerializable(fieldRename: FieldRename.snake) +class GithubComparisonItem { + List files; + String status; + int aheadBy; + int behindBy; + GithubComparisonItem(); + factory GithubComparisonItem.fromJson(Map json) => + _$GithubComparisonItemFromJson(json); +} diff --git a/lib/models/github.g.dart b/lib/models/github.g.dart index b64de8e..f6ad7e6 100644 --- a/lib/models/github.g.dart +++ b/lib/models/github.g.dart @@ -500,3 +500,24 @@ Map _$GithubFilesItemToJson(GithubFilesItem instance) => 'changes': instance.changes, 'patch': instance.patch, }; + +GithubComparisonItem _$GithubComparisonItemFromJson(Map json) { + return GithubComparisonItem() + ..files = (json['files'] as List) + ?.map((e) => e == null + ? null + : GithubFilesItem.fromJson(e as Map)) + ?.toList() + ..status = json['status'] as String + ..aheadBy = json['ahead_by'] as int + ..behindBy = json['behind_by'] as int; +} + +Map _$GithubComparisonItemToJson( + GithubComparisonItem instance) => + { + 'files': instance.files, + 'status': instance.status, + 'ahead_by': instance.aheadBy, + 'behind_by': instance.behindBy, + }; diff --git a/lib/router.dart b/lib/router.dart index d7202b6..db4bf32 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -38,6 +38,7 @@ import 'package:git_touch/screens/settings.dart'; import 'package:git_touch/screens/gh_user.dart'; import 'package:git_touch/screens/gh_users.dart'; import 'package:git_touch/screens/gh_user_organization.dart'; +import 'package:git_touch/screens/gh_compare.dart'; // import 'package:git_touch/screens/gh_gists.dart'; class RouterScreen { @@ -77,6 +78,7 @@ class GithubRouter { GithubRouter.watchers, GithubRouter.contributors, GithubRouter.files, + GithubRouter.compare, ]; static final user = RouterScreen('/:login', (_, p) { final login = p['login'].first; @@ -133,6 +135,10 @@ class GithubRouter { p['name'].first, int.parse(p['number'].first), )); + static final compare = RouterScreen( + '/:owner/:name/compare/:before/:head', + (context, p) => GhComparisonScreen(p['owner'].first, p['name'].first, + p['before'].first, p['head'].first)); static final commits = RouterScreen('/:owner/:name/commits', (context, p) => GhCommitsScreen(p['owner'].first, p['name'].first)); static final object = RouterScreen('/:owner/:name/blob/:ref', (_, p) { diff --git a/lib/screens/gh_compare.dart b/lib/screens/gh_compare.dart new file mode 100644 index 0000000..0994929 --- /dev/null +++ b/lib/screens/gh_compare.dart @@ -0,0 +1,55 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/cupertino.dart'; +import 'package:git_touch/models/github.dart'; +import 'package:git_touch/scaffolds/refresh_stateful.dart'; +import 'package:git_touch/widgets/app_bar_title.dart'; +import 'package:provider/provider.dart'; +import 'package:git_touch/widgets/files_item.dart'; +import 'package:git_touch/models/auth.dart'; +import 'package:git_touch/widgets/action_button.dart'; + +class GhComparisonScreen extends StatelessWidget { + final String owner; + final String name; + final String before; + final String head; + GhComparisonScreen(this.owner, this.name, this.before, this.head); + + Widget build(BuildContext context) { + return RefreshStatefulScaffold( + title: AppBarTitle('Files'), + fetchData: () async { + final auth = Provider.of(context); + final res = await auth.ghClient.getJSON( + '/repos/$owner/$name/compare/$before...$head', + convert: (vs) => GithubComparisonItem.fromJson(vs), + ); + print('/repos/$owner/$name/compare/$before...$head'); + return res.files; + }, + actionBuilder: (v, _) { + return ActionButton( + title: 'Actions', + items: [ + ...ActionItem.getUrlActions( + 'https://github.com/$owner/$name/compare/$before...$head'), + ], + ); + }, + bodyBuilder: (v, _) { + return Wrap( + children: v + .map((vs) => FilesItem( + filename: vs.filename, + additions: vs.additions, + deletions: vs.deletions, + status: vs.status, + changes: vs.changes, + patch: vs.patch, + )) + .toList(), + ); + }, + ); + } +} diff --git a/lib/widgets/event_item.dart b/lib/widgets/event_item.dart index c66743c..85bb6d4 100644 --- a/lib/widgets/event_item.dart +++ b/lib/widgets/event_item.dart @@ -110,7 +110,7 @@ class EventItem extends StatelessWidget { final theme = Provider.of(context); return Link( url: - 'https://github.com/${e.repoOwner}/${e.repoName}/compare/${e.payload.before}...${e.payload.head}', + '/${e.repoOwner}/${e.repoName}/compare/${e.payload.before}/${e.payload.head}', child: Container( padding: EdgeInsets.all(12), decoration: BoxDecoration(