refactor(github): repo screen lazy load

This commit is contained in:
Rongjian Zhang 2020-10-06 15:32:40 +08:00
parent 2b68d95a4a
commit e47980dd3b
1 changed files with 45 additions and 57 deletions

View File

@ -9,6 +9,7 @@ import 'package:git_touch/widgets/app_bar_title.dart';
import 'package:git_touch/widgets/entry_item.dart';
import 'package:git_touch/widgets/label.dart';
import 'package:git_touch/widgets/language_bar.dart';
import 'package:git_touch/widgets/loading.dart';
import 'package:git_touch/widgets/mutation_button.dart';
import 'package:git_touch/widgets/markdown_view.dart';
import 'package:git_touch/widgets/repo_header.dart';
@ -16,7 +17,6 @@ import 'package:git_touch/widgets/table_view.dart';
import 'package:github/github.dart';
import 'package:provider/provider.dart';
import 'package:git_touch/models/theme.dart';
import 'package:tuple/tuple.dart';
import 'package:git_touch/widgets/action_button.dart';
class GhRepoScreen extends StatelessWidget {
@ -25,16 +25,6 @@ class GhRepoScreen extends StatelessWidget {
final String branch;
GhRepoScreen(this.owner, this.name, {this.branch});
Future<GhRepoRepository> _query(BuildContext context) async {
var res = await context.read<AuthModel>().gqlClient.execute(GhRepoQuery(
variables: GhRepoArguments(
owner: owner,
name: name,
branchSpecified: branch != null,
branch: branch ?? '')));
return res.data.repository;
}
String _buildWatchState(GhRepoSubscriptionState state) {
switch (state) {
case GhRepoSubscriptionState.IGNORED:
@ -48,44 +38,21 @@ class GhRepoScreen extends StatelessWidget {
}
}
Future<String> _fetchContributors(BuildContext context) async {
final res = await context
.read<AuthModel>()
.ghClient
.getJSON('/repos/$owner/$name/stats/contributors');
return res.length.toString();
}
Future<String> _fetchReadme(BuildContext context) async {
try {
final res = await context
.read<AuthModel>()
.ghClient
.repositories
.getReadme(RepositorySlug(owner, name));
return res.text;
} catch (e) {
// 404
return null;
}
}
@override
Widget build(BuildContext context) {
final theme = Provider.of<ThemeModel>(context);
return RefreshStatefulScaffold<Tuple3<GhRepoRepository, String, String>>(
return RefreshStatefulScaffold<GhRepoRepository>(
title: AppBarTitle('Repository'),
fetchData: () async {
final rs = await Future.wait([
_query(context),
_fetchReadme(context),
_fetchContributors(context),
]);
return Tuple3(rs[0] as GhRepoRepository, rs[1] as String, rs[2]);
var res = await context.read<AuthModel>().gqlClient.execute(GhRepoQuery(
variables: GhRepoArguments(
owner: owner,
name: name,
branchSpecified: branch != null,
branch: branch ?? '')));
return res.data.repository;
},
actionBuilder: (data, setState) {
final repo = data.item1;
actionBuilder: (repo, setState) {
return ActionButton(
title: 'Repository Actions',
items: [
@ -101,10 +68,7 @@ class GhRepoScreen extends StatelessWidget {
],
);
},
bodyBuilder: (data, setState) {
final repo = data.item1;
final readme = data.item2;
final contributorsCount = data.item3;
bodyBuilder: (repo, setState) {
final ref = branch == null ? repo.defaultBranchRef : repo.ref;
final license = repo.licenseInfo?.spdxId ?? repo.licenseInfo?.name;
@ -329,20 +293,44 @@ class GhRepoScreen extends StatelessWidget {
TableViewItem(
leftIconData: Octicons.organization,
text: Text('Contributors'),
rightWidget: Text(contributorsCount),
rightWidget: FutureBuilder<String>(
future: context
.read<AuthModel>()
.ghClient
.getJSON('/repos/$owner/$name/stats/contributors')
.then((v) => v.length.toString()),
builder: (context, snapshot) {
return Text(snapshot.data ?? '');
},
),
url: '/github/$owner/$name/contributors',
)
],
),
if (readme != null)
Container(
padding: CommonStyle.padding,
color: theme.palette.background,
child: MarkdownView(
readme,
basePaths: [owner, name, branch ?? 'master'], // TODO:
),
),
FutureBuilder<String>(
future: context
.read<AuthModel>()
.ghClient
.repositories
.getReadme(RepositorySlug(owner, name))
.then((file) => file.text),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Loading();
}
if (snapshot.hasData) {
return Container(
padding: CommonStyle.padding,
color: theme.palette.background,
child: MarkdownView(
snapshot.data,
basePaths: [owner, name, branch ?? 'master'], // TODO:
),
);
}
return Container();
},
),
CommonStyle.verticalGap,
],
);