-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
decompress services -- add json parsing service and model items
- Loading branch information
1 parent
6049876
commit f92033e
Showing
13 changed files
with
380 additions
and
198 deletions.
There are no files selected for viewing
This file contains 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
38 changes: 38 additions & 0 deletions
38
src/main/java/io/github/lucasstarsz/fxdex/model/JsonDexEntryItem.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.github.lucasstarsz.fxdex.model; | ||
|
||
import io.github.lucasstarsz.fxdex.service.JsonParserService; | ||
import org.json.JSONObject; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class JsonDexEntryItem { | ||
|
||
private final String genus; | ||
private final String generation; | ||
private final List<String> eggGroups; | ||
private final Map<String, String> flavorTexts; | ||
|
||
public JsonDexEntryItem(JSONObject dexEntry, JsonParserService jsonParserService) { | ||
genus = jsonParserService.getPokemonGenus(dexEntry); | ||
generation = jsonParserService.getGenerationIntroducedIn(dexEntry); | ||
eggGroups = jsonParserService.getEggGroups(dexEntry); | ||
flavorTexts = jsonParserService.getFlavorTexts(dexEntry); | ||
} | ||
|
||
public String getGenus() { | ||
return genus; | ||
} | ||
|
||
public String getGeneration() { | ||
return generation; | ||
} | ||
|
||
public List<String> getEggGroups() { | ||
return eggGroups; | ||
} | ||
|
||
public Map<String, String> getFlavorTexts() { | ||
return flavorTexts; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/github/lucasstarsz/fxdex/model/JsonDexItem.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.lucasstarsz.fxdex.model; | ||
|
||
import org.json.JSONObject; | ||
|
||
public class JsonDexItem { | ||
|
||
private final String apiPokedexName; | ||
private final String apiPokedexUrl; | ||
|
||
public JsonDexItem(JSONObject dex) { | ||
this.apiPokedexName = dex.getString("name"); | ||
this.apiPokedexUrl = dex.getString("url"); | ||
} | ||
|
||
public String getApiPokedexName() { | ||
return apiPokedexName; | ||
} | ||
|
||
public String getApiPokedexUrl() { | ||
return apiPokedexUrl; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/github/lucasstarsz/fxdex/model/JsonDexListItem.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.lucasstarsz.fxdex.model; | ||
|
||
import org.json.JSONObject; | ||
|
||
public class JsonDexListItem { | ||
private final int dexNumber; | ||
private final String apiPokemonName; | ||
|
||
public JsonDexListItem(JSONObject dexEntryFromInfoList) { | ||
this.dexNumber = dexEntryFromInfoList.getInt("entry_number"); | ||
JSONObject pokemonSpecies = dexEntryFromInfoList.getJSONObject("pokemon_species"); | ||
this.apiPokemonName = pokemonSpecies.getString("name"); | ||
} | ||
|
||
public int getDexNumber() { | ||
return dexNumber; | ||
} | ||
|
||
public String getApiPokemonName() { | ||
return apiPokemonName; | ||
} | ||
} |
122 changes: 122 additions & 0 deletions
122
src/main/java/io/github/lucasstarsz/fxdex/service/DefaultUIService.java
This file contains 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,4 +1,126 @@ | ||
package io.github.lucasstarsz.fxdex.service; | ||
|
||
import com.google.inject.Inject; | ||
import io.github.lucasstarsz.fxdex.misc.StyleClasses; | ||
import javafx.beans.property.ListProperty; | ||
import javafx.beans.property.StringProperty; | ||
import javafx.geometry.Pos; | ||
import javafx.scene.control.Label; | ||
import javafx.scene.control.MenuItem; | ||
import javafx.scene.layout.HBox; | ||
import javafx.scene.layout.Region; | ||
import javafx.scene.layout.VBox; | ||
import org.apache.commons.text.WordUtils; | ||
import org.json.JSONObject; | ||
|
||
import java.util.List; | ||
|
||
import static io.github.lucasstarsz.fxdex.misc.ApiConversionTables.PokedexNameMap; | ||
|
||
public class DefaultUIService implements UiService { | ||
|
||
private final JsonParserService jsonParserService; | ||
|
||
@Inject | ||
public DefaultUIService(JsonParserService jsonParserService) { | ||
this.jsonParserService = jsonParserService; | ||
} | ||
|
||
@Override | ||
public List<MenuItem> createPokedexItems(JSONObject allDexes, ListProperty<Label> currentDexUi, | ||
StringProperty currentDexName, DexService dexService) { | ||
return jsonParserService.parsePokedexItems(allDexes) | ||
.stream() | ||
.map((jsonDexItem) -> { | ||
String apiPokedexName = jsonDexItem.getApiPokedexName(); | ||
String uiPokedexName = PokedexNameMap.get(apiPokedexName); | ||
|
||
MenuItem dexItem = new MenuItem(uiPokedexName); | ||
dexItem.setOnAction((event) -> dexService.loadPokedexList( | ||
currentDexUi, | ||
jsonDexItem, | ||
currentDexName | ||
)); | ||
return dexItem; | ||
}).toList(); | ||
} | ||
|
||
@Override | ||
public List<Region> createDexEntryUI(JSONObject dexEntryJSON, String currentDexEntry) { | ||
var dexEntryItem = jsonParserService.getDexEntryItem(dexEntryJSON); | ||
|
||
Label pokemonName = new Label(currentDexEntry); | ||
pokemonName.setId(StyleClasses.PokemonName); | ||
pokemonName.setText(pokemonName.getText().toUpperCase()); | ||
|
||
Label pokemonGenus = new Label(dexEntryItem.getGenus()); | ||
pokemonGenus.setId(StyleClasses.Subtitle); | ||
|
||
Label introduced = new Label(dexEntryItem.getGeneration()); | ||
introduced.setId(StyleClasses.Subtitle); | ||
|
||
HBox eggGroupContainer = new HBox(5); | ||
Label pokemonEggGroups = new Label(EggGroups); | ||
pokemonEggGroups.setMinWidth(75); | ||
eggGroupContainer.getChildren().add(pokemonEggGroups); | ||
|
||
var eggGroups = dexEntryItem.getEggGroups(); | ||
for (int i = 0; i < eggGroups.size(); i++) { | ||
String eggGroupString = eggGroups.get(i); | ||
if (i < eggGroups.size() - 1) { | ||
eggGroupString += ","; | ||
} | ||
|
||
Label eggGroupLabel = new Label(eggGroupString); | ||
eggGroupLabel.setWrapText(false); | ||
eggGroupContainer.getChildren().add(eggGroupLabel); | ||
} | ||
|
||
Label pokemonFlavorTexts = new Label(PokedexEntries); | ||
pokemonFlavorTexts.setId(StyleClasses.Subtitle); | ||
|
||
var flavorTexts = dexEntryItem.getFlavorTexts(); | ||
VBox flavorTextsContainer = new VBox(); | ||
List<HBox> flavorTextList = flavorTexts.entrySet().stream() | ||
.map((entry) -> { | ||
HBox container = new HBox(5); | ||
|
||
String gameNameString = entry.getKey() + ":"; | ||
gameNameString = gameNameString.replaceAll("-", " "); | ||
gameNameString = WordUtils.capitalize(gameNameString); | ||
|
||
Label gameName = new Label(gameNameString); | ||
gameName.setMinWidth(100); | ||
gameName.setWrapText(false); | ||
gameName.setAlignment(Pos.CENTER_RIGHT); | ||
|
||
String flavorTextString = entry.getValue(); | ||
flavorTextString = flavorTextString.replaceAll("([\n\f])", " "); | ||
flavorTextString = flavorTextString.replaceAll("- ", "-"); | ||
|
||
Label flavorText = new Label(flavorTextString); | ||
flavorText.setWrapText(true); | ||
|
||
container.getChildren().addAll(gameName, flavorText); | ||
container.setMinWidth(container.getWidth()); | ||
|
||
return container; | ||
}).toList(); | ||
|
||
flavorTextList.forEach((l) -> { | ||
l.setId(StyleClasses.Subtext); | ||
VBox.setMargin(l, InfoInsets); | ||
}); | ||
|
||
flavorTextsContainer.getChildren().addAll(flavorTextList); | ||
|
||
return List.of( | ||
pokemonName, | ||
pokemonGenus, | ||
introduced, | ||
eggGroupContainer, | ||
pokemonFlavorTexts, | ||
flavorTextsContainer | ||
); | ||
} | ||
} |
This file contains 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 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
100 changes: 100 additions & 0 deletions
100
src/main/java/io/github/lucasstarsz/fxdex/service/JsonParserService.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package io.github.lucasstarsz.fxdex.service; | ||
|
||
import io.github.lucasstarsz.fxdex.model.JsonDexEntryItem; | ||
import io.github.lucasstarsz.fxdex.model.JsonDexListItem; | ||
import io.github.lucasstarsz.fxdex.model.JsonDexItem; | ||
import org.apache.commons.text.WordUtils; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static io.github.lucasstarsz.fxdex.misc.ApiConversionTables.RomanNumeralMap; | ||
|
||
public interface JsonParserService { | ||
|
||
String IntroducedIn = "Introduced In: Generation "; | ||
String GenusNotFound = "Genus not found"; | ||
|
||
default List<JsonDexItem> parsePokedexItems(JSONObject dexesJSON) { | ||
int dexCount = dexesJSON.getInt("count"); | ||
List<JsonDexItem> dexItems = new ArrayList<>(dexCount); | ||
|
||
for (int i = 0; i < dexCount; i++) { | ||
JSONObject pokedex = dexesJSON.getJSONArray("results").getJSONObject(i); | ||
JsonDexItem dexItem = new JsonDexItem(pokedex); | ||
dexItems.add(dexItem); | ||
} | ||
|
||
return dexItems; | ||
} | ||
|
||
default JsonDexListItem parseDexItemIntoPokemon(JSONObject entry) { | ||
return new JsonDexListItem(entry); | ||
} | ||
|
||
default JsonDexEntryItem getDexEntryItem(JSONObject dexEntryJSON) { | ||
return new JsonDexEntryItem(dexEntryJSON, this); | ||
} | ||
|
||
default String getPokemonGenus(JSONObject dexEntryJSON) { | ||
String genus = GenusNotFound; | ||
JSONArray geneses = dexEntryJSON.getJSONArray("genera"); | ||
|
||
for (int i = 0; i < geneses.length(); i++) { | ||
JSONObject genusCandidate = geneses.getJSONObject(i); | ||
if (genusCandidate.getJSONObject("language").getString("name").equals("en")) { | ||
genus = genusCandidate.getString("genus"); | ||
break; | ||
} | ||
} | ||
|
||
return genus; | ||
} | ||
|
||
default List<String> getEggGroups(JSONObject dexEntryJSON) { | ||
List<String> eggGroups = new ArrayList<>(); | ||
JSONArray eggGroupJSON = dexEntryJSON.getJSONArray("egg_groups"); | ||
|
||
for (int i = 0; i < eggGroupJSON.length(); i++) { | ||
JSONObject eggGroup = eggGroupJSON.getJSONObject(i); | ||
String eggGroupString = eggGroup.getString("name"); | ||
|
||
// account for Human-Like egg group | ||
eggGroupString = eggGroupString.replaceAll("shape", " like"); | ||
eggGroupString = WordUtils.capitalize(eggGroupString); | ||
eggGroupString = eggGroupString.replaceAll(" ", "-"); | ||
|
||
eggGroups.add(eggGroupString); | ||
} | ||
|
||
return eggGroups; | ||
} | ||
|
||
default String getGenerationIntroducedIn(JSONObject dexEntryJSON) { | ||
String introducedInString = dexEntryJSON.getJSONObject("generation").getString("name"); | ||
introducedInString = introducedInString.replaceAll("generation-", ""); | ||
|
||
return IntroducedIn + RomanNumeralMap.get(introducedInString.toLowerCase()); | ||
} | ||
|
||
default Map<String, String> getFlavorTexts(JSONObject dexEntryJSON) { | ||
Map<String, String> flavorTexts = new LinkedHashMap<>(); | ||
JSONArray flavorTextsJSON = dexEntryJSON.getJSONArray("flavor_text_entries"); | ||
|
||
for (int i = 0; i < flavorTextsJSON.length(); i++) { | ||
JSONObject flavorTextCandidate = flavorTextsJSON.getJSONObject(i); | ||
|
||
if (flavorTextCandidate.getJSONObject("language").getString("name").equals("en")) { | ||
String flavorText = flavorTextCandidate.getString("flavor_text"); | ||
String textVersion = flavorTextCandidate.getJSONObject("version").getString("name"); | ||
flavorTexts.put(textVersion, flavorText); | ||
} | ||
} | ||
|
||
return flavorTexts; | ||
} | ||
} |
Oops, something went wrong.