Skip to content

Commit 5d298b7

Browse files
committed
get pro message and save API key
1 parent f44ec31 commit 5d298b7

4 files changed

Lines changed: 83 additions & 16 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Weather Widget Build Configuration
22
BINARY_NAME=weatherwidget
33
CMD_PATH=./cmd/weatherwidget/
4-
GO_CMD=/usr/local/go/bin/go
4+
GO_CMD=/usr/bin/go
55

66
# Detect OS
77
ifeq ($(OS),Windows_NT)

internal/i18n/locales/en-GB.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"settings.provider.getFreeApi": "Get FREE API",
3030
"settings.provider.getProApi": "Get PRO API",
3131
"settings.provider.note": "Note: \nFree = 120 minutes refresh rate (limited). \nPro = 10 minutes refresh rate (unlimited).",
32+
"settings.provider.apiKeyActivation.title": "Your Pro API Key",
33+
"settings.provider.apiKeyActivation.message": "Keep this key safe. It is, or will be, activated once your subscription is confirmed and will be disabled if the subscription is cancelled.",
3234
"settings.locations.savedTitle": "Saved Cities",
3335
"settings.locations.savedSubtitle": "Manage your tracked locations (1–5 cities).",
3436
"settings.locations.addTitle": "Add New City",

internal/i18n/locales/pt-BR.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
"settings.provider.getFreeApi": "Obter API GRATUITA",
3030
"settings.provider.getProApi": "Obter API PRO",
3131
"settings.provider.note": "Nota: \nGratuito = taxa de atualização de 120 minutos (limitado). \nPro = taxa de atualização de 10 minutos (ilimitado).",
32+
"settings.provider.apiKeyActivation.title": "Sua Chave API Pro",
33+
"settings.provider.apiKeyActivation.message": "Guarde esta chave com segurança. Ela será ativada assim que a sua assinatura for confirmada e será desativada se a assinatura for cancelada.",
3234
"settings.locations.savedTitle": "Cidades Salvas",
3335
"settings.locations.savedSubtitle": "Gerencie suas localizações rastreadas (1–5 cidades).",
3436
"settings.locations.addTitle": "Adicionar Nova Cidade",

internal/ui/settings.go

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ import (
2525
"weatherwidget/internal/i18n"
2626
)
2727

