feat: add break threshold to settings UI with inline button and callback

This commit is contained in:
2026-06-24 20:58:18 +03:30
parent c9dd076235
commit e399c3ee88
2 changed files with 38 additions and 4 deletions

View File

@@ -236,6 +236,8 @@ func (h *Handler) HandleCallback(ctx context.Context, cb *models.CallbackQuery)
h.handleActionCallback(ctx, chatID, msgID, cb.ID, h.toggleReport)
case "reporttime":
h.reportTimeCallback(ctx, chatID, msgID, cb.ID)
case "breakthreshold":
h.breakThresholdCallback(ctx, chatID, msgID, cb.ID)
case "back_menu":
h.backToMenu(ctx, chatID, msgID, cb.ID)
case "back_settings":

View File

@@ -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")))
}
// 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.
func (h *Handler) handleSetBreakMsg(ctx context.Context, msg *models.Message) {
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 {
return
}
text, kb := h.buildSettingsKeyboard(user)
bt := h.getTodayBreakThreshold(chatID)
text, kb := h.buildSettingsKeyboard(user, bt)
h.editText(ctx, chatID, msgID, text, &kb)
}
// 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"
if user.ReportEnabled {
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("Report: %s", reportStatus), CallbackData: "reporttoggle"}},
{{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: "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)
}
// 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.
func (h *Handler) backToSettings(ctx context.Context, chatID int64, msgID int, callbackID string) {
h.answerCb(ctx, callbackID)
@@ -262,7 +292,8 @@ func (h *Handler) backToSettings(ctx context.Context, chatID int64, msgID int, c
if err != nil {
return
}
text, kb := h.buildSettingsKeyboard(user)
bt := h.getTodayBreakThreshold(chatID)
text, kb := h.buildSettingsKeyboard(user, bt)
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 {
return
}
text, kb := h.buildSettingsKeyboard(user)
bt := h.getTodayBreakThreshold(chatID)
text, kb := h.buildSettingsKeyboard(user, bt)
h.editText(ctx, chatID, msgID, text, &kb)
}