Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonmarro committed Jan 1, 2025
1 parent a8185f0 commit 7a51363
Show file tree
Hide file tree
Showing 17 changed files with 319 additions and 24 deletions.
73 changes: 73 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch package",
"type": "go",
"request": "launch",
"mode": "auto",
"remotePath": "",
"port": 38697,
"host": "127.0.0.1",
"program": "${workspaceFolder}",
"env": {},
"args": [],
"cwd": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env",
"buildFlags": ""
},
{
"name": "Debug current package",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 38697,
"host": "127.0.0.1",
"program": "${workspaceFolder}/cmd/goldwatcher/main.go",
"env": {},
"args": [],
"cwd": "${workspaceFolder}",
"envFile": "${workspaceFolder}/.env",
"buildFlags": ""
},
{
"name": "Launch test function",
"type": "go",
"request": "launch",
"mode": "test",
"program": "${workspaceFolder}",
"args": ["-test.run", "MyTestFunction"]
},
{
"name": "Attach main",
"type": "go",
"request": "attach",
"mode": "debug",
"remotePath": "",
"port": 38697,
"host": "127.0.0.1",
"program": "${workspaceFolder}/main.go",
"env": {},
"args": [],
"cwd": "${workspaceFolder}",
"processId": "",
"envFile": "${workspaceFolder}/.env",
"buildFlags": ""
},
{
"name": "Attach to Process",
"type": "go",
"request": "attach",
"mode": "local",
"processId": 0
},
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "${file}"
}
]
}
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ build:
@cd ${SOURCE_DIR} && fyne package -os linux --release

run:
@echo "Running $(BINARY_NAME)..."
@go run $(SOURCE_DIR)
echo "Running $(BINARY_NAME)..."
@env DB_PATH="./sql.db" go run $(SOURCE_DIR)

