Skip to content

Commit 907857e

Browse files
committed
增加本地阅读历史
1 parent 97353d1 commit 907857e

File tree

5 files changed

+102
-7
lines changed

5 files changed

+102
-7
lines changed

lib/common/ab/provider/repos/ReadHistoryDbProvider.dart

Lines changed: 69 additions & 5 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/config/Config.dart';
6+
import 'package:gsy_github_app_flutter/common/model/Repository.dart';
7+
import 'package:sqflite/sqflite.dart';
38

49
/**
510
* 本地已读历史表
@@ -16,11 +21,13 @@ class ReadHistoryDbProvider extends BaseDbProvider {
1621

1722
int id;
1823
String fullName;
19-
DateTime readDate;
24+
int readDate;
2025
String data;
2126

22-
Map<String, dynamic> toMap() {
23-
Map<String, dynamic> map = {columnFullName: fullName, columnReadDate: readDate, columnData: data};
27+
ReadHistoryDbProvider();
28+
29+
Map<String, dynamic> toMap(String fullName, DateTime readDate, String data) {
30+
Map<String, dynamic> map = {columnFullName: fullName, columnReadDate: readDate.millisecondsSinceEpoch, columnData: data};
2431
if (id != null) {
2532
map[columnId] = id;
2633
}
@@ -35,12 +42,69 @@ class ReadHistoryDbProvider extends BaseDbProvider {
3542
}
3643

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

4054
@override
4155
tableName() {
4256
return name;
4357
}
44-
}
4558

59+
Future _getProvider(Database db, int page) async {
60+
List<Map<String, dynamic>> maps = await db.query(name,
61+
columns: [columnId, columnFullName, columnReadDate, columnData],
62+
limit: Config.PAGE_SIZE,
63+
offset: (page - 1) * Config.PAGE_SIZE,
64+
orderBy: "$columnReadDate DESC");
65+
if (maps.length > 0) {
66+
return maps;
67+
}
68+
return null;
69+
}
70+
71+
Future _getProviderInsert(Database db, String fullName) async {
72+
List<Map<String, dynamic>> maps = await db.query(
73+
name,
74+
columns: [columnId, columnFullName, columnReadDate, columnData],
75+
where: "$columnFullName = ?",
76+
whereArgs: [fullName],
77+
);
78+
if (maps.length > 0) {
79+
ReadHistoryDbProvider provider = ReadHistoryDbProvider.fromMap(maps.first);
80+
return provider;
81+
}
82+
return null;
83+
}
84+
85+
///插入到数据库
86+
Future insert(String fullName, DateTime dateTime, String dataMapString) async {
87+
Database db = await getDataBase();
88+
var provider = await _getProviderInsert(db, fullName);
89+
if (provider != null) {
90+
await db.delete(name, where: "$columnFullName = ?", whereArgs: [fullName]);
91+
}
92+
return await db.insert(name, toMap(fullName, dateTime, dataMapString));
93+
}
4694

95+
///获取事件数据
96+
Future<List<Repository>> geData(int page) async {
97+
Database db = await getDataBase();
98+
var provider = await _getProvider(db, page);
99+
if (provider != null) {
100+
List<Repository> list = new List();
101+
for (var providerMap in provider) {
102+
ReadHistoryDbProvider provider = ReadHistoryDbProvider.fromMap(providerMap);
103+
Map map = json.decode(provider.data);
104+
list.add(Repository.fromJson(map));
105+
}
106+
return list;
107+
}
108+
return null;
109+
}
110+
}

lib/common/dao/ReposDao.dart

Lines changed: 22 additions & 0 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/ReadHistoryDbProvider.dart';
89
import 'package:gsy_github_app_flutter/common/ab/provider/repos/RepositoryCommitsDbProvider.dart';
910
import 'package:gsy_github_app_flutter/common/ab/provider/repos/RepositoryDetailDbProvider.dart';
1011
import 'package:gsy_github_app_flutter/common/ab/provider/repos/RepositoryDetailReadmeDbProvider.dart';
@@ -99,6 +100,7 @@ class ReposDao {
99100
if (needDb) {
100101
provider.insert(fullName, json.encode(data));
101102
}
103+
saveHistoryDao(fullName, DateTime.now(), json.encode(data));
102104
return new DataResult(Repository.fromJson(data), true);
103105
} else {
104106
return new DataResult(null, false);
@@ -665,4 +667,24 @@ class ReposDao {
665667
}
666668
}
667669
}
670+
671+
/**
672+
* 获取阅读历史
673+
*/
674+
static getHistoryDao(page) async {
675+
ReadHistoryDbProvider provider = new ReadHistoryDbProvider();
676+
List<Repository> list = await provider.geData(page);
677+
if (list == null || list.length <= 0) {
678+
return new DataResult(null, false);
679+
}
680+
return new DataResult(list, true);
681+
}
682+
683+
/**
684+
* 保存阅读历史
685+
*/
686+
static saveHistoryDao(String fullName, DateTime dateTime, String data) {
687+
ReadHistoryDbProvider provider = new ReadHistoryDbProvider();
688+
provider.insert(fullName, dateTime, data);
689+
}
668690
}

lib/common/style/GSYStyle.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ class GSYStrings {
251251
static const String home_reply = "问题反馈";
252252
static const String home_about = "关于";
253253
static const String home_check_update = "检测更新";
254+
static const String home_history = "阅读历史";
254255

255256
static const String login_username_hint_text = "请输入github用户名";
256257
static const String login_password_hint_text = "请输入密码";

lib/page/CommonListPage.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class _CommonListPageState extends GSYListState<CommonListPage> {
9393
case 'notify':
9494
return null;
9595
case 'history':
96-
return null;
96+
return await ReposDao.getHistoryDao(page);
9797
case 'topics':
9898
return null;
9999
case 'user_be_stared':

lib/widget/HomeDrawer.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class HomeDrawer extends StatelessWidget {
4848
style: GSYConstant.largeTextWhite,
4949
),
5050
accountEmail: new Text(
51-
user.email ?? user.name ?? "---",
51+
user.email ?? user.name ?? "---",
5252
style: GSYConstant.subNormalText,
5353
),
5454
//用户名
@@ -86,6 +86,14 @@ class HomeDrawer extends StatelessWidget {
8686
});
8787
}, titleController: new TextEditingController(), valueController: new TextEditingController(), needTitle: false);
8888
}),
89+
new ListTile(
90+
title: new Text(
91+
GSYStrings.home_history,
92+
style: GSYConstant.normalText,
93+
),
94+
onTap: () {
95+
NavigatorUtils.gotoCommonList(context, GSYStrings.home_history, "repository", "history", userName: "", reposName: "");
96+
}),
8997
new ListTile(
9098
title: new Text(
9199
GSYStrings.home_check_update,

0 commit comments

Comments
 (0)