这里只放了核心代码,具体完整的代码可以去仓库里看看github地址 这里本地存储数据用到的库官方文档地址AsyncStorage
import AsyncStorage from '@react-native-async-storage/async-storage';
export const FLAG_STORAGE = {
flag_popular: 'popular',
flag_trending: 'trending',
};
export default class DataStore {
fetchData(url, flag) {
return new Promise((resolve, reject) => {
this.fetchLocalData(url)
.then(wrapData => {
if (wrapData && DataStore.checkTimestampValid(wrapData.timestamp)) {
resolve(wrapData);
} else {
this.fetchNetData(url, flag)
.then(data => {
resolve(this._wrapData(data));
})
.catch(error => {
reject(error);
});
}
})
.catch(error => {
this.fetchNetData(url, flag)
.then(data => {
resolve(this._wrapData(data));
})
.catch(error => {
reject(error);
});
});
});
}
saveData(url, data, callback) {
if (!data || !url) return;
AsyncStorage.setItem(url, JSON.stringify(this._wrapData(data)), callback);
}
fetchLocalData(url) {
return new Promise((resolve, reject) => {
AsyncStorage.getItem(url, (error, result) => {
if (!error) {
try {
resolve(JSON.parse(result));
} catch (e) {
reject(e);
console.error(e);
}
} else {
reject(error);
console.error(error);
}
});
});
}
fetchNetData(url, flag) {
return new Promise((resolve, reject) => {
fetch(url)
.then(response => {
if (response.ok) {
return response.json();
}
throw new Error('Network response was not ok.');
})
.then(responseData => {
this.saveData(url, responseData);
resolve(responseData);
})
.catch(error => {
reject(error);
});
});
}
_wrapData(data) {
return {data: data, timestamp: new Date().getTime()};
}
static checkTimestampValid(timestamp) {
const currentDate = new Date();
const targetDate = new Date();
targetDate.setTime(timestamp);
if (currentDate.getMonth() !== targetDate.getMonth()) return false;
if (currentDate.getDate() !== targetDate.getDate()) return false;
if (currentDate.getHours() - targetDate.getHours() > 4) return false;
return true;
}
}
import Types from '../types';
import DataStore, {FLAG_STORAGE} from '../../expand/dao/DataStore';
import {_projectModels, handleData} from '../ActionUtil';
export function onRefreshPopular(storeName, url, pageSize, favoriteDao) {
return dispatch => {
dispatch({type: Types.POPULAR_REFRESH, storeName: storeName});
let dataStore = new DataStore();
dataStore
.fetchData(url, FLAG_STORAGE.flag_popular)
.then(data => {
handleData(
Types.POPULAR_REFRESH_SUCCESS,
dispatch,
storeName,
data,
pageSize,
favoriteDao,
);
})
.catch(error => {
console.log(error);
dispatch({
type: Types.POPULAR_REFRESH_FAIL,
storeName,
error,
});
});
};
}
export function onLoadMorePopular(
storeName,
pageIndex,
pageSize,
dataArray = [],
favoriteDao,
callBack,
) {
return dispatch => {
setTimeout(() => {
if ((pageIndex - 1) * pageSize >= dataArray.length) {
if (typeof callBack === 'function') {
callBack('no more');
}
dispatch({
type: Types.POPULAR_LOAD_MORE_FAIL,
error: 'no more',
storeName: storeName,
pageIndex: --pageIndex,
});
} else {
let max =
pageSize * pageIndex > dataArray.length
? dataArray.length
: pageSize * pageIndex;
_projectModels(dataArray.slice(0, max), favoriteDao, data => {
dispatch({
type: Types.POPULAR_LOAD_MORE_SUCCESS,
storeName,
pageIndex,
projectModels: data,
});
});
}
}, 500);
};
}
export function onFlushPopularFavorite(
storeName,
pageIndex,
pageSize,
dataArray = [],
favoriteDao,
) {
return dispatch => {
let max =
pageSize * pageIndex > dataArray.length
? dataArray.length
: pageSize * pageIndex;
_projectModels(dataArray.slice(0, max), favoriteDao, data => {
dispatch({
type: Types.FLUSH_POPULAR_FAVORITE,
storeName,
pageIndex,
projectModels: data,
});
});
};
}
|