refactor(gt): merge user and org screen

This commit is contained in:
Rongjian Zhang 2020-10-05 16:57:56 +08:00
parent c950559043
commit cb26f0785e
4 changed files with 122 additions and 135 deletions

View File

@ -17,7 +17,6 @@ import 'package:git_touch/screens/gl_starrers.dart';
import 'package:git_touch/screens/gt_commits.dart'; import 'package:git_touch/screens/gt_commits.dart';
import 'package:git_touch/screens/gt_issues.dart'; import 'package:git_touch/screens/gt_issues.dart';
import 'package:git_touch/screens/gt_object.dart'; import 'package:git_touch/screens/gt_object.dart';
import 'package:git_touch/screens/gt_org.dart';
import 'package:git_touch/screens/gt_repo.dart'; import 'package:git_touch/screens/gt_repo.dart';
import 'package:git_touch/screens/gt_user.dart'; import 'package:git_touch/screens/gt_user.dart';
import 'package:git_touch/screens/gl_blob.dart'; import 'package:git_touch/screens/gl_blob.dart';
@ -270,11 +269,7 @@ class GiteaRouter {
GiteaRouter.pulls, GiteaRouter.pulls,
]; ];
static final user = RouterScreen( static final user = RouterScreen(
'/:login', '/:login', (context, params) => GtUserScreen(params['login'].first));
(context, params) => params['org'].first == '1'
? GtOrgScreen(params['login'].first)
: GtUserScreen(params['login'].first),
);
static final repo = RouterScreen( static final repo = RouterScreen(
'/:owner/:name', '/:owner/:name',
(context, params) => (context, params) =>

View File

@ -1,67 +0,0 @@
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:git_touch/models/auth.dart';
import 'package:git_touch/models/gitea.dart';
import 'package:git_touch/scaffolds/refresh_stateful.dart';
import 'package:git_touch/utils/utils.dart';
import 'package:git_touch/widgets/repository_item.dart';
import 'package:git_touch/widgets/user_header.dart';
import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart';
import 'package:timeago/timeago.dart' as timeago;
class GtOrgScreen extends StatelessWidget {
final String login;
GtOrgScreen(this.login);
@override
Widget build(BuildContext context) {
return RefreshStatefulScaffold<Tuple2<GiteaOrg, List<GiteaRepository>>>(
title: Text(login),
fetchData: () async {
final auth = context.read<AuthModel>();
final res = await Future.wait([
auth.fetchGitea('/orgs/$login'),
auth.fetchGitea('/orgs/$login/repos'),
]);
return Tuple2(
GiteaOrg.fromJson(res[0]),
[for (var v in res[1]) GiteaRepository.fromJson(v)],
);
},
bodyBuilder: (data, _) {
final org = data.item1;
final repos = data.item2;
return Column(
children: <Widget>[
UserHeader(
login: org.username,
avatarUrl: org.avatarUrl,
name: org.fullName,
createdAt: null,
bio: org.description,
),
CommonStyle.border,
Column(
children: <Widget>[
for (var v in repos)
RepositoryItem(
owner: v.owner.login,
avatarUrl: v.owner.avatarUrl,
name: v.name,
description: v.description,
starCount: v.starsCount,
forkCount: v.forksCount,
note: 'Updated ${timeago.format(v.updatedAt)}',
url: '/gitea/${v.owner.login}/${v.name}',
avatarLink: '/gitea/${v.owner.login}',
)
],
)
],
);
},
);
}
}

View File

View File

@ -1,4 +1,3 @@
import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:git_touch/models/auth.dart'; import 'package:git_touch/models/auth.dart';
@ -13,6 +12,11 @@ import 'package:provider/provider.dart';
import 'package:tuple/tuple.dart'; import 'package:tuple/tuple.dart';
import 'package:timeago/timeago.dart' as timeago; import 'package:timeago/timeago.dart' as timeago;
class GtUserScreenPayload {
Tuple3<GiteaUser, List<GiteaRepository>, List<List<ContributionDay>>> user;
Tuple2<GiteaOrg, List<GiteaRepository>> org;
}
class GtUserScreen extends StatelessWidget { class GtUserScreen extends StatelessWidget {
final String login; final String login;
GtUserScreen(this.login); GtUserScreen(this.login);
@ -20,9 +24,8 @@ class GtUserScreen extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return RefreshStatefulScaffold< return RefreshStatefulScaffold<GtUserScreenPayload>(
Tuple3<GiteaUser, List<GiteaRepository>, List<List<ContributionDay>>>>( title: Text(login ?? 'Me'),
title: Text(isViewer ? 'Me' : 'User'),
fetchData: () async { fetchData: () async {
final auth = context.read<AuthModel>(); final auth = context.read<AuthModel>();
final res = await Future.wait([ final res = await Future.wait([
@ -30,33 +33,53 @@ class GtUserScreen extends StatelessWidget {
auth.fetchGitea(isViewer ? '/user/repos' : '/users/$login/repos'), auth.fetchGitea(isViewer ? '/user/repos' : '/users/$login/repos'),
auth.fetchGitea( auth.fetchGitea(
'/users/${login ?? auth.activeAccount.login}/heatmap'), '/users/${login ?? auth.activeAccount.login}/heatmap'),
auth.fetchGitea('/orgs/$login'),
auth.fetchGitea('/orgs/$login/repos'),
]); ]);
final heatmapItems = [ final user = res[0];
for (final v in res[2]) GiteaHeatmapItem.fromJson(v) final userRepos = res[1];
]; final userHeatmap = res[2];
List<List<ContributionDay>> heatmapWeeks = [[]]; final org = res[3];
for (var i = 0; i < heatmapItems.length; i++) { final orgRepos = res[4];
if (i > 0 &&
heatmapItems[i].timestamp - heatmapItems[i - 1].timestamp >
86400) {
if (heatmapWeeks.last.length == 7) {
heatmapWeeks.add([]);
}
heatmapWeeks.last.add(ContributionDay(count: 0));
} else {
if (heatmapWeeks.last.length == 7) {
heatmapWeeks.add([]);
}
heatmapWeeks.last
.add(ContributionDay(count: heatmapItems[i].contributions));
}
}
return Tuple3( final payload = GtUserScreenPayload();
GiteaUser.fromJson(res[0]), // user api also returns data for org, use org api here.
[for (var v in res[1]) GiteaRepository.fromJson(v)], if (org['message'] == null) {
heatmapWeeks, // org
); payload.org = Tuple2(
GiteaOrg.fromJson(org),
[for (var v in orgRepos) GiteaRepository.fromJson(v)],
);
} else {
// user
final heatmapItems = [
for (final v in userHeatmap) GiteaHeatmapItem.fromJson(v)
];
List<List<ContributionDay>> heatmapWeeks = [[]];
for (var i = 0; i < heatmapItems.length; i++) {
if (i > 0 &&
heatmapItems[i].timestamp - heatmapItems[i - 1].timestamp >
86400) {
if (heatmapWeeks.last.length == 7) {
heatmapWeeks.add([]);
}
heatmapWeeks.last.add(ContributionDay(count: 0));
} else {
if (heatmapWeeks.last.length == 7) {
heatmapWeeks.add([]);
}
heatmapWeeks.last
.add(ContributionDay(count: heatmapItems[i].contributions));
}
}
payload.user = Tuple3(
GiteaUser.fromJson(user),
[for (var v in userRepos) GiteaRepository.fromJson(v)],
heatmapWeeks,
);
}
return payload;
}, },
action: isViewer action: isViewer
? ActionEntry( ? ActionEntry(
@ -65,40 +88,76 @@ class GtUserScreen extends StatelessWidget {
) )
: null, : null,
bodyBuilder: (data, _) { bodyBuilder: (data, _) {
final user = data.item1; if (data.user != null) {
final repos = data.item2; final user = data.user.item1;
final heatmapWeeks = data.item3; final repos = data.user.item2;
final heatmapWeeks = data.user.item3;
return Column( return Column(
children: <Widget>[ children: <Widget>[
UserHeader( UserHeader(
login: user.login, login: user.login,
avatarUrl: user.avatarUrl, avatarUrl: user.avatarUrl,
name: user.fullName, name: user.fullName,
createdAt: user.created, createdAt: user.created,
bio: '', bio: '',
), ),
CommonStyle.border, CommonStyle.border,
ContributionWidget(weeks: heatmapWeeks), ContributionWidget(weeks: heatmapWeeks),
CommonStyle.border, CommonStyle.border,
Column( Column(
children: <Widget>[ children: <Widget>[
for (var v in repos) for (var v in repos)
RepositoryItem( RepositoryItem(
owner: v.owner.login, owner: v.owner.login,
avatarUrl: v.owner.avatarUrl, avatarUrl: v.owner.avatarUrl,
name: v.name, name: v.name,
description: v.description, description: v.description,
starCount: v.starsCount, starCount: v.starsCount,
forkCount: v.forksCount, forkCount: v.forksCount,
note: 'Updated ${timeago.format(v.updatedAt)}', note: 'Updated ${timeago.format(v.updatedAt)}',
url: '/gitea/${v.owner.login}/${v.name}', url: '/gitea/${v.owner.login}/${v.name}',
avatarLink: '/gitea/${v.owner.login}', avatarLink: '/gitea/${v.owner.login}',
) )
], ],
) )
], ],
); );
} else if (data.org != null) {
final org = data.org.item1;
final repos = data.org.item2;
return Column(
children: <Widget>[
UserHeader(
login: org.username,
avatarUrl: org.avatarUrl,
name: org.fullName,
createdAt: null,
bio: org.description,
),
CommonStyle.border,
Column(
children: <Widget>[
for (var v in repos)
RepositoryItem(
owner: v.owner.login,
avatarUrl: v.owner.avatarUrl,
name: v.name,
description: v.description,
starCount: v.starsCount,
forkCount: v.forksCount,
note: 'Updated ${timeago.format(v.updatedAt)}',
url: '/gitea/${v.owner.login}/${v.name}',
avatarLink: '/gitea/${v.owner.login}',
)
],
)
],
);
} else {
return Text('404'); // TODO:
}
}, },
); );
} }