Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions app/src/main/java/com/pokemonshowdown/app/DmgCalcActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ private int calculateDamageRoutine(int moveIndex, double luck, boolean crit) {
private String modifyAttackType(String move, String type) {
String attackerAbility = getAttacker().getAbility();
if ("naturalgift".equals(move)) {
JSONObject itemObject = ItemDex.get(this).getItemJsonObject(getAttacker().getItem());
JSONObject itemObject = ItemDex.getItemJsonObject(this, getAttacker().getItem());
JSONObject naturalGiftObject = itemObject == null ? null : itemObject.optJSONObject("naturalGift");
if (naturalGiftObject != null) {
type = naturalGiftObject.optString("type", type);
Expand Down Expand Up @@ -546,10 +546,10 @@ private String modifyAttackType(String move, String type) {
} else if (attackerAbility.equals("Normalize")) {
type = "Normal";
} else if ("technoblast".equals(move)) {
JSONObject itemObject = ItemDex.get(this).getItemJsonObject(getAttacker().getItem());
JSONObject itemObject = ItemDex.getItemJsonObject(this, getAttacker().getItem());
type = itemObject == null ? null : itemObject.optString("onDrive", type);
} else if ("judgement".equals(move)) {
JSONObject itemObject = ItemDex.get(this).getItemJsonObject(getAttacker().getItem());
JSONObject itemObject = ItemDex.getItemJsonObject(this, getAttacker().getItem());
type = itemObject == null ? "Normal" : itemObject.optString("onPlate", "Normal");
}
return type;
Expand Down Expand Up @@ -842,7 +842,7 @@ private String[] getRealTyping(Pokemon pokemon) {
break;
}
} else if ("Multitype".equals(pokemon.getAbility()) && pokemon.getName().contains("Arceus")) {
JSONObject itemObject = ItemDex.get(this).getItemJsonObject(getAttacker().getItem());
JSONObject itemObject = ItemDex.getItemJsonObject(this, getAttacker().getItem());
typing = itemObject == null ? new String[]{"Normal"} : new String[]{itemObject.optString("onPlate", "Normal")};
}
return typing;
Expand Down Expand Up @@ -948,7 +948,7 @@ private double calculateBasePower(String move, String type, double bp) {
bp = getDefender().getItem().isEmpty() || getDefender().getItem() == null ? 65 : 97.5;
break;
case "fling":
JSONObject obj = ItemDex.get(this).getItemJsonObject(getAttacker().getItem());
JSONObject obj = ItemDex.getItemJsonObject(this, getAttacker().getItem());
JSONObject flingObject = obj == null ? null : obj.optJSONObject("fling");
if (flingObject != null) {
bp = flingObject.optInt("basePower", 0);
Expand Down Expand Up @@ -990,7 +990,7 @@ private double calculateBasePower(String move, String type, double bp) {
bp = 102;
break;
case "naturalgift":
obj = ItemDex.get(this).getItemJsonObject(getAttacker().getItem());
obj = ItemDex.getItemJsonObject(this, getAttacker().getItem());
JSONObject naturalGiftObject = obj == null ? null : obj.optJSONObject("naturalGift");
if (naturalGiftObject != null) {
bp = naturalGiftObject.optInt("basePower", 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
break;
case REQUEST_CODE_SEARCH_ITEM:
String item = data.getExtras().getString(SearchableActivity.SEARCH);
JSONObject itemJson = ItemDex.get(getApplicationContext()).getItemJsonObject(item);
JSONObject itemJson = ItemDex.getItemJsonObject(getApplicationContext(), item);
dialog = new AlertDialog.Builder(this)
.setTitle(itemJson.getString("name"))
.setMessage(itemJson.getString("desc"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
getPokemon().setItem(item);

if (mPokemonItem != null) {
JSONObject itemObject = ItemDex.get(getActivity()).getItemJsonObject(item);
JSONObject itemObject = ItemDex.getItemJsonObject(getActivity(), item);
if (itemObject != null) {
int itemDrawable = ItemDex.getItemIcon(getActivity(), item);
mPokemonItem.setCompoundDrawablesWithIntrinsicBounds(itemDrawable != 0 ? getResources().getDrawable(itemDrawable) : null, null, null, null);
Expand Down Expand Up @@ -312,7 +312,7 @@ public void onStopTrackingTouch(SeekBar seekBar) {
mPokemonItem = (TextView) view.findViewById(R.id.pokemon_fragment_item);
String item = getPokemon().getItem();
if (getPokemon().getItem() != null && !getPokemon().getItem().isEmpty()) {
JSONObject itemObject = ItemDex.get(getActivity()).getItemJsonObject(item);
JSONObject itemObject = ItemDex.getItemJsonObject(getActivity(), item);
if (itemObject != null) {
int itemDrawable = ItemDex.getItemIcon(getActivity(), item);
mPokemonItem.setCompoundDrawablesWithIntrinsicBounds(itemDrawable != 0 ? getResources().getDrawable(itemDrawable) : null, null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public void onClick(DialogInterface arg0, int arg1) {
itemNameTextView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null);
} else {
String itemString = pokemon.getItem();
JSONObject itemJSon = ItemDex.get(getActivity()).getItemJsonObject(itemString);
JSONObject itemJSon = ItemDex.getItemJsonObject(getActivity(), itemString);
if (itemJSon != null) {
try {
String itemName = itemJSon.getString("name");
Expand Down
110 changes: 40 additions & 70 deletions app/src/main/java/com/pokemonshowdown/data/ItemDex.java
Original file line number Diff line number Diff line change
@@ -1,101 +1,71 @@
package com.pokemonshowdown.data;

import android.content.Context;
import android.util.Log;

import com.pokemonshowdown.app.R;
import com.pokemonshowdown.application.MyApplication;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Arrays;
import java.util.List;

public class ItemDex {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we don't need to read the Json file anymore I think we should change this class into a Utility class instead of a Singleton. Which mean all methods are static, we don't save context and we don't keep mItemDexEntries in memory anymore.

private final static String ITAG = ItemDex.class.getName();
private static ItemDex sItemDex;
private HashMap<String, String> mItemDexEntries;
public final static JSONObject DUMMY_JSON_ITEM;
public final static String DUMMY_ITEM = "Item not loadable.";

private ItemDex(Context appContext) {
mItemDexEntries = readFile(appContext);
}

private HashMap<String, String> readFile(Context appContext) {
HashMap<String, String> ItemDexEntries = new HashMap<>();
String jsonString;
static {
try {
InputStream inputStream = appContext.getResources().openRawResource(R.raw.item);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder stringBuilder = new StringBuilder();

String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
}
jsonString = stringBuilder.toString();
inputStream.close();

JSONObject jsonObject = new JSONObject(jsonString);

Iterator<String> keys = jsonObject.keys();

while (keys.hasNext()) {
String key = keys.next();
JSONObject entry = jsonObject.getJSONObject(key);
ItemDexEntries.put(key, entry.toString());
}
DUMMY_JSON_ITEM = new JSONObject("{\"gen\":1,\"spritenum\":0,\"num\":0,\"name\":\"Broken Item ID\",\"id\":\"dummy\",\"desc\":\"This item should not be displayed here. Please report this bug.\"}");
} catch (JSONException e) {
Log.d(ITAG, "JSON Exception");
} catch (IOException e) {
Log.d(ITAG, "Input Output problem");
// Will not happen, but is needed because it's final.
throw new Error();
}

return ItemDexEntries;
}

public static ItemDex get(Context c) {
if (sItemDex == null) {
sItemDex = new ItemDex(c.getApplicationContext());
}
return sItemDex;
}

public static int getItemIcon(Context appContext, String itemName) {
return appContext.getResources()
.getIdentifier("item_" + MyApplication.toId(itemName), "drawable", appContext.getPackageName());
if (appContext != null && itemName != null) {
return appContext.getResources()
.getIdentifier("item_" + MyApplication.toId(itemName), "drawable", appContext.getPackageName());
} else {
return R.drawable.sprites_0;
}
}

public HashMap<String, String> getItemDexEntries() {
return mItemDexEntries;
public static List<String> getItemDexEntries(Context context) {
return Arrays.asList(context.getResources().getStringArray(R.array.itemdex_ids));
}

public String getItemName(String name) {
name = MyApplication.toId(name);
String item;
try {
item = mItemDexEntries.get(name);
if (item != null) {
JSONObject itemEntries = new JSONObject(item);
item = itemEntries.getString("name");
public static String getItemName(Context context, String itemId) {
if (context != null && itemId != null) {
itemId = MyApplication.toId(itemId);
try {
int stringId = context.getResources().getIdentifier(itemId, "string", context.getPackageName());
if(stringId != 0) {
String itemName = context.getResources().getString(stringId);
JSONObject itemEntries = new JSONObject(itemName);
return itemEntries.getString("name");
}
} catch (JSONException e) {
}
} catch (JSONException e) {
item = null;
}
return item;
return DUMMY_ITEM;
}

public JSONObject getItemJsonObject(String name) {
try {
String item = mItemDexEntries.get(MyApplication.toId(name));
return new JSONObject(item);
} catch (NullPointerException | JSONException e) {
return null;
public static JSONObject getItemJsonObject(Context context, String name) {
if(context != null && name != null) {
try {
name = MyApplication.toId(name);
int stringId = context.getResources().getIdentifier(name, "string", context.getPackageName());
if (stringId != 0) {
String item = context.getString(stringId);
return new JSONObject(item);
}
} catch (JSONException e) {
}
}
return DUMMY_JSON_ITEM;
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/pokemonshowdown/data/Pokemon.java
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ public String exportPokemon(Context appContext) {
sb.append(" (").append(getGender().toUpperCase()).append(")");
}
if (getItem().length() > 0) {
JSONObject itemJSon = ItemDex.get(appContext).getItemJsonObject(getItem());
JSONObject itemJSon = ItemDex.getItemJsonObject(appContext, getItem());
if (itemJSon != null) {
try {
String itemName = itemJSon.getString("name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void setItem(String item) {

public String getItemName(Context activityContext) {
if (mItem != null) {
return ItemDex.get(activityContext).getItemName(mItem);
return ItemDex.getItemName(activityContext, mItem);
} else {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SearchableActivity extends ListActivity {
public final static String STAG = SearchableActivity.class.getName();
Expand Down Expand Up @@ -65,8 +66,7 @@ public void onCreate(Bundle savedInstanceState) {
getActionBar().setTitle(R.string.search_label_ability);
break;
case REQUEST_CODE_SEARCH_ITEM:
HashMap<String, String> itemDex = ItemDex.get(getApplicationContext()).getItemDexEntries();
mAdapterList = new ArrayList<>(itemDex.keySet());
mAdapterList = new ArrayList<>(ItemDex.getItemDexEntries(getApplicationContext()));
mAdapter = new ItemAdapter(this, mAdapterList);
setListAdapter(mAdapter);
getActionBar().setTitle(R.string.search_label_item);
Expand Down Expand Up @@ -151,9 +151,9 @@ private void searchAbility(String query) {
}

private void searchItem(String query) {
HashMap<String, String> itemDex = ItemDex.get(getApplicationContext()).getItemDexEntries();
List<String> itemDex = ItemDex.getItemDexEntries(getApplicationContext());
mAdapterList = new ArrayList<>();
for (String itemName : itemDex.keySet()) {
for (String itemName : itemDex) {
if (itemName.contains(query.toLowerCase())) {
mAdapterList.add(itemName);
}
Expand Down Expand Up @@ -321,7 +321,7 @@ public View getView(int position, View convertView, ViewGroup parent) {

try {
String itemTag = getItem(position);
JSONObject itemJson = ItemDex.get(getApplicationContext()).getItemJsonObject(itemTag);
JSONObject itemJson = ItemDex.getItemJsonObject(getApplicationContext(), itemTag);
TextView textView = (TextView) convertView.findViewById(R.id.short_item_name);
textView.setText(itemJson.getString("name"));
textView.setCompoundDrawablesWithIntrinsicBounds(ItemDex.getItemIcon(getApplicationContext(), itemTag), 0, 0, 0);
Expand Down
1 change: 0 additions & 1 deletion app/src/main/res/raw/item.json

This file was deleted.

Loading