From 2b44064fad925f34645354691ea5ec343fab4994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20D=C3=ADaz?= Date: Sun, 25 Sep 2016 18:11:48 -0400 Subject: [PATCH] Code refactor --- .../module/feature/encounter/Encounter.java | 35 +++++++++--------- .../encounter/EncounterPokemon.java} | 36 +++++++++---------- .../encounter/EncounterPokemonFactory.java | 34 ++++++++++++++++++ .../EncounterProbabilityFactory.java | 35 ++++++++++++++++++ app/src/main/res/values/strings.xml | 2 +- 5 files changed, 106 insertions(+), 36 deletions(-) rename app/src/main/java/com/icecream/snorlax/module/{util/Pokemons.java => feature/encounter/EncounterPokemon.java} (78%) create mode 100644 app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPokemonFactory.java create mode 100644 app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterProbabilityFactory.java diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java index 3fce3fb..478f22a 100644 --- a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/Encounter.java @@ -29,7 +29,6 @@ import com.icecream.snorlax.module.feature.Feature; import com.icecream.snorlax.module.feature.mitm.MitmRelay; import com.icecream.snorlax.module.util.Log; -import com.icecream.snorlax.module.util.Pokemons; import com.icecream.snorlax.module.util.RxFuncitons; import rx.Observable; @@ -49,13 +48,17 @@ public final class Encounter implements Feature { private final MitmRelay mMitmRelay; private final EncounterPreferences mPreferences; private final EncounterNotification mEncounterNotification; + private final EncounterPokemonFactory mEncounterPokemonFactory; + private final EncounterProbabilityFactory mEncounterProbabilityFactory; private Subscription mSubscription; @Inject - Encounter(MitmRelay mitmRelay, EncounterPreferences preferences, EncounterNotification encounterNotification) { + Encounter(MitmRelay mitmRelay, EncounterPreferences preferences, EncounterNotification encounterNotification, EncounterPokemonFactory encounterPokemonFactory, EncounterProbabilityFactory encounterProbabilityFactory) { mMitmRelay = mitmRelay; mPreferences = preferences; mEncounterNotification = encounterNotification; + mEncounterPokemonFactory = encounterPokemonFactory; + mEncounterProbabilityFactory = encounterProbabilityFactory; } private String formatMove(String move) { @@ -70,23 +73,21 @@ private String formatMove(String move) { } private void onEncounter(PokemonData data, CaptureProbability probability) { - // TODO factory - Pokemons pokemons = new Pokemons(data); - // TODO factory - EncounterProbability encounterProbability = new EncounterProbability(probability); + EncounterPokemon encounterPokemon = mEncounterPokemonFactory.create(data); + EncounterProbability encounterProbability = mEncounterProbabilityFactory.create(probability); mEncounterNotification.show( - pokemons.getNumber(), - pokemons.getName(), - pokemons.getIvPercentage(), - pokemons.getIndividualAttack(), - pokemons.getIndividualDefense(), - pokemons.getIndividualStamina(), - pokemons.getCp(), - pokemons.getLevel(), - pokemons.getStamina(), - formatMove(pokemons.getMove1().name()), - formatMove(pokemons.getMove2().name()), + encounterPokemon.getNumber(), + encounterPokemon.getName(), + encounterPokemon.getIvPercentage(), + encounterPokemon.getIndividualAttack(), + encounterPokemon.getIndividualDefense(), + encounterPokemon.getIndividualStamina(), + encounterPokemon.getCp(), + encounterPokemon.getLevel(), + encounterPokemon.getStamina(), + formatMove(encounterPokemon.getMove1().name()), + formatMove(encounterPokemon.getMove2().name()), encounterProbability.getWithPokeball(), encounterProbability.getWithPokeballAndBerry(), encounterProbability.getWithGreatball(), diff --git a/app/src/main/java/com/icecream/snorlax/module/util/Pokemons.java b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPokemon.java similarity index 78% rename from app/src/main/java/com/icecream/snorlax/module/util/Pokemons.java rename to app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPokemon.java index 8d1034c..5a707b2 100644 --- a/app/src/main/java/com/icecream/snorlax/module/util/Pokemons.java +++ b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPokemon.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.icecream.snorlax.module.util; +package com.icecream.snorlax.module.feature.encounter; import java.util.Locale; @@ -22,19 +22,19 @@ import static POGOProtos.Enums.PokemonMoveOuterClass.PokemonMove; @SuppressWarnings({"unused", "WeakerAccess"}) -public final class Pokemons { +final class EncounterPokemon { private PokemonData mPokemon; - public Pokemons(PokemonData pokemon) { + EncounterPokemon(PokemonData pokemon) { mPokemon = pokemon; } - public int getNumber() { + int getNumber() { return mPokemon.getPokemonId().getNumber(); } - public String getName() { + String getName() { StringBuilder builder = new StringBuilder(); for (String part : mPokemon.getPokemonId().name().split("_")) { builder @@ -45,7 +45,7 @@ public String getName() { return builder.toString().trim(); } - public float getLevel() { + float getLevel() { float level; float combinedCpMultiplier = getCombinedCpMultiplier(); @@ -58,51 +58,51 @@ public float getLevel() { return Math.round((level) * 2) / 2.0f; } - public float getCombinedCpMultiplier() { + float getCombinedCpMultiplier() { return getCpMultiplier() + getAdditionalCpMultiplier(); } - public float getCpMultiplier() { + float getCpMultiplier() { return mPokemon.getCpMultiplier(); } - public float getAdditionalCpMultiplier() { + float getAdditionalCpMultiplier() { return mPokemon.getAdditionalCpMultiplier(); } - public int getCp() { + int getCp() { return mPokemon.getCp(); } - public int getStamina() { + int getStamina() { return mPokemon.getStaminaMax(); } - public PokemonMove getMove1() { + PokemonMove getMove1() { return mPokemon.getMove1(); } - public PokemonMove getMove2() { + PokemonMove getMove2() { return mPokemon.getMove2(); } - public double getIvPercentage() { + double getIvPercentage() { return ((Math.floor((getIvRatio() * 100) * 100)) / 100); } - public double getIvRatio() { + double getIvRatio() { return ((double) (getIndividualAttack() + getIndividualDefense() + getIndividualStamina())) / 45.0; } - public int getIndividualAttack() { + int getIndividualAttack() { return mPokemon.getIndividualAttack(); } - public int getIndividualDefense() { + int getIndividualDefense() { return mPokemon.getIndividualDefense(); } - public int getIndividualStamina() { + int getIndividualStamina() { return mPokemon.getIndividualStamina(); } } diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPokemonFactory.java b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPokemonFactory.java new file mode 100644 index 0000000..50a59a7 --- /dev/null +++ b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterPokemonFactory.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2016. Pedro Diaz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.icecream.snorlax.module.feature.encounter; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import static POGOProtos.Data.PokemonDataOuterClass.PokemonData; + +@Singleton +final class EncounterPokemonFactory { + + @Inject + EncounterPokemonFactory() { + } + + EncounterPokemon create(PokemonData data) { + return new EncounterPokemon(data); + } +} diff --git a/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterProbabilityFactory.java b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterProbabilityFactory.java new file mode 100644 index 0000000..f5d2a6c --- /dev/null +++ b/app/src/main/java/com/icecream/snorlax/module/feature/encounter/EncounterProbabilityFactory.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2016. Pedro Diaz + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.icecream.snorlax.module.feature.encounter; + +import javax.inject.Inject; +import javax.inject.Singleton; + +import static POGOProtos.Data.Capture.CaptureProbabilityOuterClass.CaptureProbability; + +@Singleton +final class EncounterProbabilityFactory { + + @Inject + EncounterProbabilityFactory() { + } + + EncounterProbability create(CaptureProbability probability) { + return new EncounterProbability(probability); + } +} + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 38e00f4..4457cf9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -28,7 +28,7 @@ Encounter Notifications encounter_notification_key Enable encounter notifications - Show encountered pokemons stats via system notification + Show encountered encounterPokemon stats via system notification Catch Notifications catch_notification_key