Skip to content

Commit

Permalink
Merge pull request #213 from AbdulrahmanAlRammah/Maintain_Sort_Order
Browse files Browse the repository at this point in the history
Maintaining Sort order when adding or editing contacts
  • Loading branch information
liauzhanyi authored Nov 7, 2024
2 parents 732a897 + 762e7e3 commit 534c698
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 3 deletions.
75 changes: 75 additions & 0 deletions src/main/java/seedu/address/commons/core/SortSettings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package seedu.address.commons.core;

import java.io.Serializable;
import java.util.Objects;

import seedu.address.commons.util.ToStringBuilder;
import seedu.address.model.person.PersonComparator;

/**
* A Serializable class that contains the Sort settings.
* Guarantees: immutable.
*/
public class SortSettings implements Serializable {

private static final String DEFAULT_SORT_PARAMETER = PersonComparator.NAME;
private static final boolean DEFAULT_IS_ASCENDING_ORDER = true;

private final String sortParameter;

private final boolean isAscendingOrder;

/**
* Constructs a {@code SortSettings} with the default sortParameter and Order.
*/
public SortSettings() {
sortParameter = DEFAULT_SORT_PARAMETER;
isAscendingOrder = DEFAULT_IS_ASCENDING_ORDER;
}

/**
* Constructs a {@code SortSettings} with the specified sortParameter and Order.
*/
public SortSettings(String sortParameter, boolean isAscendingOrder) {
this.sortParameter = sortParameter;
this.isAscendingOrder = isAscendingOrder;
}

public String getSortParameter() {
return this.sortParameter;
}

public boolean isAscendingOrder() {
return this.isAscendingOrder;
}

@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}

// instanceof handles nulls
if (!(other instanceof SortSettings)) {
return false;
}

SortSettings otherSortSettings = (SortSettings) other;
return this.sortParameter == otherSortSettings.sortParameter
&& (this.isAscendingOrder == otherSortSettings.isAscendingOrder);

}

@Override
public int hashCode() {
return Objects.hash(sortParameter, isAscendingOrder);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("sortParameter", sortParameter)
.add("isAscendingOrder", isAscendingOrder)
.toString();
}
}
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/commands/SeedCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static java.util.Objects.requireNonNull;

