Skip to content

Commit

Permalink
Testing things.
Browse files Browse the repository at this point in the history
Co-authored-by: kjanderson1 <kjanderson1@users.noreply.github.com>
Co-authored-by: ChristophianSulaiman <ChristophianSulaiman@users.noreply.github.com>
Co-authored-by: dashluu <dashluu@users.noreply.github.com>
Co-authored-by: Timoji <Timoji@users.noreply.github.com>
Co-authored-by: Samantha Prestrelski <sprestrelski@users.noreply.github.com>
  • Loading branch information
6 people committed Dec 6, 2023
1 parent 20c22d6 commit 9add16d
Show file tree
Hide file tree
Showing 25 changed files with 435 additions and 98 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/code/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private void drawUI(Stage primaryStage) throws IOException, URISyntaxException {
view.setScene(login);
Controller controller;
//123
ServerConnection connection = new ServerConnection(AppConfig.SERVER_HOST, 8100);
ServerConnection connection = new ServerConnection(AppConfig.SERVER_HOST, AppConfig.SERVER_PORT);

if (connection.isOnline()) {
controller = new Controller(view, model);
Expand Down
74 changes: 49 additions & 25 deletions app/src/main/java/code/client/Controllers/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Controller(View view, Model model) {
loadCredentials();
if (account != null) {
this.view.getLoginUI().setLoginCreds(account);
goToRecipeList();
goToRecipeList(true);
}
}

Expand All @@ -90,22 +90,30 @@ private void handleNewButton(ActionEvent event) throws URISyntaxException, IOExc
private void handleRecipePostButton(ActionEvent event) throws IOException {
view.getDetailedView().getRefreshButton().setVisible(false);
Recipe postedRecipe = view.getDetailedView().getDisplayedRecipe();
Date currTime = new Date();
postedRecipe.setDate(currTime.getTime());

view.callSaveAnimation();

Writer writer = new StringWriter();
recipeWriter = new RecipeCSVWriter(writer);
recipeWriter.writeRecipe(postedRecipe);

String recipe = writer.toString();

String response = model.performRecipeRequest("POST", recipe, null);
if (response.contains("Offline")) {
AppAlert.show("Connection Error", "Something went wrong. Please check your connection and try again.");
} else if (response.contains("Error")) {
AppAlert.show("Error", "Something went wrong. Please check your inputs and try again.");
}
Thread thread = new Thread(() -> {
String response = model.performRecipeRequest("POST", recipe, null);
// Changes UI to Detailed Recipe Screen
Platform.runLater(
() -> {

if (response.contains("Offline")) {
AppAlert.show("Connection Error", "Something went wrong. Please check your connection and try again.");
} else if (response.contains("Error")) {
AppAlert.show("Error", "Something went wrong. Please check your inputs and try again.");
}
getUserRecipeList();
displayUserRecipes();
});
});
thread.start();
}

private void handleLogOutOutButton(ActionEvent event) {
Expand All @@ -116,12 +124,14 @@ private void handleLogOutOutButton(ActionEvent event) {
}

private void handleHomeButton(ActionEvent event) {
goToRecipeList();
goToRecipeList(false);
}

private void goToRecipeList() {
getUserRecipeList();
displayUserRecipes();
private void goToRecipeList(boolean afterChanges) {
if(afterChanges) {
getUserRecipeList();
displayUserRecipes();
}
view.goToRecipeList();
addListenersToList();
MenuButton filterMenuButton = this.view.getAppFrameHome().getFilterMenuButton();
Expand Down Expand Up @@ -237,9 +247,13 @@ private void handleDetailedViewFromNewRecipeButton(ActionEvent event) {
chatGPTrecipe.setImage(model.performDallERequest("GET", chatGPTrecipe.getTitle()));

// Changes UI to Detailed Recipe Screen
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();
Platform.runLater(
() -> {
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();
});

});
thread.start();
} catch (Exception exception) {
Expand Down Expand Up @@ -269,7 +283,6 @@ private void handleDetailedViewListeners() {
detailedView.setDeleteButtonAction(event -> {
try {
handleDeleteButton(event);
goToRecipeList();
} catch (IOException e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -306,9 +319,17 @@ private void deleteGivenRecipe(Recipe recipe) throws IOException {
String recipeStr = writer.toString();

System.out.println("Deleting id: " + recipe.getId());
model.performRecipeRequest("DELETE", recipeStr, null);
this.view.getAppFrameHome().updateDisplay(filter);
goToRecipeList();
Thread thread = new Thread(() -> {
model.performRecipeRequest("DELETE", recipeStr, null);
Platform.runLater(
() -> {
goToRecipeList(true);
this.view.getAppFrameHome().updateDisplay(filter);
addListenersToList();
});
});

thread.start();
}

private void handleShareButton(ActionEvent event) {
Expand Down Expand Up @@ -355,9 +376,12 @@ private void handleRefreshButton(ActionEvent event) throws URISyntaxException, I
chatGPTrecipe.getTitle()));

// Changes UI to Detailed Recipe Screen
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();
Platform.runLater(
() -> {
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();
});
});
thread.start();
} catch (Exception exception) {
Expand Down Expand Up @@ -429,7 +453,7 @@ private void handleLoginButton(ActionEvent event) {
if (view.getMainScene().equals(view.getOfflineUI())) {
} else if (loginSuccessful) {
Platform.runLater(
() -> goToRecipeList());
() -> goToRecipeList(true));
if (!view.getLoginUI().getRememberLogin()) {
clearCredentials();
} else {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/code/client/Controllers/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;

public class Format {
public String buildPrompt(String mealType, String ingredients) {
Expand Down Expand Up @@ -38,6 +39,8 @@ public Recipe mapResponseToRecipe(String mealType, String responseText) {
title = title.replaceAll("Title:", "");
}
Recipe recipe = new Recipe(title.trim(), mealType);
Date now = new Date();
recipe.setDate(now.getTime());
// Parse recipe's ingredients
String ingredient;
boolean parse = false;
Expand Down
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 @@ -22,7 +22,7 @@ public class AppConfig {
public static final String MONGO_RECIPE_COLLECTION = "recipes";
public static final String MONGO_USER_COLLECTION = "users";
// server
public static final String SERVER_HOST = "localhost";
public static final String SERVER_HOST = "192.168.1.123";
public static final int SERVER_PORT = 8100;
public static final String SERVER_URL = "http://" + SERVER_HOST + ":" + SERVER_PORT;
public static final String RECIPE_PATH = "/recipe";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package code.client.View;
package code.client.Model;

import java.net.InetSocketAddress;
import java.net.Socket;
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/code/client/View/AppFrameHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Footer extends HBox {

Footer() {
GridPane grid = new GridPane();
this.setPrefSize(620, 60);
this.setStyle("-fx-background-color: #F0F8FF;");
this.setPrefSize(620, 60);
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;";
Expand Down Expand Up @@ -74,8 +74,9 @@ class Header extends HBox {

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

this.setPrefSize(620, 60);

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

Text titleText = new Text("Recipe List");
titleText.setStyle("-fx-font-weight: bold; -fx-font-size: 20;");
Expand Down Expand Up @@ -107,11 +108,15 @@ public class AppFrameHome extends VBox {
scroller = new ScrollPane(recipeList);
scroller.setFitToWidth(true);
scroller.setFitToHeight(true);

this.setStyle("-fx-background-color: #F0F8FF;");
this.setAlignment(Pos.CENTER);
this.getChildren().addAll(header,scroller,footer);
this.setSpacing(30);
// this.setTop(header);
// this.setCenter(scroller);
// this.setBottom(footer);
header.setAlignment(Pos.TOP_CENTER);
footer.setAlignment(Pos.BOTTOM_CENTER);
newButton = footer.getNewButton();
logOutButton = footer.getLogOutButton();
}
Expand Down
14 changes: 7 additions & 7 deletions app/src/main/java/code/client/View/DetailsAppFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ public Recipe getDisplayedRecipe() {
RecipeBuilder builder = new RecipeBuilder(currentRecipe.getAccountId(), title);
builder.setMealTag(currentRecipe.getMealTag());
builder.setId(currentRecipe.getId());

if (isOldRecipe) {
builder.setDate(currentRecipe.getDate());
} else {
Date currDate = new Date();
builder.setDate(currDate.getTime());
}
builder.setDate(currentRecipe.getDate());
// if (isOldRecipe) {

// } else {
// Date currDate = new Date();
// builder.setDate(currDate.getTime());
// }

Recipe edit = builder.buildRecipe();
for (String ingredient : ingr) {
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/code/client/View/Ingredients.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class Ingredients extends GridPane {
// Set the preferred vertical and horizontal gaps
this.setVgap(20);
this.setHgap(20);

// Get a picture of a microphone for the voice recording button
File file = new File(AppConfig.MICROPHONE_IMG_FILE);
microphone = new ImageView(new Image(file.toURI().toString()));
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/code/client/View/RecipeDetailsUI.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package code.client.View;

import javafx.geometry.Pos;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
Expand Down Expand Up @@ -56,6 +57,7 @@ public RecipeDetailsUI(Recipe recipe) {
// getChildren().add(titleTextField);
getChildren().add(ingredientTextArea);
getChildren().add(instructionTextArea);
this.setAlignment(Pos.CENTER);
}

public TextField getTitleField() {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/code/client/View/RecipeListUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ public class RecipeListUI extends VBox {

RecipeListUI() throws IOException {
this.setSpacing(5);
this.setPrefSize(700, 500);
this.setStyle("-fx-background-color: #F0F8FF;");
this.setPrefSize(700, 455);
//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 @@ -41,6 +41,7 @@ public class RecipeUI extends HBox {
this.getChildren().add(style);
this.setPrefSize(50, 50);
this.setMinSize(50, 50);
this.setAlignment(Pos.CENTER);
}

public Recipe getRecipe() {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/code/server/RecipeMongoDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public boolean add(Recipe recipe) {
Bson updateInstr = set("instructions", Lists.newArrayList(recipe.getInstructionIterator()));
Bson updateDate = set("date", recipe.getDate());
Bson updateImage = set("image", recipe.getImage());
updates.addAll(Arrays.asList(updateUserId,
updates.addAll(Arrays.asList(
updateUserId,
updateTitle,
updateMealTag,
updateIngr,
Expand Down Expand Up @@ -129,4 +130,6 @@ public int size() {
return (int) recipeDocumentCollection.countDocuments();
}



}
6 changes: 3 additions & 3 deletions app/src/main/java/code/server/ShareRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public void handle(HttpExchange httpExchange) throws IOException {

// Format: localhost:8100/recipes/username/recipeID

System.out.println("\n" + uri.toString());
System.out.println(username);
System.out.println(recipeID);
// System.out.println("\n" + uri.toString());
// System.out.println(username);
// System.out.println(recipeID);
response = ShareRecipe.getSharedRecipe(accountMongoDB, recipeMongoDb, username, recipeID);
// Sending back response to the client
httpExchange.sendResponseHeaders(200, response.length());
Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/code/server/TextToRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ public abstract class TextToRecipe {
public abstract void handle(HttpExchange httpExchange) throws IOException;

public abstract void setSampleRecipe(String recipe);

}
2 changes: 2 additions & 0 deletions app/src/main/java/code/server/WhisperRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
response = "Error";
}
}

// Sending back response to the client
httpExchange.sendResponseHeaders(200, response.length());
OutputStream outStream = httpExchange.getResponseBody();
Expand All @@ -83,6 +84,7 @@ private static String readLine(InputStream multipartInStream, String lineSeparat

while (multipartInStream.available() > 0) {
int nextByte = multipartInStream.read();

if (nextByte < -1) {
throw new IOException("Reached end of stream while reading the current line!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;

import com.sun.net.httpserver.*;

import code.server.TextToRecipe;
Expand All @@ -28,23 +30,42 @@ public void setSampleRecipe(String recipeText) {
@Override
public void handle(HttpExchange httpExchange) throws IOException {
String method = httpExchange.getRequestMethod();
if (method.equals("GET2")) {
sampleRecipe = """
URI uri = httpExchange.getRequestURI();
String query = uri.getRawQuery();
String response = "Error";
try {
String value = query.substring(query.indexOf("=") + 1);
String[] typeIngredients = value.split("::");
String mealType = typeIngredients[0];
String ingredients = typeIngredients[1];

if (mealType.equals("Breakfast")) {
response = sampleRecipe;
}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
if (method.equals("PUT")) {
response = """
Fried Chicken and Egg Fried Rice
breakfast
Ingredients:
- 2 chicken breasts, diced
- 2 large eggs
- 2 cups cooked rice
- 2 tablespoons vegetable oil
- 2 tablespoons soy sauce
- 1 teaspoon sesame oil
- Salt and pepper to taste""";
- Salt and pepper to taste
Instructions:
1. Crack 2 eggs into bowl.
2. Have a shrimp fry the rice.
3. Enjoy!
""";
}
httpExchange.sendResponseHeaders(200, sampleRecipe.length());
httpExchange.sendResponseHeaders(200, response.length());
OutputStream outStream = httpExchange.getResponseBody();
outStream.write(sampleRecipe.getBytes());
outStream.write(response.getBytes());
outStream.close();

}
Expand Down
Loading

0 comments on commit 9add16d

Please sign in to comment.