-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Fix for issue custom api key always saved even on cancel #11977
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One nitpick, otherwise looks good to me
src/main/java/org/jabref/gui/preferences/websearch/WebSearchTabViewModel.java
Outdated
Show resolved
Hide resolved
// Save changes from tempApiKeys back to the actual settings | ||
preferences.getImporterPreferences().getApiKeys().clear(); | ||
preferences.getImporterPreferences().getApiKeys().addAll(apiKeys); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The preferences are already saved at line 191.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Loay 'Eagleeyes' Ghreeb
// Initialize tempApiKeys with a deep copy of the actual API Keys, so changes in the UI do not directly affect the actual settings | ||
tempApiKeys.set(FXCollections.observableArrayList( | ||
preferences.getImporterPreferences().getApiKeys().stream() | ||
.map(apiKey -> new FetcherApiKey(apiKey.getName(), apiKey.shouldUse(), apiKey.getKey())) | ||
.collect(Collectors.toList()) | ||
)); | ||
// Set the UI-bound apiKeys property to tempApiKeys | ||
apiKeys.set(tempApiKeys); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do this directly on the apiKeys
list without the need for tempApiKeys
.
Also, change the apiKeys
list to an ObservableList
instead of a ListProperty
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the feedback. I have simplified the code by directly working with the apiKeys
.
I have also changed apiKeys
from a ListProperty
to an ObservableList
and deleted redundant code about saving perfermance.
I have edited and updated PR Description above. Thank you!
@@ -56,7 +56,8 @@ public class WebSearchTabViewModel implements PreferenceTabViewModel { | |||
private final BooleanProperty grobidEnabledProperty = new SimpleBooleanProperty(); | |||
private final StringProperty grobidURLProperty = new SimpleStringProperty(""); | |||
|
|||
private final ListProperty<FetcherApiKey> apiKeys = new SimpleListProperty<>(); | |||
// ObservableList to store API keys for editing in the UI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the comment
@@ -138,7 +139,11 @@ public void setValues() { | |||
grobidEnabledProperty.setValue(grobidPreferences.isGrobidEnabled()); | |||
grobidURLProperty.setValue(grobidPreferences.getGrobidURL()); | |||
|
|||
apiKeys.setValue(FXCollections.observableArrayList(preferences.getImporterPreferences().getApiKeys())); | |||
// Initialize apiKeys with a deep copy of the actual API Keys |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the comment
// Initialize apiKeys with a deep copy of the actual API Keys | ||
apiKeys.setAll(preferences.getImporterPreferences().getApiKeys().stream() | ||
.map(apiKey -> new FetcherApiKey(apiKey.getName(), apiKey.shouldUse(), apiKey.getKey())) | ||
.collect(Collectors.toList())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be simplified
.collect(Collectors.toList())); | |
.toList()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Careful, Collectors.toList() creates a new mutable array lust, .toList() an immutable one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OKay, so I reckon we'll continue using .collect(Collectors.toList())
to obtain a mutable list, since apiKeys needs to be mutable to allow modifications in the UI ?
I am a bit unsure about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we need an immutable list here.
Please test it once more to make sure that to list stuff is now done right. 😅 |
Sure, I have tested both versions, and they both work properly. Both support the normal operation of save and cancel. |
* Added temporary property to fix the issue. * Added comments. * Modified CHANGELOG.md * Rename variable and modify comments. * Use apiKeys directly and change to ObservableList * Remove comments. * Get an immutable list.
* Added temporary property to fix the issue. * Added comments. * Modified CHANGELOG.md * Rename variable and modify comments. * Use apiKeys directly and change to ObservableList * Remove comments. * Get an immutable list.
* Added temporary property to fix the issue. * Added comments. * Modified CHANGELOG.md * Rename variable and modify comments. * Use apiKeys directly and change to ObservableList * Remove comments. * Get an immutable list.
This PR fixes an issue where changes made to the "Custom API key" table in the Web Search tab of the preferences are always saved, even when the user clicks Cancel.
Fix Implemented:
Modified the
WebSearchTabViewModel
class:editableApiKeys
property.apiKeys
from aListProperty
to anObservableList
.apiKeys
with a deep copy of the preferences' API keys insetValues()
.storeSettings()
.Closes #11925
Mandatory checks
CHANGELOG.md
described in a way that is understandable for the average user (if applicable)Made changes in api key and press Cancel:
Discard now: