Skip to content

Commit

Permalink
Prepare for demo with incoming traffic + separate UI more from contro…
Browse files Browse the repository at this point in the history
…ller
  • Loading branch information
AllKeng committed Dec 6, 2023
1 parent a7d8fe8 commit e1652b7
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 184 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/code/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ private void drawUI(Stage primaryStage) throws IOException, URISyntaxException {
Model model = new Model();
Scene login = new Scene(view.getLoginUI().getRoot());
view.setScene(login);
Controller controller = new Controller(view, model);
Controller controller;

ServerConnection connection = new ServerConnection("localhost", 8100);

if (connection.isOnline()) {
controller = new Controller(view, model);
// System.out.println("Server is online");
controller.addListenersToList();
} else {
// System.out.println("Server is offline");
view.goToOfflineUI();
}

primaryStage.setScene(login);
primaryStage.setTitle(AppConfig.APP_NAME);
primaryStage.setResizable(true);
Expand Down
198 changes: 43 additions & 155 deletions app/src/main/java/code/client/Controllers/Controller.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion app/src/main/java/code/client/Model/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class AppConfig {
public static final String RECIPE_CSV_FILE = "recipes.csv";
public static final String CREDENTIALS_CSV_FILE = "userCredentials.csv";
// API
public static final boolean MOCKING_ON = false;
public static final boolean MOCKING_ON = true;
public static final String AUDIO_FILE = "recording.wav";
public static final AudioFileFormat.Type AUDIO_TYPE = AudioFileFormat.Type.WAVE;
public static final String API_KEY = "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";
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/java/code/client/Model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ public String performRecipeRequest(String method, String recipe, String userId)
}

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String tempResponse = "";
String line;
while ((line = in.readLine()) != null) {
response += line + "\n";
tempResponse += line + "\n";
}
if( !(tempResponse.toLowerCase().contains("error")) ) {
response = tempResponse;
}
in.close();
} catch (Exception ex) {
Expand Down
28 changes: 7 additions & 21 deletions app/src/main/java/code/client/View/AppFrameHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package code.client.View;

import javafx.scene.control.*;

import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import java.io.*;
Expand All @@ -18,7 +18,7 @@ class Footer extends HBox {
this.setPrefSize(620, 60);
this.setStyle("-fx-background-color: #F0F8FF;");
this.setSpacing(15);

this.setAlignment(Pos.CENTER);
String defaultButtonStyle = "-fx-font-style: italic; -fx-background-color: #FFFFFF; -fx-font-weight: bold; -fx-font: 11 arial;";

newButton = new Button("New Recipe");
Expand Down Expand Up @@ -74,15 +74,6 @@ class Header extends HBox {

sortMenuButton.getItems().addAll(sortNewToOld, sortOldToNew, sortAToZ, sortZToA);

// EventHandler<ActionEvent> event1 = new EventHandler<ActionEvent>() {
// public void handle(ActionEvent e) {
// System.out.println(((MenuItem) e.getSource()).getText() + " selected");
// }
// };

// sortMenuButton.getItems().get(2).setOnAction(event1);
// sortMenuButton.getItems().get(3).setOnAction(event1);

this.setPrefSize(620, 60);
this.setStyle("-fx-background-color: #F0F8FF;");

Expand All @@ -107,32 +98,27 @@ public class AppFrameHome extends BorderPane {
private Footer footer;
private RecipeListUI recipeList;
private Button newButton, logOutButton;
private StackPane stack;

AppFrameHome() throws IOException {
stack = new StackPane();

header = new Header();
recipeList = new RecipeListUI();
footer = new Footer();
ScrollPane scroller = new ScrollPane(recipeList);
scroller.setFitToWidth(true);
scroller.setFitToHeight(true);

scroller.setMaxSize(400,400);
scroller.setVbarPolicy(ScrollBarPolicy.ALWAYS);
this.setTop(header);
this.setCenter(scroller);
this.setBottom(footer);
scroller.setFitToWidth(true);
newButton = footer.getNewButton();
logOutButton = footer.getLogOutButton();
BorderPane.setAlignment(this, Pos.CENTER);

}

public StackPane getRoot() {
stack.getChildren().clear();
stack.getChildren().add(this);
public BorderPane getRoot() {
this.updateDisplay("none");
return stack;
return this;
}

public void updateDisplay(String filter) {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/code/client/View/RecipeListUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public class RecipeListUI extends VBox {
this.setSpacing(5);
// this.setPrefSize(700, 600);
this.setStyle("-fx-background-color: #F0F8FF;");
VBox.setVgrow(this, Priority.ALWAYS);
this.setFillWidth(true);
//VBox.setVgrow(this, Priority.ALWAYS);
// this.setAlignment(Pos.CENTER);
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/code/client/View/RecipeUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class RecipeUI extends HBox {
MealTagStyler.styleTags(recipe, mealType);
this.getChildren().add(style);
this.setPrefSize(50, 50);
this.setMinSize(50, 50);
}

public Recipe getRecipe() {
Expand Down
122 changes: 122 additions & 0 deletions app/src/main/java/code/client/View/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,26 @@
import java.net.URISyntaxException;

import code.server.Recipe;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.PauseTransition;
import javafx.animation.Timeline;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.MenuButton;
import javafx.scene.input.Clipboard;
import javafx.scene.input.ClipboardContent;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import javafx.util.Duration;

public class View {
private AppFrameHome home;
Expand All @@ -16,6 +34,7 @@ public class View {
private Scene mainScene;
private OfflineUI offlineScreen;
private LoadingUI loadingUI;
private String blinkStyle, defaultButtonStyle, onStyle, offStyle;

public View() throws IOException, URISyntaxException {
offlineScreen = new OfflineUI();
Expand All @@ -25,6 +44,9 @@ public View() throws IOException, URISyntaxException {
detailedRecipe = new DetailsAppFrame();
createAcc = new AccountCreationUI();
loadingUI = new LoadingUI();
defaultButtonStyle = "-fx-font-style: italic; -fx-background-color: #FFFFFF; -fx-font-weight: bold; -fx-font: 11 arial;";
onStyle = "-fx-font-style: italic; -fx-background-color: #90EE90; -fx-font-weight: bold; -fx-font: 11 arial;";
offStyle = "-fx-font-style: italic; -fx-background-color: #FF7377; -fx-font-weight: bold; -fx-font: 11 arial;";
}

public Parent getMainScene() {
Expand Down Expand Up @@ -91,4 +113,104 @@ public LoginUI getLoginUI() {
public AccountCreationUI getAccountCreationUI() {
return createAcc;
}

public void callSaveAnimation() {
blinkStyle = "-fx-background-color: #00FFFF; -fx-border-width: 0;";
Button saveButtonFromDetailed = detailedRecipe.getSaveButton();
saveButtonFromDetailed.setStyle(blinkStyle);
PauseTransition pause = new PauseTransition(Duration.seconds(2.5));
pause.setOnFinished(f -> saveButtonFromDetailed.setStyle(defaultButtonStyle));
pause.play();
}

public void setActiveState(MenuButton items, int index) {
for (int i = 0; i < 4; i++) {
if (i == index) {
items.getItems().get(i).setStyle("-fx-background-color: #90EE90");
} else {
items.getItems().get(i).setStyle("-fx-background-color: transparent;");
}
}
}

public void displaySharedRecipeUI(Hyperlink textArea) {
String styleAlert = "-fx-background-color: #F1FFCB; -fx-font-weight: bold; -fx-font: 14 arial";

GridPane gridPane = new GridPane();
gridPane.setMaxWidth(Double.MAX_VALUE);
gridPane.add(textArea, 0, 0);
gridPane.setStyle(styleAlert);
gridPane.setPrefSize(220, 220);
gridPane.setAlignment(Pos.TOP_CENTER);
textArea.setTextAlignment(TextAlignment.CENTER);
Button copyButton = new Button("Copy to Clipboard");
copyButton.setOnAction(event -> {
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
content.putString(textArea.getText());
clipboard.setContent(content);
});
gridPane.add(copyButton, 0, 3);

Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Share this recipe!");
alert.setHeaderText("Share this recipe with a friend!");
alert.getDialogPane().setContent(gridPane);
alert.showAndWait();
}

public void changeEditButtonColor(Button edit) {
if (detailedRecipe.getRecipeDetailsUI().isEditable()) {
edit.setStyle(onStyle);
} else {
edit.setStyle(offStyle);
}
}

public void showLoginSuccessPane(GridPane grid, boolean loginSuccessful) {
Text successText;
if (loginSuccessful) {
successText = new Text("Login successful! Welcome to Pantry Pal.");
successText.setFill(Color.GREEN);
} else {
successText = new Text("Account does not exist. Please try again.");
successText.setFill(Color.RED);
}

successText.setFont(Font.font("Arial", FontWeight.BOLD, 16));
grid.add(successText, 1, 6);

Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0), new KeyValue(successText.opacityProperty(), 1.0)),
new KeyFrame(Duration.seconds(5), new KeyValue(successText.opacityProperty(), 0.0)));
timeline.play();
}

public void showErrorPane(GridPane grid, String errorMessage) {
Text errorText = new Text(errorMessage);
errorText.setFont(Font.font("Arial", FontWeight.BOLD, 16));
errorText.setFill(Color.RED);

grid.add(errorText, 1, 6);

// Fade away after 5 seconds
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0), new KeyValue(errorText.opacityProperty(), 1.0)),
new KeyFrame(Duration.seconds(5), new KeyValue(errorText.opacityProperty(), 0.0)));
timeline.play();
}

public void showSuccessPane(GridPane grid) {
Text successText = new Text("Successfully created an account!\nPlease login to access it.");
successText.setFont(Font.font("Arial", FontWeight.BOLD, 16));
successText.setFill(Color.GREEN);

grid.add(successText, 1, 6);

// Fade away after 5 seconds
Timeline timeline = new Timeline(
new KeyFrame(Duration.seconds(0), new KeyValue(successText.opacityProperty(), 1.0)),
new KeyFrame(Duration.seconds(5), new KeyValue(successText.opacityProperty(), 0.0)));
timeline.play();
}
}
2 changes: 1 addition & 1 deletion app/src/main/java/code/server/AppServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void start() throws IOException {
// create a map to store data
// create a server
httpServer = HttpServer.create(
new InetSocketAddress(hostName, port),
new InetSocketAddress("0.0.0.0", port),
0);
// create the context to map urls
httpServer.createContext(AppConfig.RECIPE_PATH, new RecipeRequestHandler(recipeDb));
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/code/server/mocking/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void start() throws IOException {
// create a map to store data
// create a server
httpServer = HttpServer.create(
new InetSocketAddress(hostName, port),
new InetSocketAddress("0.0.0.0", port),
0);
// create the context to map urls
httpServer.createContext(AppConfig.RECIPE_PATH, new RecipeRequestHandler(recipeDb));
Expand Down

0 comments on commit e1652b7

Please sign in to comment.