Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Us9 #111

Merged
merged 13 commits into from
Dec 4, 2023
Merged

Us9 #111

Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,5 @@ test {
}

mainClassName = 'App'
//mainClassName = 'server.Server'

//mainClassName = 'server.Server'
// mainClassName = 'server.Server'
3 changes: 1 addition & 2 deletions src/main/java/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ public static void main(String[] args) {

@Override
public void start(Stage primaryStage) {

AppFrame root = new AppFrame();

Model model = new Model();
Controller controller = new Controller(root, model);

primaryStage.setResizable(false);
primaryStage.setResizable(true);
primaryStage.setScene(new Scene(root, 500, 600));
primaryStage.setTitle("Recipe Management App");
primaryStage.show();
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/client/controller/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class Controller {
private AccountPopup accountPopup;
private DetailsPopup detailsPopup;
private LoginPopup loginPopup;
private boolean loggedIn;

public Controller(AppFrame appFrame, Model model) {
this.appFrame = appFrame;
Expand All @@ -39,8 +40,20 @@ public Controller(AppFrame appFrame, Model model) {
this.recipePopup.setStartRecordingButtonAction(this::handleStartRecordingButton);
this.recipePopup.setStopRecordingButtonAction(this::handleStopRecordingButton);
this.detailsPopup.setRefreshButtonAction(this::handleRefreshButton);
this.detailsPopup.setSaveButtonAction(this::handleSaveButton);
this.detailsPopup.setDeleteButtonAction(this::handleDeleteButton);
this.accountPopup.setCreateAccountButtonAction(this::handleCreateAccountButton);
this.loginPopup.setLoginAccountButtonAction(this::handleLoginAccountButton);

loggedIn = server.Login.attemptAutoLogin();
if (loggedIn) {
System.out.println("Auto-login successful");
this.appFrame.setLoggedInUI();
} else {
System.out.println("Auto-login failed or credentials not stored");
loginPopup.show();
}

}

private void handleCreateAccountButton(ActionEvent event) {
Expand Down Expand Up @@ -116,6 +129,14 @@ private void handleRefreshButton(ActionEvent event) {
public void audioToMealType() {
String generatedText = model.requestTranscript();
System.out.println(generatedText);
generatedText = generatedText.strip();
String[] mealOptions = {"breakfast", "lunch", "dinner"};
for (String option : mealOptions) {
if (generatedText.toLowerCase().contains(option)) {
generatedText = option;
break;
}
}
if (generatedText.toLowerCase().contains("breakfast")
|| generatedText.toLowerCase().contains("lunch")
|| generatedText.toLowerCase().contains("dinner")) {
Expand Down Expand Up @@ -158,4 +179,19 @@ public String[] generateInstruction(String mealtype, String ingredients)
return instructions;
}

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());
model.sendRecipe(detailsPopup.getRecipe());
detailsPopup.close();
System.out.println("DONE");
}

public void handleDeleteButton(ActionEvent event) {
detailsPopup.getRecipe().deleteRecipe();
model.deleteRecipe(detailsPopup.getRecipe());
detailsPopup.close();
}

}
77 changes: 33 additions & 44 deletions src/main/java/client/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,42 @@ public String sendRecipe(Recipe recipe) {
try {
HttpClient client = HttpClient.newHttpClient();
JSONObject json = new JSONObject();
// json.put("id", getUserID());
json.put("id", server.Login.getID());
json.put("name", recipe.getName().getText());
json.put("mealType", recipe.getMealType().getText());
json.put("ingredients", recipe.getIngredient().getText());
json.put("instructions", recipe.getInstruction().getText());

System.out.println(json);

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json.toString()))
.build();

HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
return response.body();
} catch (Exception ex) {
ex.printStackTrace();
return "Error: " + ex.getMessage();
}
}

public String deleteRecipe(Recipe recipe) {
String url = "http://localhost:8100/api/delete";

try {
HttpClient client = HttpClient.newHttpClient();
JSONObject json = new JSONObject();
json.put("id", server.Login.getID());
json.put("name", recipe.getName().getText());
json.put("mealType", recipe.getMealType().getText());
json.put("ingredients", recipe.getIngredient().getText());
json.put("instructions", recipe.getInstruction().getText());

System.out.println(json);

HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("Content-Type", "application/json")
Expand All @@ -125,6 +155,8 @@ public String sendRecipe(Recipe recipe) {
}
}



// Doenst work all the time because of byte fixed content-lenth
public String requestInstruction(String prompt) {
String url = "http://localhost:8100/instruction";
Expand Down Expand Up @@ -178,48 +210,5 @@ public static String sendAudio(String url, String filePath) throws IOException,
return response.body();
}

// private static String sendAudio(String urlString, String filePath) throws
// IOException{
// final String POST_URL = urlString;
// final File uploadFile = new File(filePath);

// String boundary = Long.toHexString(System.currentTimeMillis());
// String CRLF = "\r\n";
// String charset = "UTF-8";
// URLConnection connection = new URL(POST_URL).openConnection();
// connection.setDoOutput(true);
// connection.setRequestProperty("Content-Type", "multipart/form-data;
// boundary=" + boundary);

// try (
// OutputStream output = connection.getOutputStream();
// PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset),
// true);
// ) {
// writer.append("--" + boundary).append(CRLF);
// writer.append("Content-Disposition: form-data; name=\"binaryFile\";
// filename=\"" + uploadFile.getName() + "\"").append(CRLF);
// writer.append("Content-Length: " + uploadFile.length()).append(CRLF);
// writer.append("Content-Type: " +
// URLConnection.guessContentTypeFromName(uploadFile.getName())).append(CRLF);
// writer.append("Content-Transfer-Encoding: binary").append(CRLF);
// writer.append(CRLF).flush();
// Files.copy(uploadFile.toPath(), output);
// output.flush();

// int responseCode = ((HttpURLConnection) connection).getResponseCode();
// System.out.println("Response code: [" + responseCode + "]");
// }

// BufferedReader reader = new BufferedReader(new
// InputStreamReader(connection.getInputStream()));
// StringBuilder response = new StringBuilder();
// String line;
// while ((line = reader.readLine()) != null) {
// response.append(line);
// }
// return response.toString();

// }

}
35 changes: 28 additions & 7 deletions src/main/java/client/view/AccountPopup.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package client.view;

import javafx.stage.Stage;
import server.Create;
import server.CreateAccount;
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;
Expand All @@ -26,12 +27,15 @@ public class AccountPopup extends Stage {
private VBox layout;
private Label usernameLabel;
private Label passwordLabel;
private boolean loggedIn = false;
private CheckBox autoLoginCheckbox;
private boolean autoLogin = false;

public AccountPopup() {

setTitle("Create Account");
setWidth(300);
setHeight(200);
setHeight(300);

usernameLabel = new Label("Username:");
usernameLabel.setStyle("-fx-alignment: center; -fx-font-weight: bold; -fx-font-family: 'Lucida Bright';");
Expand All @@ -47,6 +51,12 @@ public AccountPopup() {
username.setStyle("-fx-alignment: center; -fx-font-weight: bold;");
password.setStyle("-fx-alignment: center; -fx-font-weight: bold;");

autoLoginCheckbox = new CheckBox("Automatic Login");
autoLoginCheckbox.setStyle("-fx-font-family: 'Lucida Bright';");
autoLoginCheckbox.setOnAction(event -> {
autoLogin = autoLoginCheckbox.isSelected();
});

createAccountButton = new Button("Create Account");
createAccountButton.setStyle(
"-fx-background-color: #bdd9bd; -fx-font-weight: bold; -fx-font-size: 13; -fx-font-family: 'Lucida Bright';");
Expand All @@ -55,20 +65,31 @@ public AccountPopup() {
buttonBox.setAlignment(Pos.CENTER);
buttonBox.getChildren().addAll(createAccountButton);


}

public boolean isLoggedIn() {
return loggedIn;
}

public void setCreateAccountButtonAction(EventHandler<ActionEvent> eventHandler) {
createAccountButton.setOnAction(event -> {
String enteredUsername = username.getText();
String enteredPassword = password.getText();
createAccount(enteredUsername, enteredPassword);
server.CreateAccount.createAccount(enteredUsername, enteredPassword);
loginAccount(username.getText(), password.getText());
sendDataToServerAndMongoDB(enteredUsername, enteredPassword);
});

}

private void createAccount(String username, String password) {
// Use the entered username and password and send it to the Create class
server.Create.createAccount(username, password);
public void loginAccount(String username, String password) {
loggedIn = server.Login.loginAccount(username, password, autoLogin);
if (loggedIn) {
// If logged in successfully, close the login popup
this.close();
AppFrame.setLoggedInUI();
}
sendDataToServerAndMongoDB(username, password);
}

Expand Down Expand Up @@ -101,7 +122,7 @@ public void display() {
layout = new VBox(10);
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-background-color: #93c994;");
layout.getChildren().addAll(usernameLabel, username, passwordLabel, password, buttonBox);
layout.getChildren().addAll(usernameLabel, username, passwordLabel, password, autoLoginCheckbox, buttonBox);

Scene scene = new Scene(layout, 400, 500);
setScene(scene);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/client/view/AppFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public void addListeners() {
loginPopup.display();
});
logoutButton.setOnAction(e -> {
server.Login.clearCredentials();
setLoggedOutUI();
});
}
Expand Down
49 changes: 34 additions & 15 deletions src/main/java/client/view/DetailsPopup.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.event.ActionEvent;
Expand All @@ -15,6 +18,7 @@ public class DetailsPopup extends Stage {

private Recipe recipe;

private TextField mealType;
private TextField name;
private TextField ingredients;
private TextArea instruction;
Expand All @@ -29,6 +33,7 @@ public DetailsPopup() {
// setTitle(name.getText());
setWidth(525);
setHeight(650);

// Create controls for the popup window
name = new TextField();
this.name.setPrefSize(200, 10); // set size of text field
Expand All @@ -38,6 +43,13 @@ public DetailsPopup() {
this.name.setPadding(new Insets(10, 0, 10, 0)); // adds some padding to the text field
name.setEditable(false);

this.mealType = new TextField();
this.mealType.setPrefSize(200, 10); // set size of text field
this.mealType.setStyle( "-fx-background-color: #659966; -fx-border-width: 0; -fx-font-size: 15; -fx-font-family: 'Times New Roman';");
this.mealType.setPadding(new Insets(10, 0, 10, 0)); // adds some padding to the text field
this.mealType.setAlignment(Pos.CENTER);
this.mealType.setEditable(false);

ingredients = new TextField();
ingredients.setPrefSize(50, 10); // set size of text field
ingredients.setStyle(" -fx-font-weight: bold; -fx-font-size: 14; -fx-font-family: 'Times New Roman';");
Expand Down Expand Up @@ -67,29 +79,15 @@ public DetailsPopup() {
VBox layout = new VBox(10); // 10 pixels spacing
layout.setAlignment(Pos.CENTER);
layout.setStyle("-fx-background-color: #93c994;");
layout.getChildren().addAll(name, ingredients, instruction, backButton, editButton, deleteButton, saveButton,
layout.getChildren().addAll(name, mealType, ingredients, instruction, backButton, editButton, deleteButton, saveButton,
refreshButton);

// Add an action for the "Add" button
saveButton.setOnAction(e -> {
recipe.getName().setText(name.getText());
recipe.getIngredient().setText(ingredients.getText());
recipe.getInstruction().setText(instruction.getText());
recipe.saveRecipe();
close(); // Close the popup window
});

editButton.setOnAction(e -> {
ingredients.setEditable(!ingredients.isEditable());
instruction.setEditable(!instruction.isEditable());
});

// Add an action for the "Delete" button
deleteButton.setOnAction(e -> {
recipe.deleteRecipe();
recipe.saveRecipe();
close();
});

backButton.setOnAction(e -> {
close();
Expand All @@ -109,6 +107,7 @@ public void setRecipe(Recipe recipe) {
name.setText(recipe.getName().getText());
ingredients.setText(recipe.getIngredient().getText());
instruction.setText(recipe.getInstruction().getText());
mealType.setText(recipe.getMealType().getText());
}

public Button getEditButton() {
Expand All @@ -127,7 +126,27 @@ public Button getRefreshButton() {
return this.refreshButton;
}

public TextField getName() {
return name;
}

public TextField getIngredients() {
return ingredients;
}

public TextArea getInstruction() {
return instruction;
}

public void setRefreshButtonAction(EventHandler<ActionEvent> eventHandler) {
refreshButton.setOnAction(eventHandler);
}

public void setSaveButtonAction(EventHandler<ActionEvent> eventHandler) {
saveButton.setOnAction(eventHandler);
}

public void setDeleteButtonAction(EventHandler<ActionEvent> eventHandler) {
deleteButton.setOnAction(eventHandler);
}
}
Loading