Skip to content

Commit

Permalink
Fix a few issues with localization. Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carifio24 committed Dec 9, 2024
1 parent 33d6d35 commit 92c236f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public void setup() {
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

assertEquals("dnd.jon.spellbook", appContext.getPackageName());
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/dnd/jon/spellbook/JSONUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ static Pair<Source, List<Spell>> sourceWithSpellsFromJSON(JSONObject json, Conte
final SpellBuilder builder = new SpellBuilder(context);
for (int i = 0; i < jsonSpells.length(); i++) {
final JSONObject item = jsonSpells.getJSONObject(i);
final Spell spell = codec.parseSpell(item, builder, false);
final Spell spell = codec.parseSpell(item, builder, true);
if (spell != null) {
spells.add(spell);
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/dnd/jon/spellbook/LocalizationUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ static String getCurrentLanguage() {
return getLocalizedContext(context, desiredLocale).getResources();
}

static @NonNull Locale getInternalLocale() {
return Locale.US;
}

static @NonNull Context getInternalContext(Context context) {
return getLocalizedContext(context, getInternalLocale());
}

static CasterClass[] supportedClasses() { return CasterClass.values(); }
static Source[] supportedSources() { return Source.values(); }
static Source[] supportedCoreSourcebooks() { return Source.coreSourcebooks(); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -70,7 +71,8 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
final OutputStream outputStream = activity.getContentResolver().openOutputStream(uri);
final Source source = viewModel.getCreatedSourceByName(exportName);
final Collection<Spell> spells = viewModel.getCreatedSpellsForSource(source);
final String json = JSONUtils.asJSON(source, activity, spells).toString(4);
final Context context = LocalizationUtils.getInternalContext(getContext());
final String json = JSONUtils.asJSON(source, context, spells).toString(4);
final byte[] bytes = json.getBytes(StandardCharsets.UTF_8);
outputStream.write(bytes);
} catch (IOException | JSONException e) {
Expand Down Expand Up @@ -124,7 +126,8 @@ public void onCopyEvent(String name) {
try {
final Source source = viewModel.getCreatedSourceByName(name);
final Collection<Spell> spells = viewModel.getCreatedSpellsForSource(source);
final String json = JSONUtils.asJSON(source, activity, spells).toString();
final Context context = LocalizationUtils.getInternalContext(getContext());
final String json = JSONUtils.asJSON(source, context, spells).toString();
final String jsonString = json.toString();
final String label = name + " JSON";
AndroidUtils.copyToClipboard(activity, jsonString, label);
Expand Down
7 changes: 5 additions & 2 deletions app/src/main/java/dnd/jon/spellbook/SpellCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ class SpellCodec {
private final String concentrationPrefix;
private final String ritualEnding;
private final Context context;
SpellCodec(Context context) {
final Locale locale = SpellbookUtils.coalesce(context.getResources().getConfiguration().getLocales().get(0), Locale.US);
SpellCodec(Context context, Locale locale) {
this.context = context;
final String language = locale.getLanguage();
this.concentrationPrefix = context.getString(concentrationPrefixMap.getOrDefault(language, R.string.concentration_prefix_en));
this.ritualEnding = context.getString(ritualEndingMap.getOrDefault(language, R.string.ritual_ending_en));
}

SpellCodec(Context context) {
this(context, SpellbookUtils.coalesce(context.getResources().getConfiguration().getLocales().get(0), Locale.US));
}


Spell parseSpell(JSONObject json, SpellBuilder b, boolean useInternal) throws JSONException {

Expand Down
18 changes: 10 additions & 8 deletions app/src/main/java/dnd/jon/spellbook/SpellbookViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -837,10 +836,9 @@ boolean addCreatedSource(Source source) {
}


Pair<Boolean,String> addSourceFromJSON(JSONObject json) {
Pair<Boolean,String> addSourceFromJSON(JSONObject json, Context context) {
String message;
boolean success = false;
final Context context = getContext();
try {
final Pair<Source, List<Spell>> result = JSONUtils.sourceWithSpellsFromJSON(json, context);
final Source source = result.getValue0();
Expand All @@ -866,6 +864,10 @@ Pair<Boolean,String> addSourceFromJSON(JSONObject json) {
return new Pair<>(success, message);
}

Pair<Boolean,String> addSourceFromJSON(JSONObject json) {
return addSourceFromJSON(json, getContext());
}

CharSequence getSearchQuery() { return searchQuery; }
void setSearchQuery(CharSequence searchQuery) {
this.searchQuery = searchQuery;
Expand Down Expand Up @@ -1115,7 +1117,7 @@ JSONObject allCreatedContent() throws JSONException {

final JSONArray sourcesJSON = new JSONArray();
final List<Source> sources = createdSourcesLD.getValue();
final Context context = getContext();
final Context context = LocalizationUtils.getInternalContext(getContext());
if (sources != null) {
for (Source source: sources) {
final JSONObject sourceJSON = JSONUtils.asJSON(source, context);
Expand All @@ -1140,27 +1142,27 @@ JSONObject allCreatedContent() throws JSONException {

boolean loadCreatedContent(JSONObject json) {
boolean anyFailures = false;
final Context context = LocalizationUtils.getInternalContext(getContext());
final JSONArray sources = json.optJSONArray("sources");
if (sources != null) {
for (int i = 0; i < sources.length(); i++) {
final JSONObject sourceJSON = sources.optJSONObject(i);
if (sourceJSON != null) {
final Pair<Boolean,String> result = addSourceFromJSON(sourceJSON);
final Pair<Boolean,String> result = addSourceFromJSON(sourceJSON, context);
anyFailures = anyFailures || !result.getValue0();
}
}
}

final JSONArray spells = json.optJSONArray("spells");
if (spells != null) {
final Context context = getContext();
final SpellCodec codec = new SpellCodec(context);
final SpellBuilder builder = new SpellBuilder(context);
final SpellCodec codec = new SpellCodec(context);
for (int i = 0; i < spells.length(); i++) {
final JSONObject spellJSON = spells.optJSONObject(i);
if (spellJSON != null) {
try {
final Spell spell = codec.parseSpell(spellJSON, builder, false);
final Spell spell = codec.parseSpell(spellJSON, builder, true);
addCreatedSpell(spell);
} catch (JSONException e) {
Log.e(LOGGING_TAG, e.getMessage());
Expand Down

0 comments on commit 92c236f

Please sign in to comment.