Added base app.

This commit is contained in:
Andrew Lalis 2023-04-19 10:28:05 +02:00
parent 008af213d8
commit 8269804f9b
7 changed files with 193 additions and 0 deletions

40
recorder/.gitignore vendored Normal file
View File

@ -0,0 +1,40 @@
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
.idea/

30
recorder/pom.xml Normal file
View File

@ -0,0 +1,30 @@
<?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>com.github.andrewlalis.running-every-day</groupId>
<artifactId>recorder</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.41.2.1</version>
</dependency>
<dependency>
<groupId>com.formdev</groupId>
<artifactId>flatlaf</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,23 @@
package com.github.andrewlalis.running_every_day;
import com.formdev.flatlaf.FlatLightLaf;
import com.github.andrewlalis.running_every_day.data.DataSource;
import com.github.andrewlalis.running_every_day.view.RecorderAppWindow;
import java.sql.SQLException;
/**
* The main application entrypoint.
*/
public class RecorderApp {
public static void main(String[] args) {
try (var dataSource = new DataSource("jdbc:sqlite:runs.db")) {
FlatLightLaf.setup();
var window = new RecorderAppWindow(dataSource);
window.setVisible(true);
} catch (SQLException e) {
System.err.println("An SQL error occurred: " + e.getMessage());
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,48 @@
package com.github.andrewlalis.running_every_day.data;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DataSource implements AutoCloseable {
private final Connection conn;
public DataSource(String url) throws SQLException {
this.conn = DriverManager.getConnection(url);
this.initSchemaIfNeeded();
}
public void close() throws SQLException {
this.conn.close();
}
private void initSchemaIfNeeded() throws SQLException {
boolean shouldInitSchema;
try (
var stmt = this.conn.prepareStatement("SELECT name FROM sqlite_master WHERE type='table' AND name='run'");
var rs = stmt.executeQuery()
) {
shouldInitSchema = !rs.next();
}
if (shouldInitSchema) {
try (var stmt = this.conn.createStatement()) {
stmt.execute(readResource("schema.sql"));
}
}
}
private static String readResource(String name) {
try (var in = DataSource.class.getClassLoader().getResourceAsStream(name)) {
if (in == null) {
throw new RuntimeException("Missing resource: " + name);
}
return new String(in.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}

View File

@ -0,0 +1,26 @@
package com.github.andrewlalis.running_every_day.data;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalTime;
/**
* An immutable record that represents an entry in the `run` table.
* @param id The unique id of the record.
* @param date The date of the record.
* @param startTime The time at which the record started.
* @param distanceKm The distance in kilometers.
* @param duration The duration of the run.
* @param weightKg The body-weight recorded for the run.
* @param comment Any comments for the run.
*/
public record RunRecord(
long id,
LocalDate date,
LocalTime startTime,
BigDecimal distanceKm,
Duration duration,
BigDecimal weightKg,
String comment
) {}

View File

@ -0,0 +1,17 @@
package com.github.andrewlalis.running_every_day.view;
import com.github.andrewlalis.running_every_day.data.DataSource;
import javax.swing.*;
import java.awt.*;
public class RecorderAppWindow extends JFrame {
public RecorderAppWindow(DataSource dataSource) {
super("Run-Recorder");
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
// TODO: Build UI
this.setPreferredSize(new Dimension(800, 600));
this.pack();
this.setLocationRelativeTo(null);
}
}

View File

@ -0,0 +1,9 @@
CREATE TABLE run (
id INTEGER PRIMARY KEY AUTOINCREMENT,
date TEXT NOT NULL,
start_time TEXT,
distance INTEGER,
duration INTEGER,
weight INTEGER,
comment TEXT
)