diff --git a/src/actions/anki-get-actions.js b/src/actions/anki-get-actions.js index 3aeeb8b..4610b75 100644 --- a/src/actions/anki-get-actions.js +++ b/src/actions/anki-get-actions.js @@ -91,17 +91,22 @@ const setExistingOfAnkiLanModel = existing => { payload: existing, }; }; -export const checkAnkiLanModelForExisting = name => async dispatch => { +export const checkAnkiLanModelForExisting = ( + name, + modelList, +) => async dispatch => { try { - const [err, res] = await AnkiDroid.getFieldList(name); - if (err) { - throw err; + for (let model of modelList) { + if (model.name === name) { + console.log('founded'); + await dispatch(setExistingOfAnkiLanModel(true)); + return true; + } } - console.log(res); - await dispatch(setExistingOfAnkiLanModel(true)); - return res; + const err = 'Model not found. Displaying message...'; + throw err; } catch (err) { + console.log(err); await dispatch(setExistingOfAnkiLanModel(false)); - await createAnkiLanModel(id); } }; diff --git a/src/actions/anki-set-actions.js b/src/actions/anki-set-actions.js index b65e170..fb13d2c 100644 --- a/src/actions/anki-set-actions.js +++ b/src/actions/anki-set-actions.js @@ -1,7 +1,21 @@ import {AnkiDroid} from 'react-native-ankidroid/dist/ankidroid'; -import {SET_DECK} from '../constants/anki-constants'; +import { + SET_ANKI_NOTE_CREATOR, + SET_CREATOR_TEMPLATE, + SET_DECK, +} from '../constants/anki-constants'; export const selectDeck = id => ({ type: SET_DECK, payload: id, }); + +export const setAnkiNoteCreator = creator => ({ + type: SET_ANKI_NOTE_CREATOR, + payload: creator, +}); + +export const setCreatorTemplate = template => ({ + type: SET_CREATOR_TEMPLATE, + payload: template, +}); diff --git a/src/actions/createAnkiLanModel.js b/src/actions/createAnkiLanModel.js index cfc7a4f..f9fa6cf 100644 --- a/src/actions/createAnkiLanModel.js +++ b/src/actions/createAnkiLanModel.js @@ -1,125 +1,14 @@ import {AnkiDroid} from 'react-native-ankidroid/dist/ankidroid'; +import {setAnkiNoteCreator, setCreatorTemplate} from './anki-set-actions'; +import {checkAnkiLanModelForExisting, getModelList} from './anki-get-actions'; export const createAnkiLanModel = model => async dispatch => { try { - /* - (async function() { - const [err, res] = await AnkiDroid.getSelectedDeckName(); - console.log(model.deck); - console.log(typeof res); - })(); - try { - const modelFields = [ - 'Word or sentence', - 'Part of speech 1', - 'Translate 1', - 'Definition 1', - 'Part of speech 2', - 'Translate 2', - 'Definition 2', - 'Example', - 'Transcription', - 'Sound', - ]; - - const css = ``; - - const answerFormat = ` - {{FrontSide}} - -
- -
    -
  1. -
    {{Translate 1}}
    Qq -
    {{Definition 1}}
    -
  2. - -
  3. -
    {{Translate 2}}
    -
    {{Definition 2}}
    -
  4. -
-

{Example}}

\`; -{{Sound}} - `; - const questionFormat = ` - {{Word or sentence}} -
-
{{Transcription}}
-
-
    -
  1. -
    {{Part of speech 1}}
    -
  2. -
  3. -
    {{Part of speech 2}}
    -
  4. -
- {{Sound}}`; - const modelProperties = { - name: model.name, - reference: 'com.ankilan.models', - fields: modelFields, - tags: [], - cardNames: ['Rus->En', 'En->Rus'], - questionFormat, - answerFormat, - css, - }; - const settings = { - modelId: model.id, - modelProperties, - deckId: model.deck.id, - }; - new AnkiDroid(settings).addNote( - [ - 'Word or sentence', - 'Part of speech 1', - 'Translate 1', - 'Definition 1', - 'Part of speech 2', - 'Translate 2', - 'Definition 2', - 'Example', - 'Transcription', - 'Sound', - ], - modelFields, - ); - } catch (err) { - console.log(err); - }*/ - - /////////////////////////////////// - // SETTING UP THE DECK AND MODEL // - /////////////////////////////////// - - // Name of deck which will be created in AnkiDroid const deckName = model.deck.name; - // Name of model which will be created in AnkiDroid (can be any string) - const modelName = model.name + '_test'; - // Used to save a reference to this deck in the SharedPreferences (can be any string) + const modelName = model.name; const dbDeckReference = 'com.anki.decks'; - // Used to save a reference to this model in the SharedPreferences (can be any string) const dbModelReference = 'com.anki.models'; - // Optional space separated list of tags to add to every note const tags = ['AnkiLan']; - // List of field names that will be used in AnkiDroid model const modelFields = [ 'Word or sentence', 'Part of speech 1', @@ -132,9 +21,7 @@ export const createAnkiLanModel = model => async dispatch => { 'Transcription', 'Sound', ]; - // List of card names that will be used in AnkiDroid (one for each direction of learning) const cardNames = ['Russian>English', 'English>Russian']; - // CSS to share between all the cards (optional). const css = `.card { font-family: 'Roboto', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif; font-size: 24px; @@ -167,33 +54,33 @@ export const createAnkiLanModel = model => async dispatch => { color: #1e7efc; font-weight: bold; } -.list{ -list-style: none; -display: flex; -flex-direction:column; -align-items:center; -padding: 0; -} -.definition{ -font-size: 20px; -font-famyli: sans-serif; -font-style: italic; -display: block; -margin: 7px ; -padding: 0 20px ; -white-space: normal; -width: 70vw; -} -.usage{ -background-color: rgba(100,100,100,0.1); -padding: 10px; -font-size: 20px; -white-space: normal; -width: 70vw; -margin: 0 auto; -border-radius: 5px; -}`; - // Template for the question of each card + .list{ + list-style: none; + display: flex; + flex-direction:column; + align-items:center; + padding: 0; + } + .definition{ + font-size: 20px; + font-famyli: sans-serif; + font-style: italic; + display: block; + margin: 7px ; + padding: 0 20px ; + white-space: normal; + width: 70vw; + } + .usage{ + background-color: rgba(100,100,100,0.1); + padding: 10px; + font-size: 20px; + white-space: normal; + width: 70vw; + margin: 0 auto; + border-radius: 5px; + }`; + const questionFmt1 = `
{{Word or sentence}}