Adds hit dice and has custom HP toggle to the edit basic info screen.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -87,7 +87,28 @@
|
||||
|
||||
<!-- Hit Dice (int) -->
|
||||
|
||||
<!-- Has ustom HP (boolean) -->
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="@dimen/text_margin">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/hitDice"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:digits="0123456789"
|
||||
android:hint="@string/label_hit_dice"
|
||||
android:importantForAutofill="no"
|
||||
android:inputType="number" />
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<com.google.android.material.switchmaterial.SwitchMaterial
|
||||
android:id="@+id/hasCustomHitPoints"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="@dimen/text_margin"
|
||||
android:text="@string/label_has_custom_hp"
|
||||
android:textSize="@dimen/text_h4_size" />
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
|
||||
@@ -44,4 +44,6 @@
|
||||
<string name="label_subtype">Subtype</string>
|
||||
<string name="label_alignment">Alignment</string>
|
||||
<string name="label_custom_hp">Custom HP</string>
|
||||
<string name="label_has_custom_hp">Has Custom HP</string>
|
||||
<string name="label_hit_dice">Hit Dice</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user