2020-01-29 11:00:48 +01:00
|
|
|
import 'package:flutter/material.dart';
|
2019-12-04 15:02:22 +01:00
|
|
|
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';
|
2020-01-11 10:25:01 +01:00
|
|
|
import 'package:git_touch/utils/utils.dart';
|
2020-01-29 11:00:48 +01:00
|
|
|
import 'package:git_touch/widgets/action_entry.dart';
|
2020-10-05 10:07:07 +02:00
|
|
|
import 'package:git_touch/widgets/contribution.dart';
|
2020-10-05 11:23:43 +02:00
|
|
|
import 'package:git_touch/widgets/entry_item.dart';
|
2019-12-04 15:02:22 +01:00
|
|
|
import 'package:git_touch/widgets/repository_item.dart';
|
2020-10-06 07:25:43 +02:00
|
|
|
import 'package:git_touch/widgets/table_view.dart';
|
2020-01-29 11:00:48 +01:00
|
|
|
import 'package:git_touch/widgets/user_header.dart';
|
2019-12-04 15:02:22 +01:00
|
|
|
import 'package:provider/provider.dart';
|
2020-01-29 11:00:48 +01:00
|
|
|
import 'package:timeago/timeago.dart' as timeago;
|
2019-12-04 15:02:22 +01:00
|
|
|
|
2020-10-05 10:57:56 +02:00
|
|
|
class GtUserScreenPayload {
|
2020-10-06 06:05:51 +02:00
|
|
|
GiteaOrg org;
|
|
|
|
List<GiteaRepository> orgRepos;
|
|
|
|
int orgRepoCount;
|
2020-10-06 05:56:29 +02:00
|
|
|
GiteaUser user;
|
|
|
|
List<GiteaRepository> userRepos;
|
|
|
|
int userRepoCount;
|
|
|
|
List<List<ContributionDay>> userHeatmap;
|
2020-10-05 10:57:56 +02:00
|
|
|
}
|
|
|
|
|
2020-02-07 15:20:06 +01:00
|
|
|
class GtUserScreen extends StatelessWidget {
|
2020-01-29 10:33:54 +01:00
|
|
|
final String login;
|
2020-10-05 14:39:48 +02:00
|
|
|
final bool isViewer;
|
|
|
|
GtUserScreen(this.login, {this.isViewer = false});
|
2019-12-04 15:02:22 +01:00
|
|
|
|
2020-10-06 05:56:29 +02:00
|
|
|
static List<List<ContributionDay>> normalizeHeatmap(List userHeatmap) {
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return heatmapWeeks;
|
|
|
|
}
|
|
|
|
|
2019-12-04 15:02:22 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-10-05 10:57:56 +02:00
|
|
|
return RefreshStatefulScaffold<GtUserScreenPayload>(
|
2020-10-05 14:39:48 +02:00
|
|
|
title: Text(isViewer ? 'Me' : login),
|
2020-10-06 14:52:40 +02:00
|
|
|
fetch: () async {
|
2020-10-04 14:37:23 +02:00
|
|
|
final auth = context.read<AuthModel>();
|
2020-01-29 11:00:48 +01:00
|
|
|
final res = await Future.wait([
|
2020-10-05 10:57:56 +02:00
|
|
|
auth.fetchGitea('/orgs/$login'),
|
2020-10-06 06:05:51 +02:00
|
|
|
auth.fetchGiteaWithPage('/orgs/$login/repos', limit: 6),
|
2020-10-06 05:56:29 +02:00
|
|
|
auth.fetchGitea(isViewer ? '/user' : '/users/$login'),
|
|
|
|
auth.fetchGiteaWithPage(
|
|
|
|
isViewer ? '/user/repos' : '/users/$login/repos',
|
|
|
|
limit: 6),
|
|
|
|
auth.fetchGitea('/users/$login/heatmap'),
|
2019-12-04 15:02:22 +01:00
|
|
|
]);
|
2020-10-06 05:56:29 +02:00
|
|
|
final org = res[0];
|
2020-10-05 10:57:56 +02:00
|
|
|
|
|
|
|
final payload = GtUserScreenPayload();
|
|
|
|
// user api also returns data for org, use org api here.
|
|
|
|
if (org['message'] == null) {
|
|
|
|
// org
|
2020-10-05 11:31:17 +02:00
|
|
|
payload.org = GiteaOrg.fromJson(org);
|
2020-10-06 06:05:51 +02:00
|
|
|
final orgReposData = res[3] as DataWithPage;
|
|
|
|
payload.orgRepos = [
|
|
|
|
for (var v in orgReposData.data) GiteaRepository.fromJson(v)
|
|
|
|
];
|
|
|
|
payload.orgRepoCount = orgReposData.total;
|
2020-10-05 10:57:56 +02:00
|
|
|
} else {
|
|
|
|
// user
|
2020-10-06 05:56:29 +02:00
|
|
|
payload.user = GiteaUser.fromJson(res[2]);
|
2020-10-06 06:05:51 +02:00
|
|
|
final userRepoData = res[3] as DataWithPage;
|
2020-10-06 05:56:29 +02:00
|
|
|
payload.userRepos = [
|
2020-10-06 06:05:51 +02:00
|
|
|
for (var v in userRepoData.data) GiteaRepository.fromJson(v)
|
2020-10-05 10:57:56 +02:00
|
|
|
];
|
2020-10-06 06:05:51 +02:00
|
|
|
payload.userRepoCount = userRepoData.total;
|
2020-10-06 05:56:29 +02:00
|
|
|
payload.userHeatmap = normalizeHeatmap(res[4]);
|
2020-10-05 10:57:56 +02:00
|
|
|
}
|
|
|
|
return payload;
|
2019-12-04 15:02:22 +01:00
|
|
|
},
|
2020-01-29 11:00:48 +01:00
|
|
|
action: isViewer
|
|
|
|
? ActionEntry(
|
|
|
|
iconData: Icons.settings,
|
2020-01-31 09:54:01 +01:00
|
|
|
url: '/settings',
|
2020-01-29 11:00:48 +01:00
|
|
|
)
|
|
|
|
: null,
|
2020-10-06 05:56:29 +02:00
|
|
|
bodyBuilder: (p, _) {
|
|
|
|
if (p.user != null) {
|
2020-10-05 10:57:56 +02:00
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
|
|
|
UserHeader(
|
2020-10-06 05:56:29 +02:00
|
|
|
login: p.user.login,
|
|
|
|
avatarUrl: p.user.avatarUrl,
|
|
|
|
name: p.user.fullName,
|
|
|
|
createdAt: p.user.created,
|
2020-10-05 10:57:56 +02:00
|
|
|
bio: '',
|
|
|
|
),
|
|
|
|
CommonStyle.border,
|
2020-10-05 11:23:43 +02:00
|
|
|
Row(children: [
|
|
|
|
EntryItem(
|
2020-10-06 05:56:29 +02:00
|
|
|
count: p.userRepoCount,
|
2020-10-05 11:23:43 +02:00
|
|
|
text: 'Repositories',
|
|
|
|
url: '/gitea/$login?tab=repositories',
|
|
|
|
),
|
|
|
|
EntryItem(
|
|
|
|
text: 'Stars',
|
|
|
|
url: '/gitea/$login?tab=stars',
|
|
|
|
),
|
2020-10-05 12:01:49 +02:00
|
|
|
EntryItem(
|
|
|
|
text: 'Followers',
|
|
|
|
url: '/gitea/$login?tab=followers',
|
|
|
|
),
|
|
|
|
EntryItem(
|
|
|
|
text: 'Following',
|
|
|
|
url: '/gitea/$login?tab=following',
|
|
|
|
),
|
2020-10-05 11:23:43 +02:00
|
|
|
]),
|
2020-10-06 05:56:29 +02:00
|
|
|
ContributionWidget(weeks: p.userHeatmap),
|
2020-10-05 10:57:56 +02:00
|
|
|
CommonStyle.border,
|
2020-10-06 07:53:07 +02:00
|
|
|
TableView(
|
|
|
|
hasIcon: true,
|
|
|
|
items: [
|
|
|
|
TableViewItem(
|
|
|
|
leftIconData: Octicons.home,
|
|
|
|
text: Text('Organizations'),
|
|
|
|
url: '/gitea/$login?tab=organizations',
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
CommonStyle.border,
|
2020-10-05 10:57:56 +02:00
|
|
|
Column(
|
|
|
|
children: <Widget>[
|
2020-10-06 05:56:29 +02:00
|
|
|
for (var v in p.userRepos)
|
2020-10-05 10:57:56 +02:00
|
|
|
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}',
|
|
|
|
)
|
|
|
|
],
|
2020-10-06 07:25:43 +02:00
|
|
|
),
|
2020-10-05 10:57:56 +02:00
|
|
|
],
|
|
|
|
);
|
2020-10-06 05:56:29 +02:00
|
|
|
} else if (p.org != null) {
|
2020-10-05 10:57:56 +02:00
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
|
|
|
UserHeader(
|
2020-10-06 05:56:29 +02:00
|
|
|
login: p.org.username,
|
|
|
|
avatarUrl: p.org.avatarUrl,
|
|
|
|
name: p.org.fullName,
|
2020-10-05 10:57:56 +02:00
|
|
|
createdAt: null,
|
2020-10-06 05:56:29 +02:00
|
|
|
bio: p.org.description,
|
2020-10-05 10:57:56 +02:00
|
|
|
),
|
|
|
|
CommonStyle.border,
|
2020-10-05 11:31:17 +02:00
|
|
|
Row(children: [
|
|
|
|
EntryItem(
|
2020-10-06 06:05:51 +02:00
|
|
|
count: p.orgRepoCount,
|
2020-10-05 11:31:17 +02:00
|
|
|
text: 'Repositories',
|
|
|
|
url: '/gitea/$login?tab=orgrepo',
|
|
|
|
),
|
2020-10-05 12:08:14 +02:00
|
|
|
EntryItem(
|
|
|
|
text: 'Members',
|
|
|
|
url: '/gitea/$login?tab=people',
|
|
|
|
),
|
2020-10-05 11:31:17 +02:00
|
|
|
]),
|
2020-10-05 12:08:14 +02:00
|
|
|
CommonStyle.border,
|
2020-10-06 06:05:51 +02:00
|
|
|
CommonStyle.border,
|
|
|
|
Column(
|
|
|
|
children: <Widget>[
|
|
|
|
for (var v in p.orgRepos)
|
|
|
|
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}',
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)
|
2020-10-05 10:57:56 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Text('404'); // TODO:
|
|
|
|
}
|
2019-12-04 15:02:22 +01:00
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|