git-touch-android-ios-app/lib/screens/gt_user.dart

211 lines
7.1 KiB
Dart
Raw Normal View History

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';
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
class GtUserScreenPayload {
GiteaOrg org;
List<GiteaRepository> orgRepos;
int orgRepoCount;
GiteaUser user;
List<GiteaRepository> userRepos;
int userRepoCount;
List<List<ContributionDay>> userHeatmap;
}
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
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) {
return RefreshStatefulScaffold<GtUserScreenPayload>(
2020-10-05 14:39:48 +02:00
title: Text(isViewer ? 'Me' : login),
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([
auth.fetchGitea('/orgs/$login'),
auth.fetchGiteaWithPage('/orgs/$login/repos', limit: 6),
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
]);
final org = res[0];
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);
final orgReposData = res[3] as DataWithPage;
payload.orgRepos = [
for (var v in orgReposData.data) GiteaRepository.fromJson(v)
];
payload.orgRepoCount = orgReposData.total;
} else {
// user
payload.user = GiteaUser.fromJson(res[2]);
final userRepoData = res[3] as DataWithPage;
payload.userRepos = [
for (var v in userRepoData.data) GiteaRepository.fromJson(v)
];
payload.userRepoCount = userRepoData.total;
payload.userHeatmap = normalizeHeatmap(res[4]);
}
return payload;
2019-12-04 15:02:22 +01:00
},
2020-01-29 11:00:48 +01:00
action: isViewer
? ActionEntry(
2021-02-14 15:17:22 +01:00
iconData: Ionicons.cog,
2020-01-31 09:54:01 +01:00
url: '/settings',
2020-01-29 11:00:48 +01:00
)
: null,
bodyBuilder: (p, _) {
if (p.user != null) {
return Column(
children: <Widget>[
UserHeader(
login: p.user.login,
avatarUrl: p.user.avatarUrl,
name: p.user.fullName,
createdAt: p.user.created,
isViewer: isViewer,
bio: '',
),
CommonStyle.border,
2020-10-05 11:23:43 +02:00
Row(children: [
EntryItem(
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
]),
ContributionWidget(weeks: p.userHeatmap),
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,
Column(
children: <Widget>[
for (var v in p.userRepos)
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
),
],
);
} else if (p.org != null) {
return Column(
children: <Widget>[
UserHeader(
login: p.org.username,
avatarUrl: p.org.avatarUrl,
name: p.org.fullName,
createdAt: null,
bio: p.org.description,
),
CommonStyle.border,
2020-10-05 11:31:17 +02:00
Row(children: [
EntryItem(
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,
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}',
)
],
)
],
);
} else {
return Text('404'); // TODO:
}
2019-12-04 15:02:22 +01:00
},
);
}
}