added a Dialog sequence for when the server is not up, which was previously just a log msg. Now the user gets to provide their own server.

This commit is contained in:
Rithvik Sriram 2026-01-21 22:48:07 +01:00
commit 110e5e8163
4 changed files with 208 additions and 9 deletions

View file

@ -2,6 +2,7 @@ package client;
import client.scenes.FoodpalApplicationCtrl;
import client.scenes.MainCtrl;
import client.scenes.ServerConnectionDialogCtrl;
import client.utils.server.ServerUtils;
import com.google.inject.Injector;
import javafx.application.Application;
@ -27,9 +28,14 @@ public class UI extends Application {
var serverUtils = INJECTOR.getInstance(ServerUtils.class);
if (!serverUtils.isServerAvailable()) {
var msg = "Server needs to be started before the client, but it does not seem to be available. Shutting down.";
System.err.println(msg);
return;
var connectionHandler = INJECTOR.getInstance(ServerConnectionDialogCtrl.class);
boolean serverConnected = connectionHandler.promptForURL();
if(!serverConnected){
var msg = "User Cancelled Server connection. Shutting down";
System.err.print(msg);
return;
}
}
var foodpal = FXML.load(FoodpalApplicationCtrl.class, "client", "scenes", "FoodpalApplication.fxml");

View file

@ -0,0 +1,94 @@
package client.scenes;
import client.utils.ConfigService;
import client.utils.server.ServerUtils;
import com.google.inject.Inject;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonType;
import javafx.scene.control.TextInputDialog;
import java.util.Optional;
public class ServerConnectionDialogCtrl {
private final ConfigService configService;
private final ServerUtils serverUtils;
@Inject
public ServerConnectionDialogCtrl(ConfigService configService, ServerUtils serverUtils) {
this.configService = configService;
this.serverUtils = serverUtils;
}
/**
*
* @return a boolean for if the user got connected to server or
*/
public boolean promptForURL(){
Alert error = new Alert(Alert.AlertType.ERROR); //creates an error alert
error.setTitle("Server is Unavailable");
error.setHeaderText("Unable to connect to Server");
error.setContentText("The server at " + configService.getConfig().getServerUrl()
+ " is not available\n\n Would you like to try a different Server URL?");
error.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO);
Optional<ButtonType> userChoice = error.showAndWait(); //asks if user wants to input server URL
if(userChoice.isEmpty() || userChoice.get() == ButtonType.NO){
return false;
}
while(true){ // Keeps asking the user until either a valid url is provided or the user exits
TextInputDialog dialog =
new TextInputDialog(configService.getConfig().getServerUrl());
dialog.setTitle("Enter new server URL");
dialog.setContentText("Server URL:");
Optional<String> userRes = dialog.showAndWait();
if(userRes.isEmpty()){
return false; //user cancelled the operation
}
String newServer = userRes.get().trim();
if(newServer.isEmpty()){
Alert alert = new Alert(Alert.AlertType.WARNING);
alert.setTitle("Invalid Input");
alert.setHeaderText("Invalid server URL");
alert.setContentText("Please enter a valid URL");
alert.showAndWait();
continue;
}
configService.getConfig().setServerUrl(newServer);
if(serverUtils.isServerAvailable()){
configService.save();
Alert success = new Alert(Alert.AlertType.INFORMATION);
success.setTitle("Success");
success.setHeaderText("Connected to Server");
success.setContentText("Successfully connected to the server!");
success.showAndWait();
return true;
}
else{
Alert retry = new Alert(Alert.AlertType.ERROR);
retry.setTitle("Failed");
retry.setHeaderText("Failed to connect to Server");
retry.setContentText("Would you like to try another URL?");
retry.getButtonTypes().setAll(ButtonType.YES, ButtonType.NO);
Optional<ButtonType> result = retry.showAndWait();
if(result.isEmpty() || result.get() == ButtonType.NO){
return false;
}
}
}
}
}

View file

@ -7,13 +7,11 @@ import com.google.inject.Inject;
import commons.Ingredient;
import commons.Recipe;
import commons.RecipeIngredient;
import jakarta.ws.rs.ProcessingException;
import jakarta.ws.rs.client.ClientBuilder;
import org.glassfish.jersey.client.ClientConfig;
import java.io.IOException;
import java.net.ConnectException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
@ -171,10 +169,9 @@ public class ServerUtils {
.target(this.endpoints.baseUrl()) //
.request(APPLICATION_JSON) //
.get();
} catch (ProcessingException e) {
if (e.getCause() instanceof ConnectException) {
return false;
}
}
catch(Exception e){
return false; //any exception caught will return false, not just processing exception.
}
return true;
}