Skip to content

Commit

Permalink
refactor: extract setLanguageHintForField
Browse files Browse the repository at this point in the history
For use in testing 9618
  • Loading branch information
david-allison authored and mikehardy committed Oct 12, 2021
1 parent 72d84a1 commit bfeab4a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
7 changes: 2 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.ichi2.anki.dialogs.LocaleSelectionDialog;
import com.ichi2.anki.dialogs.ModelEditorContextMenu;
import com.ichi2.anki.exception.ConfirmModSchemaException;
import com.ichi2.anki.servicelayer.LanguageHintService;
import com.ichi2.async.CollectionTask;
import com.ichi2.async.TaskListenerWithContext;
import com.ichi2.async.TaskManager;
Expand Down Expand Up @@ -601,11 +602,7 @@ private void localeHintDialog() {
*/
@RequiresApi(api = Build.VERSION_CODES.N)
private void addFieldLocaleHint(@NonNull Locale selectedLocale) {
String input = selectedLocale.toLanguageTag();
JSONObject field = (JSONObject) mNoteFields.get(mCurrentPos);
field.put("ad-hint-locale", input);
mCol.getModels().save();
Timber.i("Set field locale to %s", selectedLocale);
LanguageHintService.setLanguageHintForField(getCol().getModels(), mMod, mCurrentPos, selectedLocale);
String format = getString(R.string.model_field_editor_language_hint_dialog_success_result, selectedLocale.getDisplayName());
UIUtils.showSimpleSnackbar(this, format, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package com.ichi2.anki.servicelayer

import android.widget.EditText
import androidx.annotation.CheckResult
import com.ichi2.libanki.Model
import com.ichi2.libanki.ModelManager
import com.ichi2.utils.JSONObject
import timber.log.Timber
import java.util.*

/**
Expand All @@ -36,4 +39,13 @@ object LanguageHintService {
}
return Locale.forLanguageTag(field.getString("ad-hint-locale"))
}

@JvmStatic
fun setLanguageHintForField(models: ModelManager, model: Model, fieldPos: Int, selectedLocale: Locale) {
val field = model.getField(fieldPos)
field.put("ad-hint-locale", selectedLocale.toLanguageTag())
models.save(model)

Timber.i("Set field locale to %s", selectedLocale)
}
}
4 changes: 4 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/libanki/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public List<String> getFieldsNames() {
return getJSONArray("flds").toStringList("name");
}

public JSONObject getField(int pos) {
return getJSONArray("flds").getJSONObject(pos);
}

public List<String> getTemplatesNames() {
return getJSONArray("tmpls").toStringList("name");
}
Expand Down

0 comments on commit bfeab4a

Please sign in to comment.