Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Branch improve sort command #119

Conversation

angrybunny123
Copy link
Collaborator

@angrybunny123 angrybunny123 commented Oct 23, 2020

Closes #120

Added Sorting Preference field to user preferences, and changed occurrences of addressbook.json to library.json.

Can sort by reading progress using prefix "rp/"

Using the sort command now updates user preference as well, and when the user exits and reopens bookmark, books will still be sorted in the specified order.

Edit and Add book commands will be based on sorting preference of the user.

@@ -142,9 +173,62 @@ public void updateFilteredBookList(Predicate<Book> predicate) {

@Override
public void sortFilteredBookList(Comparator<Book> comparator) {
if (comparator instanceof BookNameComparator) {
setSortingPreference(PREFIX_NAME.toString());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be doing these checks here? An approach worth thinking about could be to get SortCommandParser to create each SortCommands that know what prefix it's sorting by. Then in SortCommand#execute() we can just call setSortingPreference(PREFIX_XXX)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions Le Yang, I agree with you and I have made the necessary changes 😄

* Returns a comparator based on the input prefix
* @return Comparator based on input prefix
*/
public Comparator<Book> comparatorGenerator(Prefix inputPrefix) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noticed that this exact method is repeated inside SortCommandParser also. Can think about abstracting it out as a static utility method, maybe smth like BookComparatorGenerator#comparatorGenerator(Prefix inputPrefix)

* Returns a prefix based on the input string.
* @return Prefix based on the input string.
*/
public Prefix prefixGenerator(String prefix) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this method belongs here, maybe define it as a utility method in CliSyntax ?

@codecov-io
Copy link

codecov-io commented Oct 25, 2020

Codecov Report

Merging #119 into master will decrease coverage by 0.03%.
The diff coverage is 76.05%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #119      +/-   ##
============================================
- Coverage     74.10%   74.07%   -0.04%     
- Complexity      669      685      +16     
============================================
  Files           105      107       +2     
  Lines          2078     2133      +55     
  Branches        238      242       +4     
============================================
+ Hits           1540     1580      +40     
- Misses          455      465      +10     
- Partials         83       88       +5     
Impacted Files Coverage Δ Complexity Δ
src/main/java/seedu/bookmark/model/Model.java 100.00% <ø> (ø) 1.00 <0.00> (ø)
...in/java/seedu/bookmark/logic/parser/CliSyntax.java 58.33% <25.00%> (-33.34%) 2.00 <1.00> (+1.00) ⬇️
src/main/java/seedu/bookmark/model/UserPrefs.java 76.92% <70.00%> (-3.08%) 14.00 <3.00> (+4.00) ⬇️
...ava/seedu/bookmark/logic/commands/SortCommand.java 80.00% <71.42%> (ø) 4.00 <1.00> (ø)
...c/main/java/seedu/bookmark/model/ModelManager.java 97.89% <90.00%> (-0.93%) 35.00 <4.00> (+3.00) ⬇️
...ook/comparators/BookReadingProgressComparator.java 90.00% <90.00%> (ø) 4.00 <4.00> (?)
...rk/model/book/comparators/ComparatorGenerator.java 90.90% <90.90%> (ø) 5.00 <5.00> (?)
...ava/seedu/bookmark/logic/commands/EditCommand.java 95.77% <100.00%> (+0.18%) 14.00 <1.00> (+1.00)
...seedu/bookmark/logic/parser/SortCommandParser.java 88.00% <100.00%> (+1.33%) 7.00 <0.00> (-2.00) ⬆️
...rk/model/book/comparators/BookGenreComparator.java 83.33% <100.00%> (ø) 4.00 <1.00> (ø)
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 92d727b...5756611. Read the comment docs.

Copy link
Collaborator

@TanLeYang TanLeYang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -292,7 +292,7 @@ While the changes to code may be minimal, the test data will have to be updated

<div markdown="span" class="alert alert-warning">

:exclamation: You must delete AddressBook’s storage file located at `/data/addressbook.json` before running it! Not doing so will cause AddressBook to default to an empty address book!
:exclamation: You must delete AddressBook’s storage file located at `/data/library.json` before running it! Not doing so will cause AddressBook to default to an empty address book!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you could change AddressBook to Library for this part.

@@ -87,7 +87,7 @@ At this point, your application is working as intended and all your tests are pa

In `src/test/data/`, data meant for testing purposes are stored. While keeping the `address` field in the json files does not cause the tests to fail, it is not good practice to let cruft from old features accumulate.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you can change Address so Genre for this part!

Comment on lines +32 to +35
@Override
public String toString() {
return "Reading Progress";
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the toString() return value can be more meaningful?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use this toString() value to return "Sorted by: xxxx.toString()", so I think leaving it like this is fine

@@ -71,7 +71,7 @@ public void execute_validCommand_success() throws Exception {
public void execute_storageThrowsIoException_throwsCommandException() {
// Setup LogicManager with JsonAddressBookIoExceptionThrowingStub
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhap you could change this to JsonLibraryBook... ?

@@ -71,7 +71,7 @@ public void execute_validCommand_success() throws Exception {
public void execute_storageThrowsIoException_throwsCommandException() {
// Setup LogicManager with JsonAddressBookIoExceptionThrowingStub
JsonLibraryStorage addressBookStorage =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps you can change this to libraryStorage?

Comment on lines 42 to 44
default:
return PREFIX_DUMMY;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it will be better if we throw an exception instead of defaulting to a dummy sorting choice?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception would be thrown in SortCommandParser if prefix was invalid. But I'll change this to return null in the default case to make it less confusing.

@@ -292,7 +292,7 @@ While the changes to code may be minimal, the test data will have to be updated

<div markdown="span" class="alert alert-warning">

:exclamation: You must delete AddressBook’s storage file located at `/data/addressbook.json` before running it! Not doing so will cause AddressBook to default to an empty address book!
:exclamation: You must delete bookmark’s storage file located at `/data/library.json` before running it! Not doing so will cause AddressBook to default to an empty address book!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you miss out 2 AddressBook at the end of the sentence?

Copy link
Collaborator

@pangpuncake pangpuncake left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM! Just have a question on the use of String instead of Prefix in UserPrefs.

Comment on lines 41 to 43
public SortCommand(Comparator<Book> comparator, Prefix inputPrefix) {
this.comparator = comparator;
this.inputPrefix = inputPrefix;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps usage of requireNonNull for more defensive code.

@@ -87,7 +87,7 @@ At this point, your application is working as intended and all your tests are pa

In `src/test/data/`, data meant for testing purposes are stored. While keeping the `address` field in the json files does not cause the tests to fail, it is not good practice to let cruft from old features accumulate.

**`invalidPersonAddressBook.json`:**
**`invalidPersonLibrary.json`:**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

Suggested change
**`invalidPersonLibrary.json`:**
**`invalidBookLibrary.json`:**

@@ -15,6 +15,7 @@

private GuiSettings guiSettings = new GuiSettings();
private Path bookmarkFilePath = Paths.get("data" , "library.json");
private String sortingPreference = "";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to consider using Prefix instead of String. From what I see, you are converting from Prefix to String to set in UserPrefs. In order to get back the Prefix to create a Comparator, you have to convert the String back to a Prefix which seems unnecessary if Prefix was used instead. By using Prefix, you can remove the need for sortingPrefixGenerator and reduce the possibilities of bugs by the back and forth conversions.
Is there any additional benefit to using a String that I have missed out?

@@ -36,6 +39,7 @@ public ModelManager(ReadOnlyLibrary library, ReadOnlyUserPrefs userPrefs) {

this.library = new Library(library);
this.userPrefs = new UserPrefs(userPrefs);
this.comparator = comparatorGenerator(sortingPrefixGenerator(userPrefs.getSortingPreference()));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relating to my comment on using a Prefix instead of String in UserPrefs below, a Prefix would remove the need for sortingPrefixGenerator(). This line seems to have too much chaining.

Copy link
Collaborator

@pennhanlee pennhanlee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -180,6 +202,7 @@ public void updateFilteredBookList(Predicate<Book> predicate) {

@Override
public void sortFilteredBookList(Comparator<Book> comparator) {
this.comparator = comparator;
this.library.sortBooks(comparator);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add this code to the end of the method
historyManager = historyManager.addNewState(State.createState(library, userPrefs, filteredBooks.getPredicate()));
to make the sort command undoable?

@TanLeYang TanLeYang merged commit 811f25a into AY2021S1-CS2103T-F13-2:master Oct 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants