-
Notifications
You must be signed in to change notification settings - Fork 14
Reworking way of getting items. #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Lazloz
wants to merge
7
commits into
NamTThai:master
Choose a base branch
from
Lazloz:item
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d7b0ed3
New way of getting items.
Lazloz 7ece876
Merge branch 'master' of https://github.com/NamTThai/PokemonShowdownA…
Lazloz e469ff4
Removing unneeded class.
Lazloz 8ba8c21
Requested changes.
Lazloz bc17c0e
More requested changes.
Lazloz 6daf93d
Little changes to null handling.
Lazloz ffec16a
Merge branch 'master' of https://github.com/NamTThai/PokemonShowdownA…
Lazloz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 40 additions & 70 deletions
110
app/src/main/java/com/pokemonshowdown/data/ItemDex.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 { | ||
| 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; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.