Adds edit skills view.

This commit is contained in:
2021-06-10 22:50:27 -07:00
committed by Tom Hicks
parent 00463c8092
commit 1a02eab07a
7 changed files with 73 additions and 126 deletions

View File

@@ -111,6 +111,11 @@ public class EditMonsterFragment extends MCFragment {
Navigation.findNavController(requireView()).navigate(action);
});
mHolder.skills.setOnClickListener(v -> {
NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditSkillsFragment();
Navigation.findNavController(requireView()).navigate(action);
});
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {

View File

@@ -5,6 +5,7 @@ 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;
@@ -16,7 +17,9 @@ import com.majinnaibu.monstercards.models.Skill;
import com.majinnaibu.monstercards.models.Trait;
import com.majinnaibu.monstercards.utils.ChangeTrackedLiveData;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
@@ -76,7 +79,7 @@ public class EditMonsterViewModel extends ViewModel {
private final ChangeTrackedLiveData<Integer> mTruesightRange;
private final ChangeTrackedLiveData<Integer> mTelepathyRange;
private final ChangeTrackedLiveData<String> mUnderstandsButDescription;
private final ChangeTrackedLiveData<Set<Skill>> mSkills;
private final ChangeTrackedLiveData<List<Skill>> mSkills;
private final ChangeTrackedLiveData<Set<String>> mDamageImmunities;
private final ChangeTrackedLiveData<Set<String>> mDamageResistances;
private final ChangeTrackedLiveData<Set<String>> mDamageVulnerabilities;
@@ -146,7 +149,7 @@ public class EditMonsterViewModel extends ViewModel {
mTruesightRange = new ChangeTrackedLiveData<>(0, onDirtied);
mTelepathyRange = new ChangeTrackedLiveData<>(0, onDirtied);
mUnderstandsButDescription = new ChangeTrackedLiveData<>("", onDirtied);
mSkills = new ChangeTrackedLiveData<>(new HashSet<>(), onDirtied);
mSkills = new ChangeTrackedLiveData<>(new ArrayList<>(), onDirtied);
mDamageImmunities = new ChangeTrackedLiveData<>(new HashSet<>(), onDirtied);
mDamageResistances = new ChangeTrackedLiveData<>(new HashSet<>(), onDirtied);
mDamageVulnerabilities = new ChangeTrackedLiveData<>(new HashSet<>(), onDirtied);
@@ -211,7 +214,15 @@ public class EditMonsterViewModel extends ViewModel {
mTruesightRange.resetValue(monster.truesightRange);
mTelepathyRange.resetValue(monster.telepathyRange);
mUnderstandsButDescription.resetValue(monster.understandsButDescription);
mSkills.resetValue(monster.skills);
if (monster.skills.size() == 0) {
ArrayList<Skill> skills = new ArrayList<>();
skills.add(new Skill("Acrobatics", AbilityScore.STRENGTH));
skills.add(new Skill("Stealth", AbilityScore.DEXTERITY));
mSkills.resetValue(skills);
} else {
mSkills.resetValue(new ArrayList<>(monster.skills));
}
mDamageImmunities.resetValue(monster.damageImmunities);
mDamageResistances.resetValue(monster.damageResistances);
mDamageVulnerabilities.resetValue(monster.damageVulnerabilities);
@@ -794,6 +805,10 @@ public class EditMonsterViewModel extends ViewModel {
mUnderstandsButDescription.setValue(understandsButDescription);
}
public LiveData<List<Skill>> getSkills() {
return mSkills;
}
/*
// TODO: add getters and setters for
Senses
@@ -862,7 +877,7 @@ public class EditMonsterViewModel extends ViewModel {
monster.truesightRange = mTruesightRange.getValue();
monster.telepathyRange = mTelepathyRange.getValue();
monster.understandsButDescription = mUnderstandsButDescription.getValue();
monster.skills = mSkills.getValue();
monster.skills = new HashSet<>(mSkills.getValue());
monster.damageImmunities = mDamageImmunities.getValue();
monster.damageResistances = mDamageResistances.getValue();
monster.damageVulnerabilities = mDamageVulnerabilities.getValue();
@@ -877,4 +892,8 @@ public class EditMonsterViewModel extends ViewModel {
return monster;
}
public List<Skill> getSkillsArray() {
return mSkills.getValue();
}
}

View File

@@ -6,87 +6,40 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
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 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.models.Skill;
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 EditSkillsFragment extends MCFragment {
public class EditSkillsFragment extends Fragment {
private EditMonsterViewModel mViewModel;
private ViewHolder mHolder;
private void navigateToEditSkill(@NonNull Skill skill) {
NavDirections action = EditSkillsFragmentDirections.actionEditSkillsFragmentToEditSkillFragment(skill.name, skill.abilityScore, skill.proficiencyType, skill.advantageType);
Navigation.findNavController(requireView()).navigate(action);
}
// private ViewHolder mHolder;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_edit_skills_list, container, false);
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_skills_list, container, false);
mHolder = new ViewHolder(root);
setupRecyclerView(mHolder.list);
setupAddSkillButton(mHolder.addSkill);
return root;
}
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
Context context = requireContext();
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
recyclerView.setLayoutManager(layoutManager);
mViewModel.getSkills().observe(getViewLifecycleOwner(), skills -> {
EditSkillsRecyclerViewAdapter adapter = new EditSkillsRecyclerViewAdapter(mViewModel.getSkillsArray(), skill -> {
if (skill != null) {
navigateToEditSkill(skill);
} else {
Logger.logError("Can't navigate to EditSkill with a null skill");
}
});
recyclerView.setAdapter(adapter);
});
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteCallback(context, (position, direction) -> mViewModel.removeSkill(position), null));
itemTouchHelper.attachToRecyclerView(recyclerView);
}
private void setupAddSkillButton(@NonNull FloatingActionButton fab) {
fab.setOnClickListener(view -> {
Skill newSkill = mViewModel.addNewSkill();
navigateToEditSkill(newSkill);
});
}
private static class ViewHolder {
RecyclerView list;
FloatingActionButton addSkill;
ViewHolder(@NonNull View root) {
this.list = root.findViewById(R.id.list);
this.addSkill = root.findViewById(R.id.add_skill);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
recyclerView.setLayoutManager(new LinearLayoutManager(context));
mViewModel.getSkills().observe(getViewLifecycleOwner(), skills -> recyclerView.setAdapter(new EditSkillsRecyclerViewAdapter(mViewModel.getSkillsArray())));
}
return view;
}
}
}

View File

@@ -4,7 +4,6 @@ import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.majinnaibu.monstercards.databinding.FragmentEditSkillsListItemBinding;
@@ -14,31 +13,25 @@ import java.util.List;
/**
* {@link RecyclerView.Adapter} that can display a {@link Skill}.
* TODO: Replace the implementation with code for your data type.
*/
public class EditSkillsRecyclerViewAdapter extends RecyclerView.Adapter<EditSkillsRecyclerViewAdapter.ViewHolder> {
private final List<Skill> mValues;
private final ItemCallback mOnClick;
public EditSkillsRecyclerViewAdapter(List<Skill> items, ItemCallback onClick) {
private final List<Skill> mValues;
public EditSkillsRecyclerViewAdapter(List<Skill> items) {
mValues = items;
mOnClick = onClick;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new ViewHolder(FragmentEditSkillsListItemBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mContentView.setText(mValues.get(position).name);
holder.itemView.setOnClickListener(v -> {
if (mOnClick != null) {
mOnClick.onItemCallback(holder.mItem);
}
});
}
@Override
@@ -46,23 +39,18 @@ public class EditSkillsRecyclerViewAdapter extends RecyclerView.Adapter<EditSkil
return mValues.size();
}
public interface ItemCallback {
void onItemCallback(Skill skill);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public class ViewHolder extends RecyclerView.ViewHolder {
public final TextView mContentView;
public Skill mItem;
public ViewHolder(@NonNull FragmentEditSkillsListItemBinding binding) {
public ViewHolder(FragmentEditSkillsListItemBinding binding) {
super(binding.getRoot());
mContentView = binding.content;
}
@NonNull
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
}
}

View File

@@ -1,33 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.recyclerview.widget.RecyclerView 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:id="@+id/list"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditSkillsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.editmonster.EditSkillsFragment">
<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_skills_list_item" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/add_skill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:contentDescription="@string/action_add_skill"
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>
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".ui.editmonster.EditSkillsFragment"
tools:listitem="@layout/fragment_edit_skills_list_item" />

View File

@@ -1,13 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />
</LinearLayout>
android:layout_margin="@dimen/text_margin"
android:textAppearance="?attr/textAppearanceListItem" />

View File

@@ -90,6 +90,9 @@
<action
android:id="@+id/action_editMonsterFragment_to_editChallengeRatingFragment"
app:destination="@id/editChallengeRatingFragment" />
<action
android:id="@+id/action_editMonsterFragment_to_editSkillsFragment"
app:destination="@id/editSkillsFragment" />
</fragment>
<fragment
android:id="@+id/editBasicInfoFragment"
@@ -120,6 +123,11 @@
android:name="com.majinnaibu.monstercards.ui.editmonster.EditChallengeRatingFragment"
android:label="fragment_edit_challenge_rating"
tools:layout="@layout/fragment_edit_challenge_rating" />
<fragment
android:id="@+id/editSkillsFragment"
android:name="com.majinnaibu.monstercards.ui.editmonster.EditSkillsFragment"
android:label="fragment_edit_skills_list"
tools:layout="@layout/fragment_edit_skills_list" />
</navigation>
</navigation>