Update Database
Database is more usable now. All pages have been updated to use the database, and share an application configuration. Page Stack has been removed, no more pages hanging around in memory. Replaced with a 'Router' which uses an Enum to determine which page should be displayed, destroys the old page and initializes the new page all in one go. No more push, pop, rollback, etc. No more holding instances of pages. Logger has been updated with proper timestamps and sorting now works as expected."
This commit is contained in:
@@ -2,35 +2,35 @@ package page
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-gl/gl/v3.3-core/gl"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"rhemawavetech.com/rhemawave/internal/colors"
|
||||
"rhemawavetech.com/rhemawave/internal/db"
|
||||
"rhemawavetech.com/rhemawave/internal/fontManager"
|
||||
"rhemawavetech.com/rhemawave/internal/logger"
|
||||
"rhemawavetech.com/rhemawave/internal/shaderManager"
|
||||
)
|
||||
|
||||
type ProclamationOptions struct {
|
||||
Base
|
||||
font *fontManager.FontEntry
|
||||
textShader *shaderManager.Shader
|
||||
solidShader *shaderManager.Shader
|
||||
title StringItem
|
||||
prompt StringItem
|
||||
font *fontManager.FontEntry
|
||||
textShader *shaderManager.Shader
|
||||
solidShader *shaderManager.Shader
|
||||
title StringItem
|
||||
prompt StringItem
|
||||
proclamations []StringItem
|
||||
proclamationIDs []uint // Database IDs corresponding to each proclamation
|
||||
activeMap map[int]bool // Maps display index to selected state
|
||||
highlightIndex int
|
||||
verticalMargin int32
|
||||
proclamationIDs []string
|
||||
activeMap map[int]bool // Maps display index to selected state
|
||||
highlightIndex int
|
||||
verticalMargin int32
|
||||
}
|
||||
|
||||
func (p *ProclamationOptions) Init(app ApplicationInterface) error {
|
||||
func (p *ProclamationOptions) Init(ctx *PageCtx) error {
|
||||
var err error
|
||||
p.verticalMargin = int32(20)
|
||||
p.activeMap = make(map[int]bool)
|
||||
p.proclamationIDs = make([]uint, 0)
|
||||
p.proclamationIDs = make([]string, 0)
|
||||
|
||||
// Get font
|
||||
fm := fontManager.Get()
|
||||
@@ -53,19 +53,12 @@ func (p *ProclamationOptions) Init(app ApplicationInterface) error {
|
||||
}
|
||||
|
||||
// Initialize base
|
||||
if err := p.Base.Init(app); err != nil {
|
||||
if err := p.Base.Init(ctx); err != nil {
|
||||
return fmt.Errorf("base init failed: %w", err)
|
||||
}
|
||||
|
||||
// Get font size from database
|
||||
baseFontSizeStr, err := db.GetConfigValue("init_font_size")
|
||||
if err != nil {
|
||||
return fmt.Errorf("retrieving init_font_size from db: %w", err)
|
||||
}
|
||||
baseFontSize, err := strconv.Atoi(baseFontSizeStr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parsing init_font_size %q: %w", baseFontSizeStr, err)
|
||||
}
|
||||
baseFontSize := ctx.CfgPtr.StandardFontSize
|
||||
|
||||
// Calculate scales
|
||||
titleScale := float32(64) / float32(baseFontSize)
|
||||
@@ -86,9 +79,10 @@ func (p *ProclamationOptions) Init(app ApplicationInterface) error {
|
||||
p.prompt.Scale(itemScale)
|
||||
|
||||
// Load proclamations from database
|
||||
proclamations, err := db.GetProclamations()
|
||||
if err != nil {
|
||||
return fmt.Errorf("retrieving proclamations from db: %w", err)
|
||||
var proclamations []db.Proclamation
|
||||
if result := p.Context.DbPtr.Find(&proclamations); result.Error != nil {
|
||||
logger.Error("unable to query database for proclamations: %v", result.Error)
|
||||
return fmt.Errorf("%s", result.Error)
|
||||
}
|
||||
|
||||
// Create UI items for each available proclamation
|
||||
@@ -97,7 +91,7 @@ func (p *ProclamationOptions) Init(app ApplicationInterface) error {
|
||||
continue
|
||||
}
|
||||
|
||||
label, err := NewStringItem(proclamation.Title, p.font, colors.White)
|
||||
label, err := NewStringItem(proclamation.Name, p.font, colors.White)
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating proclamation label: %w", err)
|
||||
}
|
||||
@@ -152,7 +146,7 @@ func (p *ProclamationOptions) HandleEvent(event *sdl.Event) error {
|
||||
p.highlightIndex++
|
||||
}
|
||||
case sdl.K_BACKSPACE:
|
||||
p.App.UnwindToPage(&Settings{})
|
||||
p.Context.NavigateTo(SETTINGS)
|
||||
case sdl.K_SPACE:
|
||||
// Toggle selection for highlighted item
|
||||
if p.activeMap[p.highlightIndex] {
|
||||
@@ -167,7 +161,7 @@ func (p *ProclamationOptions) HandleEvent(event *sdl.Event) error {
|
||||
}
|
||||
case sdl.K_RETURN:
|
||||
// Just return to settings - no saving needed
|
||||
p.App.UnwindToPage(&Settings{})
|
||||
p.Context.NavigateTo(SETTINGS)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -183,15 +177,17 @@ func (p *ProclamationOptions) toggleProclamation(displayIndex int) error {
|
||||
isSelected := p.activeMap[displayIndex]
|
||||
|
||||
// Get the proclamation from database
|
||||
var proclamation db.Proclamations
|
||||
if err := db.DB.First(&proclamation, dbID).Error; err != nil {
|
||||
return fmt.Errorf("retrieving proclamation %d: %w", dbID, err)
|
||||
var proclamation db.Proclamation
|
||||
proclamation.ID = dbID
|
||||
if result := p.Context.DbPtr.Find(&proclamation); result.Error != nil {
|
||||
logger.Error("unable to query proclamation %s from database: %v", proclamation.ID, result.Error)
|
||||
}
|
||||
|
||||
// Update the Selected field
|
||||
proclamation.Selected = isSelected
|
||||
if err := db.DB.Save(&proclamation).Error; err != nil {
|
||||
return fmt.Errorf("updating proclamation %d: %w", dbID, err)
|
||||
if result := p.Context.DbPtr.Save(&proclamation); result.Error != nil {
|
||||
logger.Error("failed to update proclamation %s: %v", proclamation.ID, result.Error)
|
||||
return fmt.Errorf("%s", result.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user