Skip to content

Commit

Permalink
app finished
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonmarro committed Jan 2, 2025
1 parent 7a51363 commit 8118d72
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 22 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ run:
echo "Running $(BINARY_NAME)..."
@env DB_PATH="./sql.db" go run $(SOURCE_DIR)

install:
@echo "Building $(BINARY_NAME) with Fyne..."
@cd ${SOURCE_DIR} && sudo fyne install

clean:
@echo "Cleaning up..."
@go clean
Expand Down
Binary file added assets/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions cmd/goldwatcher/FyneApp.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Website = "https://example.com"

[Details]
Icon = "../../assets/icon.png"
Name = "Gold Watcher"
ID = "lilim.code.goldwatcher.app"
Version = "1.0.0"
Build = 9

[LinuxAndBSD]
GenericName = "Gold Watcher"
Categories = ["Watcher", "Gold"]
Comment = "Trackt the price of gold"
Keywords = ["watcher", "gold"]
Binary file removed cmd/goldwatcher/gold_chart.png
Binary file not shown.
3 changes: 3 additions & 0 deletions cmd/goldwatcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fyne.io/fyne/v2/app"

"github.com/nelsonmarro/gold-watcher/internal/application"
"github.com/nelsonmarro/gold-watcher/internal/helpers"
client "github.com/nelsonmarro/gold-watcher/internal/http"
"github.com/nelsonmarro/gold-watcher/internal/repository"
"github.com/nelsonmarro/gold-watcher/internal/services"
Expand Down Expand Up @@ -47,6 +48,8 @@ func main() {

myApp.MakeUI()

helpers.Currency = a.Preferences().StringWithFallback("currency", "CAD")

// show and run the application
myApp.MainWindow.ShowAndRun()
}
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.
4 changes: 3 additions & 1 deletion internal/application/holdings_tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,9 @@ func (app *Config) getHoldingsSlice() [][]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)))

amount := x.PurchasePrice / 100
currentRow = append(currentRow, fmt.Sprintf("$%2f", amount))
currentRow = append(currentRow, x.PurchaseDate.Format("2006-01-02"))
currentRow = append(currentRow, widget.NewButton("Delete", func() {
}))
Expand Down
9 changes: 0 additions & 9 deletions internal/application/holdings_tab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,4 @@ func TestApp_GetHoldingsSlice(t *testing.T) {
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 @@ -22,7 +22,7 @@ func (app *Config) pricesTab() *fyne.Container {
}

func (app *Config) getChart() *canvas.Image {
apiUrl := fmt.Sprintf("https://goldprice.org/charts/gold_3d_b_o_%s_x.png", strings.ToLower(helpers.CURRENCY))
apiUrl := fmt.Sprintf("https://goldprice.org/charts/gold_3d_b_o_%s_x.png", strings.ToLower(helpers.Currency))
var img *canvas.Image

err := app.downloadFile(apiUrl, "gold_chart.png")
Expand Down
6 changes: 3 additions & 3 deletions internal/application/price_text.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ func (app *Config) getPriceText() (*canvas.Text, *canvas.Text, *canvas.Text) {
displayColor = color.NRGBA{R: 180, G: 0, B: 0, A: 255}
}

openTxt := fmt.Sprintf("Open: $%.4f %s", gold.PreviousClose, helpers.CURRENCY)
currentTxt := fmt.Sprintf("Current: $%.4f %s", gold.Price, helpers.CURRENCY)
changeTxt := fmt.Sprintf("Change: $%.4f %s", gold.Change, helpers.CURRENCY)
openTxt := fmt.Sprintf("Open: $%.4f %s", gold.PreviousClose, helpers.Currency)
currentTxt := fmt.Sprintf("Current: $%.4f %s", gold.Price, helpers.Currency)
changeTxt := fmt.Sprintf("Change: $%.4f %s", gold.Change, helpers.Currency)

open = canvas.NewText(openTxt, nil)
current = canvas.NewText(currentTxt, displayColor)
Expand Down
36 changes: 34 additions & 2 deletions internal/application/toolbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"time"

"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/helpers"
"github.com/nelsonmarro/gold-watcher/internal/models"
)

Expand All @@ -21,16 +23,45 @@ func (app *Config) getToolBar() *widget.Toolbar {
widget.NewToolbarAction(theme.ViewRefreshIcon(), func() {
app.refreshPriceContent()
}),
widget.NewToolbarAction(theme.SettingsIcon(), func() {}),
widget.NewToolbarAction(theme.SettingsIcon(), func() {
w := app.showPreferences()
w.Resize(fyne.Size{Width: 300, Height: 200})
w.Show()
}),
)

return toolBar
}

