Skip to content

Commit

Permalink
Merge pull request #69 from Carifio24/spell-json-locale
Browse files Browse the repository at this point in the history
Serialize spells to JSON using Locale-aware context
  • Loading branch information
Carifio24 authored May 10, 2024
2 parents 94e4388 + 99394ff commit 5806875
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
2 changes: 0 additions & 2 deletions app/src/main/java/dnd/jon/spellbook/DisplayUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.SortedSet;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.ToIntFunction;
Expand Down
22 changes: 11 additions & 11 deletions app/src/main/java/dnd/jon/spellbook/SpellCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class SpellCodec {

private static final String[] COMPONENT_STRINGS = { "V", "S", "M" };

// Is there a better way to do this?
// TODO: Is there a better way to do this?
// It doesn't seem like it
private static final Map<String,Integer> concentrationPrefixMap = new HashMap<String,Integer>() {{
put(Locale.US.getLanguage(), R.string.concentration_prefix_en);
put("pt", R.string.concentration_prefix_pt);
private static final Map<String,Integer> concentrationPrefixMap = new HashMap<>() {{
put(Locale.US.getLanguage(), R.string.concentration_prefix_en);
put("pt", R.string.concentration_prefix_pt);
}};

private final String concentrationPrefix;
Expand Down Expand Up @@ -184,23 +184,23 @@ List<Spell> parseSpellList(JSONArray jsonArray) throws Exception {
return parseSpellList(jsonArray, false, LocalizationUtils.getLocale());
}

JSONObject toJSON(Spell spell) throws JSONException {
JSONObject toJSON(Spell spell, Context context) throws JSONException {

final JSONObject json = new JSONObject();

json.put(ID_KEY, spell.getID());
json.put(NAME_KEY, spell.getName());
json.put(DESCRIPTION_KEY, spell.getDescription());
json.put(HIGHER_LEVEL_KEY, spell.getHigherLevel());
json.put(RANGE_KEY, spell.getRange().internalString());
json.put(RANGE_KEY, DisplayUtils.string(context, spell.getRange()));
json.put(MATERIAL_KEY, spell.getMaterial());
json.put(ROYALTY_KEY, spell.getRoyalty());
json.put(RITUAL_KEY, spell.getRitual());
json.put(DURATION_KEY, spell.getDuration().internalString());
json.put(DURATION_KEY, DisplayUtils.string(context, spell.getDuration()));
json.put(CONCENTRATION_KEY, spell.getConcentration());
json.put(CASTING_TIME_KEY, spell.getCastingTime().internalString());
json.put(CASTING_TIME_KEY, DisplayUtils.string(context, spell.getCastingTime()));
json.put(LEVEL_KEY, spell.getLevel());
json.put(SCHOOL_KEY, spell.getSchool().getInternalName());
json.put(SCHOOL_KEY, context.getString(spell.getSchool().getDisplayNameID()));

int i = 0;
final JSONArray locations = new JSONArray();
Expand All @@ -224,7 +224,7 @@ JSONObject toJSON(Spell spell) throws JSONException {
JSONArray classes = new JSONArray();
Collection<CasterClass> spellClasses = spell.getClasses();
for (CasterClass cc : spellClasses) {
classes.put(cc.getInternalName());
classes.put(DisplayUtils.getDisplayName(context, cc));
}
json.put(CLASSES_KEY, classes);

Expand All @@ -238,7 +238,7 @@ JSONObject toJSON(Spell spell) throws JSONException {
JSONArray expandedClasses = new JSONArray();
Collection<CasterClass> spellExpandedClasses = spell.getTashasExpandedClasses();
for (CasterClass cc : spellExpandedClasses) {
expandedClasses.put(cc.getInternalName());
expandedClasses.put(DisplayUtils.getDisplayName(context, cc));
}
json.put(TCE_EXPANDED_CLASSES_KEY, expandedClasses);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ boolean addCreatedSpell(Spell spell) {
boolean saveCreatedSpell(Spell spell) {
final String filename = spell.getName() + CREATED_SPELL_EXTENSION;
final File filepath = new File(createdSpellsDir, filename);
return JSONUtils.saveAsJSON(spell, spellCodec::toJSON, filepath);
return JSONUtils.saveAsJSON(spell, (s) -> spellCodec.toJSON(s, getContext()), filepath);
}

void updateSpell(Spell oldSpell, Spell newSpell) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/spell_creation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
android:hint="@string/spell_creation_name_hint"
android:autofillHints="@string/spell_creation_name_autofill_hints"
android:background="@android:color/transparent"
android:layout_marginStart="5dp"
/>

</LinearLayout>
Expand Down

0 comments on commit 5806875

Please sign in to comment.