init commit

This commit is contained in:
Cole Landers
2023-12-21 08:24:10 -06:00
commit 9d87bf4575
24 changed files with 546 additions and 0 deletions

38
.gitignore vendored Normal file
View File

@@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

3
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

7
.idea/encodings.xml generated Normal file
View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

View File

@@ -0,0 +1,8 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,java.net.http.HttpClient,newHttpClient" />
</inspection_tool>
</profile>
</component>

15
.idea/misc.xml generated Normal file
View File

@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
<option name="workspaceImportForciblyTurnedOn" value="true" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

124
.idea/uiDesigner.xml generated Normal file
View File

@@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.svg" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.svg" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.svg" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.svg" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

6
.idea/vcs.xml generated Normal file
View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

49
pom.xml Normal file
View File

@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>dev.clanders</groupId>
<artifactId>weather-app</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.8</version>
<configuration>
<mainClass>App</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@@ -0,0 +1,27 @@
package dev.clanders.weatherapi;
import dev.clanders.weatherapi.request.SearchLocation;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Api {
private static String API_KEY = "08Yqt7FJVrndj56jsasgceKdppJ4yu90";
public static HttpResponse<String> getWeatherByLocation(SearchLocation searchLocation) throws IOException, InterruptedException {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(String.format("https://api.tomorrow.io/v4/weather/forecast?location=%s %s&units=imperial&apikey=%s", searchLocation.city, searchLocation.state, API_KEY).replace(" ", "%20")))
.header("accept", "application/json")
.method("GET", HttpRequest.BodyPublishers.noBody())
.build();
return HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
}
public static void setApiKey(String newApiKey) {
API_KEY = newApiKey;
}
}

View File

@@ -0,0 +1,6 @@
package dev.clanders.weatherapi.request;
public class SearchLocation {
public String city;
public String state;
}

View File

@@ -0,0 +1,12 @@
package dev.clanders.weatherapi.request;
public class SearchLocationBuilder {
public static SearchLocation createLocation(String locationStr) {
SearchLocation searchLocation = new SearchLocation();
String[] locationTokenized = locationStr.split(",");
searchLocation.city = locationTokenized[0].trim();
searchLocation.state = locationTokenized[1].trim(); // Refactor this to intake fullname or Abbrev.
return searchLocation;
}
}

View File

@@ -0,0 +1,16 @@
package dev.clanders.weatherapi.response;
public class Location {
public double lat;
public double lon;
public String name;
public String type;
public String getCountry() {
// Probably not efficient to be splitting this string every time, but does
// it really matter in an app this size????
String[] locationValues = name.split(",");
return locationValues[locationValues.length - 1].trim();
}
}

View File

@@ -0,0 +1,6 @@
package dev.clanders.weatherapi.response;
public class TimeLineDailyValue {
public double temperatureMax;
public double temperatureMin;
}

View File

@@ -0,0 +1,6 @@
package dev.clanders.weatherapi.response;
public class TimelineDaily {
public String time;
public TimeLineDailyValue values;
}

View File

@@ -0,0 +1,6 @@
package dev.clanders.weatherapi.response;
public class TimelineHourly {
public String time;
public TimelineHourlyValue values;
}

View File

@@ -0,0 +1,5 @@
package dev.clanders.weatherapi.response;
public class TimelineHourlyValue {
public double temperature;
}

View File

@@ -0,0 +1,6 @@
package dev.clanders.weatherapi.response;
public class Timelines {
public TimelineDaily[] daily;
public TimelineHourly[] hourly;
}

View File

@@ -0,0 +1,6 @@
package dev.clanders.weatherapi.response;
public class WeatherLocation {
public Location location;
public Timelines timelines;
}

View File

@@ -0,0 +1,9 @@
package dev.clanders.weatherapp;
public class App {
// For some reason main doesn't work within the Application class
// So gotta put it here.
public static void main(String[] args) {
WeatherApp.run(args);
}
}

View File

