Adds hit dice and has custom HP toggle to the edit basic info screen.

This commit is contained in:
2021-05-27 21:44:17 -07:00
parent f41b8d0065
commit 82ce3d3788
5 changed files with 91 additions and 3 deletions

View File

@@ -58,4 +58,12 @@ public final class StringHelper {
return sb.toString();
}
}
public static Integer parseInt(String s) {
try {
return Integer.parseInt(s);
} catch (NumberFormatException _ex) {
return null;
}
}
}

View File

@@ -12,6 +12,7 @@ import androidx.navigation.NavBackStackEntry;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.google.android.material.switchmaterial.SwitchMaterial;
import com.majinnaibu.monstercards.R;
import com.majinnaibu.monstercards.ui.MCFragment;
import com.majinnaibu.monstercards.utils.Logger;
@@ -20,6 +21,7 @@ import com.majinnaibu.monstercards.utils.TextChangedListener;
/**
* A simple {@link Fragment} subclass.
*/
@SuppressWarnings("FieldCanBeLocal")
public class EditBasicInfoFragment extends MCFragment {
private EditMonsterViewModel mViewModel;
private ViewHolder mHolder;
@@ -71,6 +73,18 @@ public class EditBasicInfoFragment extends MCFragment {
Logger.logDebug(String.format("Monster Custom Hit Points changed to %s", mViewModel.getCustomHitPoints().getValue()));
})));
mHolder.hitDice.setText(mViewModel.getHitDiceValueAsString());
mHolder.hitDice.addTextChangedListener((new TextChangedListener((TextChangedListener.OnTextChangedCallback) (s, start, before, count) -> {
mViewModel.setHitDice(s.toString());
Logger.logDebug(String.format("Monster Hit Dice changed to %s", mViewModel.getHitDiceValueAsString()));
})));
mHolder.hasCustomHitPoints.setChecked(mViewModel.getHasCustomHitPointsValueAsBoolean());
mHolder.hasCustomHitPoints.setOnCheckedChangeListener((button, isChecked) -> {
mViewModel.setHasCustomHitPoints(isChecked);
Logger.logDebug(String.format("Monster Has Custom Hit Points changed to %s", isChecked ? "TRUE" : "FALSE"));
});
return root;
}
@@ -81,6 +95,8 @@ public class EditBasicInfoFragment extends MCFragment {
private final EditText subtype;
private final EditText alignment;
private final EditText customHitPoints;
private final EditText hitDice;
private final SwitchMaterial hasCustomHitPoints;
ViewHolder(View root) {
name = root.findViewById(R.id.name);
@@ -89,7 +105,8 @@ public class EditBasicInfoFragment extends MCFragment {
subtype = root.findViewById(R.id.subtype);
alignment = root.findViewById(R.id.alignment);
customHitPoints = root.findViewById(R.id.customHitPoints);
// TODO: add hitDice, hasCustomHitPoints, and customHitPoints
hitDice = root.findViewById(R.id.hitDice);
hasCustomHitPoints = root.findViewById(R.id.hasCustomHitPoints);
}
}
}

View File

@@ -5,6 +5,7 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.majinnaibu.monstercards.helpers.StringHelper;
import com.majinnaibu.monstercards.models.Monster;
import java.util.UUID;
@@ -22,6 +23,8 @@ public class EditMonsterViewModel extends ViewModel {
private final MutableLiveData<String> mAlignment;
private final MutableLiveData<String> mCustomHitPoints;
private final MutableLiveData<Boolean> mHasChanges;
private final MutableLiveData<Integer> mHitDice;
private final MutableLiveData<Boolean> mHasCustomHitPoints;
public EditMonsterViewModel() {
@@ -35,6 +38,8 @@ public class EditMonsterViewModel extends ViewModel {
mSubtype = new MutableLiveData<>("");
mAlignment = new MutableLiveData<>("");
mCustomHitPoints = new MutableLiveData<>("");
mHitDice = new MutableLiveData<>(0);
mHasCustomHitPoints = new MutableLiveData<>(false);
// TODO: consider initializing this to true so all new monsters need saving
mHasChanges = new MutableLiveData<>(false);
}
@@ -48,6 +53,8 @@ public class EditMonsterViewModel extends ViewModel {
mSubtype.setValue(monster.subtype);
mAlignment.setValue(monster.alignment);
mCustomHitPoints.setValue(monster.customHPDescription);
mHitDice.setValue(monster.hitDice);
mHasCustomHitPoints.setValue(monster.hasCustomHP);
mHasChanges.setValue(false);
}
@@ -145,7 +152,7 @@ public class EditMonsterViewModel extends ViewModel {
return mHasChanges;
}
public void setHasChanges(@NonNull Boolean hasChanges) {
public void setHasChanges(boolean hasChanges) {
mHasChanges.setValue(hasChanges);
}
@@ -153,6 +160,37 @@ public class EditMonsterViewModel extends ViewModel {
return mHasChanges.getValue();
}
public LiveData<Integer> getHitDice() {
return mHitDice;
}
public void setHitDice(int hitDice) {
mHitDice.setValue(hitDice);
mHasChanges.setValue(true);
}
public void setHitDice(String hitDice) {
Integer parsedHitDice = StringHelper.parseInt(hitDice);
this.setHitDice(parsedHitDice != null ? parsedHitDice : 0);
}
public String getHitDiceValueAsString() {
return mHitDice.getValue().toString();
}
public LiveData<Boolean> getHasCustomHitPoints() {
return mHasCustomHitPoints;
}
public void setHasCustomHitPoints(boolean hasCustomHitPoints) {
mHasCustomHitPoints.setValue(hasCustomHitPoints);
mHasChanges.setValue(true);
}
public boolean getHasCustomHitPointsValueAsBoolean() {
return mHasCustomHitPoints.getValue();
}
public Monster buildMonster() {
Monster monster = new Monster();
@@ -163,6 +201,8 @@ public class EditMonsterViewModel extends ViewModel {
monster.subtype = mSubtype.getValue();
monster.alignment = mAlignment.getValue();
monster.customHPDescription = mCustomHitPoints.getValue();
monster.hitDice = mHitDice.getValue();
monster.hasCustomHP = mHasCustomHitPoints.getValue();
return monster;
}