Adds edit sense fragment.
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
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.MCFragment;
|
||||
import com.majinnaibu.monstercards.utils.Logger;
|
||||
import com.majinnaibu.monstercards.utils.TextChangedListener;
|
||||
|
||||
public class EditSenseFragment extends MCFragment {
|
||||
private EditMonsterViewModel mEditMonsterViewModel;
|
||||
private EditSenseViewModel mViewModel;
|
||||
private ViewHolder mHolder;
|
||||
private String mOldSense;
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
|
||||
mViewModel = new ViewModelProvider(this).get(EditSenseViewModel.class);
|
||||
if (getArguments() != null) {
|
||||
EditSenseFragmentArgs args = EditSenseFragmentArgs.fromBundle(getArguments());
|
||||
mOldSense = args.getSense();
|
||||
mViewModel.copyFromSense(mOldSense);
|
||||
} else {
|
||||
Logger.logWTF("This should never happen. EditSenseFragment needs arguments");
|
||||
mOldSense = 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_sense, container, false);
|
||||
|
||||
mHolder = new ViewHolder(root);
|
||||
|
||||
mHolder.description.setText(mViewModel.getDescription().getValue());
|
||||
mHolder.description.addTextChangedListener(new TextChangedListener((TextChangedListener.OnTextChangedCallback) (s, start, before, count) -> mViewModel.setDescription(s.toString())));
|
||||
|
||||
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(), new OnBackPressedCallback(true) {
|
||||
@Override
|
||||
public void handleOnBackPressed() {
|
||||
if (mViewModel.hasChanges()) {
|
||||
mEditMonsterViewModel.replaceSense(mViewModel.getDescription().getValue(), mOldSense);
|
||||
}
|
||||
Navigation.findNavController(requireView()).navigateUp();
|
||||
}
|
||||
});
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private static class ViewHolder {
|
||||
EditText description;
|
||||
|
||||
ViewHolder(View root) {
|
||||
description = root.findViewById(R.id.name);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.majinnaibu.monstercards.ui.editmonster;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
|
||||
import com.majinnaibu.monstercards.utils.ChangeTrackedLiveData;
|
||||
|
||||
public class EditSenseViewModel extends ViewModel {
|
||||
private final ChangeTrackedLiveData<String> mDescription;
|
||||
private final MutableLiveData<Boolean> mHasChanges;
|
||||
|
||||
public EditSenseViewModel() {
|
||||
mHasChanges = new MutableLiveData<>(false);
|
||||
ChangeTrackedLiveData.OnValueDirtiedCallback onDirtied = () -> mHasChanges.setValue(true);
|
||||
mDescription = new ChangeTrackedLiveData<>("", onDirtied);
|
||||
}
|
||||
|
||||
public void copyFromSense(String sense) {
|
||||
mDescription.resetValue(sense);
|
||||
}
|
||||
|
||||
public LiveData<String> getDescription() {
|
||||
return mDescription;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
mDescription.setValue(description);
|
||||
}
|
||||
|
||||
public boolean hasChanges() {
|
||||
Boolean value = mHasChanges.getValue();
|
||||
return value != null && value;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ 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;
|
||||
@@ -31,11 +32,10 @@ public class EditSensesFragment extends MCFragment {
|
||||
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);
|
||||
NavDirections action = EditSensesFragmentDirections.actionEditSensesFragmentToEditSenseFragment(sense);
|
||||
View view = getView();
|
||||
assert view != null;
|
||||
Navigation.findNavController(view).navigate(action);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
17
app/src/main/res/layout/fragment_edit_sense.xml
Normal file
17
app/src/main/res/layout/fragment_edit_sense.xml
Normal file
@@ -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.EditSenseFragment">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/name"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/label_description"
|
||||
android:importantForAutofill="no"
|
||||
android:inputType="text"
|
||||
tools:text="darkvision 60 ft." />
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
@@ -157,6 +157,19 @@
|
||||
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" />
|
||||
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>
|
||||
</navigation>
|
||||
</navigation>
|
||||
|
||||
@@ -79,4 +79,5 @@
|
||||
<string name="title_library">Library</string>
|
||||
<string name="title_search">Search</string>
|
||||
<string name="wisdom_abbreviation">WIS</string>
|
||||
<string name="label_description">Description</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user