Skip to content

Commit

Permalink
Merge pull request #89 from shishirbychapur/feature/preferences
Browse files Browse the repository at this point in the history
Add Preferences
  • Loading branch information
lynnlow175 authored Oct 16, 2023
2 parents 307153c + bda0ec7 commit 41fe565
Show file tree
Hide file tree
Showing 30 changed files with 618 additions and 68 deletions.
26 changes: 24 additions & 2 deletions src/main/java/seedu/lovebook/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
import seedu.lovebook.commons.util.StringUtil;
import seedu.lovebook.logic.Logic;
import seedu.lovebook.logic.LogicManager;
import seedu.lovebook.model.DatePrefs;
import seedu.lovebook.model.LoveBook;
import seedu.lovebook.model.Model;
import seedu.lovebook.model.ModelManager;
import seedu.lovebook.model.ReadOnlyDatePrefs;
import seedu.lovebook.model.ReadOnlyLoveBook;
import seedu.lovebook.model.ReadOnlyUserPrefs;
import seedu.lovebook.model.UserPrefs;
import seedu.lovebook.model.util.SampleDataUtil;
import seedu.lovebook.model.util.SampleDatePrefUtil;
import seedu.lovebook.storage.DatePrefsStorage;
import seedu.lovebook.storage.JsonLoveBookDatePrefs;
import seedu.lovebook.storage.JsonLoveBookStorage;
import seedu.lovebook.storage.JsonUserPrefsStorage;
import seedu.lovebook.storage.LoveBookStorage;
Expand Down Expand Up @@ -58,7 +63,8 @@ public void init() throws Exception {
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
LoveBookStorage loveBookStorage = new JsonLoveBookStorage(userPrefs.getLoveBookFilePath());
storage = new StorageManager(loveBookStorage, userPrefsStorage);
DatePrefsStorage datePrefsStorage = new JsonLoveBookDatePrefs(userPrefs.getDatePrefsFilePath());
storage = new StorageManager(loveBookStorage, userPrefsStorage, datePrefsStorage);

model = initModelManager(storage, userPrefs);

Expand All @@ -76,7 +82,9 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
logger.info("Using data file : " + storage.getLoveBookFilePath());

Optional<ReadOnlyLoveBook> loveBookOptional;
Optional<ReadOnlyDatePrefs> datePrefsOptional;
ReadOnlyLoveBook initialData;
ReadOnlyDatePrefs initialDatePrefs;
try {
loveBookOptional = storage.readLoveBook();
if (!loveBookOptional.isPresent()) {
Expand All @@ -90,7 +98,20 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
initialData = new LoveBook();
}

return new ModelManager(initialData, userPrefs);
try {
datePrefsOptional = storage.readDatePrefs();
if (!datePrefsOptional.isPresent()) {
logger.info("Creating a new data file " + storage.getDatePrefsFilePath()
+ " populated with a sample DatePrefs.");
}
initialDatePrefs = datePrefsOptional.orElseGet(SampleDatePrefUtil::getSamplePreferences);
} catch (DataLoadingException e) {
logger.warning("Data file at " + storage.getDatePrefsFilePath() + " could not be loaded."
+ " Will be starting with empty preferences.");
initialDatePrefs = new DatePrefs();
}

return new ModelManager(initialData, userPrefs, initialDatePrefs);
}

private void initLogging(Config config) {
Expand Down Expand Up @@ -180,6 +201,7 @@ public void stop() {
logger.info("============================ [ Stopping Height Book ] =============================");
try {
storage.saveUserPrefs(model.getUserPrefs());
storage.saveDatePrefs(model.getDatePrefs());
} catch (IOException e) {
logger.severe("Failed to save preferences " + StringUtil.getDetails(e));
}
Expand Down
107 changes: 107 additions & 0 deletions src/main/java/seedu/lovebook/model/DatePrefs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package seedu.lovebook.model;

import static java.util.Objects.requireNonNull;

import java.util.Objects;

import seedu.lovebook.commons.util.ToStringBuilder;
import seedu.lovebook.model.person.Age;
import seedu.lovebook.model.person.Gender;
import seedu.lovebook.model.person.Height;
import seedu.lovebook.model.person.Income;

/**
* Represents the preferences of the user for a date.
*/
public class DatePrefs implements ReadOnlyDatePrefs {
private Age age;
private Gender gender;
private Height height;
private Income income;

/**
* Creates a {@code DatePrefs} with default values.
*/
public DatePrefs() {
this.age = new Age("21");
this.gender = new Gender("F");
this.height = new Height("170");
this.income = new Income("10000");
}

/**
* Creates a {@code DatePrefs} with the given values.
*/
public DatePrefs(Age age, Gender gender, Height height, Income income) {
this.age = age;
this.gender = gender;
this.height = height;
this.income = income;
}

/**
* Creates a {@code DatePrefs} with the given values.
*/
public DatePrefs(ReadOnlyDatePrefs toBeCopied) {
this();
resetData(toBeCopied);
}

/**
* Resets the existing data of this {@code LoveBook} with {@code newData}.
*/
public void resetData(ReadOnlyDatePrefs newData) {
requireNonNull(newData);

setPreferences(newData.getPreferences());
}

/**
* Replaces the contents of the date list with {@code dates}.
* {@code dates} must not contain duplicate dates.
*/
public void setPreferences(DatePrefs prefs) {
this.age = prefs.age;
this.gender = prefs.gender;
this.height = prefs.height;
this.income = prefs.income;
}

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

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

DatePrefs otherPrefs = (DatePrefs) other;
return this.age == otherPrefs.age
&& this.height == otherPrefs.height
&& this.gender == otherPrefs.gender
&& this.income == otherPrefs.income;
}

@Override
public int hashCode() {
return Objects.hash(this.age, this.gender, this.height, this.income);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("age", age)
.add("gender", gender)
.add("height", height)
.add("income", income)
.toString();
}

@Override
public DatePrefs getPreferences() {
return this;
}
}
2 changes: 0 additions & 2 deletions src/main/java/seedu/lovebook/model/LoveBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public void setPersons(List<Date> dates) {
*/
public void resetData(ReadOnlyLoveBook newData) {
requireNonNull(newData);

setPersons(newData.getPersonList());
}

Expand Down Expand Up @@ -82,7 +81,6 @@ public void addPerson(Date p) {
*/
public void setPerson(Date target, Date editedDate) {
requireNonNull(editedDate);

dates.setPerson(target, editedDate);
}

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/lovebook/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ public interface Model {
* Returns a String of welcome message.
*/
String getWelcomeMessage();

Path getDatePrefsFilePath();

void setDatePrefsFilePath(Path datePrefsFilePath);

void setDatePrefs(ReadOnlyDatePrefs datePrefs);

ReadOnlyDatePrefs getDatePrefs();
}
32 changes: 28 additions & 4 deletions src/main/java/seedu/lovebook/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,26 @@ public class ModelManager implements Model {

private final LoveBook loveBook;
private final UserPrefs userPrefs;
private final DatePrefs datePrefs;
private final FilteredList<Date> filteredDates;

/**
* Initializes a ModelManager with the given LoveBook and userPrefs.
*/
public ModelManager(ReadOnlyLoveBook loveBook, ReadOnlyUserPrefs userPrefs) {
requireAllNonNull(loveBook, userPrefs);
public ModelManager(ReadOnlyLoveBook loveBook, ReadOnlyUserPrefs userPrefs, ReadOnlyDatePrefs datePrefs) {
requireAllNonNull(loveBook, userPrefs, datePrefs);

logger.fine("Initializing with lovebook book: " + loveBook + " and user prefs " + userPrefs);
logger.fine("Initializing with lovebook book: " + loveBook + " and user prefs " + userPrefs
+ " and date prefs " + datePrefs);

this.loveBook = new LoveBook(loveBook);
this.userPrefs = new UserPrefs(userPrefs);
this.datePrefs = new DatePrefs(datePrefs);
filteredDates = new FilteredList<>(this.loveBook.getPersonList());
}

public ModelManager() {
this(new LoveBook(), new UserPrefs());
this(new LoveBook(), new UserPrefs(), new DatePrefs());
}

//=========== UserPrefs ==================================================================================
Expand Down Expand Up @@ -151,4 +154,25 @@ public boolean equals(Object other) {
&& filteredDates.equals(otherModelManager.filteredDates);
}

//=========== DatePrefs ==================================================================================
@Override
public void setDatePrefs(ReadOnlyDatePrefs prefs) {
this.datePrefs.resetData(prefs);
}

@Override
public ReadOnlyDatePrefs getDatePrefs() {
return this.datePrefs.getPreferences();
}

@Override
public Path getDatePrefsFilePath() {
return this.userPrefs.getDatePrefsFilePath();
}

@Override
public void setDatePrefsFilePath(Path datePrefsFilePath) {
this.userPrefs.setDatePrefsFilePath(datePrefsFilePath);
}

}
8 changes: 8 additions & 0 deletions src/main/java/seedu/lovebook/model/ReadOnlyDatePrefs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package seedu.lovebook.model;

/**
* Unmodifiable view of date preferences.
*/
public interface ReadOnlyDatePrefs {
DatePrefs getPreferences();
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/lovebook/model/ReadOnlyLoveBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import seedu.lovebook.model.person.Date;

/**
* Unmodifiable view of an lovebook book
* Unmodifiable view of the LoveBook.
*/
public interface ReadOnlyLoveBook {

Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/lovebook/model/UserPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class UserPrefs implements ReadOnlyUserPrefs {

private GuiSettings guiSettings = new GuiSettings();
private Path loveBookFilePath = Paths.get("data" , "LoveBook.json");
private Path datePrefsFilePath = Paths.get("data", "Preferences.json");

/**
* Creates a {@code UserPrefs} with default values.
Expand Down Expand Up @@ -51,11 +52,20 @@ public Path getLoveBookFilePath() {
return loveBookFilePath;
}

public Path getDatePrefsFilePath() {
return datePrefsFilePath;
}

public void setLoveBookFilePath(Path loveBookFilePath) {
requireNonNull(loveBookFilePath);
this.loveBookFilePath = loveBookFilePath;
}

public void setDatePrefsFilePath(Path datePrefsFilePath) {
requireNonNull(datePrefsFilePath);
this.datePrefsFilePath = datePrefsFilePath;
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/seedu/lovebook/model/person/User.java

This file was deleted.

20 changes: 20 additions & 0 deletions src/main/java/seedu/lovebook/model/util/SampleDatePrefUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package seedu.lovebook.model.util;

import seedu.lovebook.model.DatePrefs;
import seedu.lovebook.model.person.Age;
import seedu.lovebook.model.person.Gender;
import seedu.lovebook.model.person.Height;
import seedu.lovebook.model.person.Income;

/**
* A utility class containing a sample {@code DatePrefs} to be used in tests.
*/
public class SampleDatePrefUtil {
public static DatePrefs getSamplePreferences() {
Age age = new Age("20");
Height height = new Height("180");
Income income = new Income("10000");
Gender gender = new Gender("F");
return new DatePrefs(age, gender, height, income);
}
}
43 changes: 43 additions & 0 deletions src/main/java/seedu/lovebook/storage/DatePrefsStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package seedu.lovebook.storage;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;

import seedu.lovebook.commons.exceptions.DataLoadingException;
import seedu.lovebook.model.ReadOnlyDatePrefs;

/**
* Represents a storage for {@link seedu.lovebook.model.DatePrefs}.
*/
public interface DatePrefsStorage {
/**
* Returns the file path of the data file.
*/
Path getDatePrefsFilePath();

/**
* Returns LoveBook data as a {@link ReadOnlyDatePrefs}.
* Returns {@code Optional.empty()} if storage file is not found.
*
* @throws DataLoadingException if loading the data from storage failed.
*/
Optional<ReadOnlyDatePrefs> readDatePrefs() throws DataLoadingException;

/**
* @see #getDatePrefsFilePath()
*/
Optional<ReadOnlyDatePrefs> readDatePrefs(Path filePath) throws DataLoadingException;

/**
* Saves the given {@link ReadOnlyDatePrefs} to the storage.
* @param prefs cannot be null.
* @throws IOException if there was any problem writing to the file.
*/
void saveDatePrefs(ReadOnlyDatePrefs prefs) throws IOException;

/**
* @see #saveDatePrefs(ReadOnlyDatePrefs)
*/
void saveDatePrefs(ReadOnlyDatePrefs prefs, Path filePath) throws IOException;
}
Loading

0 comments on commit 41fe565

Please sign in to comment.