import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Person;
import seedu.address.model.util.SampleDataUtil;
Expand All @@ -17,7 +18,7 @@ public class SeedCommand extends Command {
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Seeds dummy data into the SocialBook.";

@Override
public CommandResult execute(Model model) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
for (Person person : SampleDataUtil.getSamplePersons()) {
if (model.hasPerson(person)) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.SortSettings;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.person.Person;

Expand Down Expand Up @@ -33,6 +34,16 @@ public interface Model {
*/
GuiSettings getGuiSettings();

/**
* Sets the user prefs' Sort settings.
*/
void setSortSettings(SortSettings sortSettings);

/**
* Returns the user prefs' Sort settings.
*/
SortSettings getSortSettings();

/**
* Sets the user prefs' GUI settings.
*/
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

import java.nio.file.Path;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.Logger;

import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.SortSettings;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.person.Person;
import seedu.address.model.person.PersonComparator;
Expand All @@ -20,6 +22,8 @@
*/
public class ModelManager implements Model {
private static final Logger logger = LogsCenter.getLogger(ModelManager.class);
private static final String RESORT_ERROR = "Error: User Prefs had an invalid sort parameter. "
+ "Re-sorting after changes were made to the addressbook was abandoned.";

private final AddressBook addressBook;
private final UserPrefs userPrefs;
Expand Down Expand Up @@ -66,6 +70,17 @@ public void setGuiSettings(GuiSettings guiSettings) {
userPrefs.setGuiSettings(guiSettings);
}

@Override
public SortSettings getSortSettings() {
return userPrefs.getSortSettings();
}

@Override
public void setSortSettings(SortSettings sortSettings) {
requireNonNull(sortSettings);
userPrefs.setSortSettings(sortSettings);
}

@Override
public Path getAddressBookFilePath() {
return userPrefs.getAddressBookFilePath();
Expand Down Expand Up @@ -103,13 +118,15 @@ public void deletePerson(Person target) {
@Override
public void addPerson(Person person) {
addressBook.addPerson(person);
reSortPersonList(getSortSettings());
updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
}

@Override
public void setPerson(Person target, Person editedPerson) {
requireAllNonNull(target, editedPerson);
addressBook.setPerson(target, editedPerson);
reSortPersonList(getSortSettings());
}

//=========== Filtered Person List Accessors =============================================================
Expand Down Expand Up @@ -149,6 +166,18 @@ public boolean equals(Object other) {
@Override
public void sortPersonList(String parameter, boolean isAscending) throws CommandException {
addressBook.sort(new PersonComparator().getComparator(parameter, isAscending));
setSortSettings(new SortSettings(parameter, isAscending));
}

private void reSortPersonList(SortSettings sortSettings) {
String parameter = sortSettings.getSortParameter();
boolean isAscending = sortSettings.isAscendingOrder();
try {
addressBook.sort(new PersonComparator().getComparator(parameter, isAscending));
} catch (CommandException ce) {
//This case should only happen if the user incorrectly edits the UserPrefs manually.
logger.log(Level.WARNING, RESORT_ERROR);
}
}

}
3 changes: 3 additions & 0 deletions src/main/java/seedu/address/model/ReadOnlyUserPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.file.Path;

import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.SortSettings;

/**
* Unmodifiable view of user prefs.
Expand All @@ -11,6 +12,8 @@ public interface ReadOnlyUserPrefs {

GuiSettings getGuiSettings();

SortSettings getSortSettings();

Path getAddressBookFilePath();

}
15 changes: 14 additions & 1 deletion src/main/java/seedu/address/model/UserPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
import java.util.Objects;

import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.SortSettings;

/**
* Represents User's preferences.
*/
public class UserPrefs implements ReadOnlyUserPrefs {

private GuiSettings guiSettings = new GuiSettings();
private SortSettings sortSettings = new SortSettings();
private Path addressBookFilePath = Paths.get("data", "socialbook.json");

/**
Expand All @@ -35,18 +37,28 @@ public UserPrefs(ReadOnlyUserPrefs userPrefs) {
public void resetData(ReadOnlyUserPrefs newUserPrefs) {
requireNonNull(newUserPrefs);
setGuiSettings(newUserPrefs.getGuiSettings());
setSortSettings(newUserPrefs.getSortSettings());
setAddressBookFilePath(newUserPrefs.getAddressBookFilePath());
}

public GuiSettings getGuiSettings() {
return guiSettings;
}

public SortSettings getSortSettings() {
return sortSettings;
}

public void setGuiSettings(GuiSettings guiSettings) {
requireNonNull(guiSettings);
this.guiSettings = guiSettings;
}

public void setSortSettings(SortSettings sortSettings) {
requireNonNull(sortSettings);
this.sortSettings = sortSettings;
}

public Path getAddressBookFilePath() {
return addressBookFilePath;
}
Expand Down Expand Up @@ -74,14 +86,15 @@ public boolean equals(Object other) {

@Override
public int hashCode() {
return Objects.hash(guiSettings, addressBookFilePath);
return Objects.hash(guiSettings, addressBookFilePath, sortSettings);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Gui Settings : " + guiSettings);
sb.append("\nLocal data file location : " + addressBookFilePath);
sb.append("\nSort settings : ");
return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
public class PersonComparator {
public static final String NAME = "name";
public static final String DATE_OF_LAST_VISIT = "date of last visit";
private static final String SORT_EXCEPTION = "The specified parameter is invalid.";
private static final String SORT_EXCEPTION = "The specified sort parameter is invalid.";

public PersonComparator() {

Expand Down
16 changes: 16 additions & 0 deletions src/test/java/seedu/address/commons/core/SortSettingsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package seedu.address.commons.core;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

public class SortSettingsTest {
@Test
public void toStringMethod() {
SortSettings sortSettings = new SortSettings();
String expected = SortSettings.class.getCanonicalName() + "{sortParameter=" + sortSettings.getSortParameter()
+ ", isAscendingOrder=" + sortSettings.isAscendingOrder() + "}";
assertEquals(expected, sortSettings.toString());
}
}

11 changes: 11 additions & 0 deletions src/test/java/seedu/address/logic/commands/AddCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.SortSettings;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
Expand Down Expand Up @@ -168,6 +169,16 @@ public void setGuiSettings(GuiSettings guiSettings) {
throw new AssertionError("This method should not be called.");
}

@Override
public SortSettings getSortSettings() {
throw new AssertionError("This method should not be called.");
}

@Override
public void setSortSettings(SortSettings sortSettings) {
throw new AssertionError("This method should not be called.");
}

@Override
public Path getAddressBookFilePath() {
throw new AssertionError("This method should not be called.");
Expand Down

0 comments on commit 534c698

Please sign in to comment.