Skip to content

Commit

Permalink
SettingsController refactoring, added additional check
Browse files Browse the repository at this point in the history
  • Loading branch information
artsiom-panko committed Jun 22, 2023
1 parent 679990a commit ade16a3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
25 changes: 15 additions & 10 deletions src/main/java/com/panko/apod/controller/SettingsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import javafx.scene.control.Hyperlink;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.awt.*;
import java.io.IOException;
Expand All @@ -29,11 +27,10 @@
public class SettingsController implements Initializable, SceneController {

@FXML
private TextField newApiKey;
private TextField apiKeyField;
@FXML
private ToggleGroup languageGroup;

private Stage primaryStage;
private SceneService sceneService;

private final PreferencesManager preferencesManager = new PreferencesManager();
Expand All @@ -43,7 +40,7 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
String key = preferencesManager.readKey(NASA_API_KEY);

if (key != null && !key.isEmpty()) {
newApiKey.setText(key);
apiKeyField.setText(key);
}
}

Expand All @@ -56,11 +53,10 @@ private void openHyperlink(ActionEvent event) throws URISyntaxException, IOExcep
private void saveSettings() {
Path applicationAbsolutePath = FileSystems.getDefault().getPath("").toAbsolutePath();
String picturesPath = applicationAbsolutePath.toString().concat("\\pictures\\");
String enteredApiKey = newApiKey.getText();

if (!enteredApiKey.isBlank() && isApiKeyValid(enteredApiKey)) {
if (isEnteredApiKeyValid(apiKeyField)) {
preferencesManager.saveKey(PICTURES_FOLDER, picturesPath);
preferencesManager.saveKey(NASA_API_KEY, newApiKey.getText());
preferencesManager.saveKey(NASA_API_KEY, apiKeyField.getText());
// preferencesManager.saveKey(LANGUAGE, ((RadioButton) languageGroup.getSelectedToggle()).getText());

sceneService.launchMainThread();
Expand All @@ -77,9 +73,18 @@ public void setSceneService(SceneService sceneService) {
this.sceneService = sceneService;
}

private boolean isApiKeyValid(String apiKey) {
private boolean isEnteredApiKeyValid(TextField apiKeyToCheck) {
if (apiKeyToCheck == null) {
return false;
}

String enteredApiKeyValue = apiKeyToCheck.getText();
if (!enteredApiKeyValue.isBlank() && !preferencesManager.readKey(NASA_API_KEY).equals(enteredApiKeyValue)) {
return false;
}

ApiService apiService = new ApiService();
HttpResponse<String> httpResponse = apiService.sendHttpRequest(apiKey);
HttpResponse<String> httpResponse = apiService.sendHttpRequest(enteredApiKeyValue);

return !httpResponse.body().contains("API_KEY_INVALID");
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/scene/settings-scene.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<Insets bottom="5.0" top="20.0" />
</VBox.margin>
</Text>
<TextField fx:id="newApiKey" promptText="API key" />
<TextField fx:id="apiKeyField" promptText="API key" />
</VBox>
<Button alignment="BOTTOM_CENTER" contentDisplay="CENTER" mnemonicParsing="false" onAction="#saveSettings" prefHeight="26.0" prefWidth="74.0" style="-fx-background-color: #5E92F9;" text="Accept" textFill="WHITE" textOverrun="CENTER_ELLIPSIS">
<font>
Expand Down

0 comments on commit ade16a3

Please sign in to comment.