Skip to content

Commit 65021d8

Browse files
committed
repos detail info db
1 parent 462f02b commit 65021d8

File tree

5 files changed

+105
-29
lines changed

5 files changed

+105
-29
lines changed
Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
import 'dart:async';
2+
import 'dart:convert';
3+
14
import 'package:gsy_github_app_flutter/common/ab/SqlProvider.dart';
5+
import 'package:gsy_github_app_flutter/common/model/Repository.dart';
6+
import 'package:sqflite/sqflite.dart';
27

38
/**
49
* 仓库详情数据表
@@ -10,15 +15,15 @@ class RepositoryDetailDbProvider extends BaseDbProvider {
1015
int id;
1116
String fullName;
1217
String data;
13-
String branch;
1418

1519
final String columnId = "_id";
1620
final String columnFullName = "fullName";
17-
final String columnBranch = "branch";
1821
final String columnData = "data";
1922

20-
Map<String, dynamic> toMap() {
21-
Map<String, dynamic> map = {columnFullName: fullName, columnBranch: branch, columnData: data};
23+
RepositoryDetailDbProvider();
24+
25+
Map<String, dynamic> toMap(String fullName, String dataMapString) {
26+
Map<String, dynamic> map = {columnFullName: fullName, columnData: dataMapString};
2227
if (id != null) {
2328
map[columnId] = id;
2429
}
@@ -28,15 +33,53 @@ class RepositoryDetailDbProvider extends BaseDbProvider {
2833
RepositoryDetailDbProvider.fromMap(Map map) {
2934
id = map[columnId];
3035
fullName = map[columnFullName];
31-
branch = map[columnBranch];
3236
data = map[columnData];
3337
}
3438

3539
@override
36-
tableSqlString() {}
40+
tableSqlString() {
41+
return tableBaseString(name, columnId) +
42+
'''
43+
$columnFullName text not null,
44+
$columnData text not null)
45+
''';
46+
}
3747

3848
@override
3949
tableName() {
4050
return name;
4151
}
52+
53+
54+
Future _getUserProvider(Database db, String fullName) async {
55+
List<Map<String, dynamic>> maps =
56+
await db.query(name, columns: [columnId, columnFullName, columnData], where: "$columnFullName = ?", whereArgs: [fullName]);
57+
if (maps.length > 0) {
58+
RepositoryDetailDbProvider provider = RepositoryDetailDbProvider.fromMap(maps.first);
59+
return provider;
60+
}
61+
return null;
62+
}
63+
64+
///插入到数据库
65+
Future insert(String fullName, String dataMapString) async {
66+
Database db = await getDataBase();
67+
var userProvider = await _getUserProvider(db, fullName);
68+
if (userProvider != null) {
69+
await db.delete(name, where: "$columnFullName = ?", whereArgs: [fullName]);
70+
}
71+
return await db.insert(name, toMap(fullName, dataMapString));
72+
}
73+
74+
///获取详情
75+
Future<Repository> getRepository(String fullName) async {
76+
Database db = await getDataBase();
77+
var userProvider = await _getUserProvider(db, fullName);
78+
if (userProvider != null) {
79+
return Repository.fromJson(json.decode(userProvider.data));
80+
}
81+
return null;
82+
}
83+
84+
4285
}

lib/common/dao/ReposDao.dart

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'dart:io';
55
import 'package:dio/dio.dart';
66
import 'package:fluttertoast/fluttertoast.dart';
77
import 'package:get_version/get_version.dart';
8+
import 'package:gsy_github_app_flutter/common/ab/provider/repos/RepositoryDetailDbProvider.dart';
89
import 'package:gsy_github_app_flutter/common/ab/provider/repos/TrendRepositoryDbProvider.dart';
910
import 'package:gsy_github_app_flutter/common/config/Config.dart';
1011
import 'package:gsy_github_app_flutter/common/dao/DaoResult.dart';
@@ -75,18 +76,35 @@ class ReposDao {
7576
/**
7677
* 仓库的详情数据
7778
*/
78-
static getRepositoryDetailDao(userName, reposName, branch) async {
79-
String url = Address.getReposDetail(userName, reposName) + "?ref=" + branch;
80-
var res = await HttpManager.netFetch(url, null, {"Accept": 'application/vnd.github.mercy-preview+json'}, null);
81-
if (res != null && res.result && res.data.length > 0) {
82-
var data = res.data;
83-
if (data == null || data.length == 0) {
79+
static getRepositoryDetailDao(userName, reposName, branch, {needDb = true}) async {
80+
String fullName = userName + "/" + reposName;
81+
RepositoryDetailDbProvider provider = new RepositoryDetailDbProvider();
82+
83+
next() async {
84+
String url = Address.getReposDetail(userName, reposName) + "?ref=" + branch;
85+
var res = await HttpManager.netFetch(url, null, {"Accept": 'application/vnd.github.mercy-preview+json'}, null);
86+
if (res != null && res.result && res.data.length > 0) {
87+
var data = res.data;
88+
if (data == null || data.length == 0) {
89+
return new DataResult(null, false);
90+
}
91+
if (needDb) {
92+
provider.insert(fullName, json.encode(data));
93+
}
94+
return new DataResult(Repository.fromJson(data), true);
95+
} else {
8496
return new DataResult(null, false);
8597
}
86-
return new DataResult(Repository.fromJson(data), true);
87-
} else {
88-
return new DataResult(null, false);
8998
}
99+
if (needDb) {
100+
Repository repository = await provider.getRepository(fullName);
101+
if (repository == null) {
102+
return await next();
103+
}
104+
DataResult dataResult = new DataResult(repository, true, next: next());
105+
return dataResult;
106+
}
107+
return await next();
90108
}
91109

92110
/**

lib/common/style/GSYStyle.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ class GSYICons {
339339
static const String FONT_FAMILY = 'wxcIconFont';
340340

341341
static const String DEFAULT_USER_ICON = 'static/images/logo.png';
342+
static const String DEFAULT_REMOTE_PIC = 'https://github.com/logo.png';
342343

343344
static const IconData HOME = const IconData(0xe624, fontFamily: GSYICons.FONT_FAMILY);
344345
static const IconData MORE = const IconData(0xe674, fontFamily: GSYICons.FONT_FAMILY);

lib/page/RepositoryDetailPage.dart

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import 'package:gsy_github_app_flutter/common/dao/ReposDao.dart';
35
import 'package:gsy_github_app_flutter/common/style/GSYStyle.dart';
@@ -58,14 +60,24 @@ class _RepositoryDetailPageState extends State<RepositoryDetailPage> {
5860

5961
_RepositoryDetailPageState(this.userName, this.reposName);
6062

61-
_getReposDetail() async {
62-
var result = await ReposDao.getRepositoryDetailDao(userName, reposName, reposDetailParentControl.currentBranch);
63-
if (result != null && result.result) {
64-
setState(() {
65-
reposDetailInfoPageControl.repository = result.data;
66-
titleOptionControl.url = reposDetailInfoPageControl.repository.htmlUrl;
67-
});
68-
}
63+
_getReposDetail() {
64+
ReposDao.getRepositoryDetailDao(userName, reposName, reposDetailParentControl.currentBranch).then((result) {
65+
if (result != null && result.result) {
66+
setState(() {
67+
reposDetailInfoPageControl.repository = result.data;
68+
titleOptionControl.url = reposDetailInfoPageControl.repository.htmlUrl;
69+
});
70+
return result.next;
71+
}
72+
return new Future.value(null);
73+
}).then((result) {
74+
if (result != null && result.result) {
75+
setState(() {
76+
reposDetailInfoPageControl.repository = result.data;
77+
titleOptionControl.url = reposDetailInfoPageControl.repository.htmlUrl;
78+
});
79+
}
80+
});
6981
}
7082

7183
_getReposStatus() async {
@@ -227,10 +239,12 @@ class _RepositoryDetailPageState extends State<RepositoryDetailPage> {
227239
return [
228240
///Release Page
229241
new GSYOptionModel(GSYStrings.repos_option_release, GSYStrings.repos_option_release, (model) {
230-
String releaseUrl =
231-
reposDetailInfoPageControl.repository == null ? GSYStrings.app_default_share_url : reposDetailInfoPageControl.repository.htmlUrl + "/releases";
232-
String tagUrl =
233-
reposDetailInfoPageControl.repository == null ? GSYStrings.app_default_share_url : reposDetailInfoPageControl.repository.htmlUrl + "/tags";
242+
String releaseUrl = reposDetailInfoPageControl.repository == null
243+
? GSYStrings.app_default_share_url
244+
: reposDetailInfoPageControl.repository.htmlUrl + "/releases";
245+
String tagUrl = reposDetailInfoPageControl.repository == null
246+
? GSYStrings.app_default_share_url
247+
: reposDetailInfoPageControl.repository.htmlUrl + "/tags";
234248
NavigatorUtils.goReleasePage(context, userName, reposName, releaseUrl, tagUrl);
235249
}),
236250
];

lib/widget/ReposHeaderItem.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ReposHeaderItem extends StatelessWidget {
5959
decoration: new BoxDecoration(
6060
image: new DecorationImage(
6161
fit: BoxFit.cover,
62-
image: new NetworkImage(reposHeaderViewModel.ownerPic),
62+
image: new NetworkImage(reposHeaderViewModel.ownerPic ?? GSYICons.DEFAULT_REMOTE_PIC),
6363
),
6464
),
6565
child: new Container(
@@ -179,7 +179,7 @@ class ReposHeaderItem extends StatelessWidget {
179179

180180
class ReposHeaderViewModel {
181181
String ownerName = '---';
182-
String ownerPic = "---";
182+
String ownerPic;
183183
String repositoryName = "---";
184184
String repositorySize = "---";
185185
String repositoryStar = "---";

0 commit comments

Comments
 (0)