Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
ec5310a1f3 | |||
ec1c196752 | |||
6064d9847c | |||
235a90b0a7 | |||
4abddac94f | |||
f12c353905 | |||
2586f0749f | |||
1387eecc96 |
5
app.go
5
app.go
@@ -2,7 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"os"
|
|
||||||
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App struct
|
// App struct
|
||||||
@@ -139,5 +140,5 @@ func (a *App) SetSetting(key string, value int64) WailsGenericAck {
|
|||||||
|
|
||||||
//region other
|
//region other
|
||||||
func (a *App) Close() {
|
func (a *App) Close() {
|
||||||
os.Exit(0)
|
runtime.Quit(a.ctx)
|
||||||
}
|
}
|
22
db.go
22
db.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mattn/go-sqlite3"
|
"github.com/mattn/go-sqlite3"
|
||||||
@@ -21,6 +22,22 @@ func (db *DB) Open() error {
|
|||||||
return fmt.Errorf("database path not set")
|
return fmt.Errorf("database path not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
file, err := os.Open(db.path)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
log.Printf("Database file does not exist at %s, creating", db.path)
|
||||||
|
file, err := os.Create(db.path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create database file: %v", err)
|
||||||
|
}
|
||||||
|
log.Printf("Database created at %s", db.path)
|
||||||
|
file.Close()
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("failed to open database file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
file.Close()
|
||||||
|
|
||||||
sql.Register("spellfixlite", &sqlite3.SQLiteDriver{
|
sql.Register("spellfixlite", &sqlite3.SQLiteDriver{
|
||||||
Extensions: []string{"spellfix"},
|
Extensions: []string{"spellfix"},
|
||||||
})
|
})
|
||||||
@@ -95,6 +112,11 @@ func (db *DB) Init(ddl string) error {
|
|||||||
_, err = db.writeConn.Exec(ddl)
|
_, err = db.writeConn.Exec(ddl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Printf("%++v", err)
|
Error.Printf("%++v", err)
|
||||||
|
log.Printf("%#v", "Rolling back")
|
||||||
|
_, err2 := db.writeConn.Exec("ROLLBACK;")
|
||||||
|
if err2 != nil {
|
||||||
|
Error.Printf("Error rollingback! %++v", err)
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
5
food.ddl
5
food.ddl
@@ -1,4 +1,4 @@
|
|||||||
begin transaction;
|
begin;
|
||||||
|
|
||||||
create table weight (
|
create table weight (
|
||||||
date datetime default (datetime('now', '+2 hours')),
|
date datetime default (datetime('now', '+2 hours')),
|
||||||
@@ -124,7 +124,6 @@ where not exists (
|
|||||||
);
|
);
|
||||||
end;
|
end;
|
||||||
|
|
||||||
-- Spellfix search example
|
|
||||||
with search_results as (
|
with search_results as (
|
||||||
select word, score, f.rowid, f.*
|
select word, score, f.rowid, f.*
|
||||||
from foodfix
|
from foodfix
|
||||||
@@ -134,7 +133,7 @@ with search_results as (
|
|||||||
select rowid, food, score, date, description, amount, per100, energy
|
select rowid, food, score, date, description, amount, per100, energy
|
||||||
from search_results
|
from search_results
|
||||||
group by food
|
group by food
|
||||||
order by score asc, date desc
|
order by score asc, date desc;
|
||||||
|
|
||||||
create index dailyIdx on food(strftime('%Y-%m-%d', date));
|
create index dailyIdx on food(strftime('%Y-%m-%d', date));
|
||||||
create index weeklyIdx on food(strftime('%Y-%W', date));
|
create index weeklyIdx on food(strftime('%Y-%W', date));
|
||||||
|
@@ -30,9 +30,24 @@
|
|||||||
name = name.trim();
|
name = name.trim();
|
||||||
if (!name) {
|
if (!name) {
|
||||||
foodSearch = [];
|
foodSearch = [];
|
||||||
|
} else {
|
||||||
|
updateAutocomplete();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateAutocomplete() {
|
||||||
|
if (!per100Edited)
|
||||||
|
GetLastPer100(name.trim()).then((res) => {
|
||||||
|
// Prevent search when there's nothing to search
|
||||||
|
// Sometimes we get search results after deleting name
|
||||||
|
if (res.success && res.data && name) {
|
||||||
|
foodSearch = res.data;
|
||||||
|
} else {
|
||||||
|
foodSearch = [];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function update(event: KeyboardEvent & { currentTarget: EventTarget & HTMLTableCellElement }) {
|
async function update(event: KeyboardEvent & { currentTarget: EventTarget & HTMLTableCellElement }) {
|
||||||
name = name.trim();
|
name = name.trim();
|
||||||
amount = amount.trim();
|
amount = amount.trim();
|
||||||
@@ -69,17 +84,6 @@
|
|||||||
nameElement.focus();
|
nameElement.focus();
|
||||||
foodSearch = [];
|
foodSearch = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!per100Edited)
|
|
||||||
GetLastPer100(name.trim()).then((res) => {
|
|
||||||
// Prevent search when there's nothing to search
|
|
||||||
// Sometimes we get search results after deleting name
|
|
||||||
if (res.success && res.data && name) {
|
|
||||||
foodSearch = res.data;
|
|
||||||
} else {
|
|
||||||
foodSearch = [];
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let hiLiteIndex: number | null = null;
|
let hiLiteIndex: number | null = null;
|
||||||
@@ -99,8 +103,9 @@
|
|||||||
// return;
|
// return;
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
function setInputVal(val: string) {
|
function setInputVal(food: main.Food) {
|
||||||
name = val;
|
name = food.food;
|
||||||
|
per100 = String(food.per100);
|
||||||
hiLiteIndex = null;
|
hiLiteIndex = null;
|
||||||
foodSearch = [];
|
foodSearch = [];
|
||||||
}
|
}
|
||||||
@@ -112,6 +117,18 @@
|
|||||||
autocompleteList.style.left = `${left}px`;
|
autocompleteList.style.left = `${left}px`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let timeout: number;
|
||||||
|
function handleFocusOut() {
|
||||||
|
timeout = setTimeout(() => {
|
||||||
|
foodSearch = [];
|
||||||
|
}, 100);
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleFocusIn() {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
updateAutocomplete();
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<!-- <svelte:window on:keydown={navigateList} /> -->
|
<!-- <svelte:window on:keydown={navigateList} /> -->
|
||||||
@@ -131,6 +148,8 @@
|
|||||||
contenteditable="true"
|
contenteditable="true"
|
||||||
autofocus
|
autofocus
|
||||||
on:keydown={update}
|
on:keydown={update}
|
||||||
|
on:focusin={handleFocusIn}
|
||||||
|
on:focusout={handleFocusOut}
|
||||||
bind:this={nameElement}
|
bind:this={nameElement}
|
||||||
/>
|
/>
|
||||||
<td
|
<td
|
||||||
@@ -160,11 +179,7 @@
|
|||||||
{#if foodSearch.length > 0}
|
{#if foodSearch.length > 0}
|
||||||
<ul bind:this={autocompleteList} class="z-50 fixed top-0 left-0 w-3/12 border border-x-gray-800">
|
<ul bind:this={autocompleteList} class="z-50 fixed top-0 left-0 w-3/12 border border-x-gray-800">
|
||||||
{#each foodSearch as f, i}
|
{#each foodSearch as f, i}
|
||||||
<FoodSearchEntry
|
<FoodSearchEntry itemLabel={f.food} highlighted={i == hiLiteIndex} on:click={() => setInputVal(f)} />
|
||||||
itemLabel={f.food}
|
|
||||||
highlighted={i == hiLiteIndex}
|
|
||||||
on:click={() => setInputVal(f.food)}
|
|
||||||
/>
|
|
||||||
{/each}
|
{/each}
|
||||||
</ul>
|
</ul>
|
||||||
{/if}
|
{/if}
|
||||||
|
38
main.go
38
main.go
@@ -24,7 +24,7 @@ func init() {
|
|||||||
logFile, err := os.Create("main.log")
|
logFile, err := os.Create("main.log")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error creating log file: %v", err)
|
log.Printf("Error creating log file: %v", err)
|
||||||
os.Exit(1)
|
return
|
||||||
}
|
}
|
||||||
logger := io.MultiWriter(os.Stdout, logFile)
|
logger := io.MultiWriter(os.Stdout, logFile)
|
||||||
log.SetOutput(logger)
|
log.SetOutput(logger)
|
||||||
@@ -42,33 +42,59 @@ var (
|
|||||||
|
|
||||||
//go:embed food.ddl
|
//go:embed food.ddl
|
||||||
var foodDDL string
|
var foodDDL string
|
||||||
|
//go:embed spellfix.dll
|
||||||
|
var spellfixDll []byte
|
||||||
|
|
||||||
// TODO: Embed food.ddl and create DB if no exists
|
// TODO: Embed food.ddl and create DB if no exists
|
||||||
// TODO: Add averages to graphs (ie. R2) https://stackoverflow.com/questions/60622195/how-to-draw-a-linear-regression-line-in-chart-js
|
// TODO: Add averages to graphs (ie. R2) https://stackoverflow.com/questions/60622195/how-to-draw-a-linear-regression-line-in-chart-js
|
||||||
func main() {
|
func main() {
|
||||||
|
_, err := os.Open("spellfix.dll")
|
||||||
|
if err != nil && !os.IsNotExist(err) {
|
||||||
|
Error.Printf("Error looking for spellfix.dll: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err != nil && os.IsNotExist(err) {
|
||||||
|
log.Printf("No spellfix.dll found, creating...")
|
||||||
|
file, err := os.Create("spellfix.dll")
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("Error creating spellfix.dll: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = file.Write(spellfixDll)
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("Error writing spellfix.dll: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
file.Close()
|
||||||
|
}
|
||||||
|
|
||||||
dbpath := flag.String("db", "food.db", "Path to the database file")
|
dbpath := flag.String("db", "food.db", "Path to the database file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
db := DB{path: *dbpath}
|
db := DB{path: *dbpath}
|
||||||
err := db.Open()
|
err = db.Open()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Printf("%++v", err)
|
Error.Printf("%++v", err)
|
||||||
os.Exit(1)
|
return
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
db.Init(foodDDL)
|
err = db.Init(foodDDL)
|
||||||
|
if err != nil {
|
||||||
|
Error.Printf("Error initializing database: %++v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
settingsService = &SettingsService{db: &db}
|
settingsService = &SettingsService{db: &db}
|
||||||
err = settingsService.LoadSettings()
|
err = settingsService.LoadSettings()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
Error.Printf("%++v", err)
|
Error.Printf("%++v", err)
|
||||||
os.Exit(1)
|
return
|
||||||
}
|
}
|
||||||
log.Printf("Loaded settings as: %++v", Settings)
|
log.Printf("Loaded settings as: %++v", Settings)
|
||||||
|
|
||||||
foodService = &FoodService{db: &db}
|
foodService = &FoodService{db: &db}
|
||||||
weightService = &WeightService{db: &db}
|
weightService = &WeightService{db: &db}
|
||||||
|
|
||||||
// Create an instance of the app structure
|
// Create an instance of the app structure
|
||||||
app := NewApp()
|
app := NewApp()
|
||||||
|
|
||||||
|
BIN
spellfix.dll
Normal file
BIN
spellfix.dll
Normal file
Binary file not shown.
Reference in New Issue
Block a user