28+
// ewwAPIKeyLen is the expected length of an EasyWeatherWidget API key (UUID v4).
29+
const ewwAPIKeyLen = 36
30+
31+
// owmAPIKeyLen is the expected length of an OpenWeatherMap API key.
32+
const owmAPIKeyLen = 33
33+
2834
// generateClientReferenceID creates a random UUID v4 string (e.g. "f169fecc-de0c-4de1-b8c8-4ec3701b671c")
2935
// to be used as a unique client_reference_id for Stripe payment links.
3036
func generateClientReferenceID() string {
@@ -121,23 +127,74 @@ func (u *UIManager) ShowSettings(cfg *config.Config, onSave func(*config.Config)
121127
providerSelect.SetSelected("OpenWeatherMap (Free)")
122128
}
123129

130+
apiKeyEntry := widget.NewEntry()
131+
apiKeyEntry.SetPlaceHolder(u.t("settings.provider.apiKeyPlaceholder"))
132+
if cfg.APIConfig != nil {
133+
apiKeyEntry.SetText(cfg.APIConfig.APIKey)
134+
}
135+
136+
// Activation card — shown when a Pro API key is active or just generated.
137+
// Uses a Card with bold title, monospace key, and descriptive message
138+
// for a polished look that stands out from the plain note above.
139+
activationKeyText := widget.NewRichTextFromMarkdown("")
140+
activationMsg := widget.NewLabel("")
141+
activationMsg.Wrapping = fyne.TextWrapWord
142+
activationCard := widget.NewCard("", "", container.NewVBox(
143+
activationKeyText,
144+
activationMsg,
145+
))
146+
activationCard.Hide()
147+
148+
// showActivationCard populates and reveals the activation card.
149+
showActivationCard := func(key string) {
150+
activationCard.SetTitle(u.t("settings.provider.apiKeyActivation.title"))
151+
activationKeyText.ParseMarkdown(fmt.Sprintf("`%s`", key))
152+
activationMsg.SetText(u.t("settings.provider.apiKeyActivation.message"))
153+
activationCard.Show()
154+
}
155+
156+
// If the config already has an EWW-length key, show the activation card on load.
157+
if cfg.APIConfig != nil && cfg.APIConfig.Provider == "easyweatherwidget" && len(cfg.APIConfig.APIKey) == ewwAPIKeyLen {
158+
showActivationCard(cfg.APIConfig.APIKey)
159+
}
160+
161+
// handleGetProAPI generates a new client reference ID, persists it as
162+
// the EWW API key in config.json, updates the UI entry and activation
163+
// label, then opens the Stripe payment link.
164+
handleGetProAPI := func() {
165+
currentKey := strings.TrimSpace(apiKeyEntry.Text)
166+
167+
// If the key is already a 36-char EWW UUID, reuse it instead of
168+
// generating a new one — the user already has a key.
169+
clientRef := currentKey
170+
if len(currentKey) != ewwAPIKeyLen {
171+
clientRef = generateClientReferenceID()
172+
173+
// Persist provider + new key to config.json immediately.
174+
if cfg.APIConfig == nil {
175+
cfg.APIConfig = &config.APIConfig{}
176+
}
177+
cfg.APIConfig.Provider = "easyweatherwidget"
178+
cfg.APIConfig.APIKey = clientRef
179+
180+
// Update the UI entry so the user sees the new key.
181+
apiKeyEntry.SetText(clientRef)
182+
}
183+
184+
// Show the activation card.
185+
showActivationCard(clientRef)
186+
187+
parsedURL, _ := url.Parse("https://buy.stripe.com/bJe3cvaJOa650fQ8cPdZ603?client_reference_id=" + clientRef)
188+
_ = u.app.OpenURL(parsedURL)
189+
}
190+
124191
getApiBtn := widget.NewButton(u.t("settings.provider.getFreeApi"), func() {
125192
parsedURL, _ := url.Parse("https://openweathermap.org/")
126193
_ = u.app.OpenURL(parsedURL)
127194
})
128195
if providerSelect.Selected == "EasyWeatherWidget (Pro)" {
129196
getApiBtn.SetText(u.t("settings.provider.getProApi"))
130-
getApiBtn.OnTapped = func() {
131-
clientRef := generateClientReferenceID()
132-
parsedURL, _ := url.Parse("https://buy.stripe.com/bJe3cvaJOa650fQ8cPdZ603?client_reference_id=" + clientRef)
133-
_ = u.app.OpenURL(parsedURL)
134-
}
135-
}
136-
137-
apiKeyEntry := widget.NewEntry()
138-
apiKeyEntry.SetPlaceHolder(u.t("settings.provider.apiKeyPlaceholder"))
139-
if cfg.APIConfig != nil {
140-
apiKeyEntry.SetText(cfg.APIConfig.APIKey)
197+
getApiBtn.OnTapped = handleGetProAPI
141198
}
142199

143200
noteLabel := widget.NewLabel(u.t("settings.provider.note"))
@@ -149,6 +206,7 @@ func (u *UIManager) ShowSettings(cfg *config.Config, onSave func(*config.Config)
149206
widget.NewFormItem(u.t("settings.provider.apiKeyLabel"), apiKeyEntry),
150207
),
151208
noteLabel,
209+
activationCard,
152210
)
153211

154212
// ── Position ─────────────────────────────────────────────────────────
@@ -315,12 +373,17 @@ func (u *UIManager) ShowSettings(cfg *config.Config, onSave func(*config.Config)
315373
parsedURL, _ := url.Parse("https://openweathermap.org/")
316374
_ = u.app.OpenURL(parsedURL)
317375
}
376+
activationCard.Hide()
318377
} else {
319378
getApiBtn.SetText(u.t("settings.provider.getProApi"))
320-
getApiBtn.OnTapped = func() {
321-
clientRef := generateClientReferenceID()
322-
parsedURL, _ := url.Parse("https://buy.stripe.com/bJe3cvaJOa650fQ8cPdZ603?client_reference_id=" + clientRef)
323-
_ = u.app.OpenURL(parsedURL)
379+
getApiBtn.OnTapped = handleGetProAPI
380+
381+
// If the current key is already an EWW-length key, show the activation card.
382+
currentKey := strings.TrimSpace(apiKeyEntry.Text)
383+
if len(currentKey) == ewwAPIKeyLen {
384+
showActivationCard(currentKey)
385+
} else {
386+
activationCard.Hide()
324387
}
325388
}
326389

0 commit comments

Comments
 (0)