From 0e75bf65af34cb4859fe719c2c70641e938a6a11 Mon Sep 17 00:00:00 2001 From: potaotototo Date: Tue, 29 Oct 2024 08:56:58 +0800 Subject: [PATCH 1/4] Make remark commands update displayed person --- .../logic/commands/AddRemarkCommand.java | 4 ++++ .../logic/commands/DeleteRemarkCommand.java | 3 +++ .../logic/commands/AddRemarkCommandTest.java | 18 ++++++++++++++++++ .../commands/DeleteRemarkCommandTest.java | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+) diff --git a/src/main/java/tuteez/logic/commands/AddRemarkCommand.java b/src/main/java/tuteez/logic/commands/AddRemarkCommand.java index e6df2f70824..2ae2d087566 100644 --- a/src/main/java/tuteez/logic/commands/AddRemarkCommand.java +++ b/src/main/java/tuteez/logic/commands/AddRemarkCommand.java @@ -1,6 +1,7 @@ package tuteez.logic.commands; import java.util.ArrayList; +import java.util.Optional; import java.util.logging.Logger; import tuteez.commons.core.LogsCenter; @@ -41,6 +42,9 @@ public CommandResult execute(Model model) throws CommandException { model.setPerson(personToUpdate, updatedPerson); model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS); + // Update the last viewed person in the model after updating remarks + model.updateLastViewedPerson(updatedPerson); + return new CommandResult(String.format("Added remark to Person %1$s: %2$s", personIndex.getOneBased(), remarkToAdd.toString())); } diff --git a/src/main/java/tuteez/logic/commands/DeleteRemarkCommand.java b/src/main/java/tuteez/logic/commands/DeleteRemarkCommand.java index 8f7ca658140..2e5c4658b16 100644 --- a/src/main/java/tuteez/logic/commands/DeleteRemarkCommand.java +++ b/src/main/java/tuteez/logic/commands/DeleteRemarkCommand.java @@ -41,6 +41,9 @@ public CommandResult execute(Model model) throws CommandException { model.setPerson(personToUpdate, updatedPerson); model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS); + // Update the last viewed person in the model after updating remarks + model.updateLastViewedPerson(updatedPerson); + return new CommandResult(String.format("Deleted remark at index %1$s from Person %2$s", remarkIndex.getOneBased(), personIndex.getOneBased())); } diff --git a/src/test/java/tuteez/logic/commands/AddRemarkCommandTest.java b/src/test/java/tuteez/logic/commands/AddRemarkCommandTest.java index 033c199cd08..59f2cd7d159 100644 --- a/src/test/java/tuteez/logic/commands/AddRemarkCommandTest.java +++ b/src/test/java/tuteez/logic/commands/AddRemarkCommandTest.java @@ -89,6 +89,24 @@ public void execute_invalidPersonIndexFilteredList_failure() { assertCommandFailure(addRemarkCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); } + @Test + public void execute_addRemark_updatesLastViewedPerson() { + Person personToAddRemark = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + Person updatedPerson = new PersonBuilder(personToAddRemark).withRemarks(VALID_REMARKLIST).build(); + AddRemarkCommand addRemarkCommand = new AddRemarkCommand(INDEX_FIRST_PERSON, VALID_REMARK); + + String expectedMessage = String.format("Added remark to Person %1$s: %2$s", + INDEX_FIRST_PERSON.getOneBased(), VALID_REMARK); + + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + expectedModel.setPerson(personToAddRemark, updatedPerson); + + assertCommandSuccess(addRemarkCommand, model, expectedMessage, expectedModel); + + assertTrue(model.getLastViewedPerson().get().isPresent(), "Expected lastViewedPerson to be set"); + assertTrue(model.getLastViewedPerson().get().get().equals(updatedPerson)); + } + @Test public void equals() { AddRemarkCommand addFirstRemarkCommand = new AddRemarkCommand(INDEX_FIRST_PERSON, VALID_REMARK); diff --git a/src/test/java/tuteez/logic/commands/DeleteRemarkCommandTest.java b/src/test/java/tuteez/logic/commands/DeleteRemarkCommandTest.java index 1d6a4864ab9..8bc40b6b227 100644 --- a/src/test/java/tuteez/logic/commands/DeleteRemarkCommandTest.java +++ b/src/test/java/tuteez/logic/commands/DeleteRemarkCommandTest.java @@ -116,6 +116,25 @@ public void execute_invalidRemarkIndex_failure() { assertCommandFailure(deleteRemarkCommand, model, Messages.MESSAGE_INVALID_REMARK_INDEX); } + @Test + public void execute_deleteRemark_updatesLastViewedPerson() throws Exception { + Person personToUpdate = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + RemarkList updatedRemarkList = new RemarkList(Arrays.asList(new Remark("First Remark"), new Remark("Second Remark"))); + Person personWithRemarks = new PersonBuilder(personToUpdate).withRemarks(updatedRemarkList).build(); + model.setPerson(personToUpdate, personWithRemarks); + + DeleteRemarkCommand deleteRemarkCommand = new DeleteRemarkCommand(INDEX_FIRST_PERSON, Index.fromZeroBased(0)); + + RemarkList expectedRemarkList = new RemarkList(Arrays.asList(new Remark("Second Remark"))); + Person expectedPerson = new PersonBuilder(personToUpdate).withRemarks(expectedRemarkList).build(); + + deleteRemarkCommand.execute(model); + + assertTrue(model.getLastViewedPerson().get().isPresent(), "Expected lastViewedPerson to be set"); + assertTrue(model.getLastViewedPerson().get().get().equals(expectedPerson), + "Expected lastViewedPerson to match the person with the updated remark list"); + } + @Test public void equals() { DeleteRemarkCommand deleteFirstRemarkCommand = new DeleteRemarkCommand(INDEX_FIRST_PERSON, From 1e0e966ba55efb0d0afda8e29d56abf37a06769a Mon Sep 17 00:00:00 2001 From: potaotototo Date: Tue, 29 Oct 2024 09:18:11 +0800 Subject: [PATCH 2/4] Fix checkstyle --- src/main/java/tuteez/logic/commands/AddRemarkCommand.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/tuteez/logic/commands/AddRemarkCommand.java b/src/main/java/tuteez/logic/commands/AddRemarkCommand.java index 2ae2d087566..10af2b403de 100644 --- a/src/main/java/tuteez/logic/commands/AddRemarkCommand.java +++ b/src/main/java/tuteez/logic/commands/AddRemarkCommand.java @@ -1,7 +1,6 @@ package tuteez.logic.commands; import java.util.ArrayList; -import java.util.Optional; import java.util.logging.Logger; import tuteez.commons.core.LogsCenter; From cca0f809000edfc8b0b10b20bc388895e4187114 Mon Sep 17 00:00:00 2001 From: potaotototo Date: Tue, 29 Oct 2024 09:21:11 +0800 Subject: [PATCH 3/4] Fix bug updating remarks for person displayed --- src/main/java/tuteez/model/ModelManager.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/tuteez/model/ModelManager.java b/src/main/java/tuteez/model/ModelManager.java index 381a6ee9ead..da64c6a5d96 100644 --- a/src/main/java/tuteez/model/ModelManager.java +++ b/src/main/java/tuteez/model/ModelManager.java @@ -160,6 +160,8 @@ public ObjectProperty> getLastViewedPerson() { @Override public void updateLastViewedPerson(Person personOnDisplay) { requireNonNull(personOnDisplay); + // Clear existing person to ensure each time updateLastViewedPerson is called, personOnDisplay is updated + lastViewedPerson.set(Optional.empty()); lastViewedPerson.set(Optional.of(personOnDisplay)); logger.info("Last viewed person updated: " + personOnDisplay.getName().fullName); } From 61d344a6fc4578345f7f901d75d2d7e7a8c62865 Mon Sep 17 00:00:00 2001 From: potaotototo Date: Tue, 29 Oct 2024 09:27:08 +0800 Subject: [PATCH 4/4] Rewrite test for DeleteRemarkCommandTest --- .../commands/DeleteRemarkCommandTest.java | 23 +++++++++++-------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/test/java/tuteez/logic/commands/DeleteRemarkCommandTest.java b/src/test/java/tuteez/logic/commands/DeleteRemarkCommandTest.java index 8bc40b6b227..c272d278d2c 100644 --- a/src/test/java/tuteez/logic/commands/DeleteRemarkCommandTest.java +++ b/src/test/java/tuteez/logic/commands/DeleteRemarkCommandTest.java @@ -117,20 +117,23 @@ public void execute_invalidRemarkIndex_failure() { } @Test - public void execute_deleteRemark_updatesLastViewedPerson() throws Exception { - Person personToUpdate = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); - RemarkList updatedRemarkList = new RemarkList(Arrays.asList(new Remark("First Remark"), new Remark("Second Remark"))); - Person personWithRemarks = new PersonBuilder(personToUpdate).withRemarks(updatedRemarkList).build(); - model.setPerson(personToUpdate, personWithRemarks); + public void execute_deleteRemark_updatesLastViewedPerson() { + Person originalPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); + Person personToDeleteRemark = new PersonBuilder(originalPerson).withRemarks(VALID_REMARKLIST).build(); + + model.setPerson(originalPerson, personToDeleteRemark); - DeleteRemarkCommand deleteRemarkCommand = new DeleteRemarkCommand(INDEX_FIRST_PERSON, Index.fromZeroBased(0)); + DeleteRemarkCommand deleteRemarkCommand = new DeleteRemarkCommand(INDEX_FIRST_PERSON, INDEX_FIRST_REMARK); + + Person expectedPerson = new PersonBuilder(personToDeleteRemark).withRemarks(UPDATED_REMARKLIST).build(); - RemarkList expectedRemarkList = new RemarkList(Arrays.asList(new Remark("Second Remark"))); - Person expectedPerson = new PersonBuilder(personToUpdate).withRemarks(expectedRemarkList).build(); + String expectedMessage = String.format("Deleted remark at index %1$s from Person %2$s", 1, 1); + Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), new UserPrefs()); + expectedModel.setPerson(personToDeleteRemark, expectedPerson); - deleteRemarkCommand.execute(model); + assertCommandSuccess(deleteRemarkCommand, model, expectedMessage, expectedModel); - assertTrue(model.getLastViewedPerson().get().isPresent(), "Expected lastViewedPerson to be set"); + assertTrue(model.getLastViewedPerson().get().isPresent(), "Expected lastViewedPerson to be present"); assertTrue(model.getLastViewedPerson().get().get().equals(expectedPerson), "Expected lastViewedPerson to match the person with the updated remark list"); }