-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataRepository.js
283 lines (248 loc) · 7.15 KB
/
DataRepository.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
'use strict';
import React, { Component } from 'react';
import {
AsyncStorage
} from 'react-native';
var API_COVER_URL = "http://news-at.zhihu.com/api/4/start-image/1080*1776";
var API_LATEST_URL = 'http://news-at.zhihu.com/api/4/news/latest';
var API_HOME_URL = 'http://news.at.zhihu.com/api/4/news/before/';
var API_THEME_URL = 'http://news-at.zhihu.com/api/4/theme/';
var API_THEMES_URL = 'http://news-at.zhihu.com/api/4/themes';
var KEY_COVER = '@Cover';
var KEY_THEMES = '@Themes:';
var KEY_HOME_LIST = '@HomeList:';
var KEY_THEME_LIST = '@ThemeList:';
var KEY_THEME_TOPDATA = '@ThemeTop:';
function parseDateFromYYYYMMdd(str) {
if (!str) return new Date();
return new Date(str.slice(0, 4),str.slice(4, 6) - 1,str.slice(6, 8));
}
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear().toString();
var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
var dd = this.getDate().toString();
return yyyy + (mm[1]?mm:"0"+mm[0]) + (dd[1]?dd:"0"+dd[0]); // padding
};
function DataRepository() { // Singleton pattern
if (typeof DataRepository.instance === 'object') {
return DataRepository.instance;
}
DataRepository.instance = this;
}
DataRepository.prototype._safeStorage = function(key: string) {
return new Promise((resolve, reject) => {
AsyncStorage.getItem(key, (error, result) => {
var retData = JSON.parse(result);
if (error) {
console.error(error);
resolve(null);
} else {
resolve(retData);
}
});
});
};
DataRepository.prototype._safeFetch = function(reqUrl: string) {
console.log('reqUrl', reqUrl);
return new Promise((resolve, reject) => {
fetch(reqUrl)
.then((response) => response.json())
.then((responseData) => {
//console.log(responseData);
resolve(responseData);
})
.catch((error) => {
console.error(error);
resolve(null);
});
});
};
DataRepository.prototype.getCover = function() {
return this._safeStorage(KEY_COVER);
}
DataRepository.prototype.updateCover = function() {
fetch(API_COVER_URL)
.then((response) => response.json())
.then((responseData) => {
AsyncStorage.setItem(KEY_COVER, JSON.stringify(responseData));
})
.catch((error) => {
console.error(error);
})
.done();
}
DataRepository.prototype.fetchStories = function(date,
callback
) {
var reqUrl;
var topData = null;
if (!date) {
date = new Date();
reqUrl = API_LATEST_URL;
topData = this._safeStorage(KEY_THEME_TOPDATA+ '0');
} else {
var beforeDate = new Date(date);
beforeDate.setDate(date.getDate() + 1);
reqUrl = API_HOME_URL + beforeDate.yyyymmdd();
}
var localStorage = this._safeStorage(KEY_HOME_LIST + date.yyyymmdd());
var networking = this._safeFetch(reqUrl);
var merged = new Promise((resolve, reject) => {
Promise.all([localStorage, networking, topData])
.then((values) => {
var error, result;
result = this._mergeReadState(values[0], values[1]);
if (!result) {
error = new Error('Load story error');
}
callback && callback(error, result);
if (error) {
reject(error);
} else {
if (values[1] && values[1].top_stories) {
result.topData = values[1].top_stories;
} else {
result.topData = values[2];
}
resolve(result);
}
});
});
return merged;
};
DataRepository.prototype.fetchThemeStories = function(themeId, lastID,
callback
) {
// Home story list
if (themeId === 0) {
var date;
if (lastID) {
date = parseDateFromYYYYMMdd(lastID);
date.setDate(date.getDate() - 1);
}
return this.fetchStories(date, callback);
}
// Stroy list by theme
var isRefresh = !lastID;
var localStorage = isRefresh ? this._safeStorage(KEY_THEME_LIST + themeId) : null;
var reqUrl = API_THEME_URL + themeId;
var topData = null;
if (lastID) {
reqUrl += '/before/' + lastID;
} else {
topData = this._safeStorage(KEY_THEME_TOPDATA + themeId);
}
var networking = this._safeFetch(reqUrl);
var merged = new Promise((resolve, reject) => {
Promise.all([localStorage, networking, topData])
.then((values) => {
var error, result;
result = this._mergeReadState(values[0], values[1]);
if (!result) {
error = new Error('Load story by theme error');
}
callback && callback(error, result);
if (error) {
reject(error);
} else {
var topDataRet;
if (values[1] && values[1].background) {
topDataRet = {};
topDataRet.description = values[1].description;
topDataRet.background = values[1].background;
topDataRet.editors = values[1].editors;
} else {
topDataRet = values[2];
}
result.topData = topDataRet;
resolve(result);
}
});
});
return merged;
};
DataRepository.prototype.saveStories = function(themeList, topData,
callback
) {
var homeList = themeList[0];
var keyValuePairs = [];
for (var date in homeList) {
if (homeList.hasOwnProperty(date)) {
//console.log(date, homeData[date]);
keyValuePairs.push([KEY_HOME_LIST + date, JSON.stringify({date: date, stories: homeList[date]})]);
}
}
for (var key in themeList) {
if (themeList.hasOwnProperty(key)) {
//console.log(key, data[key]);
if (key !== '0') {
keyValuePairs.push([KEY_THEME_LIST + key, JSON.stringify(themeList[key])]);
}
}
}
for (var theme in topData) {
if (topData.hasOwnProperty(theme)) {
//console.log(theme, topData[key]);
keyValuePairs.push([KEY_THEME_TOPDATA + theme, JSON.stringify(topData[theme])]);
}
}
AsyncStorage.multiSet(keyValuePairs, callback);
};
DataRepository.prototype.getThemes = function(
callback
) {
return this._safeStorage(KEY_THEMES)
.then((result) => {
if (!result) {
throw new Error('No themes')
} else {
return result;
}
})
.catch((error) => {
console.error(error);
return this._safeFetch(API_THEMES_URL)
.then((themes) => {
AsyncStorage.setItem(KEY_THEMES, JSON.stringify(themes));
return themes;
});
})
.then((responseData) => {
var themes = [];
if (responseData.subscribed) {
var len = responseData.subscribed.length;
var theme
for (var i = 0; i < len.length; i++) {
theme = responseData.subscribed[i];
theme.subscribed = true;
themes.push(theme);
}
}
if (responseData.others) {
themes = themes.concat(responseData.others);
}
return themes;
});
};
DataRepository.prototype._mergeReadState = function(src, dst) {
if (!src) {
return dst;
}
if (!dst) {
return src;
}
var reads = {};
var story;
for (var i = src.stories.length - 1; i >= 0 ; i--) {
story = src.stories[i];
reads[story.id] = story.read;
}
for (var i = dst.stories.length - 1; i >= 0 ; i--) {
story = dst.stories[i];
if (reads[story.id]) {
story.read = true;
}
}
return dst;
};
module.exports = DataRepository;