clean:
@echo "Cleaning up..."
Expand Down
Binary file added cmd/goldwatcher/gold_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion cmd/goldwatcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func main() {

// create and size a fyne window
myApp.MainWindow = a.NewWindow("Gold Watcher")
myApp.MainWindow.Resize(fyne.NewSize(800, 500))
myApp.MainWindow.Resize(fyne.NewSize(770, 410))
myApp.MainWindow.SetFixedSize(true)
myApp.MainWindow.CenterOnScreen()
myApp.MainWindow.SetMaster()
Expand Down
Binary file modified gold_chart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 21 additions & 11 deletions internal/application/ui.go → internal/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
Expand All @@ -15,15 +14,20 @@ import (
)

type Config struct {
App fyne.App
InfoLog *log.Logger
ErrorLog *log.Logger
MainWindow fyne.Window
PriceContainer *fyne.Container
ToolBar *widget.Toolbar
PriceChartContainer *fyne.Container
GoldService services.GoldService
HoldingRepository repository.HoldingRepository
App fyne.App
InfoLog *log.Logger
ErrorLog *log.Logger
MainWindow fyne.Window
PriceContainer *fyne.Container
ToolBar *widget.Toolbar
PriceChartContainer *fyne.Container
Holdings [][]interface{}
HoldingsTable *widget.Table
GoldService services.GoldService
HoldingRepository repository.Repository
AddHoldingsPurchaseAmountEntry *widget.Entry
AddHoldingsPurchaseDateEntry *widget.Entry
AddHoldingsPurchasePriceEntry *widget.Entry
}

func (app *Config) MakeUI() {
Expand All @@ -41,11 +45,12 @@ func (app *Config) MakeUI() {
app.ToolBar = toolBar

priceTabContent := app.pricesTab()
holdingsTabContent := app.holdingsTab()

// get app tabs
tabs := container.NewAppTabs(
container.NewTabItemWithIcon("Prices", theme.HomeIcon(), priceTabContent),
container.NewTabItemWithIcon("Holdings", theme.InfoIcon(), canvas.NewText("Holdings content goes here", nil)),
container.NewTabItemWithIcon("Holdings", theme.InfoIcon(), holdingsTabContent),
)
tabs.SetTabLocation(container.TabLocationTop)

Expand All @@ -69,3 +74,8 @@ func (app *Config) refreshPriceContent() {
app.PriceChartContainer.Objects = []fyne.CanvasObject{chart}
app.PriceChartContainer.Refresh()
}

func (app *Config) refreshHoldingsTable() {
app.Holdings = app.getHoldingsSlice()
app.HoldingsTable.Refresh()
}
83 changes: 79 additions & 4 deletions internal/application/holdings_tab.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,101 @@
package application

import (
"fmt"
"strconv"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"

"github.com/nelsonmarro/gold-watcher/internal/models"
)

func (app *Config) holdingsTab() *fyne.Container {
return container.NewVBox(
widget.NewLabel("Holdings content goes here"),
app.Holdings = app.getHoldingsSlice()
app.HoldingsTable = app.getHoldingsTable()

holdingsCtr := container.NewBorder(
nil,
nil,
nil,
nil,
container.NewAdaptiveGrid(1, app.HoldingsTable),
)

return holdingsCtr
}

func (app *Config) getHoldingsTable() *widget.Table {
return nil
t := widget.NewTable(
func() (int, int) {
return len(app.Holdings), len(app.Holdings[0])
},
func() fyne.CanvasObject {
ctr := container.NewVBox(widget.NewLabel(""))
return ctr
},
func(i widget.TableCellID, o fyne.CanvasObject) {
if i.Col == len(app.Holdings[0])-1 && i.Row != 0 {
// last cell - put a button
w := widget.NewButtonWithIcon("Delete", theme.DeleteIcon(), func() {
dialog.ShowConfirm("Delete?", "", func(deleted bool) {
if deleted {
id, _ := strconv.Atoi(app.Holdings[i.Row][0].(string))
err := app.HoldingRepository.Delete(int64(id))
if err != nil {
app.ErrorLog.Println(err)
}
}
app.refreshHoldingsTable()
}, app.MainWindow)
})
w.Importance = widget.HighImportance

o.(*fyne.Container).Objects = []fyne.CanvasObject{
w,
}
} else {
// put in textual information
o.(*fyne.Container).Objects = []fyne.CanvasObject{
widget.NewLabel(app.Holdings[i.Row][i.Col].(string)),
}
}
})

colWidths := []float32{50, 200, 200, 200, 110}
for i := 0; i < len(colWidths); i++ {
t.SetColumnWidth(i, colWidths[i])
}

return t
}

func (app *Config) getHoldingSlice() [][]interface{} {
func (app *Config) getHoldingsSlice() [][]interface{} {
var slice [][]interface{}

holdings, err := app.currentHoldings()
if err != nil {
app.ErrorLog.Println(err)
}

slice = append(slice, []interface{}{"ID", "Amount", "Price", "Date", "Options"})

for _, x := range holdings {
var currentRow []interface{}

currentRow = append(currentRow, strconv.FormatInt(x.ID, 10))
currentRow = append(currentRow, fmt.Sprintf("%v toz", x.Amount))
currentRow = append(currentRow, fmt.Sprintf("$%2f", float32(x.PurchasePrice/100)))
currentRow = append(currentRow, x.PurchaseDate.Format("2006-01-02"))
currentRow = append(currentRow, widget.NewButton("Delete", func() {
}))

slice = append(slice, currentRow)
}

return slice
}

Expand Down
41 changes: 41 additions & 0 deletions internal/application/holdings_tab_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package application

import (
"slices"
"testing"
)

func TestApp_GetHoldings(t *testing.T) {
holdings, err := testApp.currentHoldings()
if err != nil {
t.Error("failed to get current holdings from db:", err)
}

if len(holdings) != 2 {
t.Errorf("expected holdings slice to return 2 values, got:%v", len(holdings))
}
}

func TestApp_GetHoldingsSlice(t *testing.T) {
slice := testApp.getHoldingsSlice()

if len(slice) != 3 {
t.Errorf("expected to get a holdings slice with 3 rows, but got: %v", len(slice))
}

lable_row := slice[0]
expected_lable_row := []interface{}{"ID", "Amount", "Price", "Date", "Options"}

if !slices.Equal(lable_row, expected_lable_row) {
t.Errorf("wrong values on table's lable row, expected: ['ID', 'Amount', 'Price', 'Date', 'Options'], but got: %s", lable_row)
}

first_holdings_row := slice[1]
first_holdings_row = first_holdings_row[:len(first_holdings_row)-1]

expected_first_holdings_row := []interface{}{"1", "1 toz", "$10.00", "2024-12-31"}

if !slices.Equal(first_holdings_row, expected_first_holdings_row) {
t.Errorf("wrong values on table's 1st row, expected: ['1', '1 toz', '$10.00', '2024-12-31'], but got: %s", first_holdings_row)
}
}
2 changes: 1 addition & 1 deletion internal/application/price_tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (app *Config) getChart() *canvas.Image {
img = canvas.NewImageFromFile("gold_chart.png")
}

img.SetMinSize(fyne.NewSize(800, 500))
img.SetMinSize(fyne.NewSize(770, 410))
img.FillMode = canvas.ImageFillStretch

return img
Expand Down
File renamed without changes.
Loading

0 comments on commit 7a51363

Please sign in to comment.