Skip to content

Commit

Permalink
Some code refactoring, fixed wallpaper updating
Browse files Browse the repository at this point in the history
  • Loading branch information
artsiom-panko committed May 12, 2023
1 parent e2c4a5c commit 03a0dfc
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/panko/apod/MainApplication.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.panko.apod;

import com.panko.apod.controller.RootController;
import com.panko.apod.controller.MainController;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
Expand All @@ -22,14 +22,14 @@ public void start(Stage primaryStage) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/scene/root-scene.fxml"));
Parent root = loader.load();

RootController rootController = loader.getController();
rootController.setStage(primaryStage);
MainController mainController = loader.getController();
mainController.setStage(primaryStage);

primaryStage.setTitle("Astronomy picture of the day");
primaryStage.getIcons().add(new Image(Objects.requireNonNull(getClass().getResourceAsStream("/img/logo.png"))));
primaryStage.setScene(new Scene(root));
primaryStage.show();

rootController.process();
mainController.launchMainThread();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import javafx.scene.layout.Region;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//import org.json.JSONObject;

import java.io.IOException;
import java.net.http.HttpResponse;
Expand All @@ -30,7 +29,7 @@
import static com.panko.apod.service.MainService.NASA_API_KEY;
import static com.panko.apod.util.PreferencesManager.NUMBER_OF_ROCKET_LAUNCHES;

public class RootController {
public class MainController {

private Stage primaryStage;

Expand All @@ -47,13 +46,13 @@ public class RootController {
private final PreferencesManager preferencesManager = new PreferencesManager();
private final HttpResponseHandlerService httpResponseHandlerService = new HttpResponseHandlerService();

private static final System.Logger logger = System.getLogger(RootController.class.getName());
private static final System.Logger logger = System.getLogger(MainController.class.getName());

public void setStage(Stage stage) {
this.primaryStage = stage;
}

public void process() {
public void launchMainThread() {
String apiKey = preferencesManager.readKey(NASA_API_KEY);

if (apiKey == null || apiKey.isBlank()) {
Expand All @@ -76,7 +75,7 @@ public void process() {
new Thread(() -> {
HttpResponse<String> httpResponse = apiService.sendHttpRequest(apiKey);
if (httpResponse != null && httpResponse.statusCode() == 200) {
Picture picture = httpResponseHandlerService.handleResponse(httpResponse);
Picture picture = httpResponseHandlerService.parseHttpResponseToPicture(httpResponse);
if (!imageSaver.savePictureToFolder(picture)) {
Platform.runLater(() -> {
showPictureSaveAlert();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public class SettingsController implements Initializable {
private ToggleGroup languageGroup;

private Stage primaryStage;
private RootController rootController;
private MainController mainController;

private final PreferencesManager preferencesManager = new PreferencesManager();

public void setRootController(RootController rootController) {
this.rootController = rootController;
public void setRootController(MainController mainController) {
this.mainController = mainController;
}

public void setRootStage(Stage primaryStage) {
Expand All @@ -56,7 +56,7 @@ private void saveNewApiKey() {
preferencesManager.saveKey(PICTURES_FOLDER, selectedFolderDirectory.getText());
// preferencesManager.saveKey(LANGUAGE, ((RadioButton) languageGroup.getSelectedToggle()).getText());

rootController.process();
mainController.launchMainThread();
}

@FXML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
public class HttpResponseHandlerService {
private static final System.Logger logger = System.getLogger(HttpResponseHandlerService.class.getName());

public Picture handleResponse(HttpResponse<String> response) {

public Picture parseHttpResponseToPicture(HttpResponse<String> response) {
Picture picture = new Picture();

JSONObject responseBody = new JSONObject(response.body());

picture.setDate(LocalDate.parse(responseBody.getString("date")));
Expand All @@ -31,8 +29,8 @@ public Picture handleResponse(HttpResponse<String> response) {
throw new RuntimeException("Unsupported image type: " + picture.getType());
}

logger.log(System.Logger.Level.INFO, "Parsing result: {1}", picture);
logger.log(System.Logger.Level.INFO, "Parsing result: {0}", picture);

return null;
return picture;
}
}
8 changes: 4 additions & 4 deletions src/main/java/com/panko/apod/service/MainService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ public class MainService {

public static final String NASA_API_KEY = "nasa.api.key";

// public void process() {
// public void launchMainThread() {
//
// Picture picture = mainService.process();
// Picture picture = mainService.launchMainThread();
// imageDescription.setText(picture.getDescription());
//
//
// HttpResponse<String> httpResponse = apiService.sendHttpRequest(key);
// Picture picture = responseHandlerService.handleResponse(httpResponse);
// Picture picture = responseHandlerService.parseHttpResponseToPicture(httpResponse);
// imageWriterService.writePictureToFolder(picture);
// WallpaperChangerService.setScreenImage(picture);
//
Expand All @@ -31,7 +31,7 @@ public class MainService {
// public void test() {
// String key = apiKeyService.readKey(NASA_API_KEY);
// HttpResponse<String> httpResponse = apiService.sendHttpRequest(key);
// Picture picture = responseHandlerService.handleResponse(httpResponse);
// Picture picture = responseHandlerService.parseHttpResponseToPicture(httpResponse);
// imageWriterService.writePictureToFolder(picture);
// WallpaperChangerService.setScreenImage(picture);
//
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/panko/apod/util/WallpaperChanger.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ private interface User32 extends Library {
}

public static void setScreenImage(Picture picture) {

User32.INSTANCE.SystemParametersInfo(0x0014, 0, picture.getLocalPath(), 1);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/scene/bottom-pane.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<?import javafx.scene.layout.HBox?>

<HBox alignment="CENTER_RIGHT" xmlns="http://javafx.com/javafx/11" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.panko.apod.controller.RootController">
fx:controller="com.panko.apod.controller.MainController">

<Button mnemonicParsing="false" onAction="#loadKeyInputSceneFXML" opacity="0.5"
style="-fx-background-color: transparent;" text="Settings">
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/scene/root-scene.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<BorderPane fx:id="rootContainer" maxHeight="400.0" maxWidth="500.0" prefHeight="380.0" prefWidth="470.0"
xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="com.panko.apod.controller.RootController">
fx:controller="com.panko.apod.controller.MainController">
<bottom>
<HBox prefHeight="26.0" prefWidth="250.0">
<HBox alignment="CENTER_LEFT">
Expand Down

0 comments on commit 03a0dfc

Please sign in to comment.