-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathholdings-tab.go
More file actions
110 lines (92 loc) · 2.71 KB
/
holdings-tab.go
File metadata and controls
110 lines (92 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"goldwatcher/repository"
"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"
)
func (app *Config) holdingsTab() *fyne.Container {
app.Holdings = app.getHoldingSlice()
app.HoldingsTable = app.getHoldingsTable()
holdingsContainer := container.NewBorder(
nil,
nil,
nil,
nil,
container.NewAdaptiveGrid(1, app.HoldingsTable),
)
return holdingsContainer
}
func (app *Config) getHoldingsTable() *widget.Table {
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 in a button
w := widget.NewButtonWithIcon("Delete", theme.DeleteIcon(), func() {
dialog.ShowConfirm("Delete?", "Are you sure you want to delete this holding?", func(deleted bool) {
if deleted {
id, _ := strconv.Atoi(app.Holdings[i.Row][0].(string))
err := app.DB.DeleteHolding(int64(id))
if err != nil {
app.ErrorLog.Println(err)
}
}
// refresh the holdings table
app.refreshHoldingsTable()
}, app.MainWindow)
})
w.Importance = widget.HighImportance
o.(*fyne.Container).Objects = []fyne.CanvasObject{
w,
}
} else {
// we're just putting in textual information
o.(*fyne.Container).Objects = []fyne.CanvasObject{
widget.NewLabel(app.Holdings[i.Row][i.Col].(string)),
}
}
})
colWidths := []float32{50, 200, 200, 200, 100}
for i := 0; i < len(colWidths); i++ {
t.SetColumnWidth(i, colWidths[i])
}
return t
}
func (app *Config) getHoldingSlice() [][]interface{} {
var slice [][]interface{}
holdings, err := app.currentHoldings()
if err != nil {
app.ErrorLog.Println(err)
}
slice = append(slice, []interface{}{"ID", "Amount", "Price", "Date", "Delete"})
for _, h := range holdings {
var currentRow []interface{}
currentAmount := float32(h.PurchasePrice) / 100
currentRow = append(currentRow, strconv.FormatInt(h.ID, 10))
currentRow = append(currentRow, fmt.Sprintf("%d toz", h.Amount))
currentRow = append(currentRow, fmt.Sprintf("$%.2f", currentAmount))
currentRow = append(currentRow, h.PurchaseDate.Format("2006-01-02"))
currentRow = append(currentRow, widget.NewButton("Delete", func() {}))
slice = append(slice, currentRow)
}
return slice
}
func (app *Config) currentHoldings() ([]repository.Holdings, error) {
holdings, err := app.DB.AllHoldings()
if err != nil {
app.ErrorLog.Println(err)
return nil, err
}
return holdings, nil
}