Adds damage resistances to monster editor.
This commit is contained in:
		| @@ -1,72 +0,0 @@ | ||||
| package com.majinnaibu.monstercards.placeholder; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.HashMap; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
|  | ||||
| /** | ||||
|  * Helper class for providing sample content for user interfaces created by | ||||
|  * Android template wizards. | ||||
|  * <p> | ||||
|  * TODO: Replace all uses of this class before publishing your app. | ||||
|  */ | ||||
| public class PlaceholderContent { | ||||
|  | ||||
|     /** | ||||
|      * An array of sample (placeholder) items. | ||||
|      */ | ||||
|     public static final List<PlaceholderItem> ITEMS = new ArrayList<PlaceholderItem>(); | ||||
|  | ||||
|     /** | ||||
|      * A map of sample (placeholder) items, by ID. | ||||
|      */ | ||||
|     public static final Map<String, PlaceholderItem> ITEM_MAP = new HashMap<String, PlaceholderItem>(); | ||||
|  | ||||
|     private static final int COUNT = 25; | ||||
|  | ||||
|     static { | ||||
|         // Add some sample items. | ||||
|         for (int i = 1; i <= COUNT; i++) { | ||||
|             addItem(createPlaceholderItem(i)); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private static void addItem(PlaceholderItem item) { | ||||
|         ITEMS.add(item); | ||||
|         ITEM_MAP.put(item.id, item); | ||||
|     } | ||||
|  | ||||
|     private static PlaceholderItem createPlaceholderItem(int position) { | ||||
|         return new PlaceholderItem(String.valueOf(position), "Item " + position, makeDetails(position)); | ||||
|     } | ||||
|  | ||||
|     private static String makeDetails(int position) { | ||||
|         StringBuilder builder = new StringBuilder(); | ||||
|         builder.append("Details about Item: ").append(position); | ||||
|         for (int i = 0; i < position; i++) { | ||||
|             builder.append("\nMore details information here."); | ||||
|         } | ||||
|         return builder.toString(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * A placeholder item representing a piece of content. | ||||
|      */ | ||||
|     public static class PlaceholderItem { | ||||
|         public final String id; | ||||
|         public final String content; | ||||
|         public final String details; | ||||
|  | ||||
|         public PlaceholderItem(String id, String content, String details) { | ||||
|             this.id = id; | ||||
|             this.content = content; | ||||
|             this.details = details; | ||||
|         } | ||||
|  | ||||
|         @Override | ||||
|         public String toString() { | ||||
|             return content; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,76 @@ | ||||
| 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.EditText; | ||||
|  | ||||
| import androidx.activity.OnBackPressedCallback; | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.annotation.Nullable; | ||||
| 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.ui.shared.MCFragment; | ||||
| import com.majinnaibu.monstercards.utils.Logger; | ||||
| import com.majinnaibu.monstercards.utils.TextChangedListener; | ||||
|  | ||||
| public class EditDamageResistanceFragment extends MCFragment { | ||||
|     private EditMonsterViewModel mEditMonsterViewModel; | ||||
|     private EditStringViewModel mViewModel; | ||||
|     private EditDamageResistanceFragment.ViewHolder mHolder; | ||||
|     private String mOldDamageResistance; | ||||
|  | ||||
|     @Override | ||||
|     public void onCreate(@Nullable Bundle savedInstanceState) { | ||||
|         mViewModel = new ViewModelProvider(this).get(EditStringViewModel.class); | ||||
|         if (getArguments() != null) { | ||||
|             EditDamageResistanceFragmentArgs args = EditDamageResistanceFragmentArgs.fromBundle(getArguments()); | ||||
|             mOldDamageResistance = args.getDamageType(); | ||||
|             mViewModel.resetValue(mOldDamageResistance); | ||||
|         } else { | ||||
|             Logger.logWTF("EditDamageResistanceFragment needs arguments"); | ||||
|             mOldDamageResistance = null; | ||||
|         } | ||||
|         super.onCreate(savedInstanceState); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, | ||||
|                              @Nullable Bundle savedInstanceState) { | ||||
|         NavController navController = Navigation.findNavController(requireActivity(), R.id.nav_host_fragment); | ||||
|         NavBackStackEntry backStackEntry = navController.getBackStackEntry(R.id.edit_monster_navigation); | ||||
|         mEditMonsterViewModel = new ViewModelProvider(backStackEntry).get(EditMonsterViewModel.class); | ||||
|  | ||||
|         View root = inflater.inflate(R.layout.fragment_edit_damage_resistance, container, false); | ||||
|  | ||||
|         mHolder = new EditDamageResistanceFragment.ViewHolder(root); | ||||
|  | ||||
|         mHolder.value.setText(mViewModel.getValueAsString()); | ||||
|         mHolder.value.addTextChangedListener(new TextChangedListener((TextChangedListener.OnTextChangedCallback) (s, start, before, count) -> mViewModel.setValue(s.toString()))); | ||||
|  | ||||
|         requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) { | ||||
|             @Override | ||||
|             public void handleOnBackPressed() { | ||||
|                 if (mViewModel.hasChanges()) { | ||||
|                     mEditMonsterViewModel.replaceDamageResistance(mOldDamageResistance, mViewModel.getValueAsString()); | ||||
|                 } | ||||
|                 Navigation.findNavController(requireView()).navigateUp(); | ||||
|             } | ||||
|         }); | ||||
|  | ||||
|         return root; | ||||
|     } | ||||
|  | ||||
|     private static class ViewHolder { | ||||
|         EditText value; | ||||
|  | ||||
|         ViewHolder(View root) { | ||||
|             value = root.findViewById(R.id.value); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,92 @@ | ||||
| package com.majinnaibu.monstercards.ui.editmonster; | ||||
|  | ||||
| import android.content.Context; | ||||
| import android.os.Bundle; | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.View; | ||||
| import android.view.ViewGroup; | ||||
|  | ||||
| import androidx.annotation.NonNull; | ||||
| import androidx.lifecycle.ViewModelProvider; | ||||
| import androidx.navigation.NavBackStackEntry; | ||||
| import androidx.navigation.NavController; | ||||
| import androidx.navigation.NavDirections; | ||||
| import androidx.navigation.Navigation; | ||||
| import androidx.recyclerview.widget.DividerItemDecoration; | ||||
| import androidx.recyclerview.widget.ItemTouchHelper; | ||||
| import androidx.recyclerview.widget.LinearLayoutManager; | ||||
| import androidx.recyclerview.widget.RecyclerView; | ||||
|  | ||||
| import com.google.android.material.floatingactionbutton.FloatingActionButton; | ||||
| import com.majinnaibu.monstercards.R; | ||||
| import com.majinnaibu.monstercards.ui.shared.MCFragment; | ||||
| import com.majinnaibu.monstercards.ui.shared.SwipeToDeleteCallback; | ||||
| import com.majinnaibu.monstercards.utils.Logger; | ||||
|  | ||||
| /** | ||||
|  * A fragment representing a list of Items. | ||||
|  */ | ||||
| public class EditDamageResistancesFragment extends MCFragment { | ||||
|     private EditMonsterViewModel mViewModel; | ||||
|     private ViewHolder mHolder; | ||||
|  | ||||
|     private void navigateToEditDamageResistance(String damageResistance) { | ||||
|         NavDirections action = EditDamageResistancesFragmentDirections.actionEditDamageResistancesFragmentToEditDamageResistanceFragment(damageResistance); | ||||
|         View view = getView(); | ||||
|         assert view != null; | ||||
|         Navigation.findNavController(view).navigate(action); | ||||
|     } | ||||
|  | ||||
|     @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); | ||||
|         View root = inflater.inflate(R.layout.fragment_edit_damage_resistances_list, container, false); | ||||
|         mHolder = new ViewHolder(root); | ||||
|         setupRecyclerView(mHolder.list); | ||||
|         setupAddDamageResistanceButton(mHolder.addDamageResistance); | ||||
|         return root; | ||||
|     } | ||||
|  | ||||
|     private void setupRecyclerView(@NonNull RecyclerView recyclerView) { | ||||
|         Context context = requireContext(); | ||||
|         LinearLayoutManager layoutManager = new LinearLayoutManager(context); | ||||
|         recyclerView.setLayoutManager(layoutManager); | ||||
|  | ||||
|         mViewModel.getDamageResistances().observe(getViewLifecycleOwner(), damageResistances -> { | ||||
|             EditDamageResistancesRecyclerViewAdapter adapter = new EditDamageResistancesRecyclerViewAdapter(mViewModel.getDamageResistancesArray(), damageResistance -> { | ||||
|                 if (damageResistance != null) { | ||||
|                     navigateToEditDamageResistance(damageResistance); | ||||
|                 } else { | ||||
|                     Logger.logError("Can't navigate to EditDamageResistance with a null damageResistance"); | ||||
|                 } | ||||
|             }); | ||||
|             recyclerView.setAdapter(adapter); | ||||
|         }); | ||||
|  | ||||
|         DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, layoutManager.getOrientation()); | ||||
|         recyclerView.addItemDecoration(dividerItemDecoration); | ||||
|  | ||||
|         ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteCallback(context, mViewModel::removeDamageResistance)); | ||||
|         itemTouchHelper.attachToRecyclerView(recyclerView); | ||||
|     } | ||||
|  | ||||
|     private void setupAddDamageResistanceButton(@NonNull FloatingActionButton fab) { | ||||
|         fab.setOnClickListener(view -> { | ||||
|             String newDamageResistance = mViewModel.addNewDamageResistance(); | ||||
|             navigateToEditDamageResistance(newDamageResistance); | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     private static class ViewHolder { | ||||
|         RecyclerView list; | ||||
|         FloatingActionButton addDamageResistance; | ||||
|  | ||||
|         ViewHolder(View root) { | ||||
|             list = root.findViewById(R.id.list); | ||||
|             addDamageResistance = root.findViewById(R.id.add_damage_type); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,69 @@ | ||||
| package com.majinnaibu.monstercards.ui.editmonster; | ||||
|  | ||||
| import android.view.LayoutInflater; | ||||
| import android.view.ViewGroup; | ||||
| import android.widget.TextView; | ||||
|  | ||||
| import androidx.recyclerview.widget.RecyclerView; | ||||
|  | ||||
| import com.majinnaibu.monstercards.databinding.FragmentEditDamageResistancesListItemBinding; | ||||
|  | ||||
| import org.jetbrains.annotations.NotNull; | ||||
|  | ||||
| import java.util.List; | ||||
|  | ||||
| /** | ||||
|  * x | ||||
|  * {@link RecyclerView.Adapter} that can display a {@link String}. | ||||
|  */ | ||||
| public class EditDamageResistancesRecyclerViewAdapter extends RecyclerView.Adapter<EditDamageResistancesRecyclerViewAdapter.ViewHolder> { | ||||
|     private final List<String> mValues; | ||||
|     private final ItemCallback mOnClick; | ||||
|  | ||||
|     public EditDamageResistancesRecyclerViewAdapter(List<String> items, ItemCallback onClick) { | ||||
|         mValues = items; | ||||
|         mOnClick = onClick; | ||||
|     } | ||||
|  | ||||
|     @NotNull | ||||
|     @Override | ||||
|     public ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) { | ||||
|         return new ViewHolder(FragmentEditDamageResistancesListItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void onBindViewHolder(final ViewHolder holder, int position) { | ||||
|         holder.mItem = mValues.get(position); | ||||
|         holder.mContentView.setText(mValues.get(position)); | ||||
|         holder.itemView.setOnClickListener(v -> { | ||||
|             if (mOnClick != null) { | ||||
|                 mOnClick.onItemCallback(holder.mItem); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public int getItemCount() { | ||||
|         return mValues.size(); | ||||
|     } | ||||
|  | ||||
|     public interface ItemCallback { | ||||
|         void onItemCallback(String sense); | ||||
|     } | ||||
|  | ||||
|     public static class ViewHolder extends RecyclerView.ViewHolder { | ||||
|         public final TextView mContentView; | ||||
|         public String mItem; | ||||
|  | ||||
|         public ViewHolder(FragmentEditDamageResistancesListItemBinding binding) { | ||||
|             super(binding.getRoot()); | ||||
|             mContentView = binding.content; | ||||
|         } | ||||
|  | ||||
|         @NotNull | ||||
|         @Override | ||||
|         public String toString() { | ||||
|             return super.toString() + " '" + mContentView.getText() + "'"; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -125,6 +125,11 @@ public class EditMonsterFragment extends MCFragment { | ||||
|             Navigation.findNavController(requireView()).navigate(action); | ||||
|         }); | ||||
|  | ||||
|         mHolder.damageResistances.setOnClickListener(v -> { | ||||
|             NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditDamageResistancesFragment(); | ||||
|             Navigation.findNavController(requireView()).navigate(action); | ||||
|         }); | ||||
|  | ||||
|         requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) { | ||||
|             @Override | ||||
|             public void handleOnBackPressed() { | ||||
|   | ||||
| @@ -73,5 +73,4 @@ public class EditSenseFragment extends MCFragment { | ||||
|             description = root.findViewById(R.id.name); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,17 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|     xmlns:tools="http://schemas.android.com/tools" | ||||
|     android:layout_width="match_parent" | ||||
|     android:layout_height="wrap_content" | ||||
|     android:layout_margin="@dimen/text_margin" | ||||
|     tools:context=".ui.editmonster.EditDamageResistanceFragment"> | ||||
|  | ||||
|     <com.google.android.material.textfield.TextInputEditText | ||||
|         android:id="@+id/value" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:hint="@string/label_damage_type" | ||||
|         android:importantForAutofill="no" | ||||
|         android:inputType="text" | ||||
|         tools:text="bludgeoning" /> | ||||
| </com.google.android.material.textfield.TextInputLayout> | ||||
| @@ -0,0 +1,33 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|     xmlns:app="http://schemas.android.com/apk/res-auto" | ||||
|     xmlns:tools="http://schemas.android.com/tools" | ||||
|     android:layout_width="match_parent" | ||||
|     android:layout_height="match_parent" | ||||
|     tools:context=".ui.editmonster.EditDamageResistancesFragment"> | ||||
|  | ||||
|     <androidx.recyclerview.widget.RecyclerView | ||||
|         android:id="@+id/list" | ||||
|         android:layout_width="match_parent" | ||||
|         android:layout_height="match_parent" | ||||
|         android:divider="?android:attr/dividerVertical" | ||||
|         android:dividerPadding="@dimen/text_margin" | ||||
|         app:layoutManager="LinearLayoutManager" | ||||
|         app:layout_constraintBottom_toBottomOf="parent" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:layout_constraintStart_toStartOf="parent" | ||||
|         app:layout_constraintTop_toTopOf="parent" | ||||
|         tools:listitem="@layout/fragment_edit_damage_resistances_list_item" /> | ||||
|  | ||||
|     <com.google.android.material.floatingactionbutton.FloatingActionButton | ||||
|         android:id="@+id/add_damage_type" | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:layout_gravity="bottom|end" | ||||
|         android:layout_margin="@dimen/fab_margin" | ||||
|         android:contentDescription="@string/add_damage_type" | ||||
|         app:layout_constraintBottom_toBottomOf="parent" | ||||
|         app:layout_constraintEnd_toEndOf="parent" | ||||
|         app:srcCompat="@android:drawable/ic_input_add" | ||||
|         app:tint="@android:color/white" /> | ||||
| </androidx.constraintlayout.widget.ConstraintLayout> | ||||
| @@ -0,0 +1,13 @@ | ||||
| <?xml version="1.0" encoding="utf-8"?> | ||||
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||||
|     android:layout_width="match_parent" | ||||
|     android:layout_height="wrap_content" | ||||
|     android:orientation="horizontal"> | ||||
|  | ||||
|     <TextView | ||||
|         android:id="@+id/content" | ||||
|         android:layout_width="wrap_content" | ||||
|         android:layout_height="wrap_content" | ||||
|         android:layout_margin="@dimen/text_margin" | ||||
|         android:textAppearance="?attr/textAppearanceListItem" /> | ||||
| </LinearLayout> | ||||
| @@ -99,6 +99,9 @@ | ||||
|             <action | ||||
|                 android:id="@+id/action_editMonsterFragment_to_editConditionImmunitiesFragment" | ||||
|                 app:destination="@id/editConditionImmunitiesFragment" /> | ||||
|             <action | ||||
|                 android:id="@+id/action_editMonsterFragment_to_editDamageResistancesFragment" | ||||
|                 app:destination="@id/editDamageResistancesFragment" /> | ||||
|         </fragment> | ||||
|         <fragment | ||||
|             android:id="@+id/editBasicInfoFragment" | ||||
| @@ -192,5 +195,23 @@ | ||||
|                 android:name="condition" | ||||
|                 app:argType="string" /> | ||||
|         </fragment> | ||||
|         <fragment | ||||
|             android:id="@+id/editDamageResistancesFragment" | ||||
|             android:name="com.majinnaibu.monstercards.ui.editmonster.EditDamageResistancesFragment" | ||||
|             android:label="fragment_edit_damage_resistances_list" | ||||
|             tools:layout="@layout/fragment_edit_damage_resistances_list"> | ||||
|             <action | ||||
|                 android:id="@+id/action_editDamageResistancesFragment_to_editDamageResistanceFragment" | ||||
|                 app:destination="@id/editDamageResistanceFragment" /> | ||||
|         </fragment> | ||||
|         <fragment | ||||
|             android:id="@+id/editDamageResistanceFragment" | ||||
|             android:name="com.majinnaibu.monstercards.ui.editmonster.EditDamageResistanceFragment" | ||||
|             android:label="fragment_edit_damage_resistance" | ||||
|             tools:layout="@layout/fragment_edit_damage_resistance"> | ||||
|             <argument | ||||
|                 android:name="damageType" | ||||
|                 app:argType="string" /> | ||||
|         </fragment> | ||||
|     </navigation> | ||||
| </navigation> | ||||
|   | ||||
| @@ -81,4 +81,6 @@ | ||||
|     <string name="title_search">Search</string> | ||||
|     <string name="wisdom_abbreviation">WIS</string> | ||||
|     <string name="label_description">Description</string> | ||||
|     <string name="label_damage_type">Damage Type</string> | ||||
|     <string name="add_damage_type">Add Damage Type</string> | ||||
| </resources> | ||||
		Reference in New Issue
	
	Block a user