Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
cbee5bd204 | |||
ec5310a1f3 | |||
ec1c196752 | |||
6064d9847c | |||
235a90b0a7 | |||
4abddac94f | |||
f12c353905 |
5
app.go
5
app.go
@@ -2,7 +2,8 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||
)
|
||||
|
||||
// App struct
|
||||
@@ -139,5 +140,5 @@ func (a *App) SetSetting(key string, value int64) WailsGenericAck {
|
||||
|
||||
//region other
|
||||
func (a *App) Close() {
|
||||
os.Exit(0)
|
||||
runtime.Quit(a.ctx)
|
||||
}
|
@@ -68,14 +68,15 @@ func (s *FoodService) GetLastPer100(name string) ([]FoodSearch, error) {
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
with search_results as (
|
||||
select word, score, f.rowid, f.*
|
||||
select word, score, f.rowid, f.*,
|
||||
row_number() over (partition by f.food order by score asc, date desc) as rn
|
||||
from foodfix
|
||||
inner join food f on f.food == word
|
||||
where word MATCH ?
|
||||
)
|
||||
select %s, score
|
||||
from search_results
|
||||
group by food
|
||||
where rn = 1
|
||||
order by score asc, date desc
|
||||
limit %d
|
||||
`, foodColumns, Settings.SearchLimit)
|
||||
|
@@ -30,9 +30,24 @@
|
||||
name = name.trim();
|
||||
if (!name) {
|
||||
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 }) {
|
||||
name = name.trim();
|
||||
amount = amount.trim();
|
||||
@@ -69,17 +84,6 @@
|
||||
nameElement.focus();
|
||||
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;
|
||||
@@ -101,7 +105,8 @@
|
||||
// }
|
||||
function setInputVal(food: main.Food) {
|
||||
name = food.food;
|
||||
per100 = String(food.per100)
|
||||
per100 = String(food.per100);
|
||||
amount = String(food.amount);
|
||||
hiLiteIndex = null;
|
||||
foodSearch = [];
|
||||
}
|
||||
@@ -113,6 +118,18 @@
|
||||
autocompleteList.style.left = `${left}px`;
|
||||
}
|
||||
}
|
||||
|
||||
let timeout: number;
|
||||
function handleFocusOut() {
|
||||
timeout = setTimeout(() => {
|
||||
foodSearch = [];
|
||||
}, 100);
|
||||
}
|
||||
|
||||
function handleFocusIn() {
|
||||
clearTimeout(timeout);
|
||||
updateAutocomplete();
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- <svelte:window on:keydown={navigateList} /> -->
|
||||
@@ -132,6 +149,8 @@
|
||||
contenteditable="true"
|
||||
autofocus
|
||||
on:keydown={update}
|
||||
on:focusin={handleFocusIn}
|
||||
on:focusout={handleFocusOut}
|
||||
bind:this={nameElement}
|
||||
/>
|
||||
<td
|
||||
@@ -161,11 +180,7 @@
|
||||
{#if foodSearch.length > 0}
|
||||
<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}
|
||||
<FoodSearchEntry
|
||||
itemLabel={f.food}
|
||||
highlighted={i == hiLiteIndex}
|
||||
on:click={() => setInputVal(f)}
|
||||
/>
|
||||
<FoodSearchEntry itemLabel={f.food} highlighted={i == hiLiteIndex} on:click={() => setInputVal(f)} />
|
||||
{/each}
|
||||
</ul>
|
||||
{/if}
|
||||
|
34
main.go
34
main.go
@@ -24,7 +24,7 @@ func init() {
|
||||
logFile, err := os.Create("main.log")
|
||||
if err != nil {
|
||||
log.Printf("Error creating log file: %v", err)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
logger := io.MultiWriter(os.Stdout, logFile)
|
||||
log.SetOutput(logger)
|
||||
@@ -42,37 +42,59 @@ var (
|
||||
|
||||
//go:embed food.ddl
|
||||
var foodDDL string
|
||||
//go:embed spellfix.dll
|
||||
var spellfixDll []byte
|
||||
|
||||
// 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
|
||||
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")
|
||||
flag.Parse()
|
||||
|
||||
db := DB{path: *dbpath}
|
||||
err := db.Open()
|
||||
err = db.Open()
|
||||
if err != nil {
|
||||
Error.Printf("%++v", err)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
defer db.Close()
|
||||
err = db.Init(foodDDL)
|
||||
if err != nil {
|
||||
Error.Printf("Error initializing database: %++v", err)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
|
||||
settingsService = &SettingsService{db: &db}
|
||||
err = settingsService.LoadSettings()
|
||||
if err != nil {
|
||||
Error.Printf("%++v", err)
|
||||
os.Exit(1)
|
||||
return
|
||||
}
|
||||
log.Printf("Loaded settings as: %++v", Settings)
|
||||
|
||||
foodService = &FoodService{db: &db}
|
||||
weightService = &WeightService{db: &db}
|
||||
|
||||
|
||||
// Create an instance of the app structure
|
||||
app := NewApp()
|
||||
|
||||
|
BIN
spellfix.dll
Normal file
BIN
spellfix.dll
Normal file
Binary file not shown.
Reference in New Issue
Block a user