Skip to content

Commit

Permalink
Merge pull request #277 from AY1920S1-CS2103-T16-3/jon/remove-path-pref
Browse files Browse the repository at this point in the history
Replace data paths with folder in prefs
  • Loading branch information
jonchan51 committed Nov 11, 2019
2 parents cc741ff + b888cd4 commit 6b15729
Show file tree
Hide file tree
Showing 33 changed files with 225 additions and 314 deletions.
2 changes: 1 addition & 1 deletion docs/UserGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ manage time properly. It will be under statistics tab, accessed via command `tab
== FAQ

*Q*: How do I transfer my data to another Computer? +
*A*: Download the jar in the other computer and copy the entire data folder over.
*A*: Download the jar in the other computer and copy the entire data folder over to the same directory. Run Weme and update the preferences.json if necessary.

== Command Summary

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/weme/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void init() throws Exception {

UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
WemeStorage wemeStorage = new JsonWemeStorage(userPrefs.getDataFilePath());
WemeStorage wemeStorage = new JsonWemeStorage(userPrefs.getDataFolderPath());
storage = new StorageManager(wemeStorage, userPrefsStorage);

initLogging(config);
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/seedu/weme/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ public interface Model {
Path getDataFilePath();

/**
* Sets the user prefs' data file path.
* Returns the user prefs' data folder path.
*/
void setDataFilePath(Path dataFilePath);
Path getDataFolderPath();

/**
* Sets the user prefs' data folder path.
*/
void setDataFolderPath(Path dataFolderPath);

/**
* Returns the user prefs' meme image path.
Expand All @@ -90,21 +95,11 @@ public interface Model {
*/
void setViewableMeme(Meme meme);

/**
* Sets the user prefs' meme image path.
*/
void setMemeImagePath(Path memeImagePath);

/**
* Returns the user prefs' template image path.
*/
Path getTemplateImagePath();

/**
* Sets the user prefs' template image path.
*/
void setTemplateImagePath(Path templateImagePath);

/**
* Replaces Weme data with the data in {@code weme}.
*/
Expand Down
22 changes: 7 additions & 15 deletions src/main/java/seedu/weme/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,13 @@ public Path getDefaultExportPath() {
}

@Override
public void setDataFilePath(Path dataFilePath) {
requireNonNull(dataFilePath);
userPrefs.setDataFilePath(dataFilePath);
public Path getDataFolderPath() {
return userPrefs.getDataFolderPath();
}
@Override
public void setDataFolderPath(Path dataFolderPath) {
requireNonNull(dataFolderPath);
userPrefs.setDataFolderPath(dataFolderPath);
}

@Override
Expand All @@ -160,23 +164,11 @@ public void setViewableMeme(Meme meme) {
versionedWeme.setViewableMeme(meme);
}

@Override
public void setMemeImagePath(Path memeImagePath) {
requireNonNull(memeImagePath);
userPrefs.setMemeImagePath(memeImagePath);
}

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

@Override
public void setTemplateImagePath(Path templateImagePath) {
requireNonNull(templateImagePath);
userPrefs.setTemplateImagePath(templateImagePath);
}

//=========== Weme ================================================================================

@Override
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/weme/model/ReadOnlyUserPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface ReadOnlyUserPrefs {

GuiSettings getGuiSettings();

Path getDataFolderPath();

Path getDataFilePath();

Path getMemeImagePath();
Expand Down
64 changes: 26 additions & 38 deletions src/main/java/seedu/weme/model/UserPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
public class UserPrefs implements ReadOnlyUserPrefs {

public static final String EXPORT_PATH_KEY = "exportPath";
public static final String DATA_FILE_PATH_KEY = "dataFilePath";
public static final String MEME_IMAGE_PATH_KEY = "memeImagePath";
public static final String TEMPLATE_IMAGE_PATH_KEY = "templateImagePath";
public static final String DATA_FOLDER_PATH_KEY = "dataFolderPath";
public static final String MEME_IMAGE_DIRECTORY_NAME = "memes";
public static final String TEMPLATE_IMAGE_DIRECTORY_NAME = "templates";
public static final String DATA_FILE_NAME = "weme.json";

private GuiSettings guiSettings = new GuiSettings();
private Path dataFilePath = Paths.get("data" , "weme.json");
private Path memeImagePath = Paths.get("data", "memes");
private Path templateImagePath = Paths.get("data", "templates");
private Path dataFolderPath = Paths.get("data");
private Path exportFilePath = Paths.get(System.getProperty("user.home"));

/**
Expand All @@ -46,9 +45,7 @@ public UserPrefs(ReadOnlyUserPrefs userPrefs) {
public void resetData(ReadOnlyUserPrefs newUserPrefs) {
requireNonNull(newUserPrefs);
setGuiSettings(newUserPrefs.getGuiSettings());
setDataFilePath(newUserPrefs.getDataFilePath());
setMemeImagePath(newUserPrefs.getMemeImagePath());
setTemplateImagePath(newUserPrefs.getTemplateImagePath());
setDataFolderPath(newUserPrefs.getDataFolderPath());
setExportPath(newUserPrefs.getExportPath());
}

Expand All @@ -61,13 +58,19 @@ public void setGuiSettings(GuiSettings guiSettings) {
this.guiSettings = guiSettings;
}

public Path getDataFilePath() {
return dataFilePath;
@Override
public Path getDataFolderPath() {
return dataFolderPath;
}

public void setDataFilePath(Path dataFilePath) {
requireNonNull(dataFilePath);
this.dataFilePath = dataFilePath;
public void setDataFolderPath(Path dataFolderPath) {
requireNonNull(dataFolderPath);
this.dataFolderPath = dataFolderPath;
}

@Override
public Path getDataFilePath() {
return dataFolderPath.resolve(DATA_FILE_NAME);
}

public Path getDefaultExportPath() {
Expand All @@ -76,32 +79,21 @@ public Path getDefaultExportPath() {

@Override
public Path getMemeImagePath() {
return memeImagePath;
return dataFolderPath.resolve(MEME_IMAGE_DIRECTORY_NAME);
}

public void setMemeImagePath(Path memeImagePath) {
requireNonNull(memeImagePath);
this.memeImagePath = memeImagePath;
@Override
public Path getTemplateImagePath() {
return dataFolderPath.resolve(TEMPLATE_IMAGE_DIRECTORY_NAME);
}

public ObservableMap<String, String> getObservableUserPreferences() {
ObservableMap<String, String> observablePreferences = FXCollections.observableHashMap();
observablePreferences.put(EXPORT_PATH_KEY, exportFilePath.toString());
observablePreferences.put(DATA_FILE_PATH_KEY, dataFilePath.toString());
observablePreferences.put(MEME_IMAGE_PATH_KEY, memeImagePath.toString());
observablePreferences.put(TEMPLATE_IMAGE_PATH_KEY, templateImagePath.toString());
observablePreferences.put(DATA_FOLDER_PATH_KEY, dataFolderPath.toString());
return observablePreferences;
}

@Override
public Path getTemplateImagePath() {
return templateImagePath;
}

public void setTemplateImagePath(Path templateImagePath) {
requireNonNull(templateImagePath);
this.templateImagePath = templateImagePath;
}

public void setExportPath(Path exportFilePath) {
requireNonNull(exportFilePath);
Expand All @@ -125,24 +117,20 @@ public boolean equals(Object other) {
UserPrefs o = (UserPrefs) other;

return guiSettings.equals(o.guiSettings)
&& dataFilePath.equals(o.dataFilePath)
&& memeImagePath.equals(o.memeImagePath)
&& exportFilePath.equals(o.exportFilePath)
&& templateImagePath.equals(o.templateImagePath);
&& dataFolderPath.equals(o.dataFolderPath)
&& exportFilePath.equals(o.exportFilePath);
}

@Override
public int hashCode() {
return Objects.hash(guiSettings, dataFilePath, memeImagePath, templateImagePath, exportFilePath);
return Objects.hash(guiSettings, dataFolderPath, exportFilePath);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Gui Settings : " + guiSettings);
sb.append("\nLocal data file location : " + dataFilePath);
sb.append("\nMemes image location : " + memeImagePath);
sb.append("\nTemplate image location : " + templateImagePath);
sb.append("\nLocal data folder location : " + dataFolderPath);
sb.append("\nExport Path directory : " + exportFilePath);
return sb.toString();
}
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/seedu/weme/storage/JsonAdaptedMeme.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package seedu.weme.storage;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -45,9 +46,11 @@ public JsonAdaptedMeme(@JsonProperty("filePath") String filePath, @JsonProperty(

/**
* Converts a given {@code Meme} into this class for Jackson use.
*
* @param folderPath the path to the folder the meme images are saved in.
*/
public JsonAdaptedMeme(Meme source) {
filePath = source.getImagePath().toString();
public JsonAdaptedMeme(Meme source, Path folderPath) {
filePath = folderPath.relativize(source.getImagePath().getFilePath()).toString(); // store only filename
description = source.getDescription().value;
tagged.addAll(source.getTags().stream()
.map(JsonAdaptedTag::new)
Expand All @@ -58,9 +61,10 @@ public JsonAdaptedMeme(Meme source) {
/**
* Converts this Json-friendly adapted meme object into the model's {@code Meme} object.
*
* @param folderPath the path to the folder the meme images are saved in.
* @throws IllegalValueException if there were any data constraints violated in the adapted meme.
*/
public Meme toModelType() throws IllegalValueException {
public Meme toModelType(Path folderPath) throws IllegalValueException {
final List<Tag> memeTags = new ArrayList<>();
for (JsonAdaptedTag tag : tagged) {
memeTags.add(tag.toModelType());
Expand All @@ -70,10 +74,11 @@ public Meme toModelType() throws IllegalValueException {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT,
ImagePath.class.getSimpleName()));
}
if (!ImagePath.isValidFilePath(filePath)) {
String imagePathString = folderPath.resolve(filePath).toString(); // convert to be usable by Weme
if (!ImagePath.isValidFilePath(imagePathString)) {
throw new IllegalValueException(ImagePath.MESSAGE_CONSTRAINTS);
}
final ImagePath modelUrl = new ImagePath(filePath);
final ImagePath modelUrl = new ImagePath(imagePathString);

if (description == null) {
throw new IllegalValueException(String.format(
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/seedu/weme/storage/JsonAdaptedTemplate.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.weme.storage;

import java.nio.file.Path;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

Expand Down Expand Up @@ -32,19 +34,21 @@ public JsonAdaptedTemplate(@JsonProperty("name") String name, @JsonProperty("fil

/**
* Converts a given {@code Template} into this class for Jackson use.
* @param folderPath the path to the folder the template images are saved in.
*/
public JsonAdaptedTemplate(Template source) {
public JsonAdaptedTemplate(Template source, Path folderPath) {
name = source.getName().toString();
filePath = source.getImagePath().toString();
filePath = folderPath.relativize(source.getImagePath().getFilePath()).toString(); // store only filename
isArchived = source.isArchived();
}

/**
* Converts this Jackson-friendly adapted template object into the model's {@code Template} object.
*
* @param folderPath the path to the folder the template images are saved in.
* @throws IllegalValueException if there were any data constraints violated in the adapted template.
*/
public Template toModelType() throws IllegalValueException {
public Template toModelType(Path folderPath) throws IllegalValueException {
if (name == null) {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT, Name.class.getSimpleName()));
}
Expand All @@ -57,10 +61,11 @@ public Template toModelType() throws IllegalValueException {
throw new IllegalValueException(String.format(MISSING_FIELD_MESSAGE_FORMAT,
ImagePath.class.getSimpleName()));
}
if (!ImagePath.isValidFilePath(filePath)) {
String imagePathString = folderPath.resolve(filePath).toString(); // convert to be usable by Weme
if (!ImagePath.isValidFilePath(imagePathString)) {
throw new IllegalValueException(ImagePath.MESSAGE_CONSTRAINTS);
}
final ImagePath modelFilePath = new ImagePath(filePath);
final ImagePath modelFilePath = new ImagePath(imagePathString);

return new Template(modelName, modelFilePath, isArchived);
}
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/seedu/weme/storage/JsonSerializableStats.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package seedu.weme.storage;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -38,25 +40,31 @@ public JsonSerializableStats(@JsonProperty("likeMap") Map<String, Integer> likeD
/**
* Converts a given {@code ReadOnlyWeme} into this class for Jackson use.
*
* @param folderPath the path to the folder the meme images are saved in.
* @param source future changes to this will not affect the created {@code JsonSerializableWeme}.
*/
public JsonSerializableStats(Stats source) {
public JsonSerializableStats(Stats source, Path folderPath) {
Map<String, SimpleIntegerProperty> likeData = source.getObservableLikeData();
Map<String, SimpleIntegerProperty> dislikeData = source.getObservableDislikeData();
for (Map.Entry<String, SimpleIntegerProperty> entry : likeData.entrySet()) {
likeMap.put(entry.getKey(), entry.getValue().get());
Path imagePath = Paths.get(entry.getKey());
String entryString = folderPath.relativize(imagePath).toString();
likeMap.put(entryString, entry.getValue().get());
}
for (Map.Entry<String, SimpleIntegerProperty> entry : dislikeData.entrySet()) {
dislikeMap.put(entry.getKey(), entry.getValue().get());
Path imagePath = Paths.get(entry.getKey());
String entryString = folderPath.relativize(imagePath).toString();
dislikeMap.put(entryString, entry.getValue().get());
}
}

/**
* Converts this stats into the model's {@code Stats} object.
*
* @param folderPath the path to the folder the meme images are saved in.
* @throws IllegalValueException if there were any data constraints violated.
*/
public Stats toModelType() throws IllegalValueException {
public Stats toModelType(Path folderPath) throws IllegalValueException {
Stats stats = new StatsManager();
Map<String, SimpleIntegerProperty> likeData = new HashMap<>();
Map<String, SimpleIntegerProperty> dislikeData = new HashMap<>();
Expand All @@ -66,15 +74,17 @@ public Stats toModelType() throws IllegalValueException {
} else if (likeData.containsKey(entry.getKey())) {
throw new IllegalValueException(MESSAGE_DUPLICATE_LIKE);
}
likeData.put(entry.getKey(), new SimpleIntegerProperty(entry.getValue()));
String imagePathString = folderPath.resolve(entry.getKey()).toString();
likeData.put(imagePathString, new SimpleIntegerProperty(entry.getValue()));
}
for (Map.Entry<String, Integer> entry : dislikeMap.entrySet()) {
if (entry.getValue() < 0) {
throw new IllegalValueException(MESSAGE_INVALID_DISLIKE);
} else if (dislikeData.containsKey(entry.getKey())) {
throw new IllegalValueException(MESSAGE_DUPLICATE_DISLIKE);
}
dislikeData.put(entry.getKey(), new SimpleIntegerProperty(entry.getValue()));
String imagePathString = folderPath.resolve(entry.getKey()).toString();
dislikeData.put(imagePathString, new SimpleIntegerProperty(entry.getValue()));
}
stats.setLikeData(likeData);
stats.setDislikeData(dislikeData);
Expand Down
Loading

0 comments on commit 6b15729

Please sign in to comment.