func (app *Config) showPreferences() fyne.Window {
win := app.App.NewWindow("Preferences")

lbl := widget.NewLabel("Preferred Currency")
curr := widget.NewSelect([]string{
"USD",
"CAD",
"GBP",
}, func(value string) {
helpers.Currency = value
app.App.Preferences().SetString("currency", value)
})
curr.Selected = helpers.Currency

btn := widget.NewButton("Save", func() {
win.Close()
app.refreshPriceContent()
})
btn.Importance = widget.HighImportance

win.SetContent(container.NewVBox(lbl, curr, btn))

return win
}

func (app *Config) addHoldingsDialog() dialog.Dialog {
addAmountEntry := widget.NewEntry()
purchaseDateEntry := widget.NewEntry()
purchasePriceEntry := widget.NewEntry()
addAmountEntry := widget.NewEntry()

app.AddHoldingsPurchaseAmountEntry = addAmountEntry
app.AddHoldingsPurchaseDateEntry = purchaseDateEntry
Expand Down Expand Up @@ -77,6 +108,7 @@ func (app *Config) addHoldingsDialog() dialog.Dialog {
amount, _ := strconv.Atoi(addAmountEntry.Text)
date, _ := time.Parse("2006-01-02", purchaseDateEntry.Text)
price, _ := strconv.ParseFloat(purchasePriceEntry.Text, 64)
price *= 100

_, err := app.HoldingRepository.Create(models.Holding{
Amount: amount,
Expand Down
26 changes: 25 additions & 1 deletion internal/application/toolbar_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package application

import "testing"
import (
"testing"

"fyne.io/fyne/v2/test"
)

func TestApp_getToolBar(t *testing.T) {
tb := testApp.getToolBar()
Expand All @@ -9,3 +13,23 @@ func TestApp_getToolBar(t *testing.T) {
t.Errorf("Expected 4 items, got %d", len(tb.Items))
}
}

func TestApp_addHoldingsDialog(t *testing.T) {
testApp.addHoldingsDialog()

test.Type(testApp.AddHoldingsPurchaseAmountEntry, "1")
test.Type(testApp.AddHoldingsPurchasePriceEntry, "1000")
test.Type(testApp.AddHoldingsPurchaseDateEntry, "2020-01-01")

if testApp.AddHoldingsPurchaseDateEntry.Text != "2020-01-01" {
t.Errorf("expected date entry to have the value: 2020-01-01, but got: %s", testApp.AddHoldingsPurchaseDateEntry.Text)
}

if testApp.AddHoldingsPurchaseAmountEntry.Text != "1" {
t.Errorf("expected amount entry to have the value: 1, but got: %s", testApp.AddHoldingsPurchaseAmountEntry.Text)
}

if testApp.AddHoldingsPurchasePriceEntry.Text != "1000" {
t.Errorf("expected price entry to have the value: 1000, but got: %s", testApp.AddHoldingsPurchasePriceEntry.Text)
}
}
3 changes: 0 additions & 3 deletions internal/helpers/constants.go

This file was deleted.

3 changes: 3 additions & 0 deletions internal/helpers/preferences.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package helpers

var Currency string = "USD"
4 changes: 2 additions & 2 deletions internal/services/gold_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewGoldService(client *client.HttpClient) *goldService {
}

func (s *goldService) GetPrices() (*models.Price, error) {
data, err := s.client.Get(helpers.CURRENCY, true)
data, err := s.client.Get(helpers.Currency, true)
if err != nil {
log.Println("error contacting goldprice.org: ", err)
return nil, err
Expand All @@ -40,7 +40,7 @@ func (s *goldService) GetPrices() (*models.Price, error) {
previous, current, change := gold.Prices[0].PreviousClose, gold.Prices[0].Price, gold.Prices[0].Change

currentInfo := models.Price{
Currency: helpers.CURRENCY,
Currency: helpers.Currency,
Price: current,
Change: change,
PreviousClose: previous,
Expand Down
Binary file removed main
Binary file not shown.

0 comments on commit 8118d72

Please sign in to comment.