From 4236c591a760170158173473f6ae8ab4260bb969 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Wed, 16 Oct 2024 07:42:34 -0600 Subject: [PATCH] sort entries for consistent user interaction --- FyneApp.toml | 2 +- main.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/FyneApp.toml b/FyneApp.toml index aa40024..4bf3b70 100644 --- a/FyneApp.toml +++ b/FyneApp.toml @@ -5,4 +5,4 @@ Website = "https://github.com/qbit/fass" Name = "fass" ID = "dev.suah.fass" Version = "1.0.0" - Build = 11 + Build = 16 diff --git a/main.go b/main.go index 06e0e29..eb2749b 100644 --- a/main.go +++ b/main.go @@ -1,12 +1,15 @@ package main import ( + "cmp" "crypto/tls" "crypto/x509" "fmt" "io" "log" "net/http" + "slices" + "strings" "time" "fyne.io/fyne/v2" @@ -20,6 +23,18 @@ import ( "github.com/pawal/go-hass" ) +func sortEntries(entries *hass.States) { + slices.SortFunc(*entries, func(a hass.State, b hass.State) int { + an := fmt.Sprintf("%s", a.Attributes["friendly_name"]) + bn := fmt.Sprintf("%s", b.Attributes["friendly_name"]) + + if n := strings.Compare(an, bn); n != 0 { + return n + } + return cmp.Compare(a.EntityID, b.EntityID) + }) +} + func loadData(h *hass.Access, lightCards *[]fyne.CanvasObject, switchCards *[]fyne.CanvasObject) { lights, err := h.FilterStates("light") if err != nil { @@ -30,6 +45,9 @@ func loadData(h *hass.Access, lightCards *[]fyne.CanvasObject, switchCards *[]fy log.Fatalln(err) } + sortEntries(&lights) + sortEntries(&switches) + for entity := range lights { e := lights[entity] card := makeEntity(e, h)