Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.starlight</groupId>
<artifactId>tabemonpal</artifactId>
<version>0.5-ALPHA</version>
<version>1.0-ALPHA</version>
<packaging>jar</packaging>

<properties>
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/com/starlight/api/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,23 @@
* Handles XML serialization/deserialization and error handling.
*/
public class ApiClient {
private static final String BASE_URL = "http://localhost:8000";
private final String baseUrl;
private final XStream xstream;

/**
* Default constructor for production use.
* Connects to the standard server URL.
*/
public ApiClient() {
this("http://localhost:8000");
}

/**
* Constructor for testing or custom server URLs.
* @param baseUrl The base URL of the User API Server.
*/
public ApiClient(String baseUrl) {
this.baseUrl = baseUrl;
this.xstream = new XStream(new DomDriver());
this.xstream.allowTypesByWildcard(new String[]{"com.starlight.model.*"});
this.xstream.alias("user", User.class);
Expand All @@ -33,7 +46,7 @@ public ApiClient() {
*/
public User login(String emailOrUsername, String password) throws ApiException {
try {
URL url = new URL(BASE_URL + "/login");
URL url = new URL(baseUrl + "/login");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/xml");
Expand Down Expand Up @@ -70,7 +83,7 @@ public User login(String emailOrUsername, String password) throws ApiException {
*/
public User register(String username, String email, String password) throws ApiException {
try {
URL url = new URL(BASE_URL + "/register");
URL url = new URL(baseUrl + "/register");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/xml");
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/starlight/api/UserApiServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.function.BooleanSupplier;
import java.util.logging.Logger;
import java.util.logging.Level;

Expand Down Expand Up @@ -325,4 +326,8 @@ private void sendXml(HttpExchange exchange, int code, String xml) throws IOExcep
os.write(bytes);
}
}

public BooleanSupplier isServerRunning() {
return () -> started;
}
}
15 changes: 3 additions & 12 deletions src/main/java/com/starlight/controller/AchievementController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,18 @@

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.text.Text;

/**
* Controller for the achievement view. Currently a stub used only to satisfy
* the JavaFX loader.
*/
public class AchievementController implements Initializable {

@FXML
private Label Completion;

@FXML
private Label Unlocked;

@FXML
private Label TitleAchievement1;

@FXML
private Label DescriptionAchievement1;
private Text badgecounter;

@FXML
private Text badgecompletion;

@Override
public void initialize(URL url, ResourceBundle rb) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void setMainController(MainController main) {
}

@FXML
void toProfile(MouseEvent event) {
public void toProfile(MouseEvent event) {
main.loadPage("profile");
}

Expand Down
101 changes: 59 additions & 42 deletions src/main/java/com/starlight/controller/CreatePostController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.starlight.api.ChatbotAPI;
import com.starlight.model.Post;
import com.starlight.util.NutritionParser;
import com.starlight.App;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
Expand Down Expand Up @@ -72,40 +71,13 @@ public class CreatePostController implements Initializable {
private final ChatbotAPI chatbotAPI = new ChatbotAPI();
private final NutritionParser nutritionParser = new NutritionParser();

/**
* Formats text by replacing newlines with vertical lines and cleaning up spacing.
* This ensures ingredients and directions are stored as vertical line-separated single lines.
*
* @param text the text to format
* @return formatted text with vertical lines instead of newlines
*/
private String formatTextAsVerticalLineSeparated(String text) {
if (text == null || text.trim().isEmpty()) {
return "";
}

// Replace newlines with vertical lines, trim whitespace, and remove empty entries
return text.trim()
.replaceAll("\\r?\\n", "| ") // Replace newlines with "| "
.replaceAll("\\|\\s*\\|", "|") // Remove duplicate vertical lines
.replaceAll("^\\|\\s*|\\|\\s*$", "") // Remove leading/trailing vertical lines
.replaceAll("\\s{2,}", " "); // Replace multiple spaces with single space
}
private MainController mainController;

/**
* Copies the selected image to the user's data directory.
*
* @param image the image file selected by the user
* @return the copied image file path
* Sets the main controller for navigation.
*/
private String copyImageToUserDir(File image) {
try {
String username = Session.getCurrentUser() != null ? Session.getCurrentUser().username : "unknown";
return com.starlight.util.FileSystemManager.copyFileToUserDirectoryWithUniqueFilename(image, username);
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to copy image to user directory: " + e.getMessage(), e);
return null;
}
public void setMainController(MainController mainController) {
this.mainController = mainController;
}

/**
Expand Down Expand Up @@ -188,6 +160,42 @@ public void initialize(URL url, ResourceBundle rb) {
});
}

/**
* Formats text by replacing newlines with vertical lines and cleaning up spacing.
* This ensures ingredients and directions are stored as vertical line-separated single lines.
*
* @param text the text to format
* @return formatted text with vertical lines instead of newlines
*/
private String formatTextAsVerticalLineSeparated(String text) {
if (text == null || text.trim().isEmpty()) {
return "";
}

// Replace newlines with vertical lines, trim whitespace, and remove empty entries
return text.trim()
.replaceAll("\\r?\\n", "| ") // Replace newlines with "| "
.replaceAll("\\|\\s*\\|", "|") // Remove duplicate vertical lines
.replaceAll("^\\|\\s*|\\|\\s*$", "") // Remove leading/trailing vertical lines
.replaceAll("\\s{2,}", " "); // Replace multiple spaces with single space
}

/**
* Copies the selected image to the user's data directory.
*
* @param image the image file selected by the user
* @return the copied image file path
*/
private String copyImageToUserDir(File image) {
try {
String username = Session.getCurrentUser() != null ? Session.getCurrentUser().username : "unknown";
return com.starlight.util.FileSystemManager.copyFileToUserDirectoryWithUniqueFilename(image, username);
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to copy image to user directory: " + e.getMessage(), e);
return null;
}
}

/**
* Shows processing dialog and performs nutrition analysis asynchronously,
* as a modal popup dialog
Expand Down Expand Up @@ -351,17 +359,21 @@ protected void succeeded() {
// Show success popup
showResultDialog("post_created_success");

// Navigate back to main view
App.setRoot("main");
// Refresh the community page instead of navigating away
if (mainController != null) {
mainController.refreshCommunityPage();
}

} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to save post or navigate to main: " + e.getMessage(), e);
logger.log(Level.SEVERE, "Failed to save post or refresh community page: " + e.getMessage(), e);

// Close dialog and show failure message
try {
processingController.closeDialog();
showResultDialog("post_creation_failed");
App.setRoot("main");
if (mainController != null) {
mainController.refreshCommunityPage();
}
} catch (Exception fallbackEx) {
logger.log(Level.SEVERE, "Fallback navigation failed: " + fallbackEx.getMessage(), fallbackEx);
}
Expand Down Expand Up @@ -389,18 +401,22 @@ protected void failed() {
// Show success popup (post was still created)
showResultDialog("post_created_success");

// Navigate back to main view
App.setRoot("main");
// Refresh the community page
if (mainController != null) {
mainController.refreshCommunityPage();
}
} catch (Exception e) {
logger.log(Level.SEVERE, "Failed to save post: " + e.getMessage(), e);

// Close the processing dialog and show failure message
try {
processingController.closeDialog();
showResultDialog("post_creation_failed");
App.setRoot("main");
if (mainController != null) {
mainController.refreshCommunityPage();
}
} catch (Exception ex) {
logger.log(Level.SEVERE, "Failed to close dialog or navigate to main view: " + ex.getMessage(), ex);
logger.log(Level.SEVERE, "Failed to close dialog or refresh community page: " + ex.getMessage(), ex);
}
}
});
Expand All @@ -422,14 +438,15 @@ protected void failed() {
posts.add(0, newPost);
repository.savePosts(posts);
success = true;
App.setRoot("main");
if (mainController != null) {
mainController.refreshCommunityPage();
}
} catch (Exception fallbackEx) {
logger.log(Level.SEVERE, "Fallback save and navigation failed: " + fallbackEx.getMessage(), fallbackEx);
}
}
}


/**
* Shows a popup dialog for nutrition analysis issues
*/
Expand Down
21 changes: 17 additions & 4 deletions src/main/java/com/starlight/controller/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void home(MouseEvent event) {
/** Handles navigation to the achievement view. */
@FXML
void achievement(MouseEvent event) {
loadPage("achievement");
loadPage("underDevelopment");
selected(achievement);
}

Expand All @@ -138,21 +138,21 @@ void consult(MouseEvent event) {
/** Handles navigation to the games view. */
@FXML
void games(MouseEvent event) {
loadPage("games");
loadPage("underDevelopment");
selected(games);
}

/** Handles navigation to the mission view. */
@FXML
void mission(MouseEvent event) {
loadPage("mission");
loadPage("underDevelopment");
selected(mission);
}

/** Handles navigation to the wiki view. */
@FXML
void wiki(MouseEvent event) {
loadPage("wiki");
loadPage("underDevelopment");
selected(wiki);
}

Expand Down Expand Up @@ -219,6 +219,18 @@ public void refreshNavigationState() {
}
}

/**
* Refreshes the community page to show new posts.
*/
public void refreshCommunityPage() {
if (bp.getCenter() != null) {
Object controller = bp.getCenter().getProperties().get("controller");
if (controller instanceof CommunityController) {
((CommunityController) controller).refreshPosts();
}
}
}

/**
* Loads the given FXML page into the grid pane container.
*/
Expand Down Expand Up @@ -270,6 +282,7 @@ protected Parent call() throws Exception {

task.setOnSucceeded(ev -> {
Parent root = task.getValue();
root.getProperties().put("controller", task.getValue().getProperties().get("controller"));
gp.getChildren().remove(loadingRoot);
gp.getChildren().removeIf(node ->
GridPane.getColumnIndex(node) != null && GridPane.getRowIndex(node) != null &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.starlight.controller;

import javafx.fxml.FXML;
import javafx.scene.layout.VBox;

public class UnderDevelopmentController {


@FXML
private VBox container;

@FXML
private void initialize() {
// Hint: initialize() will be called when the associated FXML has been completely loaded.
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/main/resources/com/starlight/view/Tabestreak.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<?import javafx.scene.text.Font?>
<?import javafx.scene.text.Text?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="2104.0" prefWidth="990.0" styleClass="background" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/23.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.starlight.controller.TabestreakController">
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="2104.0" prefWidth="990.0" styleClass="background" stylesheets="@../style.css" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.starlight.controller.TabestreakController">
<children>
<GridPane layoutY="129.0" prefHeight="1903.0" prefWidth="990.0">
<columnConstraints>
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/com/starlight/view/achievement.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="X" textAlignment="CENTER" GridPane.halignment="CENTER">
<Text fx:id="badgecounter" strokeType="OUTSIDE" strokeWidth="0.0" text="X" textAlignment="CENTER" GridPane.halignment="CENTER">
<font>
<Font size="18.0" />
</font>
Expand All @@ -66,7 +66,7 @@
<RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
</rowConstraints>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="x%" GridPane.halignment="CENTER">
<Text fx:id="badgecompletion" strokeType="OUTSIDE" strokeWidth="0.0" text="x%" GridPane.halignment="CENTER">
<font>
<Font size="18.0" />
</font>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/com/starlight/view/community.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
<HBox.margin>
<Insets left="4.0" />
</HBox.margin></Label>
</children>
</children>
<VBox.margin>
<Insets bottom="20.0" left="18.0" right="18.0" />
</VBox.margin>
Expand Down
Loading
Loading