From 396fe550c40375f9eb3d10f1ff58a11946670662 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Mon, 30 Dec 2019 19:55:59 +0800 Subject: [PATCH] fix: trending user data --- lib/models/github.dart | 24 ++++++++++++++++++++++++ lib/models/github.g.dart | 32 ++++++++++++++++++++++++++++++++ lib/screens/trending.dart | 15 ++++++++------- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/lib/models/github.dart b/lib/models/github.dart index 2c339e4..1bbb0b3 100644 --- a/lib/models/github.dart +++ b/lib/models/github.dart @@ -211,3 +211,27 @@ class GithubTrendingItem { factory GithubTrendingItem.fromJson(Map json) => _$GithubTrendingItemFromJson(json); } + +@JsonSerializable() +class GithubTrendingUser { + String username; + String name; + String avatar; + GithubTrendingUserRepo repo; + + GithubTrendingUser(); + + factory GithubTrendingUser.fromJson(Map json) => + _$GithubTrendingUserFromJson(json); +} + +@JsonSerializable() +class GithubTrendingUserRepo { + String username; + String description; + + GithubTrendingUserRepo(); + + factory GithubTrendingUserRepo.fromJson(Map json) => + _$GithubTrendingUserRepoFromJson(json); +} diff --git a/lib/models/github.g.dart b/lib/models/github.g.dart index 64dc393..1246b65 100644 --- a/lib/models/github.g.dart +++ b/lib/models/github.g.dart @@ -246,3 +246,35 @@ Map _$GithubTrendingItemToJson(GithubTrendingItem instance) => 'forks': instance.forks, 'currentPeriodStars': instance.currentPeriodStars, }; + +GithubTrendingUser _$GithubTrendingUserFromJson(Map json) { + return GithubTrendingUser() + ..username = json['username'] as String + ..name = json['name'] as String + ..avatar = json['avatar'] as String + ..repo = json['repo'] == null + ? null + : GithubTrendingUserRepo.fromJson(json['repo'] as Map); +} + +Map _$GithubTrendingUserToJson(GithubTrendingUser instance) => + { + 'username': instance.username, + 'name': instance.name, + 'avatar': instance.avatar, + 'repo': instance.repo, + }; + +GithubTrendingUserRepo _$GithubTrendingUserRepoFromJson( + Map json) { + return GithubTrendingUserRepo() + ..username = json['username'] as String + ..description = json['description'] as String; +} + +Map _$GithubTrendingUserRepoToJson( + GithubTrendingUserRepo instance) => + { + 'username': instance.username, + 'description': instance.description, + }; diff --git a/lib/screens/trending.dart b/lib/screens/trending.dart index dffa790..d086d03 100644 --- a/lib/screens/trending.dart +++ b/lib/screens/trending.dart @@ -10,24 +10,24 @@ import 'package:git_touch/widgets/repository_item.dart'; class TrendingScreen extends StatelessWidget { Widget build(BuildContext context) { - return TabStatefulScaffold>( + return TabStatefulScaffold( title: AppBarTitle('Trending'), tabs: ['Repositories', 'Users'], fetchData: (tabIndex) async { - var uri = Uri.parse('https://github-trending-api.now.sh') + final uri = Uri.parse('https://github-trending-api.now.sh') .resolve(tabIndex == 1 ? '/developers' : '/'); - var res = await http.get(uri); - return (json.decode(res.body) as List) - .map((v) => GithubTrendingItem.fromJson(v)); + final res = await http.get(uri); + return json.decode(res.body) as List; }, bodyBuilder: (payload, activeTab) { return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: join( CommonStyle.border, - payload.map((item) { + payload.map((v) { switch (activeTab) { case 0: + final item = GithubTrendingItem.fromJson(v); return RepositoryItem.raw( item.author, item.avatar, @@ -41,8 +41,9 @@ class TrendingScreen extends StatelessWidget { [], ); case 1: + final item = GithubTrendingUser.fromJson(v); return UserItem( - login: item.author, + login: item.username, name: item.name, avatarUrl: item.avatar, bio: '',