1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-16 18:28:51 +01:00

refactor: news page types

This commit is contained in:
Rongjian Zhang 2019-12-08 14:21:14 +08:00
parent 34354e4a27
commit a5fc703a24
4 changed files with 126 additions and 45 deletions

View File

@ -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<String, dynamic> payload;
Tuple2<String, String> _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<String, dynamic> json) =>
_$GithubEventFromJson(json);
}
@JsonSerializable(fieldRename: FieldRename.snake)
class GithubEventActor {
String login;
String avatarUrl;
GithubEventActor();
factory GithubEventActor.fromJson(Map<String, dynamic> json) =>
_$GithubEventActorFromJson(json);
}
@JsonSerializable(fieldRename: FieldRename.snake)
class GithubEventRepo {
String name;
GithubEventRepo();
factory GithubEventRepo.fromJson(Map<String, dynamic> json) =>
_$GithubEventRepoFromJson(json);
}

View File

@ -0,0 +1,50 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'github_event.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
GithubEvent _$GithubEventFromJson(Map<String, dynamic> json) {
return GithubEvent()
..actor = json['actor'] == null
? null
: GithubEventActor.fromJson(json['actor'] as Map<String, dynamic>)
..type = json['type'] as String
..repo = json['repo'] == null
? null
: GithubEventRepo.fromJson(json['repo'] as Map<String, dynamic>)
..createdAt = json['created_at'] as String
..payload = json['payload'] as Map<String, dynamic>;
}
Map<String, dynamic> _$GithubEventToJson(GithubEvent instance) =>
<String, dynamic>{
'actor': instance.actor,
'type': instance.type,
'repo': instance.repo,
'created_at': instance.createdAt,
'payload': instance.payload,
};
GithubEventActor _$GithubEventActorFromJson(Map<String, dynamic> json) {
return GithubEventActor()
..login = json['login'] as String
..avatarUrl = json['avatar_url'] as String;
}
Map<String, dynamic> _$GithubEventActorToJson(GithubEventActor instance) =>
<String, dynamic>{
'login': instance.login,
'avatar_url': instance.avatarUrl,
};
GithubEventRepo _$GithubEventRepoFromJson(Map<String, dynamic> json) {
return GithubEventRepo()..name = json['name'] as String;
}
Map<String, dynamic> _$GithubEventRepoToJson(GithubEventRepo instance) =>
<String, dynamic>{
'name': instance.name,
};

View File

@ -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<NewsScreen> {
});
}
Future<ListPayload<EventPayload, int>> fetchEvents([int page = 1]) async {
final settings = Provider.of<AuthModel>(context);
final login = settings.activeAccount.login;
List data = await settings.getWithCredentials(
Future<ListPayload<GithubEvent, int>> fetchEvents([int page = 1]) async {
final auth = Provider.of<AuthModel>(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<EventPayload>((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<NewsScreen> {
@override
Widget build(context) {
return ListStatefulScaffold<EventPayload, int>(
return ListStatefulScaffold<GithubEvent, int>(
title: AppBarTitle('News'),
actionBuilder: () {
return ActionEntry(

View File

@ -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<String, dynamic> 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<ActionItem> _getUserActions(List<String> users) {
// Remove duplicates
@ -96,8 +72,8 @@ class EventItem extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
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),
],
);