Skip to content

Commit

Permalink
Add Comment input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
cbebe committed Oct 27, 2021
1 parent 67a4d57 commit aad3a0c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import java.time.format.DateTimeFormatter;

public class CreateEditHabitFragment extends Fragment {
private static final String ERROR_MESSAGE = "Input is too long";
public static final String ERROR_MESSAGE = "Input is too long";
private static final int MAX_TITLE_LEN = 20;
private static final int MAX_REASON_LEN = 30;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
import com.github.cmput301f21t44.hellohabits.R;
import com.github.cmput301f21t44.hellohabits.databinding.FragmentCreateEditHabitEventBinding;
import com.github.cmput301f21t44.hellohabits.model.HabitEvent;
import com.github.cmput301f21t44.hellohabits.view.habit.CreateEditHabitFragment;
import com.github.cmput301f21t44.hellohabits.viewmodel.HabitEventViewModel;
import com.github.cmput301f21t44.hellohabits.viewmodel.HabitViewModel;

import java.util.Objects;

public class CreateEditHabitEventFragment extends Fragment {
private static final int MAX_COMMENT_LEN = 20;
private FragmentCreateEditHabitEventBinding binding;
private HabitViewModel mHabitViewModel;
private HabitEventViewModel mHabitEventViewModel;
Expand All @@ -36,29 +38,37 @@ public View onCreateView(@NonNull LayoutInflater inflater,

}

private void submitHabitEvent() {
String comment = binding.editTextComment.getText().toString();
if (comment.length() > MAX_COMMENT_LEN) {
binding.editTextComment.setError(CreateEditHabitFragment.ERROR_MESSAGE);
binding.editTextComment.requestFocus();
return;
}
if (isEdit) {
HabitEvent updatedHabitEvent = mHabitEventViewModel.update(mHabitEvent.getId(),
mHabitEvent.getHabitId(), mHabitEvent.getDate(), comment, null, null);
mHabitEventViewModel.select(updatedHabitEvent);
} else {
String habitId = Objects.requireNonNull(mHabitViewModel.getSelected().getValue())
.getId();
mHabitEventViewModel.insert(habitId, comment);
}
mNavController
.navigate(isEdit ?
R.id.action_createEditHabitEventFragment_to_viewHabitEventListFragment :
R.id.action_createEditHabitEventFragment_to_viewHabitFragment);

}

public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mHabitViewModel = new ViewModelProvider(requireActivity()).get(HabitViewModel.class);
mHabitEventViewModel = new ViewModelProvider(requireActivity())
.get(HabitEventViewModel.class);
mNavController = NavHostFragment.findNavController(this);

binding.buttonAddHabitEvent.setOnClickListener(v -> {
if (isEdit) {
HabitEvent updatedHabitEvent = mHabitEventViewModel.update(mHabitEvent.getId(),
mHabitEvent.getHabitId(), mHabitEvent.getDate(),
binding.editTextComment.getText().toString(), null, null);
mHabitEventViewModel.select(updatedHabitEvent);
} else {
mHabitEventViewModel.insert(
Objects.requireNonNull(mHabitViewModel.getSelected().getValue()).getId(),
binding.editTextComment.getText().toString());
}
mNavController
.navigate(isEdit ?
R.id.action_createEditHabitEventFragment_to_viewHabitEventListFragment :
R.id.action_createEditHabitEventFragment_to_viewHabitFragment);
});
binding.buttonAddHabitEvent.setOnClickListener(v -> submitHabitEvent());

binding.buttonBack.setOnClickListener(v ->
mNavController
Expand Down

0 comments on commit aad3a0c

Please sign in to comment.