Skip to content

Commit ef7eee3

Browse files
committed
issue db
1 parent b342564 commit ef7eee3

File tree

6 files changed

+200
-53
lines changed

6 files changed

+200
-53
lines changed

lib/common/ab/provider/issue/IssueCommentDbProvider.dart

Lines changed: 57 additions & 5 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/Issue.dart';
6+
import 'package:sqflite/sqflite.dart';
27

38
/**
49
* issue评论表
@@ -20,8 +25,10 @@ class IssueCommentDbProvider extends BaseDbProvider {
2025
String number;
2126
String data;
2227

23-
Map<String, dynamic> toMap() {
24-
Map<String, dynamic> map = {columnFullName: fullName, columnData: data, columnNumber: number, columnCommentId: commentId};
28+
IssueCommentDbProvider();
29+
30+
Map<String, dynamic> toMap(String fullName, String number, String data) {
31+
Map<String, dynamic> map = {columnFullName: fullName, columnData: data, columnNumber: number};
2532
if (id != null) {
2633
map[columnId] = id;
2734
}
@@ -31,16 +38,61 @@ class IssueCommentDbProvider extends BaseDbProvider {
3138
IssueCommentDbProvider.fromMap(Map map) {
3239
id = map[columnId];
3340
number = map[columnNumber];
34-
commentId = map[columnCommentId];
3541
fullName = map[columnFullName];
3642
data = map[columnData];
3743
}
3844

3945
@override
40-
tableSqlString() {}
46+
tableSqlString() {
47+
return tableBaseString(name, columnId) +
48+
'''
49+
$columnFullName text not null,
50+
$columnNumber text not null,
51+
$columnCommentId text,
52+
$columnData text not null)
53+
''';
54+
}
4155

4256
@override
4357
tableName() {
4458
return name;
4559
}
46-
}
60+
61+
Future _getProvider(Database db, String fullName, String number) async {
62+
List<Map<String, dynamic>> maps = await db.query(name,
63+
columns: [columnId, columnFullName, columnNumber, columnData], where: "$columnFullName = ? and $columnNumber = ?", whereArgs: [fullName, number]);
64+
if (maps.length > 0) {
65+
IssueCommentDbProvider provider = IssueCommentDbProvider.fromMap(maps.first);
66+
return provider;
67+
}
68+
return null;
69+
}
70+
71+
///插入到数据库
72+
Future insert(String fullName, String number, String dataMapString) async {
73+
Database db = await getDataBase();
74+
var provider = await _getProvider(db, fullName, number);
75+
if (provider != null) {
76+
await db.delete(name, where: "$columnFullName = ? and $columnNumber = ?", whereArgs: [fullName, number]);
77+
}
78+
return await db.insert(name, toMap(fullName, number, dataMapString));
79+
}
80+
81+
///获取事件数据
82+
Future<List<Issue>> getData(String fullName, String number) async {
83+
Database db = await getDataBase();
84+
85+
var provider = await _getProvider(db, fullName, number);
86+
if (provider != null) {
87+
List<Issue> list = new List();
88+
List<dynamic> eventMap = json.decode(provider.data);
89+
if (eventMap.length > 0) {
90+
for (var item in eventMap) {
91+
list.add(Issue.fromJson(item));
92+
}
93+
}
94+
return list;
95+
}
96+
return null;
97+
}
98+
}

lib/common/ab/provider/issue/IssueDetailDbProvider.dart

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import 'dart:async';
2+
import 'dart:convert';
13

24
import 'package:gsy_github_app_flutter/common/ab/SqlProvider.dart';
5+
import 'package:gsy_github_app_flutter/common/ab/provider/repos/RepositoryDetailDbProvider.dart';
6+
import 'package:gsy_github_app_flutter/common/model/Issue.dart';
7+
import 'package:sqflite/sqflite.dart';
38

49
/**
510
* issue详情表
@@ -19,7 +24,9 @@ class IssueDetailDbProvider extends BaseDbProvider {
1924
String number;
2025
String data;
2126

22-
Map<String, dynamic> toMap() {
27+
IssueDetailDbProvider();
28+
29+
Map<String, dynamic> toMap(String fullName, String number, String data) {
2330
Map<String, dynamic> map = {columnFullName: fullName, columnData: data, columnNumber: number};
2431
if (id != null) {
2532
map[columnId] = id;
@@ -35,10 +42,48 @@ class IssueDetailDbProvider extends BaseDbProvider {
3542
}
3643

3744
@override
38-
tableSqlString() {}
45+
tableSqlString() {
46+
return tableBaseString(name, columnId) +
47+
'''
48+
$columnFullName text not null,
49+
$columnNumber text not null,
50+
$columnData text not null)
51+
''';
52+
}
3953

4054
@override
4155
tableName() {
4256
return name;
4357
}
44-
}
58+
59+
Future _getProvider(Database db, String fullName, String number) async {
60+
List<Map<String, dynamic>> maps = await db.query(name,
61+
columns: [columnId, columnFullName, columnNumber, columnData], where: "$columnFullName = ? and $columnNumber = ?", whereArgs: [fullName, number]);
62+
if (maps.length > 0) {
63+
RepositoryDetailDbProvider provider = RepositoryDetailDbProvider.fromMap(maps.first);
64+
return provider;
65+
}
66+
return null;
67+
}
68+
69+
///插入到数据库
70+
Future insert(String fullName, String number, String dataMapString) async {
71+
Database db = await getDataBase();
72+
var provider = await _getProvider(db, fullName, number);
73+
if (provider != null) {
74+
await db.delete(name, where: "$columnFullName = ? and $columnNumber = ?", whereArgs: [fullName, number]);
75+
}
76+
return await db.insert(name, toMap(fullName, number, dataMapString));
77+
}
78+
79+
///获取详情
80+
Future<Issue> getRepository(String fullName, String number) async {
81+
Database db = await getDataBase();
82+
var provider = await _getProvider(db, fullName, number);
83+
if (provider != null) {
84+
return Issue.fromJson(json.decode(provider.data));
85+
}
86+
return null;
87+
}
88+
89+
}

lib/common/dao/IssueDao.dart

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import 'dart:convert';
22
import 'dart:io';
33

44
import 'package:dio/dio.dart';
5+
import 'package:gsy_github_app_flutter/common/ab/provider/issue/IssueCommentDbProvider.dart';
6+
import 'package:gsy_github_app_flutter/common/ab/provider/issue/IssueDetailDbProvider.dart';
57
import 'package:gsy_github_app_flutter/common/ab/provider/repos/RepositoryIssueDbProvider.dart';
68
import 'package:gsy_github_app_flutter/common/dao/DaoResult.dart';
79
import 'package:gsy_github_app_flutter/common/model/Issue.dart';
@@ -96,37 +98,74 @@ class IssueDao {
9698
/**
9799
* issue的详请
98100
*/
99-
static getIssueInfoDao(userName, repository, number) async {
100-
String url = Address.getIssueInfo(userName, repository, number);
101-
//{"Accept": 'application/vnd.github.html,application/vnd.github.VERSION.raw'}
102-
var res = await HttpManager.netFetch(url, null, {"Accept": 'application/vnd.github.VERSION.raw'}, null);
103-
if (res != null && res.result) {
104-
return new DataResult(Issue.fromJson(res.data), true);
105-
} else {
106-
return new DataResult(null, false);
101+
static getIssueInfoDao(userName, repository, number, {needDb = true}) async {
102+
String fullName = userName + "/" + repository;
103+
104+
IssueDetailDbProvider provider = new IssueDetailDbProvider();
105+
106+
next() async {
107+
String url = Address.getIssueInfo(userName, repository, number);
108+
//{"Accept": 'application/vnd.github.html,application/vnd.github.VERSION.raw'}
109+
var res = await HttpManager.netFetch(url, null, {"Accept": 'application/vnd.github.VERSION.raw'}, null);
110+
if (res != null && res.result) {
111+
if (needDb) {
112+
provider.insert(fullName, number, json.encode(res.data));
113+
}
114+
return new DataResult(Issue.fromJson(res.data), true);
115+
} else {
116+
return new DataResult(null, false);
117+
}
107118
}
119+
120+
if (needDb) {
121+
Issue issue = await provider.getRepository(fullName, number);
122+
if (issue == null) {
123+
return await next();
124+
}
125+
DataResult dataResult = new DataResult(issue, true, next: next());
126+
return dataResult;
127+
}
128+
return await next();
108129
}
109130

110131
/**
111132
* issue的详请列表
112133
*/
113-
static getIssueCommentDao(userName, repository, number, {page: 0}) async {
114-
String url = Address.getIssueComment(userName, repository, number) + Address.getPageParams("?", page);
115-
//{"Accept": 'application/vnd.github.html,application/vnd.github.VERSION.raw'}
116-
var res = await HttpManager.netFetch(url, null, {"Accept": 'application/vnd.github.VERSION.raw'}, null);
117-
if (res != null && res.result) {
118-
List<Issue> list = new List();
119-
var data = res.data;
120-
if (data == null || data.length == 0) {
134+
static getIssueCommentDao(userName, repository, number, {page: 0, needDb = false}) async {
135+
String fullName = userName + "/" + repository;
136+
IssueCommentDbProvider provider = new IssueCommentDbProvider();
137+
138+
next() async {
139+
String url = Address.getIssueComment(userName, repository, number) + Address.getPageParams("?", page);
140+
//{"Accept": 'application/vnd.github.html,application/vnd.github.VERSION.raw'}
141+
var res = await HttpManager.netFetch(url, null, {"Accept": 'application/vnd.github.VERSION.raw'}, null);
142+
if (res != null && res.result) {
143+
List<Issue> list = new List();
144+
var data = res.data;
145+
if (data == null || data.length == 0) {
146+
return new DataResult(null, false);
147+
}
148+
if (needDb) {
149+
provider.insert(fullName, number, json.encode(res.data));
150+
}
151+
for (int i = 0; i < data.length; i++) {
152+
list.add(Issue.fromJson(data[i]));
153+
}
154+
return new DataResult(list, true);
155+
} else {
121156
return new DataResult(null, false);
122157
}
123-
for (int i = 0; i < data.length; i++) {
124-
list.add(Issue.fromJson(data[i]));
158+
}
159+
160+
if (needDb) {
161+
List<Issue> list = await provider.getData(fullName, number);
162+
if (list == null) {
163+
return await next();
125164
}
126-
return new DataResult(list, true);
127-
} else {
128-
return new DataResult(null, false);
165+
DataResult dataResult = new DataResult(list, true, next: next());
166+
return dataResult;
129167
}
168+
return await next();
130169
}
131170

132171
/**
@@ -160,8 +199,8 @@ class IssueDao {
160199
*/
161200
static lockIssueDao(userName, repository, number, locked) async {
162201
String url = Address.lockIssue(userName, repository, number);
163-
var res = await HttpManager.netFetch(url, null, {"Accept": 'application/vnd.github.VERSION.full+json'},
164-
new Options(method: locked ? "DELETE" : 'PUT', contentType: ContentType.TEXT),
202+
var res = await HttpManager.netFetch(
203+
url, null, {"Accept": 'application/vnd.github.VERSION.full+json'}, new Options(method: locked ? "DELETE" : 'PUT', contentType: ContentType.TEXT),
165204
noTip: true);
166205
if (res != null && res.result) {
167206
return new DataResult(res.data, true);

lib/common/style/GSYStyle.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +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';
342+
static const String DEFAULT_REMOTE_PIC = 'https://raw.githubusercontent.com/CarGuo/GSYGithubAppFlutter/master/static/images/logo.png';
343343

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

lib/page/IssueDetailPage.dart

Lines changed: 26 additions & 15 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:fluttertoast/fluttertoast.dart';
35
import 'package:gsy_github_app_flutter/common/dao/IssueDao.dart';
@@ -117,19 +119,30 @@ class _IssueDetailPageState extends GSYListState<IssueDetailPage> {
117119
if (page <= 1) {
118120
_getHeaderInfo();
119121
}
120-
return await IssueDao.getIssueCommentDao(userName, reposName, issueNum, page: page);
122+
return await IssueDao.getIssueCommentDao(userName, reposName, issueNum, page: page, needDb: page <= 1);
121123
}
122124

123-
_getHeaderInfo() async {
124-
var res = await IssueDao.getIssueInfoDao(userName, reposName, issueNum);
125-
if (res != null && res.result) {
126-
Issue issue = res.data;
127-
setState(() {
128-
issueHeaderViewModel = IssueHeaderViewModel.fromMap(issue);
129-
titleOptionControl.url = issue.htmlUrl;
130-
headerStatus = true;
131-
});
132-
}
125+
_getHeaderInfo() {
126+
IssueDao.getIssueInfoDao(userName, reposName, issueNum).then((res) {
127+
if (res != null && res.result) {
128+
_resolveHeaderInfo(res);
129+
return res.next;
130+
}
131+
return new Future.value(null);
132+
}).then((res) {
133+
if (res != null && res.result) {
134+
_resolveHeaderInfo(res);
135+
}
136+
});
137+
}
138+
139+
_resolveHeaderInfo(res) {
140+
Issue issue = res.data;
141+
setState(() {
142+
issueHeaderViewModel = IssueHeaderViewModel.fromMap(issue);
143+
titleOptionControl.url = issue.htmlUrl;
144+
headerStatus = true;
145+
});
133146
}
134147

135148
_editCommit(id, content) {
@@ -251,14 +264,12 @@ class _IssueDetailPageState extends GSYListState<IssueDetailPage> {
251264
new FlatButton(
252265
onPressed: () {
253266
CommonUtils.showLoadingDialog(context);
254-
IssueDao.editIssueDao(userName, reposName, issueNum, {"state": (issueHeaderViewModel.state == "closed") ? 'open' : 'closed'}).then(
255-
(result) {
267+
IssueDao.editIssueDao(userName, reposName, issueNum, {"state": (issueHeaderViewModel.state == "closed") ? 'open' : 'closed'}).then((result) {
256268
_getHeaderInfo();
257269
Navigator.pop(context);
258270
});
259271
},
260-
child: new Text((issueHeaderViewModel.state == 'closed') ? GSYStrings.issue_open : GSYStrings.issue_close,
261-
style: GSYConstant.smallText)),
272+
child: new Text((issueHeaderViewModel.state == 'closed') ? GSYStrings.issue_open : GSYStrings.issue_close, style: GSYConstant.smallText)),
262273
new Container(width: 0.3, height: 30.0, color: Color(GSYColors.subLightTextColor)),
263274
new FlatButton(
264275
onPressed: () {

lib/widget/IssueHeaderItem.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ class IssueHeaderItem extends StatelessWidget {
6161

6262
///关闭操作人
6363
_renderCloseByText() {
64-
return (issueHeaderViewModel.closed_by == null || issueHeaderViewModel.closed_by.trim().length == 0)
64+
return (issueHeaderViewModel.closedBy == null || issueHeaderViewModel.closedBy.trim().length == 0)
6565
? new Container()
6666
: new Container(
6767
child: new Text(
68-
"Close By " + issueHeaderViewModel.closed_by,
68+
"Close By " + issueHeaderViewModel.closedBy,
6969
style: GSYConstant.subSmallText,
7070
),
7171
margin: new EdgeInsets.only(right: 5.0, top: 10.0, bottom: 10.0),
@@ -91,7 +91,7 @@ class IssueHeaderItem extends StatelessWidget {
9191
padding: const EdgeInsets.only(top: 0.0, right: 10.0, left: 0.0),
9292
width: 50.0,
9393
height: 50.0,
94-
image: issueHeaderViewModel.actionUserPic,
94+
image: issueHeaderViewModel.actionUserPic ?? GSYICons.DEFAULT_REMOTE_PIC,
9595
onPressed: () {
9696
NavigatorUtils.goPerson(context, issueHeaderViewModel.actionUser);
9797
}),
@@ -150,9 +150,9 @@ class IssueHeaderItem extends StatelessWidget {
150150
class IssueHeaderViewModel {
151151
String actionTime = "---";
152152
String actionUser = "---";
153-
String actionUserPic = "---";
153+
String actionUserPic;
154154

155-
String closed_by = "";
155+
String closedBy = "";
156156
bool locked = false;
157157
String issueComment = "---";
158158
String issueDesHtml = "---";
@@ -167,7 +167,7 @@ class IssueHeaderViewModel {
167167
actionTime = CommonUtils.getNewsTimeStr(issueMap.createdAt);
168168
actionUser = issueMap.user.login;
169169
actionUserPic = issueMap.user.avatar_url;
170-
closed_by = issueMap.closeBy != null ? issueMap.closeBy.login : "";
170+
closedBy = issueMap.closeBy != null ? issueMap.closeBy.login : "";
171171
locked = issueMap.locked;
172172
issueComment = issueMap.title;
173173
issueDesHtml = issueMap.bodyHtml != null ? issueMap.bodyHtml : (issueMap.body != null) ? issueMap.body : "";

0 commit comments

Comments
 (0)