Adds edit challenge rating screen.
This commit is contained in:
		| @@ -0,0 +1,93 @@ | |||||||
|  | package com.majinnaibu.monstercards.ui.editmonster; | ||||||
|  |  | ||||||
|  | import android.os.Bundle; | ||||||
|  | import android.view.LayoutInflater; | ||||||
|  | import android.view.View; | ||||||
|  | import android.view.ViewGroup; | ||||||
|  | import android.widget.AdapterView; | ||||||
|  | import android.widget.ArrayAdapter; | ||||||
|  | import android.widget.EditText; | ||||||
|  | import android.widget.Spinner; | ||||||
|  | import android.widget.TextView; | ||||||
|  |  | ||||||
|  | import androidx.annotation.NonNull; | ||||||
|  | import androidx.annotation.Nullable; | ||||||
|  | import androidx.fragment.app.Fragment; | ||||||
|  | import androidx.lifecycle.ViewModelProvider; | ||||||
|  | import androidx.navigation.NavBackStackEntry; | ||||||
|  | import androidx.navigation.NavController; | ||||||
|  | import androidx.navigation.Navigation; | ||||||
|  |  | ||||||
|  | import com.majinnaibu.monstercards.R; | ||||||
|  | import com.majinnaibu.monstercards.data.enums.ChallengeRating; | ||||||
|  | import com.majinnaibu.monstercards.helpers.ArrayHelper; | ||||||
|  | import com.majinnaibu.monstercards.utils.TextChangedListener; | ||||||
|  |  | ||||||
|  | public class EditChallengeRatingFragment extends Fragment { | ||||||
|  |     private EditMonsterViewModel mViewModel; | ||||||
|  |     private ViewHolder mHolder; | ||||||
|  |  | ||||||
|  |     @Override | ||||||
|  |     public View onCreateView(LayoutInflater inflater, ViewGroup container, | ||||||
|  |                              Bundle savedInstanceState) { | ||||||
|  |         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); | ||||||
|  |  | ||||||
|  |         // Inflate the layout for this fragment | ||||||
|  |         View root = inflater.inflate(R.layout.fragment_edit_challenge_rating, container, false); | ||||||
|  |         mHolder = new ViewHolder(root); | ||||||
|  |  | ||||||
|  |         mHolder.challengeRating.setAdapter(new ArrayAdapter<ChallengeRating>(requireContext(), R.layout.dropdown_list_item, ChallengeRating.values()) { | ||||||
|  |             @NonNull | ||||||
|  |             @Override | ||||||
|  |             public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | ||||||
|  |                 ChallengeRating item = getItem(position); | ||||||
|  |                 TextView view = (TextView) super.getView(position, convertView, parent); | ||||||
|  |                 view.setText(item.displayName); | ||||||
|  |                 return view; | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             @Override | ||||||
|  |             public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | ||||||
|  |                 ChallengeRating item = getItem(position); | ||||||
|  |                 TextView view = (TextView) super.getDropDownView(position, convertView, parent); | ||||||
|  |                 view.setText(item.displayName); | ||||||
|  |                 return view; | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |         mHolder.challengeRating.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | ||||||
|  |             @Override | ||||||
|  |             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | ||||||
|  |                 ChallengeRating selectedItem = (ChallengeRating) parent.getItemAtPosition(position); | ||||||
|  |                 mViewModel.setChallengeRating(selectedItem); | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             @Override | ||||||
|  |             public void onNothingSelected(AdapterView<?> parent) { | ||||||
|  |                 mViewModel.setChallengeRating(ChallengeRating.CUSTOM); | ||||||
|  |             } | ||||||
|  |         }); | ||||||
|  |         mHolder.challengeRating.setSelection(ArrayHelper.indexOf(ChallengeRating.values(), mViewModel.getChallengeRating().getValue())); | ||||||
|  |  | ||||||
|  |         mHolder.customChallengeRatingDescription.setText(mViewModel.getCustomChallengeRatingDescription().getValue()); | ||||||
|  |         mHolder.customChallengeRatingDescription.addTextChangedListener((new TextChangedListener((TextChangedListener.OnTextChangedCallback) (s, start, before, count) -> mViewModel.setCustomChallengeRatingDescription(s.toString())))); | ||||||
|  |  | ||||||
|  |         mHolder.customProficiencyBonus.setText(mViewModel.getCustomProficiencyBonusValueAsString()); | ||||||
|  |         mHolder.customProficiencyBonus.addTextChangedListener((new TextChangedListener((TextChangedListener.OnTextChangedCallback) (s, start, before, count) -> mViewModel.setCustomProficiencyBonus(s.toString())))); | ||||||
|  |  | ||||||
|  |         return root; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     private static class ViewHolder { | ||||||
|  |         final Spinner challengeRating; | ||||||
|  |         final EditText customChallengeRatingDescription; | ||||||
|  |         final EditText customProficiencyBonus; | ||||||
|  |  | ||||||
|  |         ViewHolder(View root) { | ||||||
|  |             challengeRating = root.findViewById(R.id.challengeRating); | ||||||
|  |             customChallengeRatingDescription = root.findViewById(R.id.customChallengeRatingDescription); | ||||||
|  |             customProficiencyBonus = root.findViewById(R.id.customProficiencyBonus); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
| @@ -83,37 +83,32 @@ public class EditMonsterFragment extends MCFragment { | |||||||
|  |  | ||||||
|         mHolder.basicInfoButton.setOnClickListener(v -> { |         mHolder.basicInfoButton.setOnClickListener(v -> { | ||||||
|             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditBasicInfoFragment(); |             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditBasicInfoFragment(); | ||||||
|             View view = getView(); |             Navigation.findNavController(requireView()).navigate(action); | ||||||
|             assert view != null; |  | ||||||
|             Navigation.findNavController(view).navigate(action); |  | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         mHolder.armorButton.setOnClickListener(v -> { |         mHolder.armorButton.setOnClickListener(v -> { | ||||||
|             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditArmorFragment(); |             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditArmorFragment(); | ||||||
|             View view = getView(); |             Navigation.findNavController(requireView()).navigate(action); | ||||||
|             assert view != null; |  | ||||||
|             Navigation.findNavController(view).navigate(action); |  | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         mHolder.speedButton.setOnClickListener(v -> { |         mHolder.speedButton.setOnClickListener(v -> { | ||||||
|             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditSpeedFragment(); |             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditSpeedFragment(); | ||||||
|             View view = getView(); |             Navigation.findNavController(requireView()).navigate(action); | ||||||
|             assert view != null; |  | ||||||
|             Navigation.findNavController(view).navigate(action); |  | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         mHolder.abilityScoresButton.setOnClickListener(v -> { |         mHolder.abilityScoresButton.setOnClickListener(v -> { | ||||||
|             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditAbilityScoresFragment(); |             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditAbilityScoresFragment(); | ||||||
|             View view = getView(); |             Navigation.findNavController(requireView()).navigate(action); | ||||||
|             assert view != null; |  | ||||||
|             Navigation.findNavController(view).navigate(action); |  | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         mHolder.savingThrows.setOnClickListener(v -> { |         mHolder.savingThrows.setOnClickListener(v -> { | ||||||
|             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditSavingThrowsFragment(); |             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditSavingThrowsFragment(); | ||||||
|             View view = getView(); |             Navigation.findNavController(requireView()).navigate(action); | ||||||
|             assert view != null; |         }); | ||||||
|             Navigation.findNavController(view).navigate(action); |  | ||||||
|  |         mHolder.challengeRating.setOnClickListener(v -> { | ||||||
|  |             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditChallengeRatingFragment(); | ||||||
|  |             Navigation.findNavController(requireView()).navigate(action); | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) { |         requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) { | ||||||
|   | |||||||
| @@ -729,6 +729,15 @@ public class EditMonsterViewModel extends ViewModel { | |||||||
|         mCustomProficiencyBonus.setValue(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<Integer> getBlindsightRange() { |     public LiveData<Integer> getBlindsightRange() { | ||||||
|         return mBlindsightRange; |         return mBlindsightRange; | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -24,7 +24,6 @@ | |||||||
|                 android:id="@+id/naturalArmorBonus" |                 android:id="@+id/naturalArmorBonus" | ||||||
|                 android:layout_width="match_parent" |                 android:layout_width="match_parent" | ||||||
|                 android:layout_height="wrap_content" |                 android:layout_height="wrap_content" | ||||||
|                 android:digits="0123456789" |  | ||||||
|                 android:hint="@string/label_natural_armor_bonus" |                 android:hint="@string/label_natural_armor_bonus" | ||||||
|                 android:importantForAutofill="no" |                 android:importantForAutofill="no" | ||||||
|                 android:inputType="numberSigned" |                 android:inputType="numberSigned" | ||||||
| @@ -48,7 +47,6 @@ | |||||||
|                 android:id="@+id/shieldBonus" |                 android:id="@+id/shieldBonus" | ||||||
|                 android:layout_width="match_parent" |                 android:layout_width="match_parent" | ||||||
|                 android:layout_height="wrap_content" |                 android:layout_height="wrap_content" | ||||||
|                 android:digits="0123456789" |  | ||||||
|                 android:hint="@string/label_shield_bonus" |                 android:hint="@string/label_shield_bonus" | ||||||
|                 android:importantForAutofill="no" |                 android:importantForAutofill="no" | ||||||
|                 android:inputType="numberSigned" |                 android:inputType="numberSigned" | ||||||
|   | |||||||
							
								
								
									
										48
									
								
								app/src/main/res/layout/fragment_edit_challenge_rating.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								app/src/main/res/layout/fragment_edit_challenge_rating.xml
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,48 @@ | |||||||
|  | <?xml version="1.0" encoding="utf-8"?> | ||||||
|  | <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" | ||||||
|  |     xmlns:tools="http://schemas.android.com/tools" | ||||||
|  |     android:layout_width="match_parent" | ||||||
|  |     android:layout_height="match_parent" | ||||||
|  |     tools:context=".ui.editmonster.EditChallengeRatingFragment"> | ||||||
|  |  | ||||||
|  |     <LinearLayout | ||||||
|  |         android:layout_width="match_parent" | ||||||
|  |         android:layout_height="wrap_content" | ||||||
|  |         android:orientation="vertical"> | ||||||
|  |  | ||||||
|  |         <Spinner | ||||||
|  |             android:id="@+id/challengeRating" | ||||||
|  |             android:layout_width="match_parent" | ||||||
|  |             android:layout_height="wrap_content" /> | ||||||
|  |  | ||||||
|  |         <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/customChallengeRatingDescription" | ||||||
|  |                 android:layout_width="match_parent" | ||||||
|  |                 android:layout_height="wrap_content" | ||||||
|  |                 android:hint="@string/label_custom_challenge_rating_description" | ||||||
|  |                 android:importantForAutofill="no" | ||||||
|  |                 android:inputType="text" | ||||||
|  |                 tools:text="John Doe" /> | ||||||
|  |         </com.google.android.material.textfield.TextInputLayout> | ||||||
|  |  | ||||||
|  |         <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/customProficiencyBonus" | ||||||
|  |                 android:layout_width="match_parent" | ||||||
|  |                 android:layout_height="wrap_content" | ||||||
|  |                 android:hint="@string/label_custom_proficiency_bonus" | ||||||
|  |                 android:importantForAutofill="no" | ||||||
|  |                 android:inputType="numberSigned" | ||||||
|  |                 tools:text="+2" /> | ||||||
|  |         </com.google.android.material.textfield.TextInputLayout> | ||||||
|  |     </LinearLayout> | ||||||
|  | </ScrollView> | ||||||
| @@ -87,6 +87,9 @@ | |||||||
|             <action |             <action | ||||||
|                 android:id="@+id/action_editMonsterFragment_to_editSavingThrowsFragment" |                 android:id="@+id/action_editMonsterFragment_to_editSavingThrowsFragment" | ||||||
|                 app:destination="@id/editSavingThrowsFragment" /> |                 app:destination="@id/editSavingThrowsFragment" /> | ||||||
|  |             <action | ||||||
|  |                 android:id="@+id/action_editMonsterFragment_to_editChallengeRatingFragment" | ||||||
|  |                 app:destination="@id/editChallengeRatingFragment" /> | ||||||
|         </fragment> |         </fragment> | ||||||
|         <fragment |         <fragment | ||||||
|             android:id="@+id/editBasicInfoFragment" |             android:id="@+id/editBasicInfoFragment" | ||||||
| @@ -112,6 +115,11 @@ | |||||||
|             android:name="com.majinnaibu.monstercards.ui.editmonster.EditSavingThrowsFragment" |             android:name="com.majinnaibu.monstercards.ui.editmonster.EditSavingThrowsFragment" | ||||||
|             android:label="fragment_edit_saving_throws" |             android:label="fragment_edit_saving_throws" | ||||||
|             tools:layout="@layout/fragment_edit_saving_throws" /> |             tools:layout="@layout/fragment_edit_saving_throws" /> | ||||||
|  |         <fragment | ||||||
|  |             android:id="@+id/editChallengeRatingFragment" | ||||||
|  |             android:name="com.majinnaibu.monstercards.ui.editmonster.EditChallengeRatingFragment" | ||||||
|  |             android:label="fragment_edit_challenge_rating" | ||||||
|  |             tools:layout="@layout/fragment_edit_challenge_rating" /> | ||||||
|     </navigation> |     </navigation> | ||||||
|  |  | ||||||
| </navigation> | </navigation> | ||||||
| @@ -28,7 +28,9 @@ | |||||||
|     <string name="label_condition_immunities">Condition Immunities</string> |     <string name="label_condition_immunities">Condition Immunities</string> | ||||||
|     <string name="label_constitution">Constitution</string> |     <string name="label_constitution">Constitution</string> | ||||||
|     <string name="label_custom_armor">Custom Armor</string> |     <string name="label_custom_armor">Custom Armor</string> | ||||||
|  |     <string name="label_custom_challenge_rating_description">Custom CR</string> | ||||||
|     <string name="label_custom_hp">Custom HP</string> |     <string name="label_custom_hp">Custom HP</string> | ||||||
|  |     <string name="label_custom_proficiency_bonus">Custom Proficiency Bonus</string> | ||||||
|     <string name="label_custom_speed">Custom Speed</string> |     <string name="label_custom_speed">Custom Speed</string> | ||||||
|     <string name="label_damage_immunities">Damage Immunities</string> |     <string name="label_damage_immunities">Damage Immunities</string> | ||||||
|     <string name="label_damage_resistances">Damage Resistances</string> |     <string name="label_damage_resistances">Damage Resistances</string> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user