diff --git a/Apps.js b/Apps.js index 6f8e9c5..3310aed 100644 --- a/Apps.js +++ b/Apps.js @@ -8,7 +8,7 @@ import { } from "react-native"; import { WebView } from "react-native-webview"; import Constants from "expo-constants"; -import AsyncStorage from "@react-native-async-storage/async-storage"; +import { AS } from "./storageControl"; import { news } from "./config/newsUpdate"; import { getStationList, lineList } from "./lib/getStationList"; import { StationDeteilView } from "./components/ActionSheetComponents/StationDeteilView"; @@ -53,7 +53,7 @@ export default function Apps(props) { useEffect(() => { //ニュース表示 - AsyncStorage.getItem("status") + AS.getItem("status") .then((d) => { if (d != news) navigate("news"); }) @@ -62,48 +62,42 @@ export default function Apps(props) { useEffect(() => { //列車アイコンスイッチ - AsyncStorage.getItem("iconSwitch") + AS.getItem("iconSwitch") .then((d) => { if (d) { setIconSetting(d); } else { - AsyncStorage.setItem("iconSwitch", "true").then(Updates.reloadAsync); + AS.setItem("iconSwitch", "true").then(Updates.reloadAsync); } }) - .catch((d) => - AsyncStorage.setItem("iconSwitch", "true").then(Updates.reloadAsync) - ); + .catch((d) => AS.setItem("iconSwitch", "true").then(Updates.reloadAsync)); }, []); useEffect(() => { //地図スイッチ - AsyncStorage.getItem("mapSwitch") + AS.getItem("mapSwitch") .then((d) => { if (d) { setMapSwitch(d); } else { - AsyncStorage.setItem("mapSwitch", "false").then(Updates.reloadAsync); + AS.setItem("mapSwitch", "false").then(Updates.reloadAsync); } }) - .catch((d) => - AsyncStorage.setItem("mapSwitch", "false").then(Updates.reloadAsync) - ); + .catch((d) => AS.setItem("mapSwitch", "false").then(Updates.reloadAsync)); }, []); useEffect(() => { //駅メニュースイッチ - AsyncStorage.getItem("stationSwitch") + AS.getItem("stationSwitch") .then((d) => { if (d) { setStationMenu(d); } else { - AsyncStorage.setItem("stationSwitch", "true").then( - Updates.reloadAsync - ); + AS.setItem("stationSwitch", "true").then(Updates.reloadAsync); } }) .catch((d) => - AsyncStorage.setItem("stationSwitch", "true").then(Updates.reloadAsync) + AS.setItem("stationSwitch", "true").then(Updates.reloadAsync) ); }, []); diff --git a/components/news.js b/components/news.js index b9883d4..0b95e3b 100644 --- a/components/news.js +++ b/components/news.js @@ -2,7 +2,7 @@ import React from "react"; import { View, Text, TouchableOpacity } from "react-native"; import { WebView } from "react-native-webview"; import StatusbarDetect from "../StatusbarDetect"; -import AsyncStorage from "@react-native-async-storage/async-storage"; +import { AS } from "../storageControl"; import { news } from "../config/newsUpdate"; var Status = StatusbarDetect(); export default function News(props) { @@ -30,7 +30,7 @@ export default function News(props) { alignItems: "center", }} onPress={() => { - AsyncStorage.setItem("status", news); + AS.setItem("status", news); navigate("Apps"); }} > diff --git a/components/settings.js b/components/settings.js index 7d567b0..f0f7cbb 100644 --- a/components/settings.js +++ b/components/settings.js @@ -2,7 +2,7 @@ import React, { useState, useEffect } from "react"; import { View, Text, TouchableOpacity } from "react-native"; import * as Updates from "expo-updates"; import StatusbarDetect from "../StatusbarDetect"; -import AsyncStorage from "@react-native-async-storage/async-storage"; +import { AS } from "../storageControl"; var Status = StatusbarDetect(); import { Switch } from "react-native-elements"; @@ -14,9 +14,9 @@ export default function Setting(props) { const [mapSwitch, setMapSwitch] = useState(undefined); const [stationMenu, setStationMenu] = useState(undefined); useEffect(() => { - AsyncStorage.getItem("iconSwitch").then(setIconSetting); - AsyncStorage.getItem("mapSwitch").then(setMapSwitch); - AsyncStorage.getItem("stationSwitch").then(setStationMenu); + AS.getItem("iconSwitch").then(setIconSetting); + AS.getItem("mapSwitch").then(setMapSwitch); + AS.getItem("stationSwitch").then(setStationMenu); }, []); return ( @@ -134,9 +134,9 @@ export default function Setting(props) { }} onPress={() => { Promise.all([ - AsyncStorage.setItem("iconSwitch", iconSetting.toString()), - AsyncStorage.setItem("mapSwitch", mapSwitch.toString()), - AsyncStorage.setItem("stationSwitch", stationMenu.toString()), + AS.setItem("iconSwitch", iconSetting.toString()), + AS.setItem("mapSwitch", mapSwitch.toString()), + AS.setItem("stationSwitch", stationMenu.toString()), ]).then(() => { Updates.reloadAsync(); }); diff --git a/components/trainMenu.js b/components/trainMenu.js index 7e9f0e9..4c9d001 100644 --- a/components/trainMenu.js +++ b/components/trainMenu.js @@ -1,6 +1,5 @@ import React, { useRef } from "react"; import { View, Text, TouchableOpacity, Linking } from "react-native"; -import AsyncStorage from "@react-native-async-storage/async-storage"; import MapView, { Marker } from "react-native-maps"; import { MaterialCommunityIcons } from "@expo/vector-icons"; export default function trainMenu({ @@ -101,7 +100,6 @@ export default function trainMenu({ alignItems: "center", }} onPress={() => { - AsyncStorage.setItem("status", "2022/04/14"); navigate("Apps"); }} > diff --git a/storageConfig.js b/storageConfig.js new file mode 100644 index 0000000..8b1c3a0 --- /dev/null +++ b/storageConfig.js @@ -0,0 +1,27 @@ +import Storage from "react-native-storage"; +import AsyncStorage from "@react-native-async-storage/async-storage"; + +const storage = new Storage({ + // maximum capacity, default 1000 key-ids + size: 10000, + + // Use AsyncStorage for RN apps, or window.localStorage for web apps. + // If storageBackend is not set, data will be lost after reload. + storageBackend: AsyncStorage, // for web: window.localStorage + + // expire time, default: 1 day (1000 * 3600 * 24 milliseconds). + // can be null, which means never expire. + defaultExpires: null, + + // cache data in the memory. default is true. + enableCache: true, + + // if data was not found in storage or expired data was found, + // the corresponding sync method will be invoked returning + // the latest data. + sync: { + // we'll talk about the details later. + }, +}); + +export default storage; diff --git a/storageControl.js b/storageControl.js new file mode 100644 index 0000000..4f43140 --- /dev/null +++ b/storageControl.js @@ -0,0 +1,14 @@ +import storage from "./storageConfig.js"; + +export const AS = { + getItem: (key) => storage.load({ key }), + setItem: (key, data) => + storage.save({ + key, // Note: Do not use underscore("_") in key! + data, + + // if expires not specified, the defaultExpires will be applied instead. + // if set to null, then it will never expire. + expires: null, + }), +};