feat: add break threshold to settings UI with inline button and callback
This commit is contained in:
@@ -236,6 +236,8 @@ func (h *Handler) HandleCallback(ctx context.Context, cb *models.CallbackQuery)
|
|||||||
h.handleActionCallback(ctx, chatID, msgID, cb.ID, h.toggleReport)
|
h.handleActionCallback(ctx, chatID, msgID, cb.ID, h.toggleReport)
|
||||||
case "reporttime":
|
case "reporttime":
|
||||||
h.reportTimeCallback(ctx, chatID, msgID, cb.ID)
|
h.reportTimeCallback(ctx, chatID, msgID, cb.ID)
|
||||||
|
case "breakthreshold":
|
||||||
|
h.breakThresholdCallback(ctx, chatID, msgID, cb.ID)
|
||||||
case "back_menu":
|
case "back_menu":
|
||||||
h.backToMenu(ctx, chatID, msgID, cb.ID)
|
h.backToMenu(ctx, chatID, msgID, cb.ID)
|
||||||
case "back_settings":
|
case "back_settings":
|
||||||
|
|||||||
@@ -95,6 +95,21 @@ func (h *Handler) handleSetTimezone(ctx context.Context, msg *models.Message) {
|
|||||||
h.sendText(ctx, msg.Chat.ID, fmt.Sprintf("Timezone set to %s (current time: %s)", args, now.Format("15:04")))
|
h.sendText(ctx, msg.Chat.ID, fmt.Sprintf("Timezone set to %s (current time: %s)", args, now.Format("15:04")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// getTodayBreakThreshold returns today's break threshold in seconds for the given chat.
|
||||||
|
func (h *Handler) getTodayBreakThreshold(chatID int64) int64 {
|
||||||
|
user, err := h.getOrCreateUser(chatID)
|
||||||
|
if err != nil {
|
||||||
|
return 300
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
date := now.Format("2006-01-02")
|
||||||
|
day, err := h.DB.GetDay(user.ID, date)
|
||||||
|
if err != nil {
|
||||||
|
return 300
|
||||||
|
}
|
||||||
|
return day.MinBreakThreshold
|
||||||
|
}
|
||||||
|
|
||||||
// handleSetBreakMsg sets the minimum break threshold for today.
|
// handleSetBreakMsg sets the minimum break threshold for today.
|
||||||
func (h *Handler) handleSetBreakMsg(ctx context.Context, msg *models.Message) {
|
func (h *Handler) handleSetBreakMsg(ctx context.Context, msg *models.Message) {
|
||||||
args := strings.TrimSpace(strings.TrimPrefix(msg.Text, "/setbreak"))
|
args := strings.TrimSpace(strings.TrimPrefix(msg.Text, "/setbreak"))
|
||||||
@@ -139,12 +154,13 @@ func (h *Handler) settingsCallback(ctx context.Context, chatID int64, msgID int,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
text, kb := h.buildSettingsKeyboard(user)
|
bt := h.getTodayBreakThreshold(chatID)
|
||||||
|
text, kb := h.buildSettingsKeyboard(user, bt)
|
||||||
h.editText(ctx, chatID, msgID, text, &kb)
|
h.editText(ctx, chatID, msgID, text, &kb)
|
||||||
}
|
}
|
||||||
|
|
||||||
// buildSettingsKeyboard returns the settings menu text and inline keyboard.
|
// buildSettingsKeyboard returns the settings menu text and inline keyboard.
|
||||||
func (h *Handler) buildSettingsKeyboard(user *db.User) (string, models.InlineKeyboardMarkup) {
|
func (h *Handler) buildSettingsKeyboard(user *db.User, breakThreshold int64) (string, models.InlineKeyboardMarkup) {
|
||||||
reportStatus := "disabled"
|
reportStatus := "disabled"
|
||||||
if user.ReportEnabled {
|
if user.ReportEnabled {
|
||||||
reportStatus = "enabled"
|
reportStatus = "enabled"
|
||||||
@@ -155,6 +171,7 @@ func (h *Handler) buildSettingsKeyboard(user *db.User) (string, models.InlineKey
|
|||||||
{{Text: fmt.Sprintf("Calendar: %s", user.Calendar), CallbackData: "caltype"}},
|
{{Text: fmt.Sprintf("Calendar: %s", user.Calendar), CallbackData: "caltype"}},
|
||||||
{{Text: fmt.Sprintf("Report: %s", reportStatus), CallbackData: "reporttoggle"}},
|
{{Text: fmt.Sprintf("Report: %s", reportStatus), CallbackData: "reporttoggle"}},
|
||||||
{{Text: fmt.Sprintf("Report time: %s", user.ReportTime), CallbackData: "reporttime"}},
|
{{Text: fmt.Sprintf("Report time: %s", user.ReportTime), CallbackData: "reporttime"}},
|
||||||
|
{{Text: fmt.Sprintf("Break threshold: %dm", breakThreshold/60), CallbackData: "breakthreshold"}},
|
||||||
{{Text: "History", CallbackData: "history"}},
|
{{Text: "History", CallbackData: "history"}},
|
||||||
{{Text: "Back to Menu", CallbackData: "back_menu"}},
|
{{Text: "Back to Menu", CallbackData: "back_menu"}},
|
||||||
}
|
}
|
||||||
@@ -255,6 +272,19 @@ func (h *Handler) reportTimeCallback(ctx context.Context, chatID int64, msgID in
|
|||||||
h.editText(ctx, chatID, msgID, text, &kb)
|
h.editText(ctx, chatID, msgID, text, &kb)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// breakThresholdCallback shows the current break threshold and how to change it.
|
||||||
|
func (h *Handler) breakThresholdCallback(ctx context.Context, chatID int64, msgID int, callbackID string) {
|
||||||
|
h.answerCb(ctx, callbackID)
|
||||||
|
bt := h.getTodayBreakThreshold(chatID)
|
||||||
|
text := fmt.Sprintf("Current break threshold: %dm\n\nUse /setbreak <minutes> to change it (0-480).\nGaps shorter than the threshold are absorbed into work time.", bt/60)
|
||||||
|
kb := models.InlineKeyboardMarkup{
|
||||||
|
InlineKeyboard: [][]models.InlineKeyboardButton{
|
||||||
|
{{Text: "Back to Settings", CallbackData: "back_settings"}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
h.editText(ctx, chatID, msgID, text, &kb)
|
||||||
|
}
|
||||||
|
|
||||||
// backToSettings returns to the main settings menu.
|
// backToSettings returns to the main settings menu.
|
||||||
func (h *Handler) backToSettings(ctx context.Context, chatID int64, msgID int, callbackID string) {
|
func (h *Handler) backToSettings(ctx context.Context, chatID int64, msgID int, callbackID string) {
|
||||||
h.answerCb(ctx, callbackID)
|
h.answerCb(ctx, callbackID)
|
||||||
@@ -262,7 +292,8 @@ func (h *Handler) backToSettings(ctx context.Context, chatID int64, msgID int, c
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
text, kb := h.buildSettingsKeyboard(user)
|
bt := h.getTodayBreakThreshold(chatID)
|
||||||
|
text, kb := h.buildSettingsKeyboard(user, bt)
|
||||||
h.editText(ctx, chatID, msgID, text, &kb)
|
h.editText(ctx, chatID, msgID, text, &kb)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,6 +347,7 @@ func (h *Handler) selectCalendar(ctx context.Context, chatID int64, msgID int, c
|
|||||||
if err := h.DB.UpdateUser(user); err != nil {
|
if err := h.DB.UpdateUser(user); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
text, kb := h.buildSettingsKeyboard(user)
|
bt := h.getTodayBreakThreshold(chatID)
|
||||||
|
text, kb := h.buildSettingsKeyboard(user, bt)
|
||||||
h.editText(ctx, chatID, msgID, text, &kb)
|
h.editText(ctx, chatID, msgID, text, &kb)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user