@@ -0,0 +1,27 @@
package dev.clanders.weatherapp;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
import java.util.Objects;
public class WeatherApp extends Application {
public static void run(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws IOException {
Parent root = FXMLLoader.load(Objects.requireNonNull(getClass().getResource("/WeatherApp.fxml")));
Scene scene = new Scene(root, 600, 400);
stage.setTitle("FXML Welcome");
stage.setScene(scene);
stage.show();
}
}

View File

@@ -0,0 +1,89 @@
package dev.clanders.weatherapp;
import com.google.gson.Gson;
import dev.clanders.weatherapi.Api;
import dev.clanders.weatherapi.request.SearchLocation;
import dev.clanders.weatherapi.request.SearchLocationBuilder;
import dev.clanders.weatherapi.response.WeatherLocation;
import dev.clanders.weatherapp.error.ApiNotResponsiveException;
import dev.clanders.weatherapp.error.InvalidCountryException;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import java.net.http.HttpResponse;
import java.util.Objects;
public class WeatherAppController {
@FXML
private TextField locationTextField;
@FXML
private Label cityNameLabel;
@FXML
private Label locationLowLabel;
@FXML
private Label locationHighLabel;
@FXML
private Label locationCurrentLabel;
@FXML
private VBox forecastContainer;
@FXML
private TextField usersApiKeyTextField;
@FXML
private Label userApiKeyMessageLabel;
@FXML protected void handleLocationEnteredAction(ActionEvent event) {
cityNameLabel.setTextFill(Color.BLACK);
cityNameLabel.setStyle("-fx-font-size: 24;");
try {
SearchLocation searchLocation = SearchLocationBuilder.createLocation(locationTextField.getText()); // This doesn't fail gracefully
HttpResponse<String> res = Api.getWeatherByLocation(searchLocation);
Gson g = new Gson();
WeatherLocation wLocation = g.fromJson(res.body(), WeatherLocation.class);
if (wLocation.location == null) {
throw new ApiNotResponsiveException();
}
if (!Objects.equals(wLocation.location.getCountry(), "United States")) {
throw new InvalidCountryException();
}
System.out.printf("Location: %s\n", wLocation.location.name);
cityNameLabel.setText(wLocation.location.name);
forecastContainer.setVisible(true);
locationLowLabel.setText(String.valueOf(wLocation.timelines.daily[0].values.temperatureMin));
locationHighLabel.setText(String.valueOf(wLocation.timelines.daily[0].values.temperatureMax));
locationCurrentLabel.setText((String.valueOf(wLocation.timelines.hourly[0].values.temperature)));
} catch(ApiNotResponsiveException e) {
cityNameLabel.setStyle("-fx-font-size: 12;");
cityNameLabel.setTextFill(Color.RED);
cityNameLabel.setText("API currently unresponsive or Invalid custom API Key. \nThis application uses the free tier of tomorrow.io.\n" +
"Due to hourly limits of API calls, you will need to wait before making further requests. Or provide a new API Key below.");
usersApiKeyTextField.setVisible(true);
}
catch (InvalidCountryException e) {
cityNameLabel.setStyle("-fx-font-size: 12;");
cityNameLabel.setTextFill(Color.RED);
cityNameLabel.setText("City must be within United States.\n Please use format City, State without abbreviations.");
}
catch (Exception e) {
cityNameLabel.setTextFill(Color.RED);
cityNameLabel.setText("INVALID");
e.printStackTrace();
}
}
@FXML protected void handleApiKeyEnteredAction(ActionEvent event) {
Api.setApiKey(usersApiKeyTextField.getText());
usersApiKeyTextField.setVisible(false);
userApiKeyMessageLabel.setVisible(true);
}
}

View File

@@ -0,0 +1,4 @@
package dev.clanders.weatherapp.error;
public class ApiNotResponsiveException extends Exception {
}

View File

@@ -0,0 +1,5 @@
package dev.clanders.weatherapp.error;
public class InvalidCountryException extends Exception{
}

View File

@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<VBox alignment="TOP_CENTER" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="dev.clanders.weatherapp.WeatherAppController">
<children>
<VBox prefWidth="100.0">
<children>
<Label text="Location">
<font>
<Font size="25.0" />
</font>
</Label>
<TextField fx:id="locationTextField" focusTraversable="false" onAction="#handleLocationEnteredAction" prefHeight="37.0" prefWidth="435.0" promptText="City, State" />
</children>
<VBox.margin>
<Insets />
</VBox.margin>
</VBox>
<Label fx:id="cityNameLabel" alignment="CENTER" contentDisplay="CENTER" maxHeight="300.0" maxWidth="400.0" prefHeight="150.0" textAlignment="CENTER" textOverrun="WORD_ELLIPSIS" wrapText="true">
<font>
<Font size="24.0" />
</font>
<padding>
<Insets top="5.0" />
</padding>
</Label>
<VBox fx:id="forecastContainer" alignment="CENTER" prefHeight="100.0" prefWidth="447.0" visible="false">
<children>
<Label text="Today's Forecast" />
<HBox alignment="CENTER" prefHeight="100.0" prefWidth="200.0">
<children>
<VBox alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0">
<children>
<Label text="High" />
<Label fx:id="locationHighLabel" />
</children></VBox>
<VBox alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0">
<children>
<Label text="Current" />
<Label fx:id="locationCurrentLabel" />
</children>
</VBox>
<VBox alignment="TOP_CENTER" prefHeight="200.0" prefWidth="100.0">
<children>
<Label text="Low" />
<Label fx:id="locationLowLabel" />
</children></VBox>
</children>
</HBox>
</children>
<VBox.margin>
<Insets top="5.0" />
</VBox.margin>
</VBox>
<Separator opacity="0.0" orientation="VERTICAL" prefHeight="200.0" />
<TextField fx:id="usersApiKeyTextField" focusTraversable="false" onAction="#handleApiKeyEnteredAction" promptText="API KEY" visible="false" />
<Label fx:id="userApiKeyMessageLabel" contentDisplay="CENTER" text="Using Custom API Key" visible="false" />
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
</VBox>