Adds ability to edit languages.

This commit is contained in:
Tom Hicks
2021-06-25 10:57:25 -07:00
parent 5bded410d2
commit cb1e3343c9
11 changed files with 214 additions and 48 deletions

View File

@@ -4,12 +4,12 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.SwitchCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.NavBackStackEntry;
import androidx.navigation.NavController;
@@ -44,11 +44,14 @@ public class EditLanguageFragment extends MCFragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.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_language, container, false);
mHolder = new ViewHolder(root);
mHolder.name.setText(mViewModel.getName().getValue());
@@ -70,17 +73,11 @@ public class EditLanguageFragment extends MCFragment {
return root;
}
@Override
public void onStart() {
super.onStart();
mHolder.name.requestFocus();
}
private static class ViewHolder {
EditText name;
SwitchCompat canSpeak;
CheckBox canSpeak;
ViewHolder(@NonNull View root) {
ViewHolder(View root) {
name = root.findViewById(R.id.name);
canSpeak = root.findViewById(R.id.canSpeak);
}

View File

@@ -1,6 +1,5 @@
package com.majinnaibu.monstercards.ui.editmonster;
import androidx.annotation.NonNull;
import androidx.lifecycle.LiveData;
import com.majinnaibu.monstercards.models.Language;
@@ -19,7 +18,7 @@ public class EditLanguageViewModel extends ChangeTrackedViewModel {
mLanguage = new ChangeTrackedLiveData<>(makeLanguage(), this::makeDirty);
}
public void copyFromLanguage(@NonNull Language language) {
public void copyFromLanguage(Language language) {
mName.resetValue(language.getName());
mCanSpeak.resetValue(language.getSpeaks());
makeClean();
@@ -59,7 +58,6 @@ public class EditLanguageViewModel extends ChangeTrackedViewModel {
return getCanSpeakValue(false);
}
@NonNull
private Language makeLanguage() {
Boolean boxedValue = mCanSpeak.getValue();
boolean canSpeak = boxedValue != null && boxedValue;

View File

@@ -26,22 +26,23 @@ import com.majinnaibu.monstercards.utils.Logger;
import com.majinnaibu.monstercards.utils.TextChangedListener;
public class EditLanguagesFragment extends MCFragment {
// TODO: Make the swipe to delete not happen for the header
private EditMonsterViewModel mViewModel;
private ViewHolder mHolder;
private void navigateToEditLanguage(@NonNull Language language) {
private void navigateToEditLanguage(Language language) {
NavDirections action = EditLanguagesFragmentDirections.actionEditLanguagesFragmentToEditLanguageFragment(language.getName(), language.getSpeaks());
Navigation.findNavController(requireView()).navigate(action);
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
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_languages_list, container, false);
mHolder = new ViewHolder(root);
setupRecyclerView(mHolder.list);
setupAddLanguageButton(mHolder.addLanguage);
@@ -74,11 +75,7 @@ public class EditLanguagesFragment extends MCFragment {
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteCallback(context, (position, direction) -> {
if (position > 0) {
mViewModel.removeLanguage(position - 1);
}
}, null));
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteCallback(context, mViewModel::removeLanguage));
itemTouchHelper.attachToRecyclerView(recyclerView);
}
@@ -93,7 +90,7 @@ public class EditLanguagesFragment extends MCFragment {
RecyclerView list;
FloatingActionButton addLanguage;
ViewHolder(@NonNull View root) {
ViewHolder(View root) {
this.list = root.findViewById(R.id.list);
this.addLanguage = root.findViewById(R.id.add_language);
}

View File

@@ -6,7 +6,6 @@ import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.majinnaibu.monstercards.databinding.FragmentEditLanguagesListHeaderBinding;
@@ -14,8 +13,9 @@ import com.majinnaibu.monstercards.databinding.FragmentEditLanguagesListItemBind
import com.majinnaibu.monstercards.models.Language;
import com.majinnaibu.monstercards.ui.components.Stepper;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Locale;
public class EditLanguagesRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final List<Language> mValues;
@@ -29,18 +29,18 @@ public class EditLanguagesRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
private final int ITEM_VIEW_TYPE = 2;
private final String DISTANCE_IN_FEET_FORMAT = "%d ft.";
public EditLanguagesRecyclerViewAdapter(List<Language> items, ItemCallback onClick, int telepathyRange, Stepper.OnValueChangeListener telepathyRangeChangedListener, String understandsBut, TextWatcher understandsButChangedListener) {
public EditLanguagesRecyclerViewAdapter(List<Language> items, ItemCallback onClick, int telepathyRange, Stepper.OnValueChangeListener telepathyRangeChangedListener, String undderstandsBut, TextWatcher understandsButChangedListener) {
mValues = items;
mOnClick = onClick;
mTelepathyRange = telepathyRange;
mOnTelepathyRangeChanged = telepathyRangeChangedListener;
mUnderstandsBut = understandsBut;
mUnderstandsBut = undderstandsBut;
mOnUnderstandsButChanged = understandsButChangedListener;
}
@NonNull
@NotNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
public RecyclerView.ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
if (viewType == HEADER_VIEW_TYPE) {
return new HeaderViewHolder(FragmentEditLanguagesListHeaderBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
}
@@ -48,10 +48,10 @@ public class EditLanguagesRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
}
@Override
public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {
public void onBindViewHolder(@NotNull final RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HeaderViewHolder) {
HeaderViewHolder headerViewHolder = (HeaderViewHolder) holder;
headerViewHolder.telepathy.setOnFormatValueCallback(value -> String.format(Locale.getDefault(), DISTANCE_IN_FEET_FORMAT, value));
headerViewHolder.telepathy.setOnFormatValueCallback(value -> String.format(DISTANCE_IN_FEET_FORMAT, value));
headerViewHolder.telepathy.setValue(mTelepathyRange);
headerViewHolder.telepathy.setOnValueChangeListener(mOnTelepathyRangeChanged);
headerViewHolder.understandsBut.setText(mUnderstandsBut);
@@ -89,7 +89,7 @@ public class EditLanguagesRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
public final Stepper telepathy;
public final EditText understandsBut;
public HeaderViewHolder(@NonNull FragmentEditLanguagesListHeaderBinding binding) {
public HeaderViewHolder(FragmentEditLanguagesListHeaderBinding binding) {
super(binding.getRoot());
telepathy = binding.telepathy;
understandsBut = binding.understandsBut;
@@ -100,12 +100,12 @@ public class EditLanguagesRecyclerViewAdapter extends RecyclerView.Adapter<Recyc
public final TextView mContentView;
public Language mItem;
public ItemViewHolder(@NonNull FragmentEditLanguagesListItemBinding binding) {
public ItemViewHolder(FragmentEditLanguagesListItemBinding binding) {
super(binding.getRoot());
mContentView = binding.content;
}
@NonNull
@NotNull
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";

View File

@@ -115,6 +115,36 @@ public class EditMonsterFragment extends MCFragment {
Navigation.findNavController(requireView()).navigate(action);
});
mHolder.senses.setOnClickListener(v -> {
NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditSensesFragment();
Navigation.findNavController(requireView()).navigate(action);
});
mHolder.conditionImmunities.setOnClickListener(v -> {
NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditConditionImmunitiesFragment();
Navigation.findNavController(requireView()).navigate(action);
});
mHolder.damageImmunities.setOnClickListener(v -> {
NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditDamageImmunitiesFragment();
Navigation.findNavController(requireView()).navigate(action);
});
mHolder.damageResistances.setOnClickListener(v -> {
NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditDamageResistancesFragment();
Navigation.findNavController(requireView()).navigate(action);
});
mHolder.damageVulnerabilities.setOnClickListener(v -> {
NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditDamageVulnerabilitiesFragment();
Navigation.findNavController(requireView()).navigate(action);
});
mHolder.languages.setOnClickListener(v -> {
NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditLanguagesFragment();
Navigation.findNavController(requireView()).navigate(action);
});
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {

View File

@@ -740,6 +740,10 @@ public class EditMonsterViewModel extends ChangeTrackedViewModel {
return mTelepathyRange;
}
public int getTelepathyRangeUnboxed() {
return Helpers.unboxInteger(mTelepathyRange.getValue(), 0);
}
public void setTelepathyRange(int telepathyRange) {
mTelepathyRange.setValue(telepathyRange);
}
@@ -1189,5 +1193,12 @@ public class EditMonsterViewModel extends ChangeTrackedViewModel {
static <T> void replaceItemInList(MutableLiveData<List<T>> listData, T oldItem, T newItem) {
replaceItemInList(listData, oldItem, newItem, null);
}
static int unboxInteger(Integer value, int defaultIfNull) {
if (value == null) {
return defaultIfNull;
}
return value;
}
}
}

View File

@@ -25,12 +25,11 @@
tools:text="Medicine" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.switchmaterial.SwitchMaterial
<CheckBox
android:id="@+id/canSpeak"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:text="@string/label_can_speak_language"
android:textAppearance="@android:style/TextAppearance.Material.Medium" />
android:text="@string/label_can_speak_language" />
</LinearLayout>
</ScrollView>

View File

@@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- understands but textbox -->

View File

@@ -6,7 +6,7 @@
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />

View File

@@ -93,6 +93,24 @@
<action
android:id="@+id/action_editMonsterFragment_to_editSkillsFragment"
app:destination="@id/editSkillsFragment" />
<action
android:id="@+id/action_editMonsterFragment_to_editSensesFragment"
app:destination="@id/editSensesFragment" />
<action
android:id="@+id/action_editMonsterFragment_to_editConditionImmunitiesFragment"
app:destination="@id/editConditionImmunitiesFragment" />
<action
android:id="@+id/action_editMonsterFragment_to_editDamageResistancesFragment"
app:destination="@id/editDamageResistancesFragment" />
<action
android:id="@+id/action_editMonsterFragment_to_editDamageVulnerabilitiesFragment"
app:destination="@id/editDamageVulnerabilitiesFragment" />
<action
android:id="@+id/action_editMonsterFragment_to_editDamageImmunitiesFragment"
app:destination="@id/editDamageImmunitiesFragment" />
<action
android:id="@+id/action_editMonsterFragment_to_editLanguagesFragment"
app:destination="@id/editLanguagesFragment" />
</fragment>
<fragment
android:id="@+id/editBasicInfoFragment"
@@ -150,5 +168,112 @@
android:name="advantage"
app:argType="com.majinnaibu.monstercards.data.enums.AdvantageType" />
</fragment>
<fragment
android:id="@+id/editSensesFragment"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditSensesFragment"
android:label="fragment_edit_senses_list"
tools:layout="@layout/fragment_edit_senses_list">
<action
android:id="@+id/action_editSensesFragment_to_editSenseFragment"
app:destination="@id/editSenseFragment" />
</fragment>
<fragment
android:id="@+id/editSenseFragment"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditSenseFragment"
android:label="fragment_edit_sense"
tools:layout="@layout/fragment_edit_sense">
<argument
android:name="sense"
app:argType="string" />
</fragment>
<fragment
android:id="@+id/editConditionImmunitiesFragment"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditConditionImmunitiesFragment"
android:label="fragment_edit_condition_immunities_list"
tools:layout="@layout/fragment_edit_condition_immunities_list">
<action
android:id="@+id/action_editConditionImmunitiesFragment_to_editConditionImmunity"
app:destination="@id/editConditionImmunity" />
</fragment>
<fragment
android:id="@+id/editConditionImmunity"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditConditionImmunityFragment"
android:label="fragment_edit_condition_immunity"
tools:layout="@layout/fragment_edit_condition_immunity">
<argument
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>
<fragment
android:id="@+id/editDamageVulnerabilityFragment"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditDamageVulnerabilityFragment"
android:label="EditDamageVulnerabilityFragment">
<argument
android:name="damageType"
app:argType="string" />
</fragment>
<fragment
android:id="@+id/editDamageImmunitiesFragment"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditDamageImmunitiesFragment"
android:label="EditDamageImmunitiesFragment">
<action
android:id="@+id/action_editDamageImmunitiesFragment_to_editDamageImmunityFragment"
app:destination="@id/editDamageImmunityFragment" />
</fragment>
<fragment
android:id="@+id/editDamageImmunityFragment"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditDamageImmunityFragment"
android:label="EditDamageImmunityFragment">
<argument
android:name="damageType"
app:argType="string" />
</fragment>
<fragment
android:id="@+id/editDamageVulnerabilitiesFragment"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditDamageVulnerabilitiesFragment"
android:label="EditDamageVulnerabilitiesFragment">
<action
android:id="@+id/action_editDamageVulnerabilitiesFragment_to_editDamageVulnerabilityFragment"
app:destination="@id/editDamageVulnerabilityFragment" />
</fragment>
<fragment
android:id="@+id/editLanguagesFragment"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditLanguagesFragment"
android:label="fragment_edit_languages_list"
tools:layout="@layout/fragment_edit_languages_list">
<action
android:id="@+id/action_editLanguagesFragment_to_editLanguageFragment"
app:destination="@id/editLanguageFragment" />
</fragment>
<fragment
android:id="@+id/editLanguageFragment"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditLanguageFragment"
android:label="fragment_edit_language"
tools:layout="@layout/fragment_edit_language">
<argument
android:name="name"
app:argType="string" />
<argument
android:name="canSpeak"
app:argType="boolean" />
</fragment>
</navigation>
</navigation>

View File

@@ -1,6 +1,9 @@
<resources>
<string name="action_add_monster">Add monster</string>
<string name="action_add_sense">Add Sense</string>
<string name="action_add_skill">Add Skill</string>
<string name="action_add_language">Add Language</string>
<string name="action_add_condition_immunity">Add Condition</string>
<string name="action_edit">Edit</string>
<string name="actions_label">Actions</string>
<string name="app_name">MonsterCards</string>
@@ -50,6 +53,7 @@
<string name="label_legendary_actions">Legendary Actions</string>
<string name="label_natural_armor_bonus">Natural Armor Bonus</string>
<string name="label_name">Name</string>
<string name="label_understands_but">Understands But</string>
<string name="label_proficiency">Proficiency</string>
<string name="label_proficiency_expertise">Expertise</string>
<string name="label_proficiency_none">None</string>
@@ -66,6 +70,7 @@
<string name="label_strength">Strength</string>
<string name="label_subtype">Subtype</string>
<string name="label_swim_speed">Swim Speed</string>
<string name="label_telepathy">Telepathy</string>
<string name="label_type">Type</string>
<string name="label_wisdom">Wisdom</string>
<string name="section_divider">section divider</string>
@@ -78,4 +83,8 @@
<string name="title_library">Library</string>
<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>
<string name="label_can_speak_language">Can Speak</string>
</resources>