Skip to content

Commit adb3b26

Browse files
committed
add: merge branch 'master' into fix-urls
2 parents c15792b + 800c472 commit adb3b26

4 files changed

Lines changed: 75 additions & 29 deletions

File tree

internal/bot/models/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ type User struct {
1212
FirstName string `json:"first_name"`
1313
LastName string `json:"last_name"`
1414
LangCode string `json:"language_code"`
15+
DarkMode bool `json:"dark_mode" gorm:"default:1"`
1516
PriceAgents []PriceAgent `json:"-"`
1617
}

internal/bot/pricehistory.go

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bot
22

33
import (
44
"GoGeizhalsBot/internal/bot/models"
5+
"GoGeizhalsBot/internal/database"
56
"GoGeizhalsBot/internal/geizhals"
67
"GoGeizhalsBot/internal/prometheus"
78
"bytes"
@@ -29,7 +30,8 @@ func showPriceHistoryHandler(b *gotgbot.Bot, ctx *ext.Context) error {
2930
return fmt.Errorf("showPriceHistoryHandler: failed to parse callback data: %w", parseErr)
3031
}
3132

32-
dateRangeKeyboard, since := generateDateRangeKeyboard(priceagent, "03")
33+
isDarkmode := database.GetDarkmode(ctx.EffectiveUser.Id)
34+
dateRangeKeyboard, since := generateDateRangeKeyboard(priceagent, "03", isDarkmode)
3335
markup := gotgbot.InlineKeyboardMarkup{
3436
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
3537
dateRangeKeyboard,
@@ -44,7 +46,7 @@ func showPriceHistoryHandler(b *gotgbot.Bot, ctx *ext.Context) error {
4446
}
4547

4648
buffer := bytes.NewBuffer([]byte{})
47-
renderChart(priceagent, history, since, buffer)
49+
renderChart(priceagent, history, since, buffer, isDarkmode)
4850

4951
_, _ = bot.DeleteMessage(ctx.EffectiveChat.Id, cb.Message.MessageId)
5052

@@ -66,8 +68,20 @@ func updatePriceHistoryGraphHandler(b *gotgbot.Bot, ctx *ext.Context) error {
6668
return fmt.Errorf("updatePriceHistoryGraphHandler: failed to parse callback data: %w", parseErr)
6769
}
6870

71+
darkMode := database.GetDarkmode(ctx.EffectiveUser.Id)
72+
if menu.Extra != "" {
73+
changeDarkmodeTo := menu.Extra
74+
switch changeDarkmodeTo {
75+
case "0":
76+
darkMode = false
77+
default:
78+
darkMode = true
79+
}
80+
}
81+
database.UpdateDarkMode(ctx.EffectiveUser.Id, darkMode)
82+
6983
dateRange := menu.SubMenu
70-
dateRangeKeyboard, since := generateDateRangeKeyboard(priceagent, dateRange)
84+
dateRangeKeyboard, since := generateDateRangeKeyboard(priceagent, dateRange, darkMode)
7185

7286
markup := gotgbot.InlineKeyboardMarkup{
7387
InlineKeyboard: [][]gotgbot.InlineKeyboardButton{
@@ -83,7 +97,7 @@ func updatePriceHistoryGraphHandler(b *gotgbot.Bot, ctx *ext.Context) error {
8397
}
8498

8599
buffer := bytes.NewBuffer([]byte{})
86-
renderChart(priceagent, history, since, buffer)
100+
renderChart(priceagent, history, since, buffer, darkMode)
87101

88102
caption := fmt.Sprintf("%s\nFür welchen Zeitraum möchtest du die Preishistorie sehen?", bold(createLink(priceagent.EntityURL(), priceagent.Name)))
89103
newPic := gotgbot.InputMediaPhoto{Media: buffer, Caption: caption, ParseMode: "HTML"}
@@ -95,12 +109,20 @@ func updatePriceHistoryGraphHandler(b *gotgbot.Bot, ctx *ext.Context) error {
95109
}
96110

97111
// generateDateRangeKeyboard generates the keyboard for the date range buttons below the pricehistory chart.
98-
func generateDateRangeKeyboard(priceagent models.PriceAgent, dateRange string) ([]gotgbot.InlineKeyboardButton, time.Time) {
112+
func generateDateRangeKeyboard(priceagent models.PriceAgent, dateRange string, isDarkmode bool) ([]gotgbot.InlineKeyboardButton, time.Time) {
113+
themeButton := "🌑"
114+
switchTheme := 1
115+
if isDarkmode {
116+
themeButton = "🌕"
117+
switchTheme = 0
118+
}
119+
99120
dateRangeKeyboard := []gotgbot.InlineKeyboardButton{
100121
{Text: "1M", CallbackData: fmt.Sprintf("m05_01_%d", priceagent.ID)},
101122
{Text: "3M", CallbackData: fmt.Sprintf("m05_03_%d", priceagent.ID)},
102123
{Text: "6M", CallbackData: fmt.Sprintf("m05_06_%d", priceagent.ID)},
103124
{Text: "12M", CallbackData: fmt.Sprintf("m05_12_%d", priceagent.ID)},
125+
{Text: themeButton, CallbackData: fmt.Sprintf("m05_%s_%d_%d", dateRange, priceagent.ID, switchTheme)},
104126
}
105127

106128
var since time.Time
@@ -118,34 +140,35 @@ func generateDateRangeKeyboard(priceagent models.PriceAgent, dateRange string) (
118140
dateRangeKeyboard[3].Text = "🔘 12M"
119141
since = time.Now().AddDate(0, -12, 0)
120142
default:
121-
return generateDateRangeKeyboard(priceagent, "03")
143+
return generateDateRangeKeyboard(priceagent, "03", isDarkmode)
122144
}
123145
return dateRangeKeyboard, since
124146
}
125147

126148
// renderChart renders a price history chart to the given writer.
127-
func renderChart(priceagent models.PriceAgent, history geizhals.PriceHistory, since time.Time, w io.Writer) {
149+
func renderChart(priceagent models.PriceAgent, history geizhals.PriceHistory, since time.Time, w io.Writer, darkmode bool) {
128150
prometheus.GraphsRendered.Inc()
129-
darkFontColor := drawing.ColorFromHex("c2c2c2")
130-
fontColor := darkFontColor
131-
132-
darkChartBackgroundColor := drawing.ColorFromHex("161b2b")
133-
chartBackgroundColor := darkChartBackgroundColor
134-
135-
darkRegressionColor := drawing.ColorFromHex("e8a71a")
136-
regressionColor := darkRegressionColor
137-
138-
darkMainSeriesColor := drawing.ColorFromHex("2569d1")
139-
mainSeriesColor := darkMainSeriesColor
140-
141-
darkLegendBackgroundColor := drawing.ColorFromHex("2d364f")
142-
legendBackgroundColor := darkLegendBackgroundColor
151+
var fontColor, chartBackgroundColor, regressionColor, mainSeriesColor, legendBackgroundColor, gridMajorStrokeColor, gridMinorStrokeColor drawing.Color
152+
153+
fontColor = chart.DefaultTextColor
154+
chartBackgroundColor = chart.DefaultBackgroundColor
155+
regressionColor = drawing.ColorFromHex("e8a71a")
156+
mainSeriesColor = drawing.ColorFromHex("2569d1")
157+
legendBackgroundColor = chart.DefaultBackgroundColor
158+
gridMajorStrokeColor = drawing.Color{R: 192, G: 192, B: 192, A: 100}
159+
gridMinorStrokeColor = drawing.Color{R: 192, G: 192, B: 192, A: 64}
160+
161+
if darkmode {
162+
fontColor = drawing.ColorFromHex("c2c2c2")
163+
chartBackgroundColor = drawing.ColorFromHex("161b2b")
164+
legendBackgroundColor = drawing.ColorFromHex("2d364f")
165+
}
143166

144167
mainSeries := chart.TimeSeries{
145168
Name: priceagent.Name,
146169
Style: chart.Style{
147170
StrokeColor: mainSeriesColor,
148-
StrokeWidth: 2,
171+
StrokeWidth: 3,
149172
},
150173
}
151174

@@ -188,7 +211,7 @@ func renderChart(priceagent models.PriceAgent, history geizhals.PriceHistory, si
188211
InnerSeries: mainSeries,
189212
Style: chart.Style{
190213
StrokeColor: regressionColor,
191-
StrokeWidth: 2,
214+
StrokeWidth: 3,
192215
StrokeDashArray: []float64{5.0, 5.0},
193216
},
194217
}
@@ -198,17 +221,17 @@ func renderChart(priceagent models.PriceAgent, history geizhals.PriceHistory, si
198221
FontColor: fontColor,
199222
}
200223

201-
fontStyle := chart.Style{FontColor: fontColor}
224+
fontStyle := chart.Style{FontColor: fontColor, FontSize: 12}
202225

203226
gridMajorStyle := chart.Style{
204227
Hidden: false,
205-
StrokeColor: drawing.Color{R: 192, G: 192, B: 192, A: 100},
228+
StrokeColor: gridMajorStrokeColor,
206229
StrokeWidth: 0.5,
207230
}
208231

209232
gridMinorStyle := chart.Style{
210233
Hidden: false,
211-
StrokeColor: drawing.Color{R: 192, G: 192, B: 192, A: 64},
234+
StrokeColor: gridMinorStrokeColor,
212235
StrokeWidth: 0.5,
213236
}
214237

@@ -242,7 +265,8 @@ func renderChart(priceagent models.PriceAgent, history geizhals.PriceHistory, si
242265
}
243266
graph.Elements = []chart.Renderable{chart.Legend(&graph, chart.Style{
244267
FillColor: legendBackgroundColor,
245-
FontColor: fontColor,
268+
FontColor: fontStyle.FontColor,
269+
FontSize: fontStyle.FontSize,
246270
})}
247271

248272
renderErr := graph.Render(chart.PNG, w)

internal/bot/util.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bot
22

33
import (
44
"GoGeizhalsBot/internal/bot/models"
5+
"errors"
56
"fmt"
67
"html"
78
"strconv"
@@ -65,11 +66,14 @@ func generateEntityKeyboard(priceagents []models.PriceAgent, menuID string, numC
6566
}
6667

6768
func parseIDFromCallbackData(callbackData string, prefix string) (int64, error) {
68-
priceagentIDString := strings.TrimPrefix(callbackData, prefix)
69+
priceagentIDString := callbackData
6970
results := strings.Split(priceagentIDString, "_")
7071

7172
// get last element from results
72-
priceagentIDString = results[len(results)-1]
73+
if len(results) != 3 && len(results) != 4 {
74+
return 0, errors.New("couldn't parse priceagent ID - wrong number of results")
75+
}
76+
priceagentIDString = results[2]
7377

7478
priceagentID, parseErr := strconv.Atoi(priceagentIDString)
7579
if parseErr != nil {

internal/database/database.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ func CreateUser(user models.User) error {
8686
return tx.Error
8787
}
8888

89+
func GetDarkmode(userID int64) bool {
90+
var user models.User
91+
tx := db.Where("id = ?", userID).First(&user)
92+
if tx.Error != nil {
93+
log.Println(tx.Error)
94+
return true
95+
}
96+
return user.DarkMode
97+
}
98+
99+
func UpdateDarkMode(userID int64, darkMode bool) {
100+
tx := db.Model(&models.User{}).Where("id = ?", userID).Update("dark_mode", darkMode)
101+
if tx.Error != nil {
102+
log.Println(tx.Error)
103+
}
104+
}
105+
89106
func GetAllUsers() []models.User {
90107
var users []models.User
91108
db.Find(&users)

0 commit comments

Comments
 (0)