Skip to content

Commit

Permalink
Move AddressBook change event handling to Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
damithc committed Sep 26, 2016
1 parent 8de0932 commit fbdb573
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 25 deletions.
Binary file modified docs/diagrams/Diagrams.pptx
Binary file not shown.
Binary file modified docs/images/SDforDeletePerson.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/images/SDforDeletePersonEventHandling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import seedu.address.commons.events.BaseEvent;
import seedu.address.model.ReadOnlyAddressBook;

/** Indicates data in the model has changed*/
public class ModelChangedEvent extends BaseEvent {
/** Indicates the AddressBook in the model has changed*/
public class AddressBookChangedEvent extends BaseEvent {

public final ReadOnlyAddressBook data;

public ModelChangedEvent(ReadOnlyAddressBook data){
public AddressBookChangedEvent(ReadOnlyAddressBook data){
this.data = data;
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ public interface Logic {

/** Returns the filtered list of persons */
ObservableList<ReadOnlyPerson> getFilteredPersonList();

}
18 changes: 1 addition & 17 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
package seedu.address.logic;

import com.google.common.eventbus.Subscribe;
import javafx.collections.ObservableList;
import seedu.address.commons.core.ComponentManager;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.events.model.ModelChangedEvent;
import seedu.address.commons.events.storage.DataSavingExceptionEvent;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.parser.Parser;
import seedu.address.model.Model;
import seedu.address.model.person.ReadOnlyPerson;
import seedu.address.storage.Storage;

import java.io.IOException;
import java.util.logging.Logger;

/**
Expand All @@ -38,19 +34,7 @@ public CommandResult execute(String commandText) {
return command.execute();
}

/**
* Creates the file if it is missing before saving.
* Raises {@link DataSavingExceptionEvent} if there was an error during saving
*/
@Subscribe
public void handleModelChangedEvent(ModelChangedEvent mce) {
logger.info("Local data changed, saving to primary data file");
try {
storage.saveAddressBook(mce.data);
} catch (IOException e) {
raise(new DataSavingExceptionEvent(e));
}
}


@Override
public ObservableList<ReadOnlyPerson> getFilteredPersonList() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.UnmodifiableObservableList;
import seedu.address.commons.util.StringUtil;
import seedu.address.commons.events.model.ModelChangedEvent;
import seedu.address.commons.events.model.AddressBookChangedEvent;
import seedu.address.commons.core.ComponentManager;
import seedu.address.model.person.Person;
import seedu.address.model.person.ReadOnlyPerson;
Expand Down Expand Up @@ -64,7 +64,7 @@ public ReadOnlyAddressBook getAddressBook() {

/** Raises an event to indicate the model has changed */
private void indicateModelChanged() {
raise(new ModelChangedEvent(addressBook));
raise(new AddressBookChangedEvent(addressBook));
}

@Override
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.address.storage;

import seedu.address.commons.events.model.AddressBookChangedEvent;
import seedu.address.commons.events.storage.DataSavingExceptionEvent;
import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.UserPrefs;
Expand Down Expand Up @@ -29,4 +31,11 @@ public interface Storage extends AddressBookStorage, UserPrefsStorage {

@Override
void saveAddressBook(ReadOnlyAddressBook addressBook) throws IOException;

/**
* Saves the current version of the Address Book to the hard disk.
* Creates the data file if it is missing.
* Raises {@link DataSavingExceptionEvent} if there was an error during saving.
*/
public void handleAddressBookChangedEvent(AddressBookChangedEvent mce);
}
16 changes: 15 additions & 1 deletion src/main/java/seedu/address/storage/StorageManager.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package seedu.address.storage;

import com.google.common.eventbus.Subscribe;
import seedu.address.commons.core.ComponentManager;
import seedu.address.commons.core.Config;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.events.model.AddressBookChangedEvent;
import seedu.address.commons.events.storage.DataSavingExceptionEvent;
import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.UserPrefs;
Expand Down Expand Up @@ -65,4 +67,16 @@ public void saveAddressBook(ReadOnlyAddressBook addressBook) throws IOException
addressBookStorage.saveAddressBook(addressBook, addressBookStorage.getAddressBookFilePath());
}


@Override
@Subscribe
public void handleAddressBookChangedEvent(AddressBookChangedEvent mce) {
logger.info("Local data changed, saving to primary data file");
try {
saveAddressBook(mce.data);
} catch (IOException e) {
raise(new DataSavingExceptionEvent(e));
}
}

}
4 changes: 2 additions & 2 deletions src/test/java/seedu/address/logic/LogicManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import seedu.address.logic.commands.*;
import seedu.address.commons.events.controller.JumpToListRequestEvent;
import seedu.address.commons.events.controller.ShowHelpEvent;
import seedu.address.commons.events.model.ModelChangedEvent;
import seedu.address.commons.events.model.AddressBookChangedEvent;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
Expand Down Expand Up @@ -46,7 +46,7 @@ public class LogicManagerTest {
private int targetedJumpIndex;

@Subscribe
private void handleLocalModelChangedEvent(ModelChangedEvent lmce) {
private void handleLocalModelChangedEvent(AddressBookChangedEvent lmce) {
latestSavedAddressBook = new AddressBook(lmce.data);
}

Expand Down

0 comments on commit fbdb573

Please sign in to comment.