This repository has been archived by the owner on Mar 2, 2019. It is now read-only.
forked from nus-cs2103-AY1617S1/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserPrefs.java
99 lines (79 loc) · 2.79 KB
/
UserPrefs.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package seedu.todo.model;
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 {
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;
}
public void updateLastUsedGuiSetting(GuiSettings guiSettings) {
this.guiSettings = guiSettings;
}
public UserPrefs(){
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){
return true;
}
if (!(other instanceof UserPrefs)){ //this handles null as well.
return false;
}
UserPrefs o = (UserPrefs)other;
return Objects.equals(guiSettings, o.guiSettings);
}
@Override
public int hashCode() {
return Objects.hash(guiSettings);
}
@Override
public String toString(){
return guiSettings.toString();
}
}