Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
bjhoohaha committed Nov 9, 2019
1 parent ea3cbaf commit e36c26d
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 120 deletions.
53 changes: 4 additions & 49 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@

import javafx.application.Application;
import javafx.stage.Stage;
import seedu.address.address.model.AddressBook;
import seedu.address.address.model.AddressBookModelManager;
import seedu.address.address.model.ReadOnlyAddressBook;
import seedu.address.address.model.util.SampleDataUtil;
import seedu.address.address.storage.AddressBookStorage;
import seedu.address.address.storage.JsonAddressBookStorage;
import seedu.address.commons.core.Config;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.Version;
Expand All @@ -22,13 +16,8 @@
import seedu.address.logic.Logic;
import seedu.address.logic.LogicManager;
import seedu.address.model.Model;
import seedu.address.model.ModelManager;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.UserPrefs;
import seedu.address.model.UserPrefsModelManager;
import seedu.address.storage.JsonUserPrefsStorage;
import seedu.address.storage.Storage;
import seedu.address.storage.StorageManager;
import seedu.address.storage.UserPrefsStorage;
import seedu.address.ui.Ui;
import seedu.address.ui.UiManager;
Expand All @@ -44,7 +33,7 @@ public class MainApp extends Application {

protected Ui ui;
protected Logic logic;
protected Storage storage;
protected UserPrefsStorage storage;
protected Model model;
protected Config config;

Expand All @@ -58,50 +47,16 @@ public void init() throws Exception {
AppParameters appParameters = AppParameters.parse(getParameters());
config = initConfig(appParameters.getConfigPath());

UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
//add more storage here
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
//overload storage manager
storage = new StorageManager(addressBookStorage, userPrefsStorage);
storage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(storage);

initLogging(config);

//model should now include all relevant models
//use of a model manager to get different models from different pages
model = initModelManager(storage, userPrefs);

//contain different logic: mainLogic + addressBookLogic
logic = new LogicManager(model, storage);
logic = new LogicManager(userPrefs);

ui = new UiManager(logic);
}

/**
* Returns a {@code AddressBookModelManager} with the data from {@code storage}'s address book and {@code userPrefs}
* .<br> The data from the sample address book will be used instead if {@code storage}'s address book is not found,
* or an empty address book will be used instead if errors occur when reading {@code storage}'s address book.
*/
private ModelManager initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
Optional<ReadOnlyAddressBook> addressBookOptional;
ReadOnlyAddressBook initialData;
try {
addressBookOptional = storage.readAddressBook();
if (!addressBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample AddressBook");
}
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
initialData = new AddressBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
initialData = new AddressBook();
}
return new ModelManager(new UserPrefsModelManager(userPrefs),
new AddressBookModelManager(initialData, userPrefs));
}

