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,10 +2,10 @@ package page
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-gl/gl/v3.3-core/gl"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
"gorm.io/gorm"
|
||||
"rhemawavetech.com/rhemawave/internal/colors"
|
||||
"rhemawavetech.com/rhemawave/internal/db"
|
||||
"rhemawavetech.com/rhemawave/internal/fontManager"
|
||||
@@ -24,9 +24,11 @@ type AudioDevices struct {
|
||||
highlightIndex int
|
||||
selectedIndex int
|
||||
verticalMargin int32
|
||||
CfgPtr *db.AppConfig
|
||||
dbPtr *gorm.DB
|
||||
}
|
||||
|
||||
func (p *AudioDevices) Init(app ApplicationInterface) error {
|
||||
func (p *AudioDevices) Init(ctx *PageCtx) error {
|
||||
var err error
|
||||
p.verticalMargin = int32(20)
|
||||
|
||||
@@ -50,19 +52,12 @@ func (p *AudioDevices) 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 := p.Base.Context.CfgPtr.StandardFontSize
|
||||
|
||||
// Calculate scales
|
||||
titleScale := float32(64) / float32(baseFontSize)
|
||||
@@ -90,10 +85,7 @@ func (p *AudioDevices) Init(app ApplicationInterface) error {
|
||||
}
|
||||
|
||||
// Get currently selected device from database
|
||||
currentDevice, err := db.GetConfigValue("audio_device_name")
|
||||
if err != nil {
|
||||
return fmt.Errorf("retrieving audio_device_name from db: %w", err)
|
||||
}
|
||||
currentDevice := p.Base.Context.CfgPtr.SelectedAudioDevice
|
||||
|
||||
// Create UI items for each device
|
||||
for i, name := range devices { // Iterate over the devices
|
||||
@@ -111,13 +103,10 @@ func (p *AudioDevices) Init(app ApplicationInterface) error {
|
||||
}
|
||||
}
|
||||
|
||||
on, err := db.GetConfigValue("audio_enabled")
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to query database for audio_enabled: %w", err)
|
||||
}
|
||||
on := p.Base.Context.CfgPtr.AudioEnabled
|
||||
|
||||
audioStatus := "Turn Audio On"
|
||||
if on == "true" {
|
||||
if on {
|
||||
audioStatus = "Turn Audio Off"
|
||||
}
|
||||
|
||||
@@ -177,22 +166,11 @@ func (p *AudioDevices) HandleEvent(event *sdl.Event) error {
|
||||
|
||||
// Handle audio disable
|
||||
if selection == "Turn Audio Off" || selection == "Turn Audio On" {
|
||||
on, err := db.GetConfigValue("audio_enabled")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch audio_enabled from database: %w", err)
|
||||
}
|
||||
|
||||
currentStatus, err := strconv.ParseBool(on)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse audio_enabled value: %w", err)
|
||||
}
|
||||
|
||||
newStatus := strconv.FormatBool(!currentStatus)
|
||||
if err := db.SetConfigValue("audio_enabled", newStatus); err != nil {
|
||||
return fmt.Errorf("failed to update audio_enabled: %w", err)
|
||||
}
|
||||
|
||||
p.App.UnwindToPage(&Settings{})
|
||||
newStatus := !p.Base.Context.CfgPtr.AudioEnabled
|
||||
updatedConfig := p.Base.Context.CfgPtr
|
||||
updatedConfig.AudioEnabled = newStatus
|
||||
p.Base.Context.DbPtr.Save(&updatedConfig)
|
||||
p.Base.Context.RefreshConfig()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -208,17 +186,12 @@ func (p *AudioDevices) HandleEvent(event *sdl.Event) error {
|
||||
}
|
||||
|
||||
// Save settings
|
||||
if err := db.SetConfigValue("audio_device_name", deviceName); err != nil {
|
||||
return fmt.Errorf("failed to save audio device: %w", err)
|
||||
}
|
||||
if err := db.SetConfigValue("audio_enabled", "true"); err != nil {
|
||||
return fmt.Errorf("failed to enable audio: %w", err)
|
||||
}
|
||||
|
||||
p.App.UnwindToPage(&Settings{})
|
||||
|
||||
p.Base.Context.CfgPtr.SelectedAudioDevice = deviceName
|
||||
p.Base.Context.CfgPtr.AudioEnabled = true
|
||||
p.Base.Context.DbPtr.Save(p.Base.Context.CfgPtr)
|
||||
p.Base.Context.RefreshConfig()
|
||||
case sdl.K_BACKSPACE:
|
||||
p.App.UnwindToPage(&Settings{})
|
||||
p.Base.Context.NavigateTo(SETTINGS)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user