1
0
mirror of https://github.com/git-touch/git-touch synced 2024-12-14 09:25:27 +01:00

refactor: extract github trending api as seperated package

This commit is contained in:
Rongjian Zhang 2019-03-06 11:26:10 +08:00
parent ec1d995660
commit 2256b86eba
2 changed files with 16 additions and 63 deletions

View File

@ -1,20 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:github_trending/github_trending.dart';
import 'package:html/parser.dart' show parse;
import '../scaffolds/refresh.dart'; import '../scaffolds/refresh.dart';
import '../widgets/repo_item.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 { class TrendingScreen extends StatefulWidget {
@override @override
_TrendingScreenState createState() => _TrendingScreenState(); _TrendingScreenState createState() => _TrendingScreenState();
@ -22,65 +10,30 @@ class TrendingScreen extends StatefulWidget {
class _TrendingScreenState extends State<TrendingScreen> { class _TrendingScreenState extends State<TrendingScreen> {
Future<List<dynamic>> _fetchTrendingRepos() async { Future<List<dynamic>> _fetchTrendingRepos() async {
var res = await http.get('https://github.com/trending'); var items = await getTrendingRepositories();
// print(res.body);
var document = parse(res.body);
var items = document.querySelectorAll('.repo-list>li');
return items.map((item) { return items.map((item) {
Map<String, String> lang; return {
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 = {
'owner': { 'owner': {
'login': item 'login': item.owner,
.querySelector('h3>a>span')
?.innerHtml
?.replaceFirst('/', '')
?.trim()
}, },
'name': item 'name': item.name,
.querySelector('h3>a') 'description': item.description,
?.innerHtml
?.replaceFirst(RegExp(r'^[\s\S]*span>'), '')
?.trim(),
'description': item.children[2]
.querySelector('p')
?.innerHtml
?.trim()
?.replaceAll(RegExp(r'<g-emoji.*?>'), '')
?.replaceAll(RegExp(r'</g-emoji>'), ''),
'stargazers': { 'stargazers': {
'totalCount': item.children[3] 'totalCount': item.starCount,
.querySelector('.octicon-star')
?.parent
?.innerHtml
?.replaceFirst(RegExp(r'^[\s\S]*svg>'), '')
?.trim()
}, },
'forks': { 'forks': {
'totalCount': item 'totalCount': item.forkCount,
.querySelector('.octicon-repo-forked') },
?.parent 'primaryLanguage': item.primaryLanguage == null
?.innerHtml ? null
?.replaceFirst(RegExp(r'^[\s\S]*svg>'), '') : {
?.trim() ?? 'name': item.primaryLanguage.name,
0, 'color': item.primaryLanguage.color,
}, },
'primaryLanguage': lang,
'isPrivate': false, 'isPrivate': false,
'isFork': false // TODO: 'isFork': false // TODO:
}; };
// print(payload);
return payload;
}).toList(); }).toList();
} }

View File

@ -15,7 +15,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
http: ^0.11.3 http: ^0.12.0
rxdart: ^0.20.0 rxdart: ^0.20.0
uri: ^0.11.3 uri: ^0.11.3
intl: ^0.15.7 intl: ^0.15.7
@ -27,7 +27,7 @@ dependencies:
nanoid: ^0.0.6 nanoid: ^0.0.6
share: ^0.6.0 share: ^0.6.0
flutter_vector_icons: ^0.0.2 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. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.