Skip to content

Commit

Permalink
#89, #90
Browse files Browse the repository at this point in the history
  • Loading branch information
dt0602 committed Dec 5, 2023
1 parent 3c9a664 commit 49a1a0d
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 14 deletions.
23 changes: 19 additions & 4 deletions src/main/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javafx.stage.Stage;

import client.view.AppFrame;
import client.view.ServerUnavailable;
import client.model.Model;
import client.controller.Controller;

Expand All @@ -19,9 +20,23 @@ public void start(Stage primaryStage) {
Model model = new Model();
Controller controller = new Controller(root, model);

primaryStage.setResizable(true);
primaryStage.setScene(new Scene(root, 500, 600));
primaryStage.setTitle("Recipe Management App");
primaryStage.show();
boolean isServerOnline = controller.checkServerStatus();

if (isServerOnline) {
primaryStage.setResizable(true);
primaryStage.setScene(new Scene(root, 500, 600));
primaryStage.setTitle("Recipe Management App");
primaryStage.show();
} else {
// Server is offline, show the "Server Unavailable" window
ServerUnavailable serverUnavailable = root.getServerUnavailable();
serverUnavailable.show();
}


// primaryStage.setResizable(true);
// primaryStage.setScene(new Scene(root, 500, 600));
// primaryStage.setTitle("Recipe Management App");
// primaryStage.show();
}
}
22 changes: 13 additions & 9 deletions src/main/java/client/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,7 @@ private void handleStopRecordingButton(ActionEvent event) {
recipePopup.getRecipe().getName().setText(instructions[0]);
recipePopup.getRecipe().getIngredient().setText(instructions[1]);
recipePopup.getRecipe().getInstruction().setText(instructions[2]);
<<<<<<< HEAD
String url = model.generateImage(recipePopup.getRecipe().getName().getText());
=======
String url = Model.generateImage(recipePopup.getRecipe().getName().getText());
// String url = Model.generateImage(instructions[0]);
>>>>>>> 8617b98b7de408ff2d16c8df8faceb7f26999c46
recipePopup.getRecipe().getImageURL().setText(url);
recipePopup.mealTypeSet = false;

Expand Down Expand Up @@ -224,14 +219,11 @@ public void handleSaveButton(ActionEvent event) {
detailsPopup.getRecipe().getName().setText(detailsPopup.getName().getText());
detailsPopup.getRecipe().getIngredient().setText(detailsPopup.getIngredients().getText());
detailsPopup.getRecipe().getInstruction().setText(detailsPopup.getInstruction().getText());
<<<<<<< HEAD
String id = loginPopup.getId();
model.sendRecipe("update", id, detailsPopup.getRecipe());
=======
// String url = Model.generateImage(recipePopup.getRecipe().getName().getText());
// detailsPopup.getRecipe().getImageURL().setText(url);
model.sendRecipe(detailsPopup.getRecipe());
>>>>>>> 8617b98b7de408ff2d16c8df8faceb7f26999c46

detailsPopup.close();
}

Expand All @@ -241,4 +233,16 @@ public void handleDeleteButton(ActionEvent event) {
model.sendRecipe("delete", id, detailsPopup.getRecipe());
detailsPopup.close();
}

public boolean checkServerStatus() {
boolean checker = Model.isServerOnline();
if (!checker) {
loginPopup.close();
}
return Model.isServerOnline();
}

public AppFrame getAppFrame() {
return appFrame;
}
}
19 changes: 18 additions & 1 deletion src/main/java/client/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import org.bson.types.ObjectId;
import java.net.Socket;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.time.Duration;
import java.net.URI;

Expand All @@ -46,8 +49,10 @@

public class Model {

private boolean isServerOnline = false;

public String sendAccount(String method, String username, String password, String autoLogin) {
String url = "http://localhost:8100/accounts/" + method;
String url = "http://localhost:8100/api/accounts" + method;

try {
HttpClient client = HttpClient.newHttpClient();
Expand Down Expand Up @@ -202,6 +207,18 @@ public String generateImage(String prompt) throws IOException, InterruptedExcept

}

public static boolean isServerOnline() {
String urlString = "http://localhost:8100/api/online";

try (Socket socket = new Socket()) {
URL url = new URL(urlString);
socket.connect(new InetSocketAddress(url.getHost(), url.getPort()), 2000);
return true;
}
catch (IOException e){
e.printStackTrace();
return false;
}

}
}
10 changes: 10 additions & 0 deletions src/main/java/client/view/AppFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import javafx.scene.Scene;
import javafx.stage.Stage;
import server.ChatGPT;
import server.Server;
import server.Whisper;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
Expand Down Expand Up @@ -103,6 +104,7 @@ public class AppFrame extends BorderPane {
private AccountPopup accountPopup;
private DetailsPopup detailsPopup;
private LoginPopup loginPopup;
private ServerUnavailable serverUnavailable;

private Button createButton;
private Button createAccountButton;
Expand All @@ -125,6 +127,7 @@ public AppFrame() {
detailsPopup = new DetailsPopup();
accountPopup = new AccountPopup();
loginPopup = new LoginPopup();
serverUnavailable = new ServerUnavailable();

ScrollPane scroll = new ScrollPane(recipeList);
scroll.setFitToWidth(true);
Expand Down Expand Up @@ -203,6 +206,13 @@ public LoginPopup getLoginPopup() {
return loginPopup;
}

public ServerUnavailable getServerUnavailable() {
if (serverUnavailable == null){
serverUnavailable = new ServerUnavailable();
}
return serverUnavailable;
}

public RecipeList getRecipeList() {
return recipeList;
}
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/client/view/ServerUnavailable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package client.view;

import client.controller.Controller;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import javax.swing.*;
import java.awt.*;

import com.sun.net.httpserver.HttpExchange;

public class ServerUnavailable extends Stage{
private Controller controller;
VBox layout;
private TextField error;
private TextField server;
private boolean serverOnline = false;

public ServerUnavailable() {

setTitle("Server Unavailable");
setWidth(525);
setHeight(650);

this.error = new TextField("ERROR");
error.setStyle("-fx-background-color: #93c994;-fx-alignment: center; -fx-text-fill: red; -fx-font-weight: bold; -fx-font-size: 80; -fx-font-family: 'Lucida Bright';");
error.setVisible(true);
error.setEditable(false);


this.server = new TextField("Server is Unavailable");
this.server.setStyle("-fx-background-color: #93c994;-fx-alignment: center; -fx-font-weight: bold; -fx-font-size: 40; -fx-font-family: 'Lucida Bright';");
server.setVisible(true);
server.setEditable(false);

layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-background-color: #93c994;");
layout.getChildren().addAll(error, server);

Scene scene = new Scene(layout, 400, 500);
setScene(scene);

this.hide();

}

public boolean isOnline() {
serverOnline = controller.checkServerStatus();
return serverOnline;
}

public void displayIfOnline() {
if (!serverOnline) {
this.show();
}
else {
this.hide();
}
}
}

0 comments on commit 49a1a0d

Please sign in to comment.