Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
igoticecream committed Sep 25, 2016
1 parent 1e6c261 commit 2b44064
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@
* limitations under the License.
*/

package com.icecream.snorlax.module.util;
package com.icecream.snorlax.module.feature.encounter;

import java.util.Locale;

import static POGOProtos.Data.PokemonDataOuterClass.PokemonData;
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
Expand All @@ -45,7 +45,7 @@ public String getName() {
return builder.toString().trim();
}

public float getLevel() {
float getLevel() {
float level;
float combinedCpMultiplier = getCombinedCpMultiplier();

Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2016. Pedro Diaz <igoticecream@gmail.com>
*
* 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);
}
}

2 changes: 1 addition & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<string name="preference_encounter_notification">Encounter Notifications</string>
<string name="preference_encounter_notification_enable_key">encounter_notification_key</string>
<string name="preference_encounter_notification_enable_title">Enable encounter notifications</string>
<string name="preference_encounter_notification_enable_summary">Show encountered pokemons stats via system notification</string>
<string name="preference_encounter_notification_enable_summary">Show encountered encounterPokemon stats via system notification</string>

<string name="preference_catch_notification">Catch Notifications</string>
<string name="preference_catch_notification_enable_key">catch_notification_key</string>
Expand Down

0 comments on commit 2b44064

Please sign in to comment.