mirror of
https://github.com/git-touch/git-touch
synced 2024-12-16 18:28:51 +01:00
refactor(github): repo screen lazy load
This commit is contained in:
parent
2b68d95a4a
commit
e47980dd3b
@ -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/entry_item.dart';
|
||||||
import 'package:git_touch/widgets/label.dart';
|
import 'package:git_touch/widgets/label.dart';
|
||||||
import 'package:git_touch/widgets/language_bar.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/mutation_button.dart';
|
||||||
import 'package:git_touch/widgets/markdown_view.dart';
|
import 'package:git_touch/widgets/markdown_view.dart';
|
||||||
import 'package:git_touch/widgets/repo_header.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:github/github.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:git_touch/models/theme.dart';
|
import 'package:git_touch/models/theme.dart';
|
||||||
import 'package:tuple/tuple.dart';
|
|
||||||
import 'package:git_touch/widgets/action_button.dart';
|
import 'package:git_touch/widgets/action_button.dart';
|
||||||
|
|
||||||
class GhRepoScreen extends StatelessWidget {
|
class GhRepoScreen extends StatelessWidget {
|
||||||
@ -25,16 +25,6 @@ class GhRepoScreen extends StatelessWidget {
|
|||||||
final String branch;
|
final String branch;
|
||||||
GhRepoScreen(this.owner, this.name, {this.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) {
|
String _buildWatchState(GhRepoSubscriptionState state) {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case GhRepoSubscriptionState.IGNORED:
|
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
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final theme = Provider.of<ThemeModel>(context);
|
final theme = Provider.of<ThemeModel>(context);
|
||||||
return RefreshStatefulScaffold<Tuple3<GhRepoRepository, String, String>>(
|
return RefreshStatefulScaffold<GhRepoRepository>(
|
||||||
title: AppBarTitle('Repository'),
|
title: AppBarTitle('Repository'),
|
||||||
fetchData: () async {
|
fetchData: () async {
|
||||||
final rs = await Future.wait([
|
var res = await context.read<AuthModel>().gqlClient.execute(GhRepoQuery(
|
||||||
_query(context),
|
variables: GhRepoArguments(
|
||||||
_fetchReadme(context),
|
owner: owner,
|
||||||
_fetchContributors(context),
|
name: name,
|
||||||
]);
|
branchSpecified: branch != null,
|
||||||
|
branch: branch ?? '')));
|
||||||
return Tuple3(rs[0] as GhRepoRepository, rs[1] as String, rs[2]);
|
return res.data.repository;
|
||||||
},
|
},
|
||||||
actionBuilder: (data, setState) {
|
actionBuilder: (repo, setState) {
|
||||||
final repo = data.item1;
|
|
||||||
return ActionButton(
|
return ActionButton(
|
||||||
title: 'Repository Actions',
|
title: 'Repository Actions',
|
||||||
items: [
|
items: [
|
||||||
@ -101,10 +68,7 @@ class GhRepoScreen extends StatelessWidget {
|
|||||||
],
|
],
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
bodyBuilder: (data, setState) {
|
bodyBuilder: (repo, setState) {
|
||||||
final repo = data.item1;
|
|
||||||
final readme = data.item2;
|
|
||||||
final contributorsCount = data.item3;
|
|
||||||
final ref = branch == null ? repo.defaultBranchRef : repo.ref;
|
final ref = branch == null ? repo.defaultBranchRef : repo.ref;
|
||||||
final license = repo.licenseInfo?.spdxId ?? repo.licenseInfo?.name;
|
final license = repo.licenseInfo?.spdxId ?? repo.licenseInfo?.name;
|
||||||
|
|
||||||
@ -329,19 +293,43 @@ class GhRepoScreen extends StatelessWidget {
|
|||||||
TableViewItem(
|
TableViewItem(
|
||||||
leftIconData: Octicons.organization,
|
leftIconData: Octicons.organization,
|
||||||
text: Text('Contributors'),
|
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',
|
url: '/github/$owner/$name/contributors',
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
if (readme != null)
|
FutureBuilder<String>(
|
||||||
Container(
|
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,
|
padding: CommonStyle.padding,
|
||||||
color: theme.palette.background,
|
color: theme.palette.background,
|
||||||
child: MarkdownView(
|
child: MarkdownView(
|
||||||
readme,
|
snapshot.data,
|
||||||
basePaths: [owner, name, branch ?? 'master'], // TODO:
|
basePaths: [owner, name, branch ?? 'master'], // TODO:
|
||||||
),
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return Container();
|
||||||
|
},
|
||||||
),
|
),
|
||||||
CommonStyle.verticalGap,
|
CommonStyle.verticalGap,
|
||||||
],
|
],
|
||||||
|
Loading…
Reference in New Issue
Block a user