Enable saving album selections

This commit is contained in:
KemoNine 2020-09-05 01:01:48 -04:00
parent 728c281ad3
commit 7afb3f17ee
2 changed files with 51 additions and 6 deletions

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"time"
"github.com/knadh/koanf" "github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml" "github.com/knadh/koanf/parsers/toml"
@ -63,6 +64,10 @@ func main() {
return return
} }
// Give the config UI a chance to save and exit clean
time.Sleep(time.Minute)
// Bail on slideshow if there is a config change so it restarts with updated config
log.Fatalf("Config file changed! Exiting!") log.Fatalf("Config file changed! Exiting!")
}) })

View file

@ -38,6 +38,11 @@ const (
PAGE_POWEROFF = "PAGE_POWEROFF" PAGE_POWEROFF = "PAGE_POWEROFF"
) )
// Bullshit to remove an element from a slice
func remove(s []string, index int) []string {
return append(s[:index], s[index+1:]...)
}
// Housekeeping // Housekeeping
var app *tview.Application var app *tview.Application
var main *tview.Flex var main *tview.Flex
@ -54,7 +59,7 @@ func ConfigGui(pfconfig *koanf.Koanf) {
meminfo := &procmeminfo.MemInfo{} meminfo := &procmeminfo.MemInfo{}
err := meminfo.Update() err := meminfo.Update()
if err != nil { if err != nil {
log.Printf("Error getting memory info : %s", err) log.Fatalf("Error getting memory info : %s", err)
} }
// Network interfaces for status panel // Network interfaces for status panel
@ -181,6 +186,7 @@ func ConfigGui(pfconfig *koanf.Koanf) {
// Select Albums Form // Select Albums Form
selectAlbumsForm := tview.NewForm() selectAlbumsForm := tview.NewForm()
configSelectedAlbums := pfconfig.Strings(config.CONFIG_KEY_ALBUMS_SELECTED) configSelectedAlbums := pfconfig.Strings(config.CONFIG_KEY_ALBUMS_SELECTED)
albumCheckboxes := []*tview.Checkbox{}
for _, album := range albums { for _, album := range albums {
albumSelected := false albumSelected := false
for _, configSelectedAlbum := range configSelectedAlbums { for _, configSelectedAlbum := range configSelectedAlbums {
@ -191,7 +197,11 @@ func ConfigGui(pfconfig *koanf.Koanf) {
albumSelected = true albumSelected = true
} }
} }
selectAlbumsForm.AddCheckbox(album, albumSelected, nil) albumCheckbox := tview.NewCheckbox()
albumCheckbox.SetLabel(album)
albumCheckbox.SetChecked(albumSelected)
albumCheckboxes = append(albumCheckboxes, albumCheckbox)
selectAlbumsForm.AddFormItem(albumCheckbox)
} }
selectAlbumsForm.AddButton("Apply", resetMain) selectAlbumsForm.AddButton("Apply", resetMain)
selectAlbumsForm.AddButton("Cancel", resetMain) selectAlbumsForm.AddButton("Cancel", resetMain)
@ -377,10 +387,37 @@ func ConfigGui(pfconfig *koanf.Koanf) {
AddButtons([]string{"Yes", "Cancel"}). AddButtons([]string{"Yes", "Cancel"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) { SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" { if buttonLabel == "Yes" {
// Housekeeping var for data to be written to disk // Deal with selected albums
var hdmiForDisk bytes.Buffer for _, checkbox := range albumCheckboxes {
checkboxLabel := checkbox.GetLabel()
// Undo beautification of checkbox label
if checkboxLabel == "Main Folder" {
checkboxLabel = "/"
}
// Track whether or not we need to add OR remove from config
selected := checkbox.IsChecked()
inConfig := false
configIndex := 0
// Walk the existing config values and bail if/when we find the album
for configIndex = range configSelectedAlbums {
if checkboxLabel == configSelectedAlbums[configIndex] {
inConfig = true
break
}
}
// We need to add to the config
if selected && !inConfig {
configSelectedAlbums = append(configSelectedAlbums, checkboxLabel)
}
// We need to remove from the config
if !selected && inConfig {
configSelectedAlbums = remove(configSelectedAlbums, configIndex)
}
}
// Apply HDMI configuration (on/off) // Apply HDMI configuration (on/off)
var hdmiForDisk bytes.Buffer
valueForTemplate := utils.SystemdTimer{OnCalendar: configHDMIOn} valueForTemplate := utils.SystemdTimer{OnCalendar: configHDMIOn}
screenOn, err := template.New("screenon.systemd.timer").Parse(utils.SCREEN_ON_DOT_TIMER) screenOn, err := template.New("screenon.systemd.timer").Parse(utils.SCREEN_ON_DOT_TIMER)
if err != nil { if err != nil {
@ -409,8 +446,11 @@ func ConfigGui(pfconfig *koanf.Koanf) {
// Apply configuration updates to main config manager prior to saving // Apply configuration updates to main config manager prior to saving
pfconfig.Load(confmap.Provider(map[string]interface{}{ pfconfig.Load(confmap.Provider(map[string]interface{}{
config.CONFIG_KEY_HDMI_ON: configHDMIOn, config.CONFIG_KEY_SLIDESHOW_INTERVAL: configSlideInterval,
config.CONFIG_KEY_HDMI_OFF: configHDMIOff, config.CONFIG_KEY_SLIDESHOW_RESTART_INTERVAL: configRestartInterval,
config.CONFIG_KEY_HDMI_ON: configHDMIOn,
config.CONFIG_KEY_HDMI_OFF: configHDMIOff,
config.CONFIG_KEY_ALBUMS_SELECTED: configSelectedAlbums,
}, "."), nil) }, "."), nil)
// Save configuration // Save configuration