Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2425S1#143 from potaotototo/remark-ca…
Browse files Browse the repository at this point in the history
…lls-display

Make remark commands update displayed person
  • Loading branch information
weijianwong authored Oct 29, 2024
2 parents 0aa7670 + 61d344a commit 45083b4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/tuteez/logic/commands/AddRemarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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("Added remark to Person %1$s: %2$s",
personIndex.getOneBased(), remarkToAdd.toString()));
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/tuteez/logic/commands/DeleteRemarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/tuteez/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ public ObjectProperty<Optional<Person>> 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);
}
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/tuteez/logic/commands/AddRemarkCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/tuteez/logic/commands/DeleteRemarkCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,28 @@ public void execute_invalidRemarkIndex_failure() {
assertCommandFailure(deleteRemarkCommand, model, Messages.MESSAGE_INVALID_REMARK_INDEX);
}

@Test
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_FIRST_REMARK);

Person expectedPerson = new PersonBuilder(personToDeleteRemark).withRemarks(UPDATED_REMARKLIST).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);

assertCommandSuccess(deleteRemarkCommand, model, expectedMessage, expectedModel);

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");
}

@Test
public void equals() {
DeleteRemarkCommand deleteFirstRemarkCommand = new DeleteRemarkCommand(INDEX_FIRST_PERSON,
Expand Down

0 comments on commit 45083b4

Please sign in to comment.