Skip to content
This repository has been archived by the owner on Mar 2, 2019. It is now read-only.

Commit

Permalink
Move initiation into UserPrefs
Browse files Browse the repository at this point in the history
  • Loading branch information
li-kai committed Nov 7, 2016
1 parent ffdb2ec commit 2f113a1
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 55 deletions.
41 changes: 2 additions & 39 deletions src/main/java/seedu/todo/MainApp.java
Expand Up @@ -21,8 +21,6 @@
import seedu.todo.model.Model;
import seedu.todo.model.TodoModel;
import seedu.todo.model.UserPrefs;
import seedu.todo.storage.FixedStorage;
import seedu.todo.storage.UserPrefsStorage;
import seedu.todo.ui.Ui;
import seedu.todo.ui.UiManager;

Expand All @@ -41,7 +39,6 @@ public class MainApp extends Application {

protected Ui ui;
protected Logic logic;
protected FixedStorage<UserPrefs> storage;
protected Model model;
protected Dispatcher dispatcher;
protected Parser parser;
Expand All @@ -56,9 +53,7 @@ public void init() throws Exception {
super.init();

config = initConfig(getApplicationParameter("config"));
storage = new UserPrefsStorage(config.getUserPrefsFilePath());

userPrefs = initPrefs(config);
userPrefs = new UserPrefs(config);

initLogging(config);

Expand Down Expand Up @@ -114,34 +109,6 @@ protected Config initConfig(String configFilePath) {
return initializedConfig;
}

protected UserPrefs initPrefs(Config config) {
assert config != null;

String prefsFilePath = config.getUserPrefsFilePath();
logger.info("Using prefs file : " + prefsFilePath);

UserPrefs initializedPrefs;
try {
initializedPrefs = storage.read();
} 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 Todo-List");
initializedPrefs = new UserPrefs();
}

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

return initializedPrefs;
}

private void initEventsCenter() {
EventsCenter.getInstance().registerHandler(this);
}
Expand All @@ -156,11 +123,7 @@ public void start(Stage primaryStage) {
public void stop() {
logger.info("============================ [ Stopping Uncle Jim's Discount To-do List ] =============================");
ui.stop();
try {
storage.save(userPrefs);
} catch (IOException e) {
logger.severe("Failed to save preferences " + StringUtil.getDetails(e));
}

Platform.exit();
System.exit(0);
}
Expand Down
53 changes: 49 additions & 4 deletions src/main/java/seedu/todo/model/UserPrefs.java
@@ -1,15 +1,26 @@
package seedu.todo.model;

import java.util.Objects;

import seedu.todo.commons.core.Config;
import seedu.todo.commons.core.GuiSettings;
import seedu.todo.commons.core.LogsCenter;
import seedu.todo.commons.exceptions.DataConversionException;
import seedu.todo.commons.util.StringUtil;
import seedu.todo.storage.FixedStorage;
import seedu.todo.storage.UserPrefsStorage;

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

//@@author A0139021U-reused
/**
* Represents User's preferences.
*/
public class UserPrefs {

public GuiSettings guiSettings;
private GuiSettings guiSettings;
private static final Logger logger = LogsCenter.getLogger(UserPrefs.class);
private FixedStorage<UserPrefs> storage;

public GuiSettings getGuiSettings() {
return guiSettings == null ? new GuiSettings() : guiSettings;
Expand All @@ -20,13 +31,47 @@ public void updateLastUsedGuiSetting(GuiSettings guiSettings) {
}

public UserPrefs(){
this.setGuiSettings(500, 500, 0, 0);
this.setDefaultGuiSettings();
}

public UserPrefs(Config config) {
assert config != null;

String prefsFilePath = config.getUserPrefsFilePath();
logger.info("Using prefs file : " + prefsFilePath);

this.storage = new UserPrefsStorage(prefsFilePath);
try {
this.updateLastUsedGuiSetting(storage.read().getGuiSettings());
} catch (DataConversionException e) {
logger.warning("UserPrefs file at " + prefsFilePath + " is not in the correct format. " +
"Using default user prefs");
this.setDefaultGuiSettings();
} catch (IOException e) {
logger.warning("Problem while reading from the file. . Will be starting with an empty Todo-List");
this.setDefaultGuiSettings();
}

//Update prefs file in case it was missing to begin with or there are new/unused fields
this.save();
}

private void setDefaultGuiSettings() {
this.setGuiSettings(680, 780, 0, 0);
}

public void setGuiSettings(double width, double height, int x, int y) {
guiSettings = new GuiSettings(width, height, x, y);
}

public void save() {
try {
storage.save(this);
} catch (IOException e) {
logger.severe("Failed to save preferences " + StringUtil.getDetails(e));
}
}

@Override
public boolean equals(Object other) {
if (other == this){
Expand Down
12 changes: 0 additions & 12 deletions src/test/java/seedu/todo/TestApp.java
@@ -1,11 +1,8 @@
package seedu.todo;

import javafx.stage.Screen;
import javafx.stage.Stage;
import seedu.todo.commons.core.Config;
import seedu.todo.commons.core.GuiSettings;
import seedu.todo.model.ImmutableTodoList;
import seedu.todo.model.UserPrefs;
import seedu.todo.storage.XmlSerializableTodoList;
import seedu.todo.testutil.TestUtil;

Expand Down Expand Up @@ -61,15 +58,6 @@ protected Config initConfig(String configFilePath) {
return config;
}

@Override
protected UserPrefs initPrefs(Config config) {
UserPrefs userPrefs = super.initPrefs(config);
double x = Screen.getPrimary().getVisualBounds().getMinX();
double y = Screen.getPrimary().getVisualBounds().getMinY();
userPrefs.updateLastUsedGuiSetting(new GuiSettings(600.0, 600.0, (int) x, (int) y));
return userPrefs;
}

@Override
public void start(Stage primaryStage) {
ui.start(primaryStage);
Expand Down

0 comments on commit 2f113a1

Please sign in to comment.