Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from soggy-sandwich/v3.x-dev
Browse files Browse the repository at this point in the history
v3.1.2
  • Loading branch information
marcelohdez authored Dec 15, 2021
2 parents f647584 + e4fbd7f commit 6fc4103
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 74 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'me.soggysandwich'
version '3.1.1'
version '3.1.2'

sourceCompatibility = JavaVersion.VERSION_16
targetCompatibility = JavaVersion.VERSION_16
Expand All @@ -14,7 +14,6 @@ repositories {

application {
mainClass = 'me.soggysandwich.bedroom.Main'
applicationDefaultJvmArgs = ['-Xmx32M', '-Xms16M']
}

jar {
Expand Down
109 changes: 55 additions & 54 deletions src/main/java/me/soggysandwich/bedroom/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.soggysandwich.bedroom;

import me.soggysandwich.bedroom.dialog.alert.AlertDialog;
import me.soggysandwich.bedroom.dialog.alert.YesNoDialog;
import me.soggysandwich.bedroom.dialog.time.SelectTimeDialog;
import me.soggysandwich.bedroom.util.TimeWindowType;
import me.soggysandwich.bedroom.main.BedroomWindow;
Expand All @@ -20,13 +21,14 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.Locale;
import java.util.TreeMap;
import java.util.prefs.Preferences;

public class Main {

// ======= Global Variables =======
public static final String VERSION = "3.1.1";
public static final String VERSION = "3.1.2";
public static final Preferences userPrefs = Preferences.userRoot(); // User preferences directory

// ======= Variables =======
Expand Down Expand Up @@ -61,25 +63,12 @@ public static void main(String[] args) {
Theme.setColors(); // Set extra color accents through UIManager
init();
SwingUtilities.invokeLater(() -> {

Main.openStartupItems();

try { // Try to load shift history
shiftHistory = Settings.loadShiftHistory();
} catch (NumberFormatException e) { // If unable to load due to NumberFormatException show error:
new AlertDialog(null, """
Bedroom was unable to load
your past shift history as
a character loaded was not
a number. Please check
your history file.""");
}

Main.loadShiftHistory();
});

// Create a timer to run every second, updating the time
new Timer(1000, e -> update()).start();
System.out.println(LocalDateTime.now().toString().substring(0, 16));

}

Expand Down Expand Up @@ -134,33 +123,52 @@ private static boolean lastSavedBreakIsInShift() {

}

private static void openStartupItems() {

for (String location : Settings.getStartupItemsList()) {

if (!location.equals("")) {

File workApp = new File(location);
if (workApp.exists()) {

try {
Desktop.getDesktop().open(workApp);
} catch (IOException e) {
e.printStackTrace();
}
private static void loadShiftHistory() {
try { // Try to load shift history
shiftHistory = Settings.loadShiftHistory();
} catch (NumberFormatException e) { // If unable to load due to NumberFormatException show error:
new AlertDialog(null, """
Bedroom was unable to load
your past shift history as
a character loaded was not
a number. Please check
your history file.""");
}
}

} else {
new AlertDialog(wnd, """
One of your startup items was
not able to be started as it
no longer exists. Please go to
Settings > Manage Startup Items.""");
}
private static void openStartupItems() {
String[] list = Settings.getStartupItemsList();

for (String location : list) {
if (!location.isEmpty()) {
openItem(location);
}

}
}

private static void openItem(String loc) {
File workApp = new File(loc);
if (!loc.toLowerCase(Locale.ROOT).endsWith(".jar") || new YesNoDialog(null, """
For safety reasons, Bedroom does not
automatically open .jar files so users
do not create an endless loop of
Bedroom processes, is this startup item
ok to run?:
""" + loc).accepted()) {

if (workApp.exists()) {
try {
Desktop.getDesktop().open(workApp);
} catch (IOException e) {
e.printStackTrace();
}
} else new AlertDialog(wnd, """
One of your startup items was
not able to be started as it
no longer exists. Please go to
Settings > Manage Startup Items.""");
}
}

public static void updateSettings() {
Expand Down Expand Up @@ -257,7 +265,7 @@ private static String getStats() {
}

private static String getUntilTargetText() {
int ordersNeeded = getOrdersNeededForTarget();
int ordersNeeded = getOrdersLeftForTarget();

if (ordersNeeded > 0) {
return "$u until target of $t/hr"
Expand All @@ -268,8 +276,8 @@ private static String getUntilTargetText() {
}
}

// Tell us how many orders we need to reach our target
public static int getOrdersNeededForTarget() {
/** Returns how many orders user has left to reach their target */
public static int getOrdersLeftForTarget() {

double neededForTarget = (double) secondsWorked/3600 * target;
if (neededForTarget > orders) {
Expand Down Expand Up @@ -455,26 +463,19 @@ private static void createHistoryFileAt(Path path) {

try {

if (shiftHistoryFile.createNewFile()) { // If the file does not exist attempt to make it:

FileWriter writer = new FileWriter(shiftHistoryFile);

if (shiftHistory != null) {

writer.write(shiftHistory.toString()); // Write history
if (shiftHistoryFile.createNewFile()) { // If the file does not exist attempt to make it
try (FileWriter writer = new FileWriter(shiftHistoryFile)) {
if (shiftHistory != null) {

} else writer.write("{}"); // If history is null, just write empty brackets
writer.write(shiftHistory.toString()); // Write history

writer.close();

// Else if it exists, attempt to delete and remake it:
} else if (shiftHistoryFile.delete()) saveHistoryToFile();
} else writer.write("{}"); // If history is null, just write empty brackets
}
} else if (shiftHistoryFile.delete()) saveHistoryToFile(); // delete and remake it if it does exists

} catch (Exception e) {

e.printStackTrace();
new AlertDialog(wnd, "Unable to save history to file.");

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public SelectTimeDialog(BedroomWindow parent, TimeWindowType type) {
init();
}

// Creates a continued select time dialog , given a parent and window type, plus the last selected time
// and the original parent of this group of select time dialogs
/**
* Creates a continued select time dialog , given a parent and window type, plus the last selected time
* and the original parent of this group of select time dialogs
*/
public SelectTimeDialog(SelectTimeDialog parent, TimeWindowType type, LocalDateTime lastTime) {
this.type = type;
initParent = parent.getInitParent();
Expand Down Expand Up @@ -87,9 +89,7 @@ public void close() {
} else initParent.setEnabled(true); // Re-enable parent
}

/**
* Dispose this window and its parent, to finish this set and clear up memory
*/
/** Dispose this window and its parent, to finish this set and clear up memory */
protected void finish() {
if (lastDialog != null) lastDialog.dispose();
dispose();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/soggysandwich/bedroom/main/UI.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public void display(String message) {

private void setAddOrderToolTip() {

if (Main.getOrdersNeededForTarget() > Main.getOrders()) { // Tell us how many orders we need to reach our target
if (Main.getOrdersLeftForTarget() > 0) { // Tell us how many orders we need to reach our target
addOrder.setToolTipText("<html><b>You are $n orders behind your hourly target."
.replace("$n", String.valueOf(Main.getOrdersNeededForTarget())));
.replace("$n", String.valueOf(Main.getOrdersLeftForTarget())));
} else if (Main.getOrders() > Main.getOrdersNeeded()) {
addOrder.setToolTipText("<html><b>You are done for the day!</b></html>");
} else { // If we have gotten all the orders needed for our shift.
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/me/soggysandwich/bedroom/util/Ops.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ public static String addZeroUnder10(int number) {
return (number < 10) ? "0" + number : String.valueOf(number);
}

/**
* Sets hand cursor on the needed components in the given JPanel
*/
/** Sets hand cursor on the needed components in the given JPanel */
public static void setHandCursorOnCompsFrom(Container container) {

for (Component c : container.getComponents()) { // Go through the component list of this container
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/me/soggysandwich/bedroom/util/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public static boolean isDoneLoadingShiftHistory() {
*
* @return an ArrayList of the String's items
*/
public static ArrayList<String> getStartupItemsList() {
public static String[] getStartupItemsList() {

ArrayList<String> list = new ArrayList<>();
String str = Main.userPrefs.get("workApps", "[]");
Expand All @@ -188,15 +188,15 @@ public static ArrayList<String> getStartupItemsList() {
if (str.charAt(i) != 44) { // If it is not a comma, extend end point
end++;
} else { // Else return the string we have
list.add(str.substring(start, end));
if (!str.substring(start,end).isBlank()) list.add(str.substring(start, end));
start = i+2; // Go 2 characters ahead to avoid the space in between items.
end = i+1;
}

}
list.add(str.substring(start, end));
if (!str.substring(start, end).isBlank()) list.add(str.substring(start, end));

return list;
return list.toArray(new String[0]);

}

Expand Down Expand Up @@ -272,11 +272,10 @@ private static String readShiftHistory() {
File file = new File(Settings.getWorkingDir() + File.separator + "shift.history"); // Get file

if (file.exists()) {
try {
try (Scanner reader = new Scanner(file)) {

Scanner reader = new Scanner(file); // Make a new scanner
if (reader.hasNextLine()) // If there is a line to read:
return reader.nextLine(); // Read line of data
if (reader.hasNextLine()) // Read the next line (we only save history in a single line)
return reader.nextLine();

} catch (FileNotFoundException e) {
e.printStackTrace();
Expand Down

0 comments on commit 6fc4103

Please sign in to comment.