@@ -2,6 +2,7 @@ package bot
22
33import (
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\n Fü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 )
0 commit comments