AsyncStorage→ReactNativeStorageへの変更

This commit is contained in:
harukin-DeskMini 2023-01-28 08:49:19 +09:00
parent 29963ab876
commit 1a1227dfeb
6 changed files with 61 additions and 28 deletions

28
Apps.js
View File

@ -8,7 +8,7 @@ import {
} from "react-native"; } from "react-native";
import { WebView } from "react-native-webview"; import { WebView } from "react-native-webview";
import Constants from "expo-constants"; import Constants from "expo-constants";
import AsyncStorage from "@react-native-async-storage/async-storage"; import { AS } from "./storageControl";
import { news } from "./config/newsUpdate"; import { news } from "./config/newsUpdate";
import { getStationList, lineList } from "./lib/getStationList"; import { getStationList, lineList } from "./lib/getStationList";
import { StationDeteilView } from "./components/ActionSheetComponents/StationDeteilView"; import { StationDeteilView } from "./components/ActionSheetComponents/StationDeteilView";
@ -53,7 +53,7 @@ export default function Apps(props) {
useEffect(() => { useEffect(() => {
//ニュース表示 //ニュース表示
AsyncStorage.getItem("status") AS.getItem("status")
.then((d) => { .then((d) => {
if (d != news) navigate("news"); if (d != news) navigate("news");
}) })
@ -62,48 +62,42 @@ export default function Apps(props) {
useEffect(() => { useEffect(() => {
//列車アイコンスイッチ //列車アイコンスイッチ
AsyncStorage.getItem("iconSwitch") AS.getItem("iconSwitch")
.then((d) => { .then((d) => {
if (d) { if (d) {
setIconSetting(d); setIconSetting(d);
} else { } else {
AsyncStorage.setItem("iconSwitch", "true").then(Updates.reloadAsync); AS.setItem("iconSwitch", "true").then(Updates.reloadAsync);
} }
}) })
.catch((d) => .catch((d) => AS.setItem("iconSwitch", "true").then(Updates.reloadAsync));
AsyncStorage.setItem("iconSwitch", "true").then(Updates.reloadAsync)
);
}, []); }, []);
useEffect(() => { useEffect(() => {
//地図スイッチ //地図スイッチ
AsyncStorage.getItem("mapSwitch") AS.getItem("mapSwitch")
.then((d) => { .then((d) => {
if (d) { if (d) {
setMapSwitch(d); setMapSwitch(d);
} else { } else {
AsyncStorage.setItem("mapSwitch", "false").then(Updates.reloadAsync); AS.setItem("mapSwitch", "false").then(Updates.reloadAsync);
} }
}) })
.catch((d) => .catch((d) => AS.setItem("mapSwitch", "false").then(Updates.reloadAsync));
AsyncStorage.setItem("mapSwitch", "false").then(Updates.reloadAsync)
);
}, []); }, []);
useEffect(() => { useEffect(() => {
//駅メニュースイッチ //駅メニュースイッチ
AsyncStorage.getItem("stationSwitch") AS.getItem("stationSwitch")
.then((d) => { .then((d) => {
if (d) { if (d) {
setStationMenu(d); setStationMenu(d);
} else { } else {
AsyncStorage.setItem("stationSwitch", "true").then( AS.setItem("stationSwitch", "true").then(Updates.reloadAsync);
Updates.reloadAsync
);
} }
}) })
.catch((d) => .catch((d) =>
AsyncStorage.setItem("stationSwitch", "true").then(Updates.reloadAsync) AS.setItem("stationSwitch", "true").then(Updates.reloadAsync)
); );
}, []); }, []);

View File

@ -2,7 +2,7 @@ import React from "react";
import { View, Text, TouchableOpacity } from "react-native"; import { View, Text, TouchableOpacity } from "react-native";
import { WebView } from "react-native-webview"; import { WebView } from "react-native-webview";
import StatusbarDetect from "../StatusbarDetect"; import StatusbarDetect from "../StatusbarDetect";
import AsyncStorage from "@react-native-async-storage/async-storage"; import { AS } from "../storageControl";
import { news } from "../config/newsUpdate"; import { news } from "../config/newsUpdate";
var Status = StatusbarDetect(); var Status = StatusbarDetect();
export default function News(props) { export default function News(props) {
@ -30,7 +30,7 @@ export default function News(props) {
alignItems: "center", alignItems: "center",
}} }}
onPress={() => { onPress={() => {
AsyncStorage.setItem("status", news); AS.setItem("status", news);
navigate("Apps"); navigate("Apps");
}} }}
> >

View File

@ -2,7 +2,7 @@ import React, { useState, useEffect } from "react";
import { View, Text, TouchableOpacity } from "react-native"; import { View, Text, TouchableOpacity } from "react-native";
import * as Updates from "expo-updates"; import * as Updates from "expo-updates";
import StatusbarDetect from "../StatusbarDetect"; import StatusbarDetect from "../StatusbarDetect";
import AsyncStorage from "@react-native-async-storage/async-storage"; import { AS } from "../storageControl";
var Status = StatusbarDetect(); var Status = StatusbarDetect();
import { Switch } from "react-native-elements"; import { Switch } from "react-native-elements";
@ -14,9 +14,9 @@ export default function Setting(props) {
const [mapSwitch, setMapSwitch] = useState(undefined); const [mapSwitch, setMapSwitch] = useState(undefined);
const [stationMenu, setStationMenu] = useState(undefined); const [stationMenu, setStationMenu] = useState(undefined);
useEffect(() => { useEffect(() => {
AsyncStorage.getItem("iconSwitch").then(setIconSetting); AS.getItem("iconSwitch").then(setIconSetting);
AsyncStorage.getItem("mapSwitch").then(setMapSwitch); AS.getItem("mapSwitch").then(setMapSwitch);
AsyncStorage.getItem("stationSwitch").then(setStationMenu); AS.getItem("stationSwitch").then(setStationMenu);
}, []); }, []);
return ( return (
<View style={{ height: "100%", backgroundColor: "#0099CC" }}> <View style={{ height: "100%", backgroundColor: "#0099CC" }}>
@ -134,9 +134,9 @@ export default function Setting(props) {
}} }}
onPress={() => { onPress={() => {
Promise.all([ Promise.all([
AsyncStorage.setItem("iconSwitch", iconSetting.toString()), AS.setItem("iconSwitch", iconSetting.toString()),
AsyncStorage.setItem("mapSwitch", mapSwitch.toString()), AS.setItem("mapSwitch", mapSwitch.toString()),
AsyncStorage.setItem("stationSwitch", stationMenu.toString()), AS.setItem("stationSwitch", stationMenu.toString()),
]).then(() => { ]).then(() => {
Updates.reloadAsync(); Updates.reloadAsync();
}); });

View File

@ -1,6 +1,5 @@
import React, { useRef } from "react"; import React, { useRef } from "react";
import { View, Text, TouchableOpacity, Linking } from "react-native"; 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 MapView, { Marker } from "react-native-maps";
import { MaterialCommunityIcons } from "@expo/vector-icons"; import { MaterialCommunityIcons } from "@expo/vector-icons";
export default function trainMenu({ export default function trainMenu({
@ -101,7 +100,6 @@ export default function trainMenu({
alignItems: "center", alignItems: "center",
}} }}
onPress={() => { onPress={() => {
AsyncStorage.setItem("status", "2022/04/14");
navigate("Apps"); navigate("Apps");
}} }}
> >

27
storageConfig.js Normal file
View File

@ -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;

14
storageControl.js Normal file
View File

@ -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,
}),
};