From f8b2ff9cccf11d67c3e7db8d39e113553ebc1489 Mon Sep 17 00:00:00 2001 From: horhik Date: Tue, 24 Mar 2020 11:05:54 -0300 Subject: [PATCH] store ankiLan note creator and template in react native async storage --- android/app/build.gradle | 1 + .../java/com/ankilan/MainApplication.java | 1 + android/settings.gradle | 2 + ios/Podfile | 2 + src/actions/createAnkiLanModel.js | 6 +++ src/actions/filesystem.js | 40 +++++++++++++++++++ src/constants/anki-constants.js | 4 ++ src/reducers/anki-reducer.js | 2 +- 8 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/actions/filesystem.js diff --git a/android/app/build.gradle b/android/app/build.gradle index ce1e5c2..e1796ef 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -179,6 +179,7 @@ android { } dependencies { + implementation project(':@react-native-community_async-storage') implementation fileTree(dir: "libs", include: ["*.jar"]) implementation "com.facebook.react:react-native:+" // From node_modules diff --git a/android/app/src/main/java/com/ankilan/MainApplication.java b/android/app/src/main/java/com/ankilan/MainApplication.java index 69ee2fb..dcbf61c 100644 --- a/android/app/src/main/java/com/ankilan/MainApplication.java +++ b/android/app/src/main/java/com/ankilan/MainApplication.java @@ -4,6 +4,7 @@ import android.app.Application; import android.content.Context; import com.facebook.react.PackageList; import com.facebook.react.ReactApplication; +import com.reactnativecommunity.asyncstorage.AsyncStoragePackage; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.soloader.SoLoader; diff --git a/android/settings.gradle b/android/settings.gradle index af32d65..8078645 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -1,3 +1,5 @@ rootProject.name = 'ankilan' +include ':@react-native-community_async-storage' +project(':@react-native-community_async-storage').projectDir = new File(rootProject.projectDir, '../node_modules/@react-native-community/async-storage/android') apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) include ':app' diff --git a/ios/Podfile b/ios/Podfile index 74196d8..0cc14b5 100644 --- a/ios/Podfile +++ b/ios/Podfile @@ -34,6 +34,8 @@ target 'ankilan' do pod 'glog', :podspec => '../node_modules/react-native/third-party-podspecs/glog.podspec' pod 'Folly', :podspec => '../node_modules/react-native/third-party-podspecs/Folly.podspec' + pod 'RNCAsyncStorage', :path => '../node_modules/@react-native-community/async-storage' + target 'ankilanTests' do inherit! :search_paths # Pods for testing diff --git a/src/actions/createAnkiLanModel.js b/src/actions/createAnkiLanModel.js index 5e0f5ae..4290b53 100644 --- a/src/actions/createAnkiLanModel.js +++ b/src/actions/createAnkiLanModel.js @@ -1,5 +1,6 @@ import {AnkiDroid} from 'react-native-ankidroid/dist/ankidroid'; import {setAnkiNoteCreator, setCreatorTemplate} from './anki-set-actions'; +import sendDataToLocaleStorage from './filesystem'; import { checkAnkiLanModelForExisting, getFieldList, @@ -171,6 +172,10 @@ export const createAnkiLanModel = model => async dispatch => { const selectedDeck = new AnkiDroid(settings); await dispatch(setAnkiNoteCreator(selectedDeck)); await dispatch(setCreatorTemplate(modelFields)); + sendDataToLocaleStorage( + setAnkiNoteCreator(selectedDeck), //send creator to locale storage + setCreatorTemplate(modelFields), + ); addNote(selectedDeck, valueFields, modelFields); checkAnkiLanModelForExisting(model.name, model.list); await dispatch(getModelList()); @@ -180,6 +185,7 @@ export const createAnkiLanModel = model => async dispatch => { } }; +//creator is an object what have to store in locale storage. export const addNote = (creator, words, template) => { creator.addNote(words, template); }; diff --git a/src/actions/filesystem.js b/src/actions/filesystem.js new file mode 100644 index 0000000..4833258 --- /dev/null +++ b/src/actions/filesystem.js @@ -0,0 +1,40 @@ +import AsyncStorage from '@react-native-community/async-storage'; +import { + ANKILAN_NOTE_CREATOR, + ANKILAN_NOTE_TEMPLATE, +} from '../constants/anki-constants'; +const sendDataToLocaleStorage = async (creator, template) => { + try { + await AsyncStorage.setItem(ANKILAN_NOTE_CREATOR, JSON.stringify(creator)); + await AsyncStorage.setItem(ANKILAN_NOTE_TEMPLATE, JSON.stringify(template)); + } catch (e) { + // saving error + alert('Error while syncing with filesystem'); + console.log(e); + } +}; +export default sendDataToLocaleStorage; + +export const getTemplate = async () => { + try { + const value = await AsyncStorage.getItem(ANKILAN_NOTE_TEMPLATE); + if (value !== null) { + // value previously stored + return value; + } + } catch (e) { + // error reading value + } +}; + +export const getCreator = async () => { + try { + const value = await AsyncStorage.getItem(ANKILAN_NOTE_TEMPLATE); + if (value !== null) { + // value previously stored + return value; + } + } catch (e) { + // error reading value + } +}; diff --git a/src/constants/anki-constants.js b/src/constants/anki-constants.js index a94fd19..75b2927 100644 --- a/src/constants/anki-constants.js +++ b/src/constants/anki-constants.js @@ -22,3 +22,7 @@ export const SET_FIELDS = 'SET_FIELDS'; export const SHOW_FIELDS = 'SHOW_FIELDS'; // Anki check actions export const SET_EXISTING_OF_ANKI_LAN_MODEL = 'SET_EXISTING_OF_ANKI_LAN_MODEL'; + +//rn-async-storage kesy +export const ANKILAN_NOTE_CREATOR = '@ANKILAN_NOTE_CREATOR'; +export const ANKILAN_NOTE_TEMPLATE = '@ANKILAN_NOTE_TEMPLATE'; diff --git a/src/reducers/anki-reducer.js b/src/reducers/anki-reducer.js index 281d8ce..91042ff 100644 --- a/src/reducers/anki-reducer.js +++ b/src/reducers/anki-reducer.js @@ -20,7 +20,7 @@ const initialState = { mainFieldIsAvailable: false, fieldList: [], ankiLanModelIsAlreadyExists: false, - ankiLanModelName: 'AnkiLan_final', + ankiLanModelName: 'AnkiLan_final1', noteCreator: {}, noteTemplate: [], };