From 6bb1e419c81317b9e90f9a88702ca8a891daa7bc Mon Sep 17 00:00:00 2001 From: Tom Hicks Date: Thu, 27 May 2021 01:15:46 -0700 Subject: [PATCH] Replaces EditMonsterFragment with a basic ConstraintLayout. --- Android/app/build.gradle | 2 + .../ui/editmonster/EditMonsterFragment.java | 264 +--- .../ui/editmonster/EditMonsterViewModel.java | 1093 +---------------- .../ui/monster/EditMonsterFragment.java | 72 -- .../EditMonsterRecyclerViewAdapter.java | 61 - .../main/res/layout/fragment_edit_monster.xml | 32 +- .../res/layout/fragment_edit_monster_list.xml | 13 - .../main/res/navigation/mobile_navigation.xml | 6 +- 8 files changed, 39 insertions(+), 1504 deletions(-) delete mode 100644 Android/app/src/main/java/com/majinnaibu/monstercards/ui/monster/EditMonsterFragment.java delete mode 100644 Android/app/src/main/java/com/majinnaibu/monstercards/ui/monster/EditMonsterRecyclerViewAdapter.java delete mode 100644 Android/app/src/main/res/layout/fragment_edit_monster_list.xml diff --git a/Android/app/build.gradle b/Android/app/build.gradle index 0c421fc..7ded5fa 100644 --- a/Android/app/build.gradle +++ b/Android/app/build.gradle @@ -78,6 +78,8 @@ dependencies { implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'androidx.recyclerview:recyclerview:1.1.0' + implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1' + implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1' // Testing testImplementation 'junit:junit:4.13.2' diff --git a/Android/app/src/main/java/com/majinnaibu/monstercards/ui/editmonster/EditMonsterFragment.java b/Android/app/src/main/java/com/majinnaibu/monstercards/ui/editmonster/EditMonsterFragment.java index 0e24067..b7a0885 100644 --- a/Android/app/src/main/java/com/majinnaibu/monstercards/ui/editmonster/EditMonsterFragment.java +++ b/Android/app/src/main/java/com/majinnaibu/monstercards/ui/editmonster/EditMonsterFragment.java @@ -4,269 +4,33 @@ import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.TextView; -import androidx.activity.OnBackPressedCallback; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; +import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; -import androidx.navigation.NavBackStackEntry; -import androidx.navigation.NavController; -import androidx.navigation.NavDirections; -import androidx.navigation.Navigation; -import com.google.android.material.snackbar.Snackbar; import com.majinnaibu.monstercards.R; -import com.majinnaibu.monstercards.data.MonsterRepository; -import com.majinnaibu.monstercards.data.enums.StringType; -import com.majinnaibu.monstercards.data.enums.TraitType; -import com.majinnaibu.monstercards.models.Monster; -import com.majinnaibu.monstercards.ui.monster.MonsterDetailFragmentArgs; -import com.majinnaibu.monstercards.ui.shared.MCFragment; -import com.majinnaibu.monstercards.utils.Logger; -import java.util.Objects; -import java.util.UUID; - -import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers; -import io.reactivex.rxjava3.observers.DisposableCompletableObserver; -import io.reactivex.rxjava3.observers.DisposableSingleObserver; -import io.reactivex.rxjava3.schedulers.Schedulers; - -public class EditMonsterFragment extends MCFragment { +public class EditMonsterFragment extends Fragment { private EditMonsterViewModel mViewModel; - private ViewHolder mHolder; + + public static EditMonsterFragment newInstance() { + return new EditMonsterFragment(); + } @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - - MonsterRepository repository = getMonsterRepository(); - Bundle arguments = getArguments(); - assert arguments != null; - UUID monsterId = UUID.fromString(MonsterDetailFragmentArgs.fromBundle(arguments).getMonsterId()); - - NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment); - NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.edit_monster_navigation); - mViewModel = new ViewModelProvider(backStackEntry).get(EditMonsterViewModel.class); - - View root = inflater.inflate(R.layout.fragment_edit_monster, container, false); - mHolder = new ViewHolder(root); - - setTitle(getString(R.string.title_editMonster_fmt, getString(R.string.default_monster_name))); - - // TODO: Show a loading spinner until we have the monster loaded. - if (mViewModel.hasError() || !mViewModel.hasLoaded() || !Objects.equals(mViewModel.getMonsterId().getValue(), monsterId)) { - repository.getMonster(monsterId).toObservable() - .firstOrError() - .subscribe(new DisposableSingleObserver() { - @Override - public void onSuccess(@io.reactivex.rxjava3.annotations.NonNull Monster monster) { - mViewModel.setHasLoaded(true); - mViewModel.setHasError(false); - mViewModel.copyFromMonster(monster); - setTitle(getString(R.string.title_editMonster_fmt, monster.name)); - dispose(); - } - - @Override - public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) { - // TODO: Show an error state. - Logger.logError(e); - mViewModel.setHasError(true); - mViewModel.setErrorMessage(e.toString()); - dispose(); - } - }); - } - - mHolder.basicInfoButton.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditBasicInfoFragment(); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.armorButton.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditArmorFragment(); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.speedButton.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditSpeedFragment(); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.abilityScoresButton.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditAbilityScoresFragment(); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.savingThrows.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditSavingThrowsFragment(); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.challengeRating.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditChallengeRatingFragment(); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.skills.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditSkillsFragment(); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.senses.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditStringsFragment(StringType.SENSE); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.conditionImmunities.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditStringsFragment(StringType.CONDITION_IMMUNITY); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.damageImmunities.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditStringsFragment(StringType.DAMAGE_IMMUNITY); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.damageResistances.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditStringsFragment(StringType.DAMAGE_RESISTANCE); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.damageVulnerabilities.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditStringsFragment(StringType.DAMAGE_VULNERABILITY); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.languages.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditLanguagesFragment(); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.abilities.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditTraitListFragment(TraitType.ABILITY); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.actions.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditTraitListFragment(TraitType.ACTION); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.lairActions.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditTraitListFragment(TraitType.LAIR_ACTION); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.legendaryActions.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditTraitListFragment(TraitType.LEGENDARY_ACTION); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.reactions.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditTraitListFragment(TraitType.REACTIONS); - Navigation.findNavController(requireView()).navigate(action); - }); - - mHolder.regionalActions.setOnClickListener(v -> { - NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditTraitListFragment(TraitType.REGIONAL_ACTION); - Navigation.findNavController(requireView()).navigate(action); - }); - - - requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) { - @Override - public void handleOnBackPressed() { - if (mViewModel.hasChanges()) { - View view = getView(); - AlertDialog alertDialog = new AlertDialog.Builder(requireContext()).create(); - alertDialog.setTitle("Unsaved Changes"); - alertDialog.setMessage("Do you want to save your changes?"); - alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Save", (dialog, id) -> { - // Save the monster. Navigate up if the save is successful. Show a SnackBar if there was an error. - getMonsterRepository().saveMonster(mViewModel.buildMonster()) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - .subscribe( - new DisposableCompletableObserver() { - @Override - public void onComplete() { - Navigation.findNavController(requireView()).navigateUp(); - } - - @Override - public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) { - Logger.logError("Error creating monster", e); - assert view != null; - Snackbar.make(view, getString(R.string.snackbar_failed_to_create_monster), Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - } - }); - }); - alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Discard", (dialog, id) -> { - // Navigate up ignoring unsaved changes. - Navigation.findNavController(requireView()).navigateUp(); - }); - alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Cancel", (dialog, id) -> { - // Do nothing. - }); - alertDialog.show(); - } else { - // No changes so we can safely leave. - Navigation.findNavController(requireView()).navigateUp(); - } - } - }); - - return root; + return inflater.inflate(R.layout.fragment_edit_monster, container, false); } - private static class ViewHolder { - - TextView basicInfoButton; - TextView armorButton; - TextView speedButton; - TextView abilityScoresButton; - TextView savingThrows; - TextView skills; - TextView conditionImmunities; - TextView damageImmunities; - TextView damageResistances; - TextView damageVulnerabilities; - TextView senses; - TextView languages; - TextView challengeRating; - TextView abilities; - TextView actions; - TextView reactions; - TextView legendaryActions; - TextView lairActions; - TextView regionalActions; - - ViewHolder(@NonNull View root) { - basicInfoButton = root.findViewById(R.id.basicInfo); - armorButton = root.findViewById(R.id.armor); - speedButton = root.findViewById(R.id.speed); - abilityScoresButton = root.findViewById(R.id.abilityScores); - savingThrows = root.findViewById(R.id.savingThrows); - skills = root.findViewById(R.id.skills); - conditionImmunities = root.findViewById(R.id.conditionImmunities); - damageImmunities = root.findViewById(R.id.damageImmunities); - damageResistances = root.findViewById(R.id.damageResistances); - damageVulnerabilities = root.findViewById(R.id.damageVulnerabilities); - senses = root.findViewById(R.id.senses); - languages = root.findViewById(R.id.languages); - challengeRating = root.findViewById(R.id.challengeRating); - abilities = root.findViewById(R.id.abilities); - actions = root.findViewById(R.id.actions); - reactions = root.findViewById(R.id.reactions); - legendaryActions = root.findViewById(R.id.legendaryActions); - lairActions = root.findViewById(R.id.lairActions); - regionalActions = root.findViewById(R.id.regionalActions); - } + @Override + public void onActivityCreated(@Nullable Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + mViewModel = new ViewModelProvider(this).get(EditMonsterViewModel.class); + // TODO: Use the ViewModel } -} + +} \ No newline at end of file diff --git a/Android/app/src/main/java/com/majinnaibu/monstercards/ui/editmonster/EditMonsterViewModel.java b/Android/app/src/main/java/com/majinnaibu/monstercards/ui/editmonster/EditMonsterViewModel.java index 00f76f8..08d3cc6 100644 --- a/Android/app/src/main/java/com/majinnaibu/monstercards/ui/editmonster/EditMonsterViewModel.java +++ b/Android/app/src/main/java/com/majinnaibu/monstercards/ui/editmonster/EditMonsterViewModel.java @@ -1,1092 +1,7 @@ package com.majinnaibu.monstercards.ui.editmonster; -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; -import com.majinnaibu.monstercards.data.enums.AbilityScore; -import com.majinnaibu.monstercards.data.enums.AdvantageType; -import com.majinnaibu.monstercards.data.enums.ArmorType; -import com.majinnaibu.monstercards.data.enums.ChallengeRating; -import com.majinnaibu.monstercards.data.enums.ProficiencyType; -import com.majinnaibu.monstercards.data.enums.StringType; -import com.majinnaibu.monstercards.data.enums.TraitType; -import com.majinnaibu.monstercards.helpers.StringHelper; -import com.majinnaibu.monstercards.models.Language; -import com.majinnaibu.monstercards.models.Monster; -import com.majinnaibu.monstercards.models.Skill; -import com.majinnaibu.monstercards.models.Trait; -import com.majinnaibu.monstercards.ui.shared.ChangeTrackedViewModel; -import com.majinnaibu.monstercards.utils.ChangeTrackedLiveData; -import com.majinnaibu.monstercards.utils.Logger; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashSet; -import java.util.List; -import java.util.Objects; -import java.util.UUID; - -@SuppressWarnings({"ConstantConditions"}) -public class EditMonsterViewModel extends ChangeTrackedViewModel { - private final ChangeTrackedLiveData mMonsterId; - private final MutableLiveData mHasError; - private final MutableLiveData mHasLoaded; - private final ChangeTrackedLiveData mHasCustomHitPoints; - private final ChangeTrackedLiveData mHasShield; - private final ChangeTrackedLiveData mCanHover; - private final ChangeTrackedLiveData mHasCustomSpeed; - private final ChangeTrackedLiveData mArmorType; - private final ChangeTrackedLiveData mStrengthProficiency; - private final ChangeTrackedLiveData mStrengthAdvantage; - private final ChangeTrackedLiveData mDexterityProficiency; - private final ChangeTrackedLiveData mDexterityAdvantage; - private final ChangeTrackedLiveData mConstitutionProficiency; - private final ChangeTrackedLiveData mConstitutionAdvantage; - private final ChangeTrackedLiveData mIntelligenceProficiency; - private final ChangeTrackedLiveData mIntelligenceAdvantage; - private final ChangeTrackedLiveData mWisdomProficiency; - private final ChangeTrackedLiveData mWisdomAdvantage; - private final ChangeTrackedLiveData mCharismaProficiency; - private final ChangeTrackedLiveData mCharismaAdvantage; - private final ChangeTrackedLiveData mHitDice; - private final ChangeTrackedLiveData mNaturalArmorBonus; - private final ChangeTrackedLiveData mShieldBonus; - private final ChangeTrackedLiveData mWalkSpeed; - private final ChangeTrackedLiveData mBurrowSpeed; - private final ChangeTrackedLiveData mClimbSpeed; - private final ChangeTrackedLiveData mFlySpeed; - private final ChangeTrackedLiveData mSwimSpeed; - private final ChangeTrackedLiveData mStrength; - private final ChangeTrackedLiveData mDexterity; - private final ChangeTrackedLiveData mConstitution; - private final ChangeTrackedLiveData mIntelligence; - private final ChangeTrackedLiveData mWisdom; - private final ChangeTrackedLiveData mCharisma; - private final ChangeTrackedLiveData mName; - private final MutableLiveData mErrorMessage; - private final ChangeTrackedLiveData mSize; - private final ChangeTrackedLiveData mType; - private final ChangeTrackedLiveData mSubtype; - private final ChangeTrackedLiveData mAlignment; - private final ChangeTrackedLiveData mCustomHitPoints; - private final ChangeTrackedLiveData mCustomArmor; - private final ChangeTrackedLiveData mCustomSpeed; - private final ChangeTrackedLiveData mChallengeRating; - private final ChangeTrackedLiveData mCustomChallengeRatingDescription; - private final ChangeTrackedLiveData mCustomProficiencyBonus; - private final ChangeTrackedLiveData mTelepathyRange; - private final ChangeTrackedLiveData mUnderstandsButDescription; - private final ChangeTrackedLiveData> mSkills; - private final ChangeTrackedLiveData> mSenses; - private final ChangeTrackedLiveData> mDamageImmunities; - private final ChangeTrackedLiveData> mDamageResistances; - private final ChangeTrackedLiveData> mDamageVulnerabilities; - private final ChangeTrackedLiveData> mConditionImmunities; - private final ChangeTrackedLiveData> mLanguages; - private final ChangeTrackedLiveData> mAbilities; - private final ChangeTrackedLiveData> mActions; - private final ChangeTrackedLiveData> mReactions; - private final ChangeTrackedLiveData> mLairActions; - private final ChangeTrackedLiveData> mLegendaryActions; - private final ChangeTrackedLiveData> mRegionalActions; - - public EditMonsterViewModel() { - super(); - mErrorMessage = new MutableLiveData<>(""); - mHasError = new MutableLiveData<>(false); - mHasLoaded = new MutableLiveData<>(false); - - mName = new ChangeTrackedLiveData<>("", this::makeDirty); - mMonsterId = new ChangeTrackedLiveData<>(UUID.randomUUID(), this::makeDirty); - mSize = new ChangeTrackedLiveData<>("", this::makeDirty); - mType = new ChangeTrackedLiveData<>("", this::makeDirty); - mSubtype = new ChangeTrackedLiveData<>("", this::makeDirty); - mAlignment = new ChangeTrackedLiveData<>("", this::makeDirty); - mCustomHitPoints = new ChangeTrackedLiveData<>("", this::makeDirty); - mHitDice = new ChangeTrackedLiveData<>(0, this::makeDirty); - mNaturalArmorBonus = new ChangeTrackedLiveData<>(0, this::makeDirty); - mHasCustomHitPoints = new ChangeTrackedLiveData<>(false, this::makeDirty); - mArmorType = new ChangeTrackedLiveData<>(ArmorType.NONE, this::makeDirty); - mHasShield = new ChangeTrackedLiveData<>(false, this::makeDirty); - mShieldBonus = new ChangeTrackedLiveData<>(0, this::makeDirty); - mCustomArmor = new ChangeTrackedLiveData<>("", this::makeDirty); - mWalkSpeed = new ChangeTrackedLiveData<>(0, this::makeDirty); - mBurrowSpeed = new ChangeTrackedLiveData<>(0, this::makeDirty); - mClimbSpeed = new ChangeTrackedLiveData<>(0, this::makeDirty); - mFlySpeed = new ChangeTrackedLiveData<>(0, this::makeDirty); - mSwimSpeed = new ChangeTrackedLiveData<>(0, this::makeDirty); - mCanHover = new ChangeTrackedLiveData<>(false, this::makeDirty); - mHasCustomSpeed = new ChangeTrackedLiveData<>(false, this::makeDirty); - mCustomSpeed = new ChangeTrackedLiveData<>("", this::makeDirty); - mStrength = new ChangeTrackedLiveData<>(10, this::makeDirty); - mDexterity = new ChangeTrackedLiveData<>(10, this::makeDirty); - mConstitution = new ChangeTrackedLiveData<>(10, this::makeDirty); - mIntelligence = new ChangeTrackedLiveData<>(10, this::makeDirty); - mWisdom = new ChangeTrackedLiveData<>(10, this::makeDirty); - mCharisma = new ChangeTrackedLiveData<>(10, this::makeDirty); - mStrengthProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, this::makeDirty); - mStrengthAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, this::makeDirty); - mDexterityProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, this::makeDirty); - mDexterityAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, this::makeDirty); - mConstitutionProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, this::makeDirty); - mConstitutionAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, this::makeDirty); - mIntelligenceProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, this::makeDirty); - mIntelligenceAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, this::makeDirty); - mWisdomProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, this::makeDirty); - mWisdomAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, this::makeDirty); - mCharismaProficiency = new ChangeTrackedLiveData<>(ProficiencyType.NONE, this::makeDirty); - mCharismaAdvantage = new ChangeTrackedLiveData<>(AdvantageType.NONE, this::makeDirty); - mChallengeRating = new ChangeTrackedLiveData<>(ChallengeRating.ONE_EIGHTH, this::makeDirty); - mCustomChallengeRatingDescription = new ChangeTrackedLiveData<>("", this::makeDirty); - mCustomProficiencyBonus = new ChangeTrackedLiveData<>(0, this::makeDirty); - mTelepathyRange = new ChangeTrackedLiveData<>(0, this::makeDirty); - mUnderstandsButDescription = new ChangeTrackedLiveData<>("", this::makeDirty); - mSkills = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mSenses = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mDamageImmunities = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mDamageResistances = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mDamageVulnerabilities = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mConditionImmunities = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mLanguages = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mAbilities = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mActions = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mReactions = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mLairActions = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mLegendaryActions = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - mRegionalActions = new ChangeTrackedLiveData<>(new ArrayList<>(), this::makeDirty); - } - - public void copyFromMonster(@NonNull Monster monster) { - mMonsterId.resetValue(monster.id); - mName.resetValue(monster.name); - mSize.resetValue(monster.size); - mType.resetValue(monster.type); - mSubtype.resetValue(monster.subtype); - mAlignment.resetValue(monster.alignment); - mCustomHitPoints.resetValue(monster.customHPDescription); - mHitDice.resetValue(monster.hitDice); - mNaturalArmorBonus.resetValue(monster.naturalArmorBonus); - mHasCustomHitPoints.resetValue(monster.hasCustomHP); - mArmorType.resetValue(monster.armorType); - mHasShield.resetValue(monster.shieldBonus != 0); - mShieldBonus.resetValue(monster.shieldBonus); - mCustomArmor.resetValue(monster.otherArmorDescription); - mWalkSpeed.resetValue(monster.walkSpeed); - mBurrowSpeed.resetValue(monster.burrowSpeed); - mClimbSpeed.resetValue(monster.climbSpeed); - mFlySpeed.resetValue(monster.flySpeed); - mSwimSpeed.resetValue(monster.swimSpeed); - mCanHover.resetValue(monster.canHover); - mHasCustomSpeed.resetValue(monster.hasCustomSpeed); - mCustomSpeed.resetValue(monster.customSpeedDescription); - mStrength.resetValue(monster.strengthScore); - mDexterity.resetValue(monster.dexterityScore); - mConstitution.resetValue(monster.constitutionScore); - mIntelligence.resetValue(monster.intelligenceScore); - mWisdom.resetValue(monster.wisdomScore); - mCharisma.resetValue(monster.charismaScore); - mStrengthProficiency.resetValue(monster.strengthSavingThrowProficiency); - mStrengthAdvantage.resetValue(monster.strengthSavingThrowAdvantage); - mDexterityProficiency.resetValue(monster.dexteritySavingThrowProficiency); - mDexterityAdvantage.resetValue(monster.dexteritySavingThrowAdvantage); - mConstitutionProficiency.resetValue(monster.constitutionSavingThrowProficiency); - mConstitutionAdvantage.resetValue(monster.constitutionSavingThrowAdvantage); - mIntelligenceProficiency.resetValue(monster.intelligenceSavingThrowProficiency); - mIntelligenceAdvantage.resetValue(monster.intelligenceSavingThrowAdvantage); - mWisdomProficiency.resetValue(monster.wisdomSavingThrowProficiency); - mWisdomAdvantage.resetValue(monster.wisdomSavingThrowAdvantage); - mCharismaProficiency.resetValue(monster.charismaSavingThrowProficiency); - mCharismaAdvantage.resetValue(monster.charismaSavingThrowAdvantage); - mChallengeRating.resetValue(monster.challengeRating); - mCustomChallengeRatingDescription.resetValue(monster.customChallengeRatingDescription); - mCustomProficiencyBonus.resetValue(monster.customProficiencyBonus); - mTelepathyRange.resetValue(monster.telepathyRange); - mUnderstandsButDescription.resetValue(monster.understandsButDescription); - - ArrayList skills = new ArrayList<>(monster.skills); - Collections.sort(skills, Skill::compareTo); - mSkills.resetValue(skills); - ArrayList senses = new ArrayList<>(monster.senses); - Collections.sort(senses, String::compareToIgnoreCase); - mSenses.resetValue(senses); - ArrayList damageImmunities = new ArrayList<>(monster.damageImmunities); - Collections.sort(damageImmunities, String::compareToIgnoreCase); - mDamageImmunities.resetValue(damageImmunities); - ArrayList damageResistances = new ArrayList<>(monster.damageResistances); - Collections.sort(damageResistances, String::compareToIgnoreCase); - mDamageResistances.resetValue(damageResistances); - ArrayList damageVulnerabilities = new ArrayList<>(monster.damageVulnerabilities); - Collections.sort(damageVulnerabilities, String::compareToIgnoreCase); - mDamageVulnerabilities.resetValue(damageVulnerabilities); - ArrayList conditionImmunities = new ArrayList<>(monster.conditionImmunities); - Collections.sort(conditionImmunities, String::compareToIgnoreCase); - mConditionImmunities.resetValue(conditionImmunities); - ArrayList languages = new ArrayList<>(monster.languages); - Collections.sort(languages, Language::compareTo); - mLanguages.resetValue(languages); - mAbilities.resetValue(new ArrayList<>(monster.abilities)); - mActions.resetValue(new ArrayList<>(monster.actions)); - mReactions.resetValue(new ArrayList<>(monster.reactions)); - mLairActions.resetValue(new ArrayList<>(monster.lairActions)); - mLegendaryActions.resetValue(new ArrayList<>(monster.legendaryActions)); - mRegionalActions.resetValue(new ArrayList<>(monster.regionalActions)); - makeClean(); - } - - public LiveData getName() { - return mName; - } - - public void setName(@NonNull String name) { - mName.setValue(name); - } - - public LiveData getMonsterId() { - return mMonsterId; - } - - public void setErrorMessage(@NonNull String errorMessage) { - mErrorMessage.setValue(errorMessage); - } - - public LiveData getHasError() { - return mHasError; - } - - public void setHasError(@NonNull Boolean hasError) { - mHasError.setValue(hasError); - } - - public boolean hasError() { - return getHasError().getValue(); - } - - public LiveData getHasLoaded() { - return mHasLoaded; - } - - public void setHasLoaded(@NonNull Boolean hasLoaded) { - mHasLoaded.setValue(hasLoaded); - } - - public boolean hasLoaded() { - return getHasLoaded().getValue(); - } - - public LiveData getSize() { - return mSize; - } - - public void setSize(@NonNull String size) { - mSize.setValue(size); - } - - public LiveData getType() { - return mType; - } - - public void setType(@NonNull String type) { - mType.setValue(type); - } - - public LiveData getSubtype() { - return mSubtype; - } - - public void setSubtype(@NonNull String subtype) { - mSubtype.setValue(subtype); - } - - public LiveData getAlignment() { - return mAlignment; - } - - public void setAlignment(@NonNull String alignment) { - mAlignment.setValue(alignment); - } - - public LiveData getCustomHitPoints() { - return mCustomHitPoints; - } - - public void setCustomHitPoints(String customHitPoints) { - mCustomHitPoints.setValue(customHitPoints); - } - - public void setHitDice(int hitDice) { - mHitDice.setValue(hitDice); - } - - public int getHitDiceUnboxed() { - return Helpers.unboxInteger(mHitDice.getValue(), 1); - } - - public void setNaturalArmorBonus(int naturalArmorBonus) { - mNaturalArmorBonus.setValue(naturalArmorBonus); - } - - public int getNaturalArmorBonusUnboxed() { - return Helpers.unboxInteger(mNaturalArmorBonus.getValue(), 0); - } - - public LiveData getHasCustomHitPoints() { - return mHasCustomHitPoints; - } - - public void setHasCustomHitPoints(boolean hasCustomHitPoints) { - mHasCustomHitPoints.setValue(hasCustomHitPoints); - } - - public boolean getHasCustomHitPointsValueAsBoolean() { - return mHasCustomHitPoints.getValue(); - } - - public LiveData getArmorType() { - return mArmorType; - } - - public void setArmorType(ArmorType armorType) { - mArmorType.setValue(armorType); - } - - public void setHasShield(boolean hasShield) { - mHasShield.setValue(hasShield); - } - - public boolean getHasShieldValueAsBoolean() { - return mHasShield.getValue(); - } - - public void setShieldBonus(int shieldBonus) { - mShieldBonus.setValue(shieldBonus); - } - - public int getShieldBonusUnboxed() { - return Helpers.unboxInteger(mShieldBonus.getValue(), 0); - } - - public LiveData getCustomArmor() { - return mCustomArmor; - } - - public void setCustomArmor(String customArmor) { - mCustomArmor.setValue(customArmor); - } - - public String getShieldBonusValueAsString() { - return mShieldBonus.getValue().toString(); - } - - public LiveData getWalkSpeed() { - return mWalkSpeed; - } - - public void setWalkSpeed(int walkSpeed) { - mWalkSpeed.setValue(walkSpeed); - } - - public LiveData getBurrowSpeed() { - return mBurrowSpeed; - } - - public void setBurrowSpeed(int burrowSpeed) { - mBurrowSpeed.setValue(burrowSpeed); - } - - public LiveData getClimbSpeed() { - return mClimbSpeed; - } - - public void setClimbSpeed(int climbSpeed) { - mClimbSpeed.setValue(climbSpeed); - } - - public LiveData getFlySpeed() { - return mFlySpeed; - } - - public void setFlySpeed(int flySpeed) { - mFlySpeed.setValue(flySpeed); - } - - public LiveData getSwimSpeed() { - return mSwimSpeed; - } - - public void setSwimSpeed(int swimSpeed) { - mSwimSpeed.setValue(swimSpeed); - } - - public LiveData getCanHover() { - return mCanHover; - } - - public void setCanHover(boolean canHover) { - mCanHover.setValue(canHover); - } - - public LiveData getHasCustomSpeed() { - return mHasCustomSpeed; - } - - public void setHasCustomSpeed(boolean hasCustomSpeed) { - mHasCustomSpeed.setValue(hasCustomSpeed); - } - - public LiveData getCustomSpeed() { - return mCustomSpeed; - } - - public void setCustomSpeed(String customSpeed) { - mCustomSpeed.setValue(customSpeed); - } - - public LiveData getStrength() { - return mStrength; - } - - public void setStrength(int strength) { - mStrength.setValue(strength); - } - - public LiveData getDexterity() { - return mDexterity; - } - - public void setDexterity(int dexterity) { - mDexterity.setValue(dexterity); - } - - public LiveData getConstitution() { - return mConstitution; - } - - public void setConstitution(int constitution) { - mConstitution.setValue(constitution); - } - - public LiveData getIntelligence() { - return mIntelligence; - } - - public void setIntelligence(int intelligence) { - mIntelligence.setValue(intelligence); - } - - public LiveData getWisdom() { - return mWisdom; - } - - public void setWisdom(int wisdom) { - mWisdom.setValue(wisdom); - } - - public LiveData getCharisma() { - return mCharisma; - } - - public void setCharisma(int charisma) { - mCharisma.setValue(charisma); - } - - public LiveData getStrengthProficiency() { - return mStrengthProficiency; - } - - public void setStrengthProficiency(ProficiencyType proficiency) { - mStrengthProficiency.setValue(proficiency); - } - - public LiveData getStrengthAdvantage() { - return mStrengthAdvantage; - } - - public void setStrengthAdvantage(AdvantageType advantage) { - mStrengthAdvantage.setValue(advantage); - } - - public LiveData getDexterityProficiency() { - return mDexterityProficiency; - } - - public void setDexterityProficiency(ProficiencyType proficiency) { - mDexterityProficiency.setValue(proficiency); - } - - public LiveData getDexterityAdvantage() { - return mDexterityAdvantage; - } - - public void setDexterityAdvantage(AdvantageType advantage) { - mDexterityAdvantage.setValue(advantage); - } - - public LiveData getConstitutionProficiency() { - return mConstitutionProficiency; - } - - public void setConstitutionProficiency(ProficiencyType proficiency) { - mConstitutionProficiency.setValue(proficiency); - } - - public LiveData getConstitutionAdvantage() { - return mConstitutionAdvantage; - } - - public void setConstitutionAdvantage(AdvantageType advantage) { - mConstitutionAdvantage.setValue(advantage); - } - - public LiveData getIntelligenceProficiency() { - return mIntelligenceProficiency; - } - - public void setIntelligenceProficiency(ProficiencyType proficiency) { - mIntelligenceProficiency.setValue(proficiency); - } - - public LiveData getIntelligenceAdvantage() { - return mIntelligenceAdvantage; - } - - public void setIntelligenceAdvantage(AdvantageType advantage) { - mIntelligenceAdvantage.setValue(advantage); - } - - public LiveData getWisdomProficiency() { - return mWisdomProficiency; - } - - public void setWisdomProficiency(ProficiencyType proficiency) { - mWisdomProficiency.setValue(proficiency); - } - - public LiveData getWisdomAdvantage() { - return mWisdomAdvantage; - } - - public void setWisdomAdvantage(AdvantageType advantage) { - mWisdomAdvantage.setValue(advantage); - } - - public LiveData getCharismaProficiency() { - return mCharismaProficiency; - } - - public void setCharismaProficiency(ProficiencyType proficiency) { - mCharismaProficiency.setValue(proficiency); - } - - public LiveData getCharismaAdvantage() { - return mCharismaAdvantage; - } - - public void setCharismaAdvantage(AdvantageType advantage) { - mCharismaAdvantage.setValue(advantage); - } - - public LiveData getChallengeRating() { - return mChallengeRating; - } - - public void setChallengeRating(ChallengeRating challengeRating) { - mChallengeRating.setValue(challengeRating); - } - - public LiveData getCustomChallengeRatingDescription() { - return mCustomChallengeRatingDescription; - } - - public void setCustomChallengeRatingDescription(String customChallengeRatingDescription) { - mCustomChallengeRatingDescription.setValue(customChallengeRatingDescription); - } - - public LiveData getCustomProficiencyBonus() { - return mCustomProficiencyBonus; - } - - public void setCustomProficiencyBonus(int proficiencyBonus) { - mCustomProficiencyBonus.setValue(proficiencyBonus); - } - - public void setCustomProficiencyBonus(String proficiencyBonus) { - Integer parsedValue = StringHelper.parseInt(proficiencyBonus); - this.setCustomProficiencyBonus(parsedValue != null ? parsedValue : 0); - } - - public String getCustomProficiencyBonusValueAsString() { - return mCustomProficiencyBonus.getValue().toString(); - } - - public LiveData getTelepathyRange() { - return mTelepathyRange; - } - - public void setTelepathyRange(int telepathyRange) { - mTelepathyRange.setValue(telepathyRange); - } - - public int getTelepathyRangeUnboxed() { - return Helpers.unboxInteger(mTelepathyRange.getValue(), 0); - } - - public LiveData getUnderstandsButDescription() { - return mUnderstandsButDescription; - } - - public void setUnderstandsButDescription(String understandsButDescription) { - mUnderstandsButDescription.setValue(understandsButDescription); - } - - public LiveData> getSkills() { - return mSkills; - } - - public List getSkillsArray() { - return mSkills.getValue(); - } - - public Skill addNewSkill() { - Skill newSkill = new Skill("Unnamed Skill", AbilityScore.DEXTERITY); - ArrayList newSkills = new ArrayList<>(mSkills.getValue()); - newSkills.add(newSkill); - Collections.sort(newSkills, (skill1, skill2) -> skill1.name.compareToIgnoreCase(skill2.name)); - mSkills.setValue(newSkills); - return newSkill; - } - - public void removeSkill(int position) { - List skills = mSkills.getValue(); - ArrayList newSkills = new ArrayList<>(skills); - newSkills.remove(position); - mSkills.setValue(newSkills); - } - - public void replaceSkill(Skill newSkill, Skill oldSkill) { - List oldSkills = mSkills.getValue(); - if (oldSkills == null) { - oldSkills = new ArrayList<>(); - } - boolean hasReplaced = false; - ArrayList newSkills = new ArrayList<>(oldSkills.size()); - for (Skill skill : oldSkills) { - if (Objects.equals(skill, oldSkill)) { - newSkills.add(newSkill); - hasReplaced = true; - } else { - newSkills.add(skill); - } - } - if (!hasReplaced) { - newSkills.add(newSkill); - } - Collections.sort(newSkills, (skill1, skill2) -> skill1.name.compareToIgnoreCase(skill2.name)); - mSkills.setValue(newSkills); - } - - public LiveData> getSenses() { - return mSenses; - } - - public LiveData> getDamageImmunities() { - return mDamageImmunities; - } - - public List getDamageImmunitiesArray() { - return mDamageImmunities.getValue(); - } - - public LiveData> getDamageResistances() { - return mDamageResistances; - } - - public LiveData> getLanguages() { - return mLanguages; - } - - public List getLanguagesArray() { - return mLanguages.getValue(); - } - - public Language addNewLanguage() { - Language newLanguage = new Language("", true); - return Helpers.addItemToList(mLanguages, newLanguage, Language::compareTo); - } - - public void removeLanguage(int position) { - Helpers.removeFromList(mLanguages, position); - } - - public void replaceLanguage(Language oldLanguage, Language newLanguage) { - Helpers.replaceItemInList(mLanguages, oldLanguage, newLanguage, Language::compareTo); - } - - public Monster buildMonster() { - Monster monster = new Monster(); - - monster.id = mMonsterId.getValue(); - monster.name = mName.getValue(); - monster.size = mSize.getValue(); - monster.type = mType.getValue(); - monster.subtype = mSubtype.getValue(); - monster.alignment = mAlignment.getValue(); - monster.customHPDescription = mCustomHitPoints.getValue(); - monster.hitDice = mHitDice.getValue(); - monster.hasCustomHP = mHasCustomHitPoints.getValue(); - monster.armorType = mArmorType.getValue(); - monster.naturalArmorBonus = mNaturalArmorBonus.getValue(); - monster.shieldBonus = mShieldBonus.getValue(); - monster.otherArmorDescription = mCustomArmor.getValue(); - monster.walkSpeed = mWalkSpeed.getValue(); - monster.burrowSpeed = mBurrowSpeed.getValue(); - monster.climbSpeed = mClimbSpeed.getValue(); - monster.flySpeed = mFlySpeed.getValue(); - monster.swimSpeed = mSwimSpeed.getValue(); - monster.canHover = mCanHover.getValue(); - monster.hasCustomSpeed = mHasCustomSpeed.getValue(); - monster.customSpeedDescription = mCustomSpeed.getValue(); - monster.strengthScore = mStrength.getValue(); - monster.dexterityScore = mDexterity.getValue(); - monster.constitutionScore = mConstitution.getValue(); - monster.intelligenceScore = mIntelligence.getValue(); - monster.wisdomScore = mWisdom.getValue(); - monster.charismaScore = mCharisma.getValue(); - monster.strengthSavingThrowAdvantage = mStrengthAdvantage.getValue(); - monster.strengthSavingThrowProficiency = mStrengthProficiency.getValue(); - monster.dexteritySavingThrowAdvantage = mDexterityAdvantage.getValue(); - monster.dexteritySavingThrowProficiency = mDexterityProficiency.getValue(); - monster.constitutionSavingThrowAdvantage = mConstitutionAdvantage.getValue(); - monster.constitutionSavingThrowProficiency = mConstitutionProficiency.getValue(); - monster.intelligenceSavingThrowAdvantage = mIntelligenceAdvantage.getValue(); - monster.intelligenceSavingThrowProficiency = mIntelligenceProficiency.getValue(); - monster.wisdomSavingThrowAdvantage = mWisdomAdvantage.getValue(); - monster.wisdomSavingThrowProficiency = mWisdomProficiency.getValue(); - monster.charismaSavingThrowAdvantage = mCharismaAdvantage.getValue(); - monster.charismaSavingThrowProficiency = mCharismaProficiency.getValue(); - monster.challengeRating = mChallengeRating.getValue(); - monster.customChallengeRatingDescription = mCustomChallengeRatingDescription.getValue(); - monster.customProficiencyBonus = mCustomProficiencyBonus.getValue(); - monster.telepathyRange = mTelepathyRange.getValue(); - monster.understandsButDescription = mUnderstandsButDescription.getValue(); - monster.skills = new HashSet<>(mSkills.getValue()); - monster.senses = new HashSet<>(mSenses.getValue()); - monster.damageImmunities = new HashSet<>(mDamageImmunities.getValue()); - monster.damageResistances = new HashSet<>(mDamageResistances.getValue()); - monster.damageVulnerabilities = new HashSet<>(mDamageVulnerabilities.getValue()); - monster.conditionImmunities = new HashSet<>(mConditionImmunities.getValue()); - monster.languages = new HashSet<>(mLanguages.getValue()); - monster.abilities = new ArrayList<>(mAbilities.getValue()); - monster.actions = new ArrayList<>(mActions.getValue()); - monster.reactions = new ArrayList<>(mReactions.getValue()); - monster.lairActions = new ArrayList<>(mLairActions.getValue()); - monster.legendaryActions = new ArrayList<>(mLegendaryActions.getValue()); - monster.regionalActions = new ArrayList<>(mRegionalActions.getValue()); - - return monster; - } - - public LiveData> getTraits(@NonNull TraitType type) { - switch (type) { - case ABILITY: - return mAbilities; - case ACTION: - return mActions; - case LAIR_ACTION: - return mLairActions; - case LEGENDARY_ACTION: - return mLegendaryActions; - case REACTIONS: - return mReactions; - case REGIONAL_ACTION: - return mRegionalActions; - default: - Logger.logUnimplementedFeature(String.format("Unrecognized TraitType: %s", type)); - return null; - } - } - - public void removeTrait(@NonNull TraitType type, int position) { - switch (type) { - case ABILITY: - Helpers.removeFromList(mAbilities, position); - break; - case ACTION: - Helpers.removeFromList(mActions, position); - break; - case LAIR_ACTION: - Helpers.removeFromList(mLairActions, position); - break; - case LEGENDARY_ACTION: - Helpers.removeFromList(mLegendaryActions, position); - break; - case REACTIONS: - Helpers.removeFromList(mReactions, position); - break; - case REGIONAL_ACTION: - Helpers.removeFromList(mRegionalActions, position); - break; - default: - Logger.logUnimplementedFeature(String.format("Unrecognized TraitType: %s", type)); - break; - } - } - - public void replaceTrait(@NonNull TraitType type, Trait oldTrait, Trait newTrait) { - switch (type) { - case ABILITY: - Helpers.replaceItemInList(mAbilities, oldTrait, newTrait); - break; - case ACTION: - Helpers.replaceItemInList(mActions, oldTrait, newTrait); - break; - case LAIR_ACTION: - Helpers.replaceItemInList(mLairActions, oldTrait, newTrait); - break; - case LEGENDARY_ACTION: - Helpers.replaceItemInList(mLegendaryActions, oldTrait, newTrait); - break; - case REACTIONS: - Helpers.replaceItemInList(mReactions, oldTrait, newTrait); - break; - case REGIONAL_ACTION: - Helpers.replaceItemInList(mRegionalActions, oldTrait, newTrait); - break; - default: - Logger.logUnimplementedFeature(String.format("Unrecognized TraitType: %s", type)); - } - } - - public Trait addNewTrait(@NonNull TraitType type) { - Trait newAction = new Trait("", ""); - switch (type) { - case ABILITY: - return Helpers.addItemToList(mAbilities, newAction); - case ACTION: - return Helpers.addItemToList(mActions, newAction); - case LAIR_ACTION: - return Helpers.addItemToList(mLairActions, newAction); - case LEGENDARY_ACTION: - return Helpers.addItemToList(mLegendaryActions, newAction); - case REACTIONS: - return Helpers.addItemToList(mReactions, newAction); - case REGIONAL_ACTION: - return Helpers.addItemToList(mRegionalActions, newAction); - default: - Logger.logUnimplementedFeature(String.format("Unrecognized TraitType: %s", type)); - return null; - } - } - - public LiveData> getStrings(@NonNull StringType type) { - switch (type) { - case CONDITION_IMMUNITY: - return mConditionImmunities; - case DAMAGE_IMMUNITY: - return mDamageImmunities; - case DAMAGE_RESISTANCE: - return mDamageResistances; - case DAMAGE_VULNERABILITY: - return mDamageVulnerabilities; - case SENSE: - return mSenses; - default: - Logger.logUnimplementedFeature(String.format("Unrecognized StringType: %s", type)); - return null; - } - } - - public void removeString(@NonNull StringType type, int position) { - switch (type) { - case CONDITION_IMMUNITY: - Helpers.removeFromList(mConditionImmunities, position); - break; - case DAMAGE_IMMUNITY: - Helpers.removeFromList(mDamageImmunities, position); - break; - case DAMAGE_RESISTANCE: - Helpers.removeFromList(mDamageResistances, position); - break; - case DAMAGE_VULNERABILITY: - Helpers.removeFromList(mDamageVulnerabilities, position); - break; - case SENSE: - Helpers.removeFromList(mSenses, position); - break; - default: - Logger.logUnimplementedFeature(String.format("Unrecognized StringType: %s", type)); - break; - } - } - - public String addNewString(@NonNull StringType type) { - String newString = ""; - switch (type) { - case CONDITION_IMMUNITY: - return Helpers.addItemToList(mConditionImmunities, newString); - case DAMAGE_IMMUNITY: - return Helpers.addItemToList(mDamageImmunities, newString); - case DAMAGE_RESISTANCE: - return Helpers.addItemToList(mDamageResistances, newString); - case DAMAGE_VULNERABILITY: - return Helpers.addItemToList(mDamageVulnerabilities, newString); - case SENSE: - return Helpers.addItemToList(mSenses, newString); - default: - Logger.logUnimplementedFeature(String.format("Unrecognized StringType: %s", type)); - return null; - } - } - - public void replaceString(@NonNull StringType type, String oldValue, String newValue) { - switch (type) { - case CONDITION_IMMUNITY: - Helpers.replaceItemInList(mConditionImmunities, oldValue, newValue); - break; - case DAMAGE_IMMUNITY: - Helpers.replaceItemInList(mDamageImmunities, oldValue, newValue); - break; - case DAMAGE_RESISTANCE: - Helpers.replaceItemInList(mDamageResistances, oldValue, newValue); - break; - case DAMAGE_VULNERABILITY: - Helpers.replaceItemInList(mDamageVulnerabilities, oldValue, newValue); - break; - case SENSE: - Helpers.replaceItemInList(mSenses, oldValue, newValue); - break; - default: - Logger.logUnimplementedFeature(String.format("Unrecognized StringType: %s", type)); - } - } - - public boolean moveTrait(@NonNull TraitType type, int from, int to) { - switch (type) { - case ABILITY: - return Helpers.moveItemInList(mAbilities, from, to); - case ACTION: - return Helpers.moveItemInList(mActions, from, to); - case LAIR_ACTION: - return Helpers.moveItemInList(mLairActions, from, to); - case LEGENDARY_ACTION: - return Helpers.moveItemInList(mLegendaryActions, from, to); - case REACTIONS: - return Helpers.moveItemInList(mReactions, from, to); - case REGIONAL_ACTION: - return Helpers.moveItemInList(mRegionalActions, from, to); - default: - Logger.logUnimplementedFeature(String.format("Unrecognized TraitType: %s", type)); - return false; - } - - - } - - @SuppressWarnings("SameParameterValue") - private static class Helpers { - static T addItemToList(MutableLiveData> listData, T newItem) { - return addItemToList(listData, newItem, null); - } - - static T addItemToList(@NonNull MutableLiveData> listData, T newItem, Comparator comparator) { - ArrayList newList = new ArrayList<>(listData.getValue()); - newList.add(newItem); - if (comparator != null) { - Collections.sort(newList, comparator); - } - listData.setValue(newList); - return newItem; - } - - static void removeFromList(@NonNull MutableLiveData> listData, int position) { - List oldList = listData.getValue(); - ArrayList newList = new ArrayList<>(oldList); - newList.remove(position); - listData.setValue(newList); - } - - static void replaceItemInList(@NonNull MutableLiveData> listData, int position, T newItem, Comparator comparator) { - List oldList = listData.getValue(); - if (oldList == null) { - oldList = new ArrayList<>(); - } - int size = oldList.size(); - boolean hasReplaced = false; - ArrayList newList = new ArrayList<>(size); - for (int index = 0; index < size; index++) { - if (index == position) { - newList.add(newItem); - hasReplaced = true; - } else { - newList.add(oldList.get(index)); - } - } - if (!hasReplaced) { - newList.add(newItem); - } - if (comparator != null) { - Collections.sort(newList, comparator); - } - listData.setValue(newList); - } - - @SuppressWarnings("unused") - static void replaceItemInList(MutableLiveData> listData, int position, T newItem) { - replaceItemInList(listData, position, newItem, null); - } - - static void replaceItemInList(@NonNull MutableLiveData> listData, T oldItem, T newItem, Comparator comparator) { - List oldList = listData.getValue(); - if (oldList == null) { - oldList = new ArrayList<>(); - } - boolean hasReplaced = false; - ArrayList newList = new ArrayList<>(oldList.size()); - for (T item : oldList) { - if (!hasReplaced && Objects.equals(item, oldItem)) { - newList.add(newItem); - hasReplaced = true; - } else { - newList.add(item); - } - } - if (!hasReplaced) { - newList.add(newItem); - } - if (comparator != null) { - Collections.sort(newList, comparator); - } - listData.setValue(newList); - } - - static void replaceItemInList(MutableLiveData> listData, T oldItem, T newItem) { - replaceItemInList(listData, oldItem, newItem, null); - } - - static int unboxInteger(Integer value, int defaultIfNull) { - if (value == null) { - return defaultIfNull; - } - return value; - } - - static boolean moveItemInList(@NonNull ChangeTrackedLiveData> listData, int from, int to) { - List oldList = listData.getValue(); - if (oldList == null) { - oldList = new ArrayList<>(); - } - ArrayList newList = new ArrayList<>(oldList); - T item = oldList.get(from); - if (from > to) { - from = from + 1; - } else if (to > from) { - to = to + 1; - } - newList.add(to, item); - newList.remove(from); - listData.setValue(newList); - return true; - } - } -} +public class EditMonsterViewModel extends ViewModel { + // TODO: Implement the ViewModel +} \ No newline at end of file diff --git a/Android/app/src/main/java/com/majinnaibu/monstercards/ui/monster/EditMonsterFragment.java b/Android/app/src/main/java/com/majinnaibu/monstercards/ui/monster/EditMonsterFragment.java deleted file mode 100644 index b910f49..0000000 --- a/Android/app/src/main/java/com/majinnaibu/monstercards/ui/monster/EditMonsterFragment.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.majinnaibu.monstercards.ui.monster; - -import android.content.Context; -import android.os.Bundle; - -import androidx.fragment.app.Fragment; -import androidx.recyclerview.widget.GridLayoutManager; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; - -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.majinnaibu.monstercards.R; -import com.majinnaibu.monstercards.placeholder.PlaceholderContent; - -/** - * A fragment representing a list of Items. - */ -public class EditMonsterFragment extends Fragment { - - // TODO: Customize parameter argument names - private static final String ARG_COLUMN_COUNT = "column-count"; - // TODO: Customize parameters - private int mColumnCount = 1; - - /** - * Mandatory empty constructor for the fragment manager to instantiate the - * fragment (e.g. upon screen orientation changes). - */ - public EditMonsterFragment() { - } - - // TODO: Customize parameter initialization - @SuppressWarnings("unused") - public static EditMonsterFragment newInstance(int columnCount) { - EditMonsterFragment fragment = new EditMonsterFragment(); - Bundle args = new Bundle(); - args.putInt(ARG_COLUMN_COUNT, columnCount); - fragment.setArguments(args); - return fragment; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - if (getArguments() != null) { - mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT); - } - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.fragment_edit_monster_list, container, false); - - // Set the adapter - if (view instanceof RecyclerView) { - Context context = view.getContext(); - RecyclerView recyclerView = (RecyclerView) view; - if (mColumnCount <= 1) { - recyclerView.setLayoutManager(new LinearLayoutManager(context)); - } else { - recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)); - } - recyclerView.setAdapter(new EditMonsterRecyclerViewAdapter(PlaceholderContent.ITEMS)); - } - return view; - } -} \ No newline at end of file diff --git a/Android/app/src/main/java/com/majinnaibu/monstercards/ui/monster/EditMonsterRecyclerViewAdapter.java b/Android/app/src/main/java/com/majinnaibu/monstercards/ui/monster/EditMonsterRecyclerViewAdapter.java deleted file mode 100644 index d16a49a..0000000 --- a/Android/app/src/main/java/com/majinnaibu/monstercards/ui/monster/EditMonsterRecyclerViewAdapter.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.majinnaibu.monstercards.ui.monster; - -import androidx.recyclerview.widget.RecyclerView; - -import android.view.LayoutInflater; -import android.view.ViewGroup; -import android.widget.TextView; - -import com.majinnaibu.monstercards.placeholder.PlaceholderContent.PlaceholderItem; -import com.majinnaibu.monstercards.databinding.FragmentEditMonsterBinding; - -import java.util.List; - -/** - * {@link RecyclerView.Adapter} that can display a {@link PlaceholderItem}. - * TODO: Replace the implementation with code for your data type. - */ -public class EditMonsterRecyclerViewAdapter extends RecyclerView.Adapter { - - private final List mValues; - - public EditMonsterRecyclerViewAdapter(List items) { - mValues = items; - } - - @Override - public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - - return new ViewHolder(FragmentEditMonsterBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); - - } - - @Override - public void onBindViewHolder(final ViewHolder holder, int position) { - holder.mItem = mValues.get(position); - holder.mIdView.setText(mValues.get(position).id); - holder.mContentView.setText(mValues.get(position).content); - } - - @Override - public int getItemCount() { - return mValues.size(); - } - - public class ViewHolder extends RecyclerView.ViewHolder { - public final TextView mIdView; - public final TextView mContentView; - public PlaceholderItem mItem; - - public ViewHolder(FragmentEditMonsterBinding binding) { - super(binding.getRoot()); - mIdView = binding.itemNumber; - mContentView = binding.content; - } - - @Override - public String toString() { - return super.toString() + " '" + mContentView.getText() + "'"; - } - } -} \ No newline at end of file diff --git a/Android/app/src/main/res/layout/fragment_edit_monster.xml b/Android/app/src/main/res/layout/fragment_edit_monster.xml index 1877568..e6cb973 100644 --- a/Android/app/src/main/res/layout/fragment_edit_monster.xml +++ b/Android/app/src/main/res/layout/fragment_edit_monster.xml @@ -1,20 +1,20 @@ - + + android:id="@+id/textView" + android:layout_width="0dp" + android:layout_height="0dp" + android:text="Edit Monster" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> - - \ No newline at end of file + \ No newline at end of file diff --git a/Android/app/src/main/res/layout/fragment_edit_monster_list.xml b/Android/app/src/main/res/layout/fragment_edit_monster_list.xml deleted file mode 100644 index b39ee2d..0000000 --- a/Android/app/src/main/res/layout/fragment_edit_monster_list.xml +++ /dev/null @@ -1,13 +0,0 @@ - - \ No newline at end of file diff --git a/Android/app/src/main/res/navigation/mobile_navigation.xml b/Android/app/src/main/res/navigation/mobile_navigation.xml index 889d58a..6df7fb3 100644 --- a/Android/app/src/main/res/navigation/mobile_navigation.xml +++ b/Android/app/src/main/res/navigation/mobile_navigation.xml @@ -59,9 +59,9 @@ + android:name="com.majinnaibu.monstercards.ui.editmonster.EditMonsterFragment" + android:label="fragment_edit_monster" + tools:layout="@layout/fragment_edit_monster" >