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

212 lines
7.2 KiB
Dart
Raw Normal View History

2022-09-20 20:00:03 +02:00
import 'package:antd_mobile/antd_mobile.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-01-29 11:00:48 +01:00
import 'package:git_touch/widgets/user_header.dart';
2022-09-20 20:00:03 +02:00
import 'package:go_router/go_router.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 {
2021-05-16 09:16:35 +02:00
GiteaOrg? org;
late List<GiteaRepository> orgRepos;
int? orgRepoCount;
GiteaUser? user;
late List<GiteaRepository> userRepos;
int? userRepoCount;
List<List<ContributionDay>>? userHeatmap;
}
2020-02-07 15:20:06 +01:00
class GtUserScreen extends StatelessWidget {
2022-09-21 18:28:21 +02:00
const GtUserScreen(this.login, {this.isViewer = false});
2020-01-29 10:33:54 +01:00
final String login;
2020-10-05 14:39:48 +02:00
final bool isViewer;
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 &&
2021-05-16 09:16:35 +02:00
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
2022-09-06 18:28:12 +02:00
? const 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(
2021-05-16 09:16:35 +02:00
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,
2022-09-20 20:00:03 +02:00
AntList(
2020-10-06 07:53:07 +02:00
items: [
2022-09-20 20:00:03 +02:00
AntListItem(
prefix: const Icon(Octicons.home),
2022-09-13 19:19:52 +02:00
child: const Text('Organizations'),
2022-09-20 20:00:03 +02:00
onClick: () {
context.push('/gitea/$login?tab=organizations');
},
2020-10-06 07:53:07 +02:00
),
],
),
CommonStyle.border,
Column(
children: <Widget>[
for (var v in p.userRepos)
RepositoryItem(
2021-05-16 09:16:35 +02:00
owner: v.owner!.login,
avatarUrl: v.owner!.avatarUrl,
name: v.name,
description: v.description,
starCount: v.starsCount,
forkCount: v.forksCount,
2021-05-16 09:16:35 +02:00
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(
2021-05-16 09:16:35 +02:00
login: p.org!.username,
avatarUrl: p.org!.avatarUrl,
name: p.org!.fullName,
createdAt: null,
2021-05-16 09:16:35 +02:00
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(
2021-05-16 09:16:35 +02:00
owner: v.owner!.login,
avatarUrl: v.owner!.avatarUrl,
name: v.name,
description: v.description,
starCount: v.starsCount,
forkCount: v.forksCount,
2021-05-16 09:16:35 +02:00
note: 'Updated ${timeago.format(v.updatedAt!)}',
url: '/gitea/${v.owner!.login}/${v.name}',
avatarLink: '/gitea/${v.owner!.login}',
)
],
)
],
);
} else {
2022-09-06 18:28:12 +02:00
return const Text('404'); // TODO:
}
2019-12-04 15:02:22 +01:00
},
);
}
}