private void initLogging(Config config) {
LogsCenter.init(config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@

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

import javafx.collections.ObservableList;
import javafx.scene.chart.XYChart;
import seedu.address.address.logic.parser.AddressBookParser;
import seedu.address.address.model.AddressBook;
import seedu.address.address.model.AddressBookModel;
import seedu.address.address.model.AddressBookModelManager;
import seedu.address.address.model.ReadOnlyAddressBook;
import seedu.address.address.model.person.Person;
import seedu.address.address.model.util.AddressBookStatistics;
import seedu.address.address.model.util.SampleDataUtil;
import seedu.address.address.storage.AddressBookStorage;
import seedu.address.address.storage.JsonAddressBookStorage;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.UserPrefs;

/**
* The main AddressBookLogicManager of the app.
Expand All @@ -29,12 +37,35 @@ public class AddressBookLogicManager implements AddressBookLogic {
private final AddressBookStorage addressBookStorage;
private final AddressBookParser addressBookParser;

public AddressBookLogicManager(AddressBookModel addressBookModel, AddressBookStorage addressBookStorage) {
this.addressBookModel = addressBookModel;
this.addressBookStorage = addressBookStorage;
public AddressBookLogicManager(UserPrefs userPrefs) {
this.addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
this.addressBookModel = initModelManager(addressBookStorage, userPrefs);
this.addressBookParser = new AddressBookParser();
}

/**
* Returns a {@code AddressBookModelManager} with the data from {@code storage}'s address book and {@code userPrefs}
* .<br> The data from the sample address book will be used instead if {@code storage}'s address book is not found,
* or an empty address book will be used instead if errors occur when reading {@code storage}'s address book.
*/
private AddressBookModelManager initModelManager(AddressBookStorage storage, ReadOnlyUserPrefs userPrefs) {
Optional<ReadOnlyAddressBook> addressBookOptional;
ReadOnlyAddressBook initialData;
try {
addressBookOptional = storage.readAddressBook();
if (!addressBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample AddressBook");
}
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
initialData = new AddressBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
initialData = new AddressBook();
}
return new AddressBookModelManager(initialData, userPrefs);
}

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import seedu.address.diaryfeature.logic.DiaryBookLogic;
import seedu.address.financialtracker.logic.FinancialTrackerLogic;
import seedu.address.itinerary.logic.ItineraryLogic;
import seedu.address.storage.Storage;


/**
Expand All @@ -16,8 +15,6 @@

public interface Logic extends GuiSettingsLogic {

Storage getStorage();

/**
* Gets address book logic.
*
Expand Down
46 changes: 18 additions & 28 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
import seedu.address.diaryfeature.logic.DiaryBookLogic;
import seedu.address.financialtracker.logic.FinancialTrackerLogic;
import seedu.address.itinerary.logic.ItineraryLogic;
import seedu.address.model.Model;
import seedu.address.model.UserPrefsModel;
import seedu.address.storage.Storage;
import seedu.address.model.UserPrefs;

/**
* The main AddressBookLogicManager of the app.
Expand All @@ -24,39 +22,31 @@ public class LogicManager implements Logic {

private AddressBookLogic addressBookLogic;
private AchievementsLogic achievementsLogic;
private UserPrefsModel userPrefsModel;
private UserPrefs userPrefs;
private DiaryBookLogic diaryLogic;
private CalendarLogic calendarLogic;
private FinancialTrackerLogic financialTrackerLogic;
private ItineraryLogic itineraryLogic;
private MainLogic mainLogic;
private Storage storage;

public LogicManager(Model model, Storage storage) {
// overloaded AddressBook Logic Manager to pass main model in
// main model is used to save gui settings
this.userPrefsModel = model.getUserPrefsModel();
this.addressBookLogic = new AddressBookLogicManager(model.getAddressBookModel(), storage);
this.achievementsLogic = new AchievementsLogicManager(new Supplier<StatisticsModel>() {
@Override
public StatisticsModel get() {
return new StatisticsModelManager(addressBookLogic.getStatistics(),
calendarLogic.getStatistics(),
diaryLogic.getStatistics(),
financialTrackerLogic.getStatistics(),
itineraryLogic.getStatistics());
}
});
this.mainLogic = new MainLogicManager(userPrefsModel, storage);
public LogicManager(UserPrefs userPrefs) {
this.userPrefs = userPrefs;
this.addressBookLogic = new AddressBookLogicManager(userPrefs);
this.mainLogic = new MainLogicManager(userPrefs);
this.diaryLogic = new DiaryBookLogic();
this.calendarLogic = new CalendarLogic();
this.financialTrackerLogic = new FinancialTrackerLogic();
this.itineraryLogic = new ItineraryLogic();
this.storage = storage;
}

public Storage getStorage() {
return storage;
this.achievementsLogic = new AchievementsLogicManager(new Supplier<StatisticsModel>() {
@Override
public StatisticsModel get() {
return new StatisticsModelManager(addressBookLogic.getStatistics(),
calendarLogic.getStatistics(),
diaryLogic.getStatistics(),
financialTrackerLogic.getStatistics(),
itineraryLogic.getStatistics());
}
});
}

@Override
Expand Down Expand Up @@ -95,11 +85,11 @@ public MainLogic getMainLogic() {

@Override
public GuiSettings getGuiSettings() {
return userPrefsModel.getGuiSettings();
return userPrefs.getGuiSettings();
}

@Override
public void setGuiSettings(GuiSettings guiSettings) {
userPrefsModel.setGuiSettings(guiSettings);
userPrefs.setGuiSettings(guiSettings);
}
}
10 changes: 4 additions & 6 deletions src/main/java/seedu/address/logic/MainLogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.MainParser;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.UserPrefs;
import seedu.address.model.UserPrefsModel;
import seedu.address.storage.UserPrefsStorage;
import seedu.address.model.UserPrefsModelManager;

/**
* Logic Manager for the main page.
Expand All @@ -21,13 +22,10 @@ public class MainLogicManager implements MainLogic {

private final UserPrefsModel userPrefsModel;

private final UserPrefsStorage userPrefsStorage;

private final MainParser mainParser;

public MainLogicManager(UserPrefsModel userPrefsModel, UserPrefsStorage userPrefsStorage) {
this.userPrefsModel = userPrefsModel;
this.userPrefsStorage = userPrefsStorage;
public MainLogicManager(UserPrefs userPrefs) {
this.userPrefsModel = new UserPrefsModelManager(userPrefs);
this.mainParser = new MainParser();
}

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/seedu/address/model/UserPrefsModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import static java.util.Objects.requireNonNull;

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

import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.exceptions.DataConversionException;
import seedu.address.commons.util.StringUtil;
import seedu.address.storage.JsonUserPrefsStorage;
import seedu.address.storage.UserPrefsStorage;

/**
* Represents the in-memory addressBookModel of the address book data.
Expand All @@ -15,6 +22,8 @@ public class UserPrefsModelManager implements UserPrefsModel {

private UserPrefs userPrefs;

private UserPrefsStorage userPrefsStorage;

public UserPrefsModelManager() {
this.userPrefs = new UserPrefs();
}
Expand All @@ -23,6 +32,38 @@ public UserPrefsModelManager(ReadOnlyUserPrefs userPrefs) {
this.userPrefs = new UserPrefs(userPrefs);
}

public UserPrefsModelManager(Path userPrefsFilePath) {
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(userPrefsFilePath);
this.userPrefs = initPrefs(userPrefsStorage);
}

protected UserPrefs initPrefs(UserPrefsStorage storage) {
Path prefsFilePath = storage.getUserPrefsFilePath();
logger.info("Using prefs file : " + prefsFilePath);

UserPrefs initializedPrefs;
try {
Optional<UserPrefs> prefsOptional = storage.readUserPrefs();
initializedPrefs = prefsOptional.orElse(new UserPrefs());
} catch (DataConversionException e) {
logger.warning("UserPrefs file at " + prefsFilePath + " is not in the correct format. "
+ "Using default user prefs");
initializedPrefs = new UserPrefs();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
initializedPrefs = new UserPrefs();
}

//Update prefs file in case it was missing to begin with or there are new/unused fields
try {
storage.saveUserPrefs(initializedPrefs);
} catch (IOException e) {
logger.warning("Failed to save config file : " + StringUtil.getDetails(e));
}

return initializedPrefs;
}

@Override
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) {
requireNonNull(userPrefs);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/seedu/address/ui/PageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import javafx.stage.Stage;
import javafx.util.Duration;
import seedu.address.commons.core.GuiSettings;
import seedu.address.logic.Logic;
import seedu.address.logic.GuiSettingsLogic;

/**
* A singleton task that handles all UI for page navigation. It must be initialised using {@code getInstance} before
Expand All @@ -23,10 +23,10 @@ public class PageManager {
private static List<Page> pages;
private static Scene commonScene;
private static Stage primaryStage;
private static Logic guiSettingsLogic;
private static GuiSettingsLogic guiSettingsLogic;
private static Optional<PageManager> pageManager = Optional.empty();
// prevent multiple instances of pages
private PageManager(Stage primaryStage, Scene commonScene, Logic guiSettingsLogic, Page... pages) {
private PageManager(Stage primaryStage, Scene commonScene, GuiSettingsLogic guiSettingsLogic, Page... pages) {
this.primaryStage = primaryStage;
this.commonScene = commonScene;
this.guiSettingsLogic = guiSettingsLogic;
Expand All @@ -35,7 +35,7 @@ private PageManager(Stage primaryStage, Scene commonScene, Logic guiSettingsLogi
}

public static PageManager getInstance(Stage primaryStage, Scene commonScene,
Logic guiSettingsLogic, Page... pages) {
GuiSettingsLogic guiSettingsLogic, Page... pages) {
pageManager = Optional.of(new PageManager(primaryStage, commonScene, guiSettingsLogic, pages));
return pageManager.get();
}
Expand Down
Loading

0 comments on commit e36c26d

Please sign in to comment.