diff --git a/lib/models/github_event.dart b/lib/models/github_event.dart new file mode 100644 index 0000000..9a0b111 --- /dev/null +++ b/lib/models/github_event.dart @@ -0,0 +1,55 @@ +import 'package:git_touch/utils/utils.dart'; +import 'package:json_annotation/json_annotation.dart'; +import 'package:tuple/tuple.dart'; + +part 'github_event.g.dart'; + +@JsonSerializable(fieldRename: FieldRename.snake) +class GithubEvent { + GithubEventActor actor; + String type; + GithubEventRepo repo; + String createdAt; + Map payload; + + Tuple2 _repo; + String get repoOwner { + if (_repo == null) { + _repo = parseRepositoryFullName(repo.name); + } + return _repo.item1; + } + + String get repoName { + if (_repo == null) { + _repo = parseRepositoryFullName(repo.name); + } + return _repo.item2; + } + + GithubEvent(); + + factory GithubEvent.fromJson(Map json) => + _$GithubEventFromJson(json); +} + +@JsonSerializable(fieldRename: FieldRename.snake) +class GithubEventActor { + String login; + String avatarUrl; + + GithubEventActor(); + + factory GithubEventActor.fromJson(Map json) => + _$GithubEventActorFromJson(json); +} + +@JsonSerializable(fieldRename: FieldRename.snake) +class GithubEventRepo { + String name; + + GithubEventRepo(); + + factory GithubEventRepo.fromJson(Map json) => + _$GithubEventRepoFromJson(json); +} diff --git a/lib/models/github_event.g.dart b/lib/models/github_event.g.dart new file mode 100644 index 0000000..596ede7 --- /dev/null +++ b/lib/models/github_event.g.dart @@ -0,0 +1,50 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'github_event.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +GithubEvent _$GithubEventFromJson(Map json) { + return GithubEvent() + ..actor = json['actor'] == null + ? null + : GithubEventActor.fromJson(json['actor'] as Map) + ..type = json['type'] as String + ..repo = json['repo'] == null + ? null + : GithubEventRepo.fromJson(json['repo'] as Map) + ..createdAt = json['created_at'] as String + ..payload = json['payload'] as Map; +} + +Map _$GithubEventToJson(GithubEvent instance) => + { + 'actor': instance.actor, + 'type': instance.type, + 'repo': instance.repo, + 'created_at': instance.createdAt, + 'payload': instance.payload, + }; + +GithubEventActor _$GithubEventActorFromJson(Map json) { + return GithubEventActor() + ..login = json['login'] as String + ..avatarUrl = json['avatar_url'] as String; +} + +Map _$GithubEventActorToJson(GithubEventActor instance) => + { + 'login': instance.login, + 'avatar_url': instance.avatarUrl, + }; + +GithubEventRepo _$GithubEventRepoFromJson(Map json) { + return GithubEventRepo()..name = json['name'] as String; +} + +Map _$GithubEventRepoToJson(GithubEventRepo instance) => + { + 'name': instance.name, + }; diff --git a/lib/screens/news.dart b/lib/screens/news.dart index 8d54fd9..f6a2700 100644 --- a/lib/screens/news.dart +++ b/lib/screens/news.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; +import 'package:git_touch/models/github_event.dart'; import 'package:git_touch/models/notification.dart'; import 'package:git_touch/models/theme.dart'; import 'package:git_touch/scaffolds/list_stateful.dart'; @@ -32,15 +33,14 @@ class NewsScreenState extends State { }); } - Future> fetchEvents([int page = 1]) async { - final settings = Provider.of(context); - final login = settings.activeAccount.login; - List data = await settings.getWithCredentials( + Future> fetchEvents([int page = 1]) async { + final auth = Provider.of(context); + final login = auth.activeAccount.login; + List data = await auth.getWithCredentials( '/users/$login/received_events?page=$page&per_page=$pageSize'); // Fimber.d(data.length); var hasMore = data.length == pageSize; - var events = - data.map((item) => EventPayload.fromJson(item)).toList(); + var events = data.map((item) => GithubEvent.fromJson(item)).toList(); return ListPayload( cursor: page + 1, @@ -51,7 +51,7 @@ class NewsScreenState extends State { @override Widget build(context) { - return ListStatefulScaffold( + return ListStatefulScaffold( title: AppBarTitle('News'), actionBuilder: () { return ActionEntry( diff --git a/lib/widgets/event_item.dart b/lib/widgets/event_item.dart index f6aa06a..32e8073 100644 --- a/lib/widgets/event_item.dart +++ b/lib/widgets/event_item.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; +import 'package:git_touch/models/github_event.dart'; import 'package:git_touch/models/theme.dart'; import 'package:git_touch/screens/repository.dart'; import 'package:git_touch/widgets/action_button.dart'; @@ -11,33 +12,8 @@ import 'avatar.dart'; import '../widgets/link.dart'; import '../utils/utils.dart'; -class EventPayload { - String actorLogin; - String actorAvatarUrl; - String type; - String repoOwner; - String repoName; - Map payload; - DateTime createdAt; - - EventPayload.fromJson(input) { - actorLogin = input['actor']['login']; - actorAvatarUrl = input['actor']['avatar_url']; - type = input['type']; - payload = input['payload']; - createdAt = DateTime.parse(input['created_at']); - - final repoFullName = input['repo']['name'] as String; - if (repoFullName != null) { - final ls = parseRepositoryFullName(repoFullName); - repoOwner = ls.item1; - repoName = ls.item2; - } - } -} - class EventItem extends StatelessWidget { - final EventPayload event; + final GithubEvent event; EventItem(this.event); @@ -52,7 +28,7 @@ class EventItem extends StatelessWidget { } TextSpan _buildRepo(ThemeModel theme) => - _buildLinkSpan(theme, '${event.repoOwner}/${event.repoName}'); + _buildLinkSpan(theme, event.repo.name); Iterable _getUserActions(List users) { // Remove duplicates @@ -96,8 +72,8 @@ class EventItem extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.start, children: [ Link( - child: Avatar.medium(url: event.actorAvatarUrl), - screenBuilder: (_) => UserScreen(event.actorLogin), + child: Avatar.medium(url: event.actor.avatarUrl), + screenBuilder: (_) => UserScreen(event.actor.login), ), SizedBox(width: 10), Expanded( @@ -112,7 +88,7 @@ class EventItem extends StatelessWidget { fontWeight: FontWeight.w500, ), children: [ - _buildLinkSpan(theme, event.actorLogin), + _buildLinkSpan(theme, event.actor.login), ...spans, ], ), @@ -129,7 +105,7 @@ class EventItem extends StatelessWidget { Icon(iconData, color: theme.palette.tertiaryText, size: 14), SizedBox(width: 4), - Text(timeago.format(event.createdAt), + Text(timeago.format(DateTime.parse(event.createdAt)), style: TextStyle( fontSize: 13, color: theme.palette.tertiaryText, @@ -192,7 +168,7 @@ class EventItem extends StatelessWidget { iconData: Octicons.repo_forked, screenBuilder: (_) => RepositoryScreen(forkeeOwner, forkeeName), actionItems: [ - ..._getUserActions([event.actorLogin, forkeeOwner]), + ..._getUserActions([event.actor.login, forkeeOwner]), ActionItem.repository(forkeeOwner, forkeeName), ActionItem.repository(event.repoOwner, event.repoName), ], @@ -228,7 +204,7 @@ class EventItem extends StatelessWidget { isPullRequest: isPullRequest, ), actionItems: [ - ..._getUserActions([event.actorLogin, event.repoOwner]), + ..._getUserActions([event.actor.login, event.repoOwner]), ActionItem.issue(event.repoOwner, event.repoName, number, isPullRequest: isPullRequest), ], @@ -250,7 +226,7 @@ class EventItem extends StatelessWidget { screenBuilder: (_) => IssueScreen(event.repoOwner, event.repoName, number), actionItems: [ - ..._getUserActions([event.actorLogin, event.repoOwner]), + ..._getUserActions([event.actor.login, event.repoOwner]), ActionItem.repository(event.repoOwner, event.repoName), ActionItem.issue(event.repoOwner, event.repoName, number), ], @@ -290,7 +266,7 @@ class EventItem extends StatelessWidget { isPullRequest: true, ), actionItems: [ - ..._getUserActions([event.actorLogin, event.repoOwner]), + ..._getUserActions([event.actor.login, event.repoOwner]), ActionItem.repository(event.repoOwner, event.repoName), ActionItem.issue(event.repoOwner, event.repoName, number, isPullRequest: true), @@ -318,7 +294,7 @@ class EventItem extends StatelessWidget { isPullRequest: true, ), actionItems: [ - ..._getUserActions([event.actorLogin, event.repoOwner]), + ..._getUserActions([event.actor.login, event.repoOwner]), ActionItem.repository(event.repoOwner, event.repoName), ActionItem.issue(event.repoOwner, event.repoName, number, isPullRequest: true), @@ -366,7 +342,7 @@ class EventItem extends StatelessWidget { url: 'https://github.com/${event.repoOwner}/${event.repoName}/compare/${event.payload['before']}...${event.payload['head']}', actionItems: [ - ..._getUserActions([event.actorLogin, event.repoOwner]), + ..._getUserActions([event.actor.login, event.repoOwner]), ActionItem.repository(event.repoOwner, event.repoName), ], ); @@ -388,7 +364,7 @@ class EventItem extends StatelessWidget { screenBuilder: (_) => RepositoryScreen(event.repoOwner, event.repoName), actionItems: [ - ..._getUserActions([event.actorLogin, event.repoOwner]), + ..._getUserActions([event.actor.login, event.repoOwner]), ActionItem.repository(event.repoOwner, event.repoName), ], );