Adds Edit Senses fragment.
This commit is contained in:
@@ -116,6 +116,11 @@ public class EditMonsterFragment extends MCFragment {
|
|||||||
Navigation.findNavController(requireView()).navigate(action);
|
Navigation.findNavController(requireView()).navigate(action);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
mHolder.senses.setOnClickListener(v -> {
|
||||||
|
NavDirections action = EditMonsterFragmentDirections.actionEditMonsterFragmentToEditSensesFragment();
|
||||||
|
Navigation.findNavController(requireView()).navigate(action);
|
||||||
|
});
|
||||||
|
|
||||||
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
|
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
|
||||||
@Override
|
@Override
|
||||||
public void handleOnBackPressed() {
|
public void handleOnBackPressed() {
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
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.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.MCFragment;
|
||||||
|
import com.majinnaibu.monstercards.ui.shared.SwipeToDeleteCallback;
|
||||||
|
import com.majinnaibu.monstercards.utils.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A fragment representing a list of Items.
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("FieldCanBeLocal")
|
||||||
|
public class EditSensesFragment extends MCFragment {
|
||||||
|
private EditMonsterViewModel mViewModel;
|
||||||
|
private ViewHolder mHolder;
|
||||||
|
|
||||||
|
private void navigateToEditSense(String sense) {
|
||||||
|
Logger.logUnimplementedFeature("Navigate to edit sense fragment.");
|
||||||
|
// NavDirections action = EditSensesFragmentDirections.actionEditSensesFragmentToEditSenseFragment(sense);
|
||||||
|
// 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_senses_list, container, false);
|
||||||
|
|
||||||
|
mHolder = new ViewHolder(root);
|
||||||
|
setupRecyclerView(mHolder.list);
|
||||||
|
setupAddSenseButton(mHolder.addSense);
|
||||||
|
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupRecyclerView(@NonNull RecyclerView recyclerView) {
|
||||||
|
Context context = requireContext();
|
||||||
|
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
|
||||||
|
recyclerView.setLayoutManager(layoutManager);
|
||||||
|
|
||||||
|
mViewModel.getSenses().observe(getViewLifecycleOwner(), senses -> {
|
||||||
|
EditSensesRecyclerViewAdapter adapter = new EditSensesRecyclerViewAdapter(mViewModel.getSensesArray(), sense -> {
|
||||||
|
if (sense != null) {
|
||||||
|
navigateToEditSense(sense);
|
||||||
|
} else {
|
||||||
|
Logger.logError("Can't navigate to EditSense with a null sense");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
recyclerView.setAdapter(adapter);
|
||||||
|
});
|
||||||
|
|
||||||
|
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(context, layoutManager.getOrientation());
|
||||||
|
recyclerView.addItemDecoration(dividerItemDecoration);
|
||||||
|
|
||||||
|
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new SwipeToDeleteCallback(context, mViewModel::removeSense));
|
||||||
|
itemTouchHelper.attachToRecyclerView(recyclerView);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupAddSenseButton(@NonNull FloatingActionButton fab) {
|
||||||
|
fab.setOnClickListener(view -> {
|
||||||
|
String newSense = mViewModel.addNewSense();
|
||||||
|
navigateToEditSense(newSense);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ViewHolder {
|
||||||
|
RecyclerView list;
|
||||||
|
FloatingActionButton addSense;
|
||||||
|
|
||||||
|
ViewHolder(View root) {
|
||||||
|
this.list = root.findViewById(R.id.list);
|
||||||
|
this.addSense = root.findViewById(R.id.add_sense);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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.FragmentEditSensesListItemBinding;
|
||||||
|
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* x
|
||||||
|
* {@link RecyclerView.Adapter} that can display a {@link String}.
|
||||||
|
*/
|
||||||
|
public class EditSensesRecyclerViewAdapter extends RecyclerView.Adapter<EditSensesRecyclerViewAdapter.ViewHolder> {
|
||||||
|
private final List<String> mValues;
|
||||||
|
private final ItemCallback mOnClick;
|
||||||
|
|
||||||
|
public EditSensesRecyclerViewAdapter(List<String> items, ItemCallback onClick) {
|
||||||
|
mValues = items;
|
||||||
|
mOnClick = onClick;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
|
||||||
|
return new ViewHolder(FragmentEditSensesListItemBinding.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(FragmentEditSensesListItemBinding binding) {
|
||||||
|
super(binding.getRoot());
|
||||||
|
mContentView = binding.content;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return super.toString() + " '" + mContentView.getText() + "'";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
33
app/src/main/res/layout/fragment_edit_senses_list.xml
Normal file
33
app/src/main/res/layout/fragment_edit_senses_list.xml
Normal file
@@ -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.EditSensesFragment">
|
||||||
|
|
||||||
|
<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_senses_list_item" />
|
||||||
|
|
||||||
|
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||||
|
android:id="@+id/add_sense"
|
||||||
|
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_sense"
|
||||||
|
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>
|
||||||
13
app/src/main/res/layout/fragment_edit_senses_list_item.xml
Normal file
13
app/src/main/res/layout/fragment_edit_senses_list_item.xml
Normal file
@@ -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>
|
||||||
@@ -93,6 +93,9 @@
|
|||||||
<action
|
<action
|
||||||
android:id="@+id/action_editMonsterFragment_to_editSkillsFragment"
|
android:id="@+id/action_editMonsterFragment_to_editSkillsFragment"
|
||||||
app:destination="@id/editSkillsFragment" />
|
app:destination="@id/editSkillsFragment" />
|
||||||
|
<action
|
||||||
|
android:id="@+id/action_editMonsterFragment_to_editSensesFragment"
|
||||||
|
app:destination="@id/editSensesFragment" />
|
||||||
</fragment>
|
</fragment>
|
||||||
<fragment
|
<fragment
|
||||||
android:id="@+id/editBasicInfoFragment"
|
android:id="@+id/editBasicInfoFragment"
|
||||||
@@ -150,5 +153,10 @@
|
|||||||
android:name="advantage"
|
android:name="advantage"
|
||||||
app:argType="com.majinnaibu.monstercards.data.enums.AdvantageType" />
|
app:argType="com.majinnaibu.monstercards.data.enums.AdvantageType" />
|
||||||
</fragment>
|
</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" />
|
||||||
</navigation>
|
</navigation>
|
||||||
</navigation>
|
</navigation>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
<resources>
|
<resources>
|
||||||
<string name="action_add_monster">Add monster</string>
|
<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_skill">Add Skill</string>
|
||||||
<string name="action_edit">Edit</string>
|
<string name="action_edit">Edit</string>
|
||||||
<string name="actions_label">Actions</string>
|
<string name="actions_label">Actions</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user