Skip to content

Commit

Permalink
Loading Screen + Threads :)
Browse files Browse the repository at this point in the history
Co-authored-by: dashluu <dashluu@users.noreply.github.com>
Co-authored-by: Samantha Prestrelski <sprestrelski@users.noreply.github.com>
3 people committed Dec 5, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent f2e29ca commit 5713cee
Showing 4 changed files with 116 additions and 21 deletions.
96 changes: 75 additions & 21 deletions app/src/main/java/code/client/Controllers/Controller.java
Original file line number Diff line number Diff line change
@@ -60,6 +60,7 @@ public class Controller {
private final AppAudioRecorder recorder = new AppAudioRecorder();
private String mealType; // stores the meal type specified by the user
private String ingredients; // stores the ingredients listed out by the user
private ProgressBar progressBar;

public Controller(View view, Model model) {

@@ -99,6 +100,7 @@ public void setTitle(String title) {
}

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());
@@ -117,7 +119,12 @@ private void handleRecipePostButton(ActionEvent event) throws IOException {
// Debugging
// System.out.println("Posting: " + recipe);

model.performRecipeRequest("POST", recipe, null);
String response = model.performRecipeRequest("POST", recipe, null);
if (response.contains("Offline")) {

} else if (response.contains("Error")) {

}
}

private void goToRecipeList() {
@@ -292,17 +299,30 @@ public void addListenersToList() {
private void handleDetailedViewFromNewRecipeButton(ActionEvent event) {
// Get ChatGPT response from the Model
if (mealType != null && ingredients != null) {
view.goToLoading();
try {
String responseText = model.performChatGPTRequest("GET", mealType, ingredients);
Recipe chatGPTrecipe = format.mapResponseToRecipe(mealType, responseText);
chatGPTrecipe.setAccountId(account.getId());
chatGPTrecipe.setImage(model.performDallERequest("GET", chatGPTrecipe.getTitle()));

// Changes UI to Detailed Recipe Screen
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();

Thread thread = new Thread(
new Runnable() {
@Override
public void run() {
String responseText = model.performChatGPTRequest("GET", mealType, ingredients);
Recipe chatGPTrecipe = format.mapResponseToRecipe(mealType, responseText);
chatGPTrecipe.setAccountId(account.getId());
chatGPTrecipe.setImage(model.performDallERequest("GET", chatGPTrecipe.getTitle()));

// Changes UI to Detailed Recipe Screen
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();
}
});
thread.start();
AppFrameMic mic = view.getAppFrameMic();
mic.getRecordingIngredientsLabel()
.setText("Processing mealType and ingredients. Please wait.");
mic.getRecordingIngredientsLabel()
.setStyle("-fx-font-weight: bold; -fx-font: 20 arial;");
mic.getRecordingIngredientsLabel().setVisible(true);
} catch (Exception exception) {
AppAlert.show("Connection Error", "Something went wrong. Please check your connection and try again.");
exception.printStackTrace();
@@ -335,7 +355,14 @@ private void handleDetailedViewListeners() {
});
detailedView.setHomeButtonAction(this::handleHomeButton);
detailedView.setShareButtonAction(this::handleShareButton);
detailedView.setRefreshButtonAction(this::handleRefreshButton);
detailedView.setRefreshButtonAction(event -> {
try {
handleRefreshButton(event);
} catch (URISyntaxException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
}

private void handleEditButton(ActionEvent event) {
@@ -360,7 +387,7 @@ private void deleteGivenRecipe(Recipe recipe) throws IOException {

System.out.println("Deleting id: " + recipe.getId());
model.performRecipeRequest("DELETE", recipeStr, null);
this.view.getAppFrameHome().updateDisplay(filter);
this.view.getAppFrameHome().updateDisplay(filter);
goToRecipeList();
}

@@ -424,17 +451,44 @@ private void handleGoToLogin(ActionEvent event) {
view.goToLoginUI();
}

private void handleRefreshButton(ActionEvent event) {
private void handleRefreshButton(ActionEvent event) throws URISyntaxException, IOException {
// Get ChatGPT response from the Model
if (mealType != null && ingredients != null) {
view.goToLoading();
try {
String responseText = model.performChatGPTRequest("GET", mealType, ingredients);
Recipe chatGPTrecipe = format.mapResponseToRecipe(mealType, responseText);
chatGPTrecipe.setAccountId(account.getId());
chatGPTrecipe.setImage(model.performDallERequest("GET", chatGPTrecipe.getTitle()));

// Changes UI to Detailed Recipe Screen
view.getDetailedView().setRecipe(chatGPTrecipe);
Thread thread = new Thread(
new Runnable() {
@Override
public void run() {

String responseText = model.performChatGPTRequest("GET", mealType,
ingredients);
Recipe chatGPTrecipe = format.mapResponseToRecipe(mealType, responseText);
chatGPTrecipe.setAccountId(account.getId());
chatGPTrecipe.setImage(model.performDallERequest("GET",
chatGPTrecipe.getTitle()));

// Changes UI to Detailed Recipe Screen
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();
}
});
thread.start();

// AppAlert.show("Loading", "Please wait for the recipe to regenerate.");
// Thread.sleep(2000);
// String responseText = model.performChatGPTRequest("GET", mealType,
// ingredients);
// Recipe chatGPTrecipe = format.mapResponseToRecipe(mealType, responseText);
// chatGPTrecipe.setAccountId(account.getId());
// chatGPTrecipe.setImage(model.performDallERequest("GET",
// chatGPTrecipe.getTitle()));

// // Changes UI to Detailed Recipe Screen
// view.goToDetailedView(chatGPTrecipe, false);
// view.getDetailedView().getRecipeDetailsUI().setEditable(false);
// handleDetailedViewListeners();

} catch (Exception exception) {
AppAlert.show("Connection Error", "Something went wrong. Please check your connection and try again.");
35 changes: 35 additions & 0 deletions app/src/main/java/code/client/View/LoadingUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package code.client.View;

import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.text.Font;
import javafx.geometry.Insets;
import java.io.File;

import code.client.Model.AppConfig;

public class LoadingUI extends HBox {
private final Label loadingLabel;
private final ImageView loadingImg;

LoadingUI() {
GridPane gridPane = new GridPane();
gridPane.setPrefSize(620, 620);
gridPane.setAlignment(javafx.geometry.Pos.CENTER);
gridPane.setHgap(10);
gridPane.setVgap(10);
gridPane.setPadding(new Insets(25, 25, 25, 25));
gridPane.setStyle("-fx-background-color: #F0F8FF;");
loadingLabel = new Label("Loading...");
loadingLabel.setFont(Font.font("Comic Sans MS", 20)); // Adjust the size as needed
gridPane.add(loadingLabel, 1, 1);
// Show an image when the server is offline
File file = new File(AppConfig.LOADING_IMG_FILE);
loadingImg = new ImageView(new Image(file.toURI().toString()));
loadingImg.setFitWidth(Region.USE_COMPUTED_SIZE);
gridPane.add(loadingImg, 1, 2);
getChildren().addAll(gridPane);
}
}
6 changes: 6 additions & 0 deletions app/src/main/java/code/client/View/View.java
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ public class View {
private AccountCreationUI createAcc;
private Scene mainScene;
private OfflineUI offlineScreen;
private LoadingUI loadingUI;

public View() throws IOException, URISyntaxException {
offlineScreen = new OfflineUI();
@@ -22,6 +23,7 @@ public View() throws IOException, URISyntaxException {
audioCapture = new AppFrameMic();
detailedRecipe = new DetailsAppFrame();
createAcc = new AccountCreationUI();
loadingUI = new LoadingUI();
}

public void setScene(Scene scene) {
@@ -32,6 +34,10 @@ public void goToRecipeList() {
mainScene.setRoot(home.getRoot());
}

public void goToLoading() {
mainScene.setRoot(loadingUI);
}

public void goToAudioCapture() throws URISyntaxException, IOException {
audioCapture = new AppFrameMic();
mainScene.setRoot(audioCapture.getRoot());
Binary file added app/src/main/java/code/client/View/loading.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5713cee

Please sign in to comment.