Adds better change tracking to know if a monster needs to be saved.

This commit is contained in:
2021-05-31 14:34:42 -07:00
parent 8fd313e3df
commit a6cced2ccf
2 changed files with 226 additions and 287 deletions

View File

@@ -10,150 +10,147 @@ import com.majinnaibu.monstercards.data.enums.ArmorType;
import com.majinnaibu.monstercards.data.enums.ProficiencyType; import com.majinnaibu.monstercards.data.enums.ProficiencyType;
import com.majinnaibu.monstercards.helpers.StringHelper; import com.majinnaibu.monstercards.helpers.StringHelper;
import com.majinnaibu.monstercards.models.Monster; import com.majinnaibu.monstercards.models.Monster;
import com.majinnaibu.monstercards.utils.Logger; import com.majinnaibu.monstercards.utils.ChangeTrackedLiveData;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
@SuppressWarnings({"ConstantConditions", "unused"}) @SuppressWarnings({"ConstantConditions", "unused"})
public class EditMonsterViewModel extends ViewModel { public class EditMonsterViewModel extends ViewModel {
private final MutableLiveData<UUID> mMonsterId; private final ChangeTrackedLiveData<UUID> mMonsterId;
private final MutableLiveData<Boolean> mHasError; private final MutableLiveData<Boolean> mHasError;
private final MutableLiveData<Boolean> mHasLoaded; private final MutableLiveData<Boolean> mHasLoaded;
private final MutableLiveData<Boolean> mHasChanges; private final MutableLiveData<Boolean> mHasChanges;
private final MutableLiveData<Boolean> mHasCustomHitPoints; private final ChangeTrackedLiveData<Boolean> mHasCustomHitPoints;
private final MutableLiveData<Boolean> mHasShield; private final ChangeTrackedLiveData<Boolean> mHasShield;
private final MutableLiveData<Boolean> mCanHover; private final ChangeTrackedLiveData<Boolean> mCanHover;
private final MutableLiveData<Boolean> mHasCustomSpeed; private final ChangeTrackedLiveData<Boolean> mHasCustomSpeed;
private final MutableLiveData<ArmorType> mArmorType; private final ChangeTrackedLiveData<ArmorType> mArmorType;
private final MutableLiveData<ProficiencyType> mStrengthProficiency; private final ChangeTrackedLiveData<ProficiencyType> mStrengthProficiency;
private final MutableLiveData<AdvantageType> mStrengthAdvantage; private final ChangeTrackedLiveData<AdvantageType> mStrengthAdvantage;
private final MutableLiveData<ProficiencyType> mDexterityProficiency; private final ChangeTrackedLiveData<ProficiencyType> mDexterityProficiency;
private final MutableLiveData<AdvantageType> mDexterityAdvantage; private final ChangeTrackedLiveData<AdvantageType> mDexterityAdvantage;
private final MutableLiveData<ProficiencyType> mConstitutionProficiency; private final ChangeTrackedLiveData<ProficiencyType> mConstitutionProficiency;
private final MutableLiveData<AdvantageType> mConstitutionAdvantage; private final ChangeTrackedLiveData<AdvantageType> mConstitutionAdvantage;
private final MutableLiveData<ProficiencyType> mIntelligenceProficiency; private final ChangeTrackedLiveData<ProficiencyType> mIntelligenceProficiency;
private final MutableLiveData<AdvantageType> mIntelligenceAdvantage; private final ChangeTrackedLiveData<AdvantageType> mIntelligenceAdvantage;
private final MutableLiveData<ProficiencyType> mWisdomProficiency; private final ChangeTrackedLiveData<ProficiencyType> mWisdomProficiency;
private final MutableLiveData<AdvantageType> mWisdomAdvantage; private final ChangeTrackedLiveData<AdvantageType> mWisdomAdvantage;
private final MutableLiveData<ProficiencyType> mCharismaProficiency; private final ChangeTrackedLiveData<ProficiencyType> mCharismaProficiency;
private final MutableLiveData<AdvantageType> mCharismaAdvantage; private final ChangeTrackedLiveData<AdvantageType> mCharismaAdvantage;
private final MutableLiveData<Integer> mHitDice; private final ChangeTrackedLiveData<Integer> mHitDice;
private final MutableLiveData<Integer> mNaturalArmorBonus; private final ChangeTrackedLiveData<Integer> mNaturalArmorBonus;
private final MutableLiveData<Integer> mShieldBonus; private final ChangeTrackedLiveData<Integer> mShieldBonus;
private final MutableLiveData<Integer> mWalkSpeed; private final ChangeTrackedLiveData<Integer> mWalkSpeed;
private final MutableLiveData<Integer> mBurrowSpeed; private final ChangeTrackedLiveData<Integer> mBurrowSpeed;
private final MutableLiveData<Integer> mClimbSpeed; private final ChangeTrackedLiveData<Integer> mClimbSpeed;
private final MutableLiveData<Integer> mFlySpeed; private final ChangeTrackedLiveData<Integer> mFlySpeed;
private final MutableLiveData<Integer> mSwimSpeed; private final ChangeTrackedLiveData<Integer> mSwimSpeed;
private final MutableLiveData<Integer> mStrength; private final ChangeTrackedLiveData<Integer> mStrength;
private final MutableLiveData<Integer> mDexterity; private final ChangeTrackedLiveData<Integer> mDexterity;
private final MutableLiveData<Integer> mConstitution; private final ChangeTrackedLiveData<Integer> mConstitution;
private final MutableLiveData<Integer> mIntelligence; private final ChangeTrackedLiveData<Integer> mIntelligence;
private final MutableLiveData<Integer> mWisdom; private final ChangeTrackedLiveData<Integer> mWisdom;
private final MutableLiveData<Integer> mCharisma; private final ChangeTrackedLiveData<Integer> mCharisma;
private final MutableLiveData<String> mName; private final ChangeTrackedLiveData<String> mName;
private final MutableLiveData<String> mErrorMessage; private final MutableLiveData<String> mErrorMessage;
private final MutableLiveData<String> mSize; private final ChangeTrackedLiveData<String> mSize;
private final MutableLiveData<String> mType; private final ChangeTrackedLiveData<String> mType;
private final MutableLiveData<String> mSubtype; private final ChangeTrackedLiveData<String> mSubtype;
private final MutableLiveData<String> mAlignment; private final ChangeTrackedLiveData<String> mAlignment;
private final MutableLiveData<String> mCustomHitPoints; private final ChangeTrackedLiveData<String> mCustomHitPoints;
private final MutableLiveData<String> mCustomArmor; private final ChangeTrackedLiveData<String> mCustomArmor;
private final MutableLiveData<String> mCustomSpeed; private final ChangeTrackedLiveData<String> mCustomSpeed;
public EditMonsterViewModel() { public EditMonsterViewModel() {
mName = new MutableLiveData<>("");
mMonsterId = new MutableLiveData<>(UUID.randomUUID());
mErrorMessage = new MutableLiveData<>(""); mErrorMessage = new MutableLiveData<>("");
mHasError = new MutableLiveData<>(false); mHasError = new MutableLiveData<>(false);
mHasLoaded = new MutableLiveData<>(false); mHasLoaded = new MutableLiveData<>(false);
mSize = new MutableLiveData<>("");
mType = new MutableLiveData<>("");
mSubtype = new MutableLiveData<>("");
mAlignment = new MutableLiveData<>("");
mCustomHitPoints = new MutableLiveData<>("");
mHitDice = new MutableLiveData<>(0);
mNaturalArmorBonus = new MutableLiveData<>(0);
mHasCustomHitPoints = new MutableLiveData<>(false);
mArmorType = new MutableLiveData<>(ArmorType.NONE);
mHasShield = new MutableLiveData<>(false);
mShieldBonus = new MutableLiveData<>(0);
mCustomArmor = new MutableLiveData<>("");
mWalkSpeed = new MutableLiveData<>(0);
mBurrowSpeed = new MutableLiveData<>(0);
mClimbSpeed = new MutableLiveData<>(0);
mFlySpeed = new MutableLiveData<>(0);
mSwimSpeed = new MutableLiveData<>(0);
mCanHover = new MutableLiveData<>(false);
mHasCustomSpeed = new MutableLiveData<>(false);
mCustomSpeed = new MutableLiveData<>("");
mStrength = new MutableLiveData<>(10);
mDexterity = new MutableLiveData<>(10);
mConstitution = new MutableLiveData<>(10);
mIntelligence = new MutableLiveData<>(10);
mWisdom = new MutableLiveData<>(10);
mCharisma = new MutableLiveData<>(10);
mStrengthProficiency = new MutableLiveData<>(ProficiencyType.NONE);
mStrengthAdvantage = new MutableLiveData<>(AdvantageType.NONE);
mDexterityProficiency = new MutableLiveData<>(ProficiencyType.NONE);
mDexterityAdvantage = new MutableLiveData<>(AdvantageType.NONE);
mConstitutionProficiency = new MutableLiveData<>(ProficiencyType.NONE);
mConstitutionAdvantage = new MutableLiveData<>(AdvantageType.NONE);
mIntelligenceProficiency = new MutableLiveData<>(ProficiencyType.NONE);
mIntelligenceAdvantage = new MutableLiveData<>(AdvantageType.NONE);
mWisdomProficiency = new MutableLiveData<>(ProficiencyType.NONE);
mWisdomAdvantage = new MutableLiveData<>(AdvantageType.NONE);
mCharismaProficiency = new MutableLiveData<>(ProficiencyType.NONE);
mCharismaAdvantage = new MutableLiveData<>(AdvantageType.NONE);
// TODO: consider initializing this to true so all new monsters need saving
mHasChanges = new MutableLiveData<>(false); mHasChanges = new MutableLiveData<>(false);
ChangeTrackedLiveData.OnValueDirtiedCallback onDirtied = () -> mHasChanges.setValue(true);
mName = new ChangeTrackedLiveData<>("", onDirtied);
mMonsterId = new ChangeTrackedLiveData<>(UUID.randomUUID(), onDirtied);
mSize = new ChangeTrackedLiveData<>("", onDirtied);
mType = new ChangeTrackedLiveData<>("", onDirtied);
mSubtype = new ChangeTrackedLiveData<>("", onDirtied);
mAlignment = new ChangeTrackedLiveData<>("", onDirtied);
mCustomHitPoints = new ChangeTrackedLiveData<>("", onDirtied);
mHitDice = new ChangeTrackedLiveData<>(0, onDirtied);
mNaturalArmorBonus = new ChangeTrackedLiveData<>(0, onDirtied);
mHasCustomHitPoints = new ChangeTrackedLiveData<>(false, onDirtied);
mArmorType = new ChangeTrackedLiveData<>(ArmorType.NONE, onDirtied);
mHasShield = new ChangeTrackedLiveData<>(false, onDirtied);
mShieldBonus = new ChangeTrackedLiveData<>(0, onDirtied);
mCustomArmor = new ChangeTrackedLiveData<>("", onDirtied);
mWalkSpeed = new ChangeTrackedLiveData<>(0, onDirtied);
mBurrowSpeed = new ChangeTrackedLiveData<>(0, onDirtied);
mClimbSpeed = new ChangeTrackedLiveData<>(0, onDirtied);
mFlySpeed = new ChangeTrackedLiveData<>(0, onDirtied);
mSwimSpeed = new ChangeTrackedLiveData<>(0, onDirtied);
mCanHover = new ChangeTrackedLiveData<>(false, onDirtied);
mHasCustomSpeed = new ChangeTrackedLiveData<>(false, onDirtied);
mCustomSpeed = new ChangeTrackedLiveData<>("", onDirtied);
mStrength = new ChangeTrackedLiveData<>(10, onDirtied);
mDexterity = new ChangeTrackedLiveData<>(10, onDirtied);
mConstitution = new ChangeTrackedLiveData<>(10, onDirtied);
mIntelligence = new ChangeTrackedLiveData<>(10, onDirtied);
mWisdom = new ChangeTrackedLiveData<>(10, onDirtied);
mCharisma = new ChangeTrackedLiveData<>(10, onDirtied);
mStrengthProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, onDirtied);
mStrengthAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, onDirtied);
mDexterityProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, onDirtied);
mDexterityAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, onDirtied);
mConstitutionProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, onDirtied);
mConstitutionAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, onDirtied);
mIntelligenceProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, onDirtied);
mIntelligenceAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, onDirtied);
mWisdomProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, onDirtied);
mWisdomAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, onDirtied);
mCharismaProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, onDirtied);
mCharismaAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, onDirtied);
} }
public void copyFromMonster(Monster monster) { public void copyFromMonster(Monster monster) {
// TODO: copy from monster to other fields mMonsterId.resetValue(monster.id);
mMonsterId.setValue(monster.id); mName.resetValue(monster.name);
mName.setValue(monster.name); mSize.resetValue(monster.size);
mSize.setValue(monster.size); mType.resetValue(monster.type);
mType.setValue(monster.type); mSubtype.resetValue(monster.subtype);
mSubtype.setValue(monster.subtype); mAlignment.resetValue(monster.alignment);
mAlignment.setValue(monster.alignment); mCustomHitPoints.resetValue(monster.customHPDescription);
mCustomHitPoints.setValue(monster.customHPDescription); mHitDice.resetValue(monster.hitDice);
mHitDice.setValue(monster.hitDice); mNaturalArmorBonus.resetValue(monster.naturalArmorBonus);
mNaturalArmorBonus.setValue(monster.naturalArmorBonus); mHasCustomHitPoints.resetValue(monster.hasCustomHP);
mHasCustomHitPoints.setValue(monster.hasCustomHP); mArmorType.resetValue(monster.armorType);
mArmorType.setValue(monster.armorType); mHasShield.resetValue(monster.shieldBonus != 0);
mHasShield.setValue(monster.shieldBonus != 0); mShieldBonus.resetValue(monster.shieldBonus);
mShieldBonus.setValue(monster.shieldBonus); mCustomArmor.resetValue(monster.otherArmorDescription);
mCustomArmor.setValue(monster.otherArmorDescription); mWalkSpeed.resetValue(monster.walkSpeed);
mWalkSpeed.setValue(monster.walkSpeed); mBurrowSpeed.resetValue(monster.burrowSpeed);
mBurrowSpeed.setValue(monster.burrowSpeed); mClimbSpeed.resetValue(monster.climbSpeed);
mClimbSpeed.setValue(monster.climbSpeed); mFlySpeed.resetValue(monster.flySpeed);
mFlySpeed.setValue(monster.flySpeed); mSwimSpeed.resetValue(monster.swimSpeed);
mSwimSpeed.setValue(monster.swimSpeed); mCanHover.resetValue(monster.canHover);
mCanHover.setValue(monster.canHover); mHasCustomSpeed.resetValue(monster.hasCustomSpeed);
mHasCustomSpeed.setValue(monster.hasCustomSpeed); mCustomSpeed.resetValue(monster.customSpeedDescription);
mCustomSpeed.setValue(monster.customSpeedDescription); mStrength.resetValue(monster.strengthScore);
mStrength.setValue(monster.strengthScore); mDexterity.resetValue(monster.dexterityScore);
mDexterity.setValue(monster.dexterityScore); mConstitution.resetValue(monster.constitutionScore);
mConstitution.setValue(monster.constitutionScore); mIntelligence.resetValue(monster.intelligenceScore);
mIntelligence.setValue(monster.intelligenceScore); mWisdom.resetValue(monster.wisdomScore);
mWisdom.setValue(monster.wisdomScore); mCharisma.resetValue(monster.charismaScore);
mCharisma.setValue(monster.charismaScore); mStrengthProficiency.resetValue(monster.strengthSavingThrowProficiency);
mStrengthProficiency.setValue(monster.strengthSavingThrowProficiency); mStrengthAdvantage.resetValue(monster.strengthSavingThrowAdvantage);
mStrengthAdvantage.setValue(monster.strengthSavingThrowAdvantage); mDexterityProficiency.resetValue(monster.dexteritySavingThrowProficiency);
mDexterityProficiency.setValue(monster.dexteritySavingThrowProficiency); mDexterityAdvantage.resetValue(monster.dexteritySavingThrowAdvantage);
mDexterityAdvantage.setValue(monster.dexteritySavingThrowAdvantage); mConstitutionProficiency.resetValue(monster.constitutionSavingThrowProficiency);
mConstitutionProficiency.setValue(monster.constitutionSavingThrowProficiency); mConstitutionAdvantage.resetValue(monster.constitutionSavingThrowAdvantage);
mConstitutionAdvantage.setValue(monster.constitutionSavingThrowAdvantage); mIntelligenceProficiency.resetValue(monster.intelligenceSavingThrowProficiency);
mIntelligenceProficiency.setValue(monster.intelligenceSavingThrowProficiency); mIntelligenceAdvantage.resetValue(monster.intelligenceSavingThrowAdvantage);
mIntelligenceAdvantage.setValue(monster.intelligenceSavingThrowAdvantage); mWisdomProficiency.resetValue(monster.wisdomSavingThrowProficiency);
mWisdomProficiency.setValue(monster.wisdomSavingThrowProficiency); mWisdomAdvantage.resetValue(monster.wisdomSavingThrowAdvantage);
mWisdomAdvantage.setValue(monster.wisdomSavingThrowAdvantage); mCharismaProficiency.resetValue(monster.charismaSavingThrowProficiency);
mCharismaProficiency.setValue(monster.charismaSavingThrowProficiency); mCharismaAdvantage.resetValue(monster.charismaSavingThrowAdvantage);
mCharismaAdvantage.setValue(monster.charismaSavingThrowAdvantage);
mHasChanges.setValue(false); mHasChanges.setValue(false);
} }
@@ -163,10 +160,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setName(@NonNull String name) { public void setName(@NonNull String name) {
if (!Objects.equals(mName.getValue(), name)) {
mName.setValue(name); mName.setValue(name);
mHasChanges.setValue(true);
}
} }
public LiveData<UUID> getMonsterId() { public LiveData<UUID> getMonsterId() {
@@ -210,10 +204,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setSize(@NonNull String size) { public void setSize(@NonNull String size) {
if (!Objects.equals(mSize.getValue(), size)) {
mSize.setValue(size); mSize.setValue(size);
mHasChanges.setValue(true);
}
} }
public LiveData<String> getType() { public LiveData<String> getType() {
@@ -221,10 +212,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setType(@NonNull String type) { public void setType(@NonNull String type) {
if (!Objects.equals(mType.getValue(), type)) {
mType.setValue(type); mType.setValue(type);
mHasChanges.setValue(true);
}
} }
public LiveData<String> getSubtype() { public LiveData<String> getSubtype() {
@@ -232,10 +220,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setSubtype(@NonNull String subtype) { public void setSubtype(@NonNull String subtype) {
if (!Objects.equals(mSubtype.getValue(), subtype)) {
mSubtype.setValue(subtype); mSubtype.setValue(subtype);
mHasChanges.setValue(true);
}
} }
public LiveData<String> getAlignment() { public LiveData<String> getAlignment() {
@@ -243,10 +228,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setAlignment(@NonNull String alignment) { public void setAlignment(@NonNull String alignment) {
if (!Objects.equals(mAlignment.getValue(), alignment)) {
mAlignment.setValue(alignment); mAlignment.setValue(alignment);
mHasChanges.setValue(true);
}
} }
public LiveData<String> getCustomHitPoints() { public LiveData<String> getCustomHitPoints() {
@@ -254,20 +236,13 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setCustomHitPoints(String customHitPoints) { public void setCustomHitPoints(String customHitPoints) {
if (!Objects.equals(mCustomHitPoints.getValue(), customHitPoints)) {
mCustomHitPoints.setValue(customHitPoints); mCustomHitPoints.setValue(customHitPoints);
mHasChanges.setValue(true);
}
} }
public LiveData<Boolean> getHasChanges() { public LiveData<Boolean> getHasChanges() {
return mHasChanges; return mHasChanges;
} }
public void setHasChanges(boolean hasChanges) {
mHasChanges.setValue(hasChanges);
}
public boolean hasChanges() { public boolean hasChanges() {
return mHasChanges.getValue(); return mHasChanges.getValue();
} }
@@ -277,10 +252,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setHitDice(int hitDice) { public void setHitDice(int hitDice) {
if (!Objects.equals(mHitDice.getValue(), hitDice)) {
mHitDice.setValue(hitDice); mHitDice.setValue(hitDice);
mHasChanges.setValue(true);
}
} }
public void setHitDice(String hitDice) { public void setHitDice(String hitDice) {
@@ -297,10 +269,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setNaturalArmorBonus(int naturalArmorBonus) { public void setNaturalArmorBonus(int naturalArmorBonus) {
if (!Objects.equals(mNaturalArmorBonus.getValue(), naturalArmorBonus)) {
mNaturalArmorBonus.setValue(naturalArmorBonus); mNaturalArmorBonus.setValue(naturalArmorBonus);
mHasChanges.setValue(true);
}
} }
public void setNaturalArmorBonus(String naturalArmorBonus) { public void setNaturalArmorBonus(String naturalArmorBonus) {
@@ -317,10 +286,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setHasCustomHitPoints(boolean hasCustomHitPoints) { public void setHasCustomHitPoints(boolean hasCustomHitPoints) {
if (!Objects.equals(mHasCustomHitPoints.getValue(), hasCustomHitPoints)) {
mHasCustomHitPoints.setValue(hasCustomHitPoints); mHasCustomHitPoints.setValue(hasCustomHitPoints);
mHasChanges.setValue(true);
}
} }
public boolean getHasCustomHitPointsValueAsBoolean() { public boolean getHasCustomHitPointsValueAsBoolean() {
@@ -332,11 +298,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setArmorType(ArmorType armorType) { public void setArmorType(ArmorType armorType) {
Logger.logDebug(String.format("Setting ArmorType to %s", armorType.displayName));
if (!Objects.equals(mArmorType.getValue(), armorType)) {
mArmorType.setValue(armorType); mArmorType.setValue(armorType);
mHasChanges.setValue(true);
}
} }
public LiveData<Boolean> getHasShield() { public LiveData<Boolean> getHasShield() {
@@ -344,10 +306,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setHasShield(boolean hasShield) { public void setHasShield(boolean hasShield) {
if (!Objects.equals(mHasShield.getValue(), hasShield)) {
mHasShield.setValue(hasShield); mHasShield.setValue(hasShield);
mHasChanges.setValue(true);
}
} }
public boolean getHasShieldValueAsBoolean() { public boolean getHasShieldValueAsBoolean() {
@@ -359,10 +318,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setShieldBonus(int shieldBonus) { public void setShieldBonus(int shieldBonus) {
if (!Objects.equals(mShieldBonus.getValue(), shieldBonus)) {
mShieldBonus.setValue(shieldBonus); mShieldBonus.setValue(shieldBonus);
mHasChanges.setValue(true);
}
} }
public void setShieldBonus(String shieldBonus) { public void setShieldBonus(String shieldBonus) {
@@ -375,10 +331,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setCustomArmor(String customArmor) { public void setCustomArmor(String customArmor) {
if (!Objects.equals(mCustomArmor.getValue(), customArmor)) {
mCustomArmor.setValue(customArmor); mCustomArmor.setValue(customArmor);
mHasChanges.setValue(true);
}
} }
public String getShieldBonusValueAsString() { public String getShieldBonusValueAsString() {
@@ -390,10 +343,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setWalkSpeed(int walkSpeed) { public void setWalkSpeed(int walkSpeed) {
if (!Objects.equals(mWalkSpeed.getValue(), walkSpeed)) {
mWalkSpeed.setValue(walkSpeed); mWalkSpeed.setValue(walkSpeed);
mHasChanges.setValue(true);
}
} }
public void incrementWalkSpeed() { public void incrementWalkSpeed() {
@@ -409,10 +359,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setBurrowSpeed(int burrowSpeed) { public void setBurrowSpeed(int burrowSpeed) {
if (!Objects.equals(mBurrowSpeed.getValue(), burrowSpeed)) {
mBurrowSpeed.setValue(burrowSpeed); mBurrowSpeed.setValue(burrowSpeed);
mHasChanges.setValue(true);
}
} }
public LiveData<Integer> getClimbSpeed() { public LiveData<Integer> getClimbSpeed() {
@@ -420,10 +367,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setClimbSpeed(int climbSpeed) { public void setClimbSpeed(int climbSpeed) {
if (!Objects.equals(mClimbSpeed.getValue(), climbSpeed)) {
mClimbSpeed.setValue(climbSpeed); mClimbSpeed.setValue(climbSpeed);
mHasChanges.setValue(true);
}
} }
public LiveData<Integer> getFlySpeed() { public LiveData<Integer> getFlySpeed() {
@@ -431,10 +375,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setFlySpeed(int flySpeed) { public void setFlySpeed(int flySpeed) {
if (!Objects.equals(mFlySpeed.getValue(), flySpeed)) {
mFlySpeed.setValue(flySpeed); mFlySpeed.setValue(flySpeed);
mHasChanges.setValue(true);
}
} }
public LiveData<Integer> getSwimSpeed() { public LiveData<Integer> getSwimSpeed() {
@@ -442,10 +383,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setSwimSpeed(int swimSpeed) { public void setSwimSpeed(int swimSpeed) {
if (!Objects.equals(mSwimSpeed.getValue(), swimSpeed)) {
mSwimSpeed.setValue(swimSpeed); mSwimSpeed.setValue(swimSpeed);
mHasChanges.setValue(true);
}
} }
public LiveData<Boolean> getCanHover() { public LiveData<Boolean> getCanHover() {
@@ -453,10 +391,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setCanHover(boolean canHover) { public void setCanHover(boolean canHover) {
if (!Objects.equals(mCanHover.getValue(), canHover)) {
mCanHover.setValue(canHover); mCanHover.setValue(canHover);
mHasChanges.setValue(true);
}
} }
public LiveData<Boolean> getHasCustomSpeed() { public LiveData<Boolean> getHasCustomSpeed() {
@@ -464,10 +399,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setHasCustomSpeed(boolean hasCustomSpeed) { public void setHasCustomSpeed(boolean hasCustomSpeed) {
if (!Objects.equals(mHasCustomSpeed.getValue(), hasCustomSpeed)) {
mHasCustomSpeed.setValue(hasCustomSpeed); mHasCustomSpeed.setValue(hasCustomSpeed);
mHasChanges.setValue(true);
}
} }
public LiveData<String> getCustomSpeed() { public LiveData<String> getCustomSpeed() {
@@ -475,10 +407,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setCustomSpeed(String customSpeed) { public void setCustomSpeed(String customSpeed) {
if (!Objects.equals(mCustomSpeed.getValue(), customSpeed)) {
mCustomSpeed.setValue(customSpeed); mCustomSpeed.setValue(customSpeed);
mHasChanges.setValue(true);
}
} }
public void incrementBurrowSpeed() { public void incrementBurrowSpeed() {
@@ -518,10 +447,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setStrength(int strength) { public void setStrength(int strength) {
if (!Objects.equals(mStrength.getValue(), strength)) {
mStrength.setValue(strength); mStrength.setValue(strength);
mHasChanges.setValue(true);
}
} }
public void incrementStrength() { public void incrementStrength() {
@@ -537,10 +463,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setDexterity(int dexterity) { public void setDexterity(int dexterity) {
if (!Objects.equals(mDexterity.getValue(), dexterity)) {
mDexterity.setValue(dexterity); mDexterity.setValue(dexterity);
mHasChanges.setValue(true);
}
} }
public void incrementDexterity() { public void incrementDexterity() {
@@ -556,10 +479,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setConstitution(int constitution) { public void setConstitution(int constitution) {
if (!Objects.equals(mConstitution.getValue(), constitution)) {
mConstitution.setValue(constitution); mConstitution.setValue(constitution);
mHasChanges.setValue(true);
}
} }
public void incrementConstitution() { public void incrementConstitution() {
@@ -575,10 +495,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setIntelligence(int intelligence) { public void setIntelligence(int intelligence) {
if (!Objects.equals(mIntelligence.getValue(), intelligence)) {
mIntelligence.setValue(intelligence); mIntelligence.setValue(intelligence);
mHasChanges.setValue(true);
}
} }
public void incrementIntelligence() { public void incrementIntelligence() {
@@ -594,10 +511,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setWisdom(int wisdom) { public void setWisdom(int wisdom) {
if (!Objects.equals(mWisdom.getValue(), wisdom)) {
mWisdom.setValue(wisdom); mWisdom.setValue(wisdom);
mHasChanges.setValue(true);
}
} }
public void incrementWisdom() { public void incrementWisdom() {
@@ -613,10 +527,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setCharisma(int charisma) { public void setCharisma(int charisma) {
if (!Objects.equals(mCharisma.getValue(), charisma)) {
mCharisma.setValue(charisma); mCharisma.setValue(charisma);
mHasChanges.setValue(true);
}
} }
public void incrementCharisma() { public void incrementCharisma() {
@@ -632,10 +543,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setStrengthProficiency(ProficiencyType proficiency) { public void setStrengthProficiency(ProficiencyType proficiency) {
if (!Objects.equals(mStrengthProficiency.getValue(), proficiency)) {
mStrengthProficiency.setValue(proficiency); mStrengthProficiency.setValue(proficiency);
mHasChanges.setValue(true);
}
} }
public LiveData<AdvantageType> getStrengthAdvantage() { public LiveData<AdvantageType> getStrengthAdvantage() {
@@ -643,10 +551,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setStrengthAdvantage(AdvantageType advantage) { public void setStrengthAdvantage(AdvantageType advantage) {
if (!Objects.equals(mStrengthAdvantage.getValue(), advantage)) {
mStrengthAdvantage.setValue(advantage); mStrengthAdvantage.setValue(advantage);
mHasChanges.setValue(true);
}
} }
public LiveData<ProficiencyType> getDexterityProficiency() { public LiveData<ProficiencyType> getDexterityProficiency() {
@@ -654,10 +559,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setDexterityProficiency(ProficiencyType proficiency) { public void setDexterityProficiency(ProficiencyType proficiency) {
if (!Objects.equals(mDexterityProficiency.getValue(), proficiency)) {
mDexterityProficiency.setValue(proficiency); mDexterityProficiency.setValue(proficiency);
mHasChanges.setValue(true);
}
} }
public LiveData<AdvantageType> getDexterityAdvantage() { public LiveData<AdvantageType> getDexterityAdvantage() {
@@ -665,10 +567,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setDexterityAdvantage(AdvantageType advantage) { public void setDexterityAdvantage(AdvantageType advantage) {
if (!Objects.equals(mDexterityAdvantage.getValue(), advantage)) {
mDexterityAdvantage.setValue(advantage); mDexterityAdvantage.setValue(advantage);
mHasChanges.setValue(true);
}
} }
public LiveData<ProficiencyType> getConstitutionProficiency() { public LiveData<ProficiencyType> getConstitutionProficiency() {
@@ -676,10 +575,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setConstitutionProficiency(ProficiencyType proficiency) { public void setConstitutionProficiency(ProficiencyType proficiency) {
if (!Objects.equals(mConstitutionProficiency.getValue(), proficiency)) {
mConstitutionProficiency.setValue(proficiency); mConstitutionProficiency.setValue(proficiency);
mHasChanges.setValue(true);
}
} }
public LiveData<AdvantageType> getConstitutionAdvantage() { public LiveData<AdvantageType> getConstitutionAdvantage() {
@@ -687,10 +583,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setConstitutionAdvantage(AdvantageType advantage) { public void setConstitutionAdvantage(AdvantageType advantage) {
if (!Objects.equals(mConstitutionAdvantage.getValue(), advantage)) {
mConstitutionAdvantage.setValue(advantage); mConstitutionAdvantage.setValue(advantage);
mHasChanges.setValue(true);
}
} }
public LiveData<ProficiencyType> getIntelligenceProficiency() { public LiveData<ProficiencyType> getIntelligenceProficiency() {
@@ -698,10 +591,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setIntelligenceProficiency(ProficiencyType proficiency) { public void setIntelligenceProficiency(ProficiencyType proficiency) {
if (!Objects.equals(mIntelligenceProficiency.getValue(), proficiency)) {
mIntelligenceProficiency.setValue(proficiency); mIntelligenceProficiency.setValue(proficiency);
mHasChanges.setValue(true);
}
} }
public LiveData<AdvantageType> getIntelligenceAdvantage() { public LiveData<AdvantageType> getIntelligenceAdvantage() {
@@ -709,10 +599,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setIntelligenceAdvantage(AdvantageType advantage) { public void setIntelligenceAdvantage(AdvantageType advantage) {
if (!Objects.equals(mIntelligenceAdvantage.getValue(), advantage)) {
mIntelligenceAdvantage.setValue(advantage); mIntelligenceAdvantage.setValue(advantage);
mHasChanges.setValue(true);
}
} }
public LiveData<ProficiencyType> getWisdomProficiency() { public LiveData<ProficiencyType> getWisdomProficiency() {
@@ -720,10 +607,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setWisdomProficiency(ProficiencyType proficiency) { public void setWisdomProficiency(ProficiencyType proficiency) {
if (!Objects.equals(mWisdomProficiency.getValue(), proficiency)) {
mWisdomProficiency.setValue(proficiency); mWisdomProficiency.setValue(proficiency);
mHasChanges.setValue(true);
}
} }
public LiveData<AdvantageType> getWisdomAdvantage() { public LiveData<AdvantageType> getWisdomAdvantage() {
@@ -731,10 +615,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setWisdomAdvantage(AdvantageType advantage) { public void setWisdomAdvantage(AdvantageType advantage) {
if (!Objects.equals(mWisdomAdvantage.getValue(), advantage)) {
mWisdomAdvantage.setValue(advantage); mWisdomAdvantage.setValue(advantage);
mHasChanges.setValue(true);
}
} }
public LiveData<ProficiencyType> getCharismaProficiency() { public LiveData<ProficiencyType> getCharismaProficiency() {
@@ -742,10 +623,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setCharismaProficiency(ProficiencyType proficiency) { public void setCharismaProficiency(ProficiencyType proficiency) {
if (!Objects.equals(mCharismaProficiency.getValue(), proficiency)) {
mCharismaProficiency.setValue(proficiency); mCharismaProficiency.setValue(proficiency);
mHasChanges.setValue(true);
}
} }
public LiveData<AdvantageType> getCharismaAdvantage() { public LiveData<AdvantageType> getCharismaAdvantage() {
@@ -753,10 +631,7 @@ public class EditMonsterViewModel extends ViewModel {
} }
public void setCharismaAdvantage(AdvantageType advantage) { public void setCharismaAdvantage(AdvantageType advantage) {
if (!Objects.equals(mCharismaAdvantage.getValue(), advantage)) {
mCharismaAdvantage.setValue(advantage); mCharismaAdvantage.setValue(advantage);
mHasChanges.setValue(true);
}
} }
public Monster buildMonster() { public Monster buildMonster() {

View File

@@ -0,0 +1,64 @@
package com.majinnaibu.monstercards.utils;
import androidx.lifecycle.MutableLiveData;
import java.util.Objects;
public class ChangeTrackedLiveData<T> extends MutableLiveData<T> {
private final OnValueChangedCallback<T> mOnValueChangedCallback;
private final OnValueDirtiedCallback mOnValueDirtiedCallback;
private T mReferenceValue;
public ChangeTrackedLiveData(T initialValue, OnValueChangedCallback<T> onValueChanged, OnValueDirtiedCallback onValueDirtied) {
super(initialValue);
mReferenceValue = initialValue;
mOnValueChangedCallback = onValueChanged;
if (mOnValueChangedCallback != null) {
mOnValueChangedCallback.onValueChanged(initialValue);
}
mOnValueDirtiedCallback = onValueDirtied;
}
public ChangeTrackedLiveData(T initialValue, OnValueChangedCallback<T> callback) {
this(initialValue, callback, null);
}
public ChangeTrackedLiveData(T initialValue, OnValueDirtiedCallback callback) {
this(initialValue, null, callback);
}
public void setReferenceValue(T referenceValue) {
mReferenceValue = referenceValue;
}
public void setCurrentValueAsReference() {
mReferenceValue = getValue();
}
public void resetValue(T value) {
mReferenceValue = value;
setValue(value);
}
@Override
public void setValue(T value) {
if (!Objects.equals(getValue(), value)) {
super.setValue(value);
if (mOnValueChangedCallback != null) {
mOnValueChangedCallback.onValueChanged(value);
}
if (!Objects.equals(mReferenceValue, value) && mOnValueDirtiedCallback != null) {
mOnValueDirtiedCallback.onValueDirtied();
}
}
}
public interface OnValueDirtiedCallback {
void onValueDirtied();
}
public interface OnValueChangedCallback<T> {
void onValueChanged(T value);
}
}