From 2256b86ebaf256dcef3edce6cfd6cf457138f9a8 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Wed, 6 Mar 2019 11:26:10 +0800 Subject: [PATCH] refactor: extract github trending api as seperated package --- lib/screens/trending.dart | 75 ++++++++------------------------------- pubspec.yaml | 4 +-- 2 files changed, 16 insertions(+), 63 deletions(-) diff --git a/lib/screens/trending.dart b/lib/screens/trending.dart index 26a5d8f..2a35b0d 100644 --- a/lib/screens/trending.dart +++ b/lib/screens/trending.dart @@ -1,20 +1,8 @@ import 'package:flutter/material.dart'; -import 'package:http/http.dart' as http; -import 'package:html/parser.dart' show parse; +import 'package:github_trending/github_trending.dart'; import '../scaffolds/refresh.dart'; import '../widgets/repo_item.dart'; -// class TrendingRepo { -// String owner; -// String name; -// String description; -// String languageName; -// String languageColor; -// int starCount; -// int forkCount; -// int stars; -// } - class TrendingScreen extends StatefulWidget { @override _TrendingScreenState createState() => _TrendingScreenState(); @@ -22,65 +10,30 @@ class TrendingScreen extends StatefulWidget { class _TrendingScreenState extends State { Future> _fetchTrendingRepos() async { - var res = await http.get('https://github.com/trending'); - // print(res.body); - var document = parse(res.body); - var items = document.querySelectorAll('.repo-list>li'); + var items = await getTrendingRepositories(); return items.map((item) { - Map lang; - var colorNode = item.querySelector('.repo-language-color'); - if (colorNode != null) { - lang = { - 'name': colorNode.nextElementSibling.innerHtml.trim(), - 'color': RegExp(r'(#\w{6})') - .firstMatch(colorNode.attributes['style']) - .group(0), - }; - } - - var payload = { + return { 'owner': { - 'login': item - .querySelector('h3>a>span') - ?.innerHtml - ?.replaceFirst('/', '') - ?.trim() + 'login': item.owner, }, - 'name': item - .querySelector('h3>a') - ?.innerHtml - ?.replaceFirst(RegExp(r'^[\s\S]*span>'), '') - ?.trim(), - 'description': item.children[2] - .querySelector('p') - ?.innerHtml - ?.trim() - ?.replaceAll(RegExp(r''), '') - ?.replaceAll(RegExp(r''), ''), + 'name': item.name, + 'description': item.description, 'stargazers': { - 'totalCount': item.children[3] - .querySelector('.octicon-star') - ?.parent - ?.innerHtml - ?.replaceFirst(RegExp(r'^[\s\S]*svg>'), '') - ?.trim() + 'totalCount': item.starCount, }, 'forks': { - 'totalCount': item - .querySelector('.octicon-repo-forked') - ?.parent - ?.innerHtml - ?.replaceFirst(RegExp(r'^[\s\S]*svg>'), '') - ?.trim() ?? - 0, + 'totalCount': item.forkCount, }, - 'primaryLanguage': lang, + 'primaryLanguage': item.primaryLanguage == null + ? null + : { + 'name': item.primaryLanguage.name, + 'color': item.primaryLanguage.color, + }, 'isPrivate': false, 'isFork': false // TODO: }; - // print(payload); - return payload; }).toList(); } diff --git a/pubspec.yaml b/pubspec.yaml index 7195402..2420fb6 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -15,7 +15,7 @@ environment: dependencies: flutter: sdk: flutter - http: ^0.11.3 + http: ^0.12.0 rxdart: ^0.20.0 uri: ^0.11.3 intl: ^0.15.7 @@ -27,7 +27,7 @@ dependencies: nanoid: ^0.0.6 share: ^0.6.0 flutter_vector_icons: ^0.0.2 - html: ^0.13.3 + github_trending: ^0.0.1 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.