From 40dff68e765945fc44095f3900effbabd4e8362c Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Tue, 3 Nov 2020 18:44:05 +0800 Subject: [PATCH] refactor(github): lazy load readme --- lib/screens/gh_repo.dart | 65 +++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/lib/screens/gh_repo.dart b/lib/screens/gh_repo.dart index 7057818..a8deaeb 100644 --- a/lib/screens/gh_repo.dart +++ b/lib/screens/gh_repo.dart @@ -49,31 +49,33 @@ class GhRepoScreen extends StatelessWidget { } } - Future _fetchReadme(BuildContext context) async { - try { - final res = await context.read().ghClient.request( - 'GET', '/repos/$owner/$name/readme', headers: { - HttpHeaders.acceptHeader: 'application/vnd.github.v3.html' - }); - return res.body; - } catch (e) { - // 404 - return null; - } - } - @override Widget build(BuildContext context) { final theme = Provider.of(context); - return RefreshStatefulScaffold>( + return RefreshStatefulScaffold< + Tuple3, Future>>( title: AppBarTitle('Repository'), fetch: () async { - final rs = await Future.wait([ - _query(context), - _fetchReadme(context), - ]); + final ghClient = context.read().ghClient; - return Tuple2(rs[0] as GhRepoRepository, rs[1] as String); + final repo = await _query(context); + + final countFuture = ghClient + .getJSON('/repos/$owner/$name/stats/contributors') + .then((v) => (v as List).length); + + final readmeFuture = ghClient.request( + 'GET', + '/repos/$owner/$name/readme', + headers: {HttpHeaders.acceptHeader: 'application/vnd.github.v3.html'}, + ).then((res) { + return res.body; + }).catchError((err) { + // 404 + return null; + }); + + return Tuple3(repo, countFuture, readmeFuture); }, actionBuilder: (data, setState) { final repo = data.item1; @@ -94,7 +96,9 @@ class GhRepoScreen extends StatelessWidget { }, bodyBuilder: (data, setState) { final repo = data.item1; - final readme = data.item2; + final contributionFuture = data.item2; + final readmeFuture = data.item3; + final ref = branch == null ? repo.defaultBranchRef : repo.ref; final license = repo.licenseInfo?.spdxId ?? repo.licenseInfo?.name; @@ -317,14 +321,10 @@ class GhRepoScreen extends StatelessWidget { TableViewItem( leftIconData: Octicons.organization, text: Text('Contributors'), - rightWidget: FutureBuilder( - future: context - .read() - .ghClient - .getJSON('/repos/$owner/$name/stats/contributors') - .then((v) => v.length.toString()), + rightWidget: FutureBuilder( + future: contributionFuture, builder: (context, snapshot) { - return Text(snapshot.data ?? ''); + return Text(snapshot.data?.toString() ?? ''); }, ), url: '/github/$owner/$name/contributors', @@ -332,7 +332,16 @@ class GhRepoScreen extends StatelessWidget { ], ], ), - if (readme != null) MarkdownHtmlView(readme) + FutureBuilder( + future: readmeFuture, + builder: (context, snapshot) { + if (snapshot.data == null) { + return Container(); + } else { + return MarkdownHtmlView(snapshot.data); + } + }, + ) ], ); },