2020-10-05 16:07:07 +08:00
|
|
|
import 'dart:math';
|
2020-01-29 18:00:48 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2019-12-04 22:02:22 +08: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 17:25:01 +08:00
|
|
|
import 'package:git_touch/utils/utils.dart';
|
2020-01-29 18:00:48 +08:00
|
|
|
import 'package:git_touch/widgets/action_entry.dart';
|
2020-10-05 16:07:07 +08:00
|
|
|
import 'package:git_touch/widgets/contribution.dart';
|
2019-12-04 22:02:22 +08:00
|
|
|
import 'package:git_touch/widgets/repository_item.dart';
|
2020-01-29 18:00:48 +08:00
|
|
|
import 'package:git_touch/widgets/user_header.dart';
|
2019-12-04 22:02:22 +08:00
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:tuple/tuple.dart';
|
2020-01-29 18:00:48 +08:00
|
|
|
import 'package:timeago/timeago.dart' as timeago;
|
2019-12-04 22:02:22 +08:00
|
|
|
|
2020-02-07 22:20:06 +08:00
|
|
|
class GtUserScreen extends StatelessWidget {
|
2020-01-29 17:33:54 +08:00
|
|
|
final String login;
|
2020-02-07 22:20:06 +08:00
|
|
|
GtUserScreen(this.login);
|
2020-01-29 18:00:48 +08:00
|
|
|
bool get isViewer => login == null;
|
2019-12-04 22:02:22 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-10-05 16:07:07 +08:00
|
|
|
return RefreshStatefulScaffold<
|
|
|
|
Tuple3<GiteaUser, List<GiteaRepository>, List<List<ContributionDay>>>>(
|
2020-01-29 18:00:48 +08:00
|
|
|
title: Text(isViewer ? 'Me' : 'User'),
|
2019-12-04 22:02:22 +08:00
|
|
|
fetchData: () async {
|
2020-10-04 20:37:23 +08:00
|
|
|
final auth = context.read<AuthModel>();
|
2020-01-29 18:00:48 +08:00
|
|
|
final res = await Future.wait([
|
|
|
|
auth.fetchGitea(isViewer ? '/user' : '/users/$login'),
|
|
|
|
auth.fetchGitea(isViewer ? '/user/repos' : '/users/$login/repos'),
|
2020-10-05 16:07:07 +08:00
|
|
|
auth.fetchGitea(
|
|
|
|
'/users/${login ?? auth.activeAccount.login}/heatmap'),
|
2019-12-04 22:02:22 +08:00
|
|
|
]);
|
2020-10-05 16:07:07 +08:00
|
|
|
final heatmapItems = [
|
|
|
|
for (final v in res[2]) 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 Tuple3(
|
2020-01-29 18:00:48 +08:00
|
|
|
GiteaUser.fromJson(res[0]),
|
|
|
|
[for (var v in res[1]) GiteaRepository.fromJson(v)],
|
2020-10-05 16:07:07 +08:00
|
|
|
heatmapWeeks,
|
2020-01-29 17:33:54 +08:00
|
|
|
);
|
2019-12-04 22:02:22 +08:00
|
|
|
},
|
2020-01-29 18:00:48 +08:00
|
|
|
action: isViewer
|
|
|
|
? ActionEntry(
|
|
|
|
iconData: Icons.settings,
|
2020-01-31 16:54:01 +08:00
|
|
|
url: '/settings',
|
2020-01-29 18:00:48 +08:00
|
|
|
)
|
|
|
|
: null,
|
2019-12-04 22:02:22 +08:00
|
|
|
bodyBuilder: (data, _) {
|
|
|
|
final user = data.item1;
|
|
|
|
final repos = data.item2;
|
2020-10-05 16:07:07 +08:00
|
|
|
final heatmapWeeks = data.item3;
|
2019-12-04 22:02:22 +08:00
|
|
|
|
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
2020-01-29 18:00:48 +08:00
|
|
|
UserHeader(
|
2019-12-04 22:02:22 +08:00
|
|
|
login: user.login,
|
|
|
|
avatarUrl: user.avatarUrl,
|
|
|
|
name: user.fullName,
|
2020-01-29 18:00:48 +08:00
|
|
|
createdAt: user.created,
|
|
|
|
bio: '',
|
2019-12-04 22:02:22 +08:00
|
|
|
),
|
2020-01-12 14:49:46 +08:00
|
|
|
CommonStyle.border,
|
2020-10-05 16:07:07 +08:00
|
|
|
ContributionWidget(weeks: heatmapWeeks),
|
|
|
|
CommonStyle.border,
|
2019-12-04 22:02:22 +08:00
|
|
|
Column(
|
2020-01-29 13:06:55 +08:00
|
|
|
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,
|
2020-01-29 18:00:48 +08:00
|
|
|
note: 'Updated ${timeago.format(v.updatedAt)}',
|
2020-01-30 12:35:11 +08:00
|
|
|
url: '/gitea/${v.owner.login}/${v.name}',
|
|
|
|
avatarLink: '/gitea/${v.owner.login}',
|
2020-01-29 13:06:55 +08:00
|
|
|
)
|
|
|
|
],
|
2019-12-04 22:02:22 +08:00
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|