Skip to content

Commit

Permalink
decompress services -- add json parsing service and model items
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasstarsz committed Jul 27, 2024
1 parent 6049876 commit f92033e
Show file tree
Hide file tree
Showing 13 changed files with 380 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class DexModule extends AbstractModule {

@Override
protected void configure() {
bind(JsonParserService.class).to(PokeApiJsonParserService.class);
bind(UiService.class).to(DefaultUIService.class);
bind(DexService.class).to(PokeApiDexService.class);
bind(HttpService.class).to(DefaultHttpService.class);
Expand Down
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 src/main/java/io/github/lucasstarsz/fxdex/model/JsonDexItem.java
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;
}
}
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;
}
}
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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.net.URISyntaxException;
import java.net.http.HttpRequest;

import static io.github.lucasstarsz.fxdex.misc.ApiConversionTables.PokedexNameToIdMap;

public class DexRequestOptions {
private final Property<URI> linkProperty;

Expand All @@ -35,8 +33,8 @@ private void setLinkAsDexEntry(String dexEntry) throws URISyntaxException {
linkProperty.setValue(new URI(ApiLinks.DexEntryUrl + dexEntry.toLowerCase() + '/'));
}

private void setLinkAsDexList(String pokedex) throws URISyntaxException {
linkProperty.setValue(new URI(ApiLinks.DexUrl + PokedexNameToIdMap.get(pokedex.toLowerCase()) + '/'));
private void setLinkAsDexList(String dexLink) throws URISyntaxException {
linkProperty.setValue(new URI(dexLink));
}

public HttpRequest buildGetRequest(HttpService httpService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.IOException;
import java.net.URISyntaxException;

import io.github.lucasstarsz.fxdex.model.JsonDexItem;
import javafx.beans.property.ListProperty;
import javafx.beans.property.StringProperty;
import javafx.scene.control.Label;
Expand All @@ -25,10 +26,12 @@

public interface DexService {

void loadPokedexesForMenu(ListProperty<Label> currentDex, MenuButton dexMenu, StringProperty currentDexDisplayedProperty)
void loadPokedexesForMenu(ListProperty<Label> currentDexUi, MenuButton dexMenu, StringProperty currentDexName)
throws IOException, InterruptedException, URISyntaxException;

void loadDefaultPokedex(ListProperty<Label> currentDex, StringProperty currentDexDisplayedProperty)
void loadPokedexList(ListProperty<Label> currentDexUi, JsonDexItem dexItem, StringProperty currentDexName);

void loadDefaultPokedex(ListProperty<Label> currentDexUi, StringProperty currentDexName)
throws IOException, InterruptedException, URISyntaxException;

void loadDexEntry(ListProperty<Region> dexEntriesProperty, String currentDexEntry)
Expand Down
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;
}
}
Loading

0 comments on commit f92033e

Please sign in to comment.