diff --git a/api/endpoint.go b/api/endpoint.go index 7f8bcb2..0973a7b 100644 --- a/api/endpoint.go +++ b/api/endpoint.go @@ -44,7 +44,7 @@ func ReqSignIs(email string) (user User, err error) { Email: email, } var ret RetSignIs - ret, _, err = HttpPost[ArgSignIs, RetSignIs]("/user/is", Admin.Access, &arg) + ret, _, err = HttpPost[ArgSignIs, RetSignIs]("/signin", Admin.Access, &arg) user.UID = ret.UID user.Email = ret.Email user.Name = ret.Name @@ -64,6 +64,40 @@ func ReqRefresh() (ret AuthResp, err error) { return } +type ( + useritem struct { + XMLName xml.Name `json:"-" yaml:"-" xml:"user"` + UID uint64 `json:"uid,omitempty" yaml:"uid,omitempty" xml:"uid,attr,omitempty"` + Email string `json:"email,omitempty" yaml:"email,omitempty" xml:"email,attr,omitempty"` + Name string `json:"name,omitempty" yaml:"name,omitempty" xml:"name,attr,omitempty"` + } + ArgUserIs struct { + XMLName xml.Name `json:"-" yaml:"-" xml:"arg"` + List []useritem `json:"list" yaml:"list" xml:"list>user"` + } + RetUserIs struct { + XMLName xml.Name `json:"-" yaml:"-" xml:"ret"` + List []useritem `json:"list" yaml:"list" xml:"list>user"` + } +) + +func ReqUserIs(emails []string) (users []User, err error) { + var arg ArgUserIs + var ret RetUserIs + arg.List = make([]useritem, len(emails)) + for i, email := range emails { + arg.List[i].Email = email + } + ret, _, err = HttpPost[ArgUserIs, RetUserIs]("/user/is", Admin.Access, &arg) + users = make([]User, len(ret.List)) + for i, item := range ret.List { + users[i].UID = item.UID + users[i].Email = item.Email + users[i].Name = item.Name + } + return +} + type ( clubitem struct { XMLName xml.Name `json:"-" yaml:"-" xml:"club"` diff --git a/api/http.go b/api/http.go index d66a27b..917bf9e 100644 --- a/api/http.go +++ b/api/http.go @@ -11,13 +11,13 @@ import ( cfg "github.com/slotopol/balance/config" ) -type ajaxerr struct { +type AjaxErr struct { What string `json:"what" yaml:"what" xml:"what"` Code int `json:"code,omitempty" yaml:"code,omitempty" xml:"code,omitempty"` UID uint64 `json:"uid,omitempty" yaml:"uid,omitempty" xml:"uid,omitempty,attr"` } -func (err ajaxerr) Error() string { +func (err AjaxErr) Error() string { return fmt.Sprintf("what: %s, code: %d", err.What, err.Code) } @@ -54,7 +54,7 @@ func HttpPost[Ta, Tr any](path string, token string, arg *Ta) (ret Tr, status in return } if resp.StatusCode >= http.StatusBadRequest { - var aerr ajaxerr + var aerr AjaxErr if err = json.Unmarshal(b, &aerr); err != nil { return } diff --git a/config/cfgpath.go b/config/cfgpath.go index c775039..59e232f 100644 --- a/config/cfgpath.go +++ b/config/cfgpath.go @@ -1,24 +1,24 @@ package cfg import ( + "errors" "log" "os" "path/filepath" - "github.com/spf13/viper" "gopkg.in/yaml.v3" ) +const AppID = "slotopol.balance" + const ( - cfgcredentials = "balance-credentials.yaml" - cfguserlist = "balance-userlist.yaml" + cfgcredentials = "credentials.yaml" + cfguserlist = "userlist.yaml" ) var ( // Executable path. ExePath string - // Configuration file with path. - CfgFile string // Configuration path. CfgPath string ) @@ -35,59 +35,78 @@ func init() { } }() - // Config path - const sub = "config" - // Search config in home directory with name "balance" (without extension). - viper.SetConfigName("balance-app") - viper.SetConfigType("yaml") - if env, ok := os.LookupEnv("CFGFILE"); ok { - viper.AddConfigPath(env) + // Configuration path + var oscfgpath string + if oscfgpath, err = os.UserConfigDir(); err != nil { + log.Printf("can not obtain user config directory, any settings can not be saved: %s", err.Error()) + return } - viper.AddConfigPath(filepath.Join(ExePath, sub)) - viper.AddConfigPath(ExePath) - viper.AddConfigPath(sub) - viper.AddConfigPath(".") - if home, err := os.UserHomeDir(); err == nil { - viper.AddConfigPath(filepath.Join(home, sub)) - viper.AddConfigPath(home) + CfgPath = filepath.Join(oscfgpath, "fyne", AppID) + log.Printf("config path: %s\n", CfgPath) + + if err = ReadCredentials(); err != nil { + log.Printf("failure on reading credentials, using default: %s\n", err.Error()) } - if env, ok := os.LookupEnv("GOBIN"); ok { - viper.AddConfigPath(filepath.Join(env, sub)) - viper.AddConfigPath(env) - } else if env, ok := os.LookupEnv("GOPATH"); ok { - viper.AddConfigPath(filepath.Join(env, "bin", sub)) - viper.AddConfigPath(filepath.Join(env, "bin")) + if err = ReadUserList(); err != nil { + log.Printf("failure on reading userlist, using default: %s\n", err.Error()) } +} - viper.AutomaticEnv() +var ( + ErrNoCfgPath = errors.New("configuration path does not obtained") +) - if err = viper.ReadInConfig(); err != nil { - log.Println("config file not found!") - } else { - viper.Unmarshal(&Cfg) - CfgFile = viper.ConfigFileUsed() - CfgPath = filepath.Dir(CfgFile) - log.Printf("config path: %s\n", CfgPath) +func ReadCredentials() (err error) { + if CfgPath == "" { + return ErrNoCfgPath } + var b []byte + if b, err = os.ReadFile(filepath.Join(CfgPath, cfgcredentials)); err != nil { + return + } + if err = yaml.Unmarshal(b, Credentials); err != nil { + return + } + return } -func ReadCredentials() (err error) { +func SaveCredentials() (err error) { + if CfgPath == "" { + return ErrNoCfgPath + } var b []byte - if b, err = os.ReadFile(filepath.Join(CfgPath, cfgcredentials)); err != nil { + if b, err = yaml.Marshal(Credentials); err != nil { return } - if yaml.Unmarshal(b, Credentials); err != nil { + if err = os.WriteFile(filepath.Join(CfgPath, cfgcredentials), b, 0666); err != nil { return } return } func ReadUserList() (err error) { + if CfgPath == "" { + return ErrNoCfgPath + } var b []byte if b, err = os.ReadFile(filepath.Join(CfgPath, cfguserlist)); err != nil { return } - if yaml.Unmarshal(b, &UserList); err != nil { + if err = yaml.Unmarshal(b, &UserList); err != nil { + return + } + return +} + +func SaveUserList() (err error) { + if CfgPath == "" { + return ErrNoCfgPath + } + var b []byte + if b, err = yaml.Marshal(&UserList); err != nil { + return + } + if err = os.WriteFile(filepath.Join(CfgPath, cfguserlist), b, 0666); err != nil { return } return diff --git a/go.mod b/go.mod index 8c80636..2b60fdb 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.23 require ( fyne.io/fyne/v2 v2.5.2 - github.com/spf13/viper v1.8.1 golang.org/x/image v0.18.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -24,28 +23,17 @@ require ( github.com/go-text/typesetting v0.2.0 // indirect github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/gopherjs/gopherjs v1.17.2 // indirect - github.com/hashicorp/hcl v1.0.0 // indirect github.com/jeandeaual/go-locale v0.0.0-20240223122105-ce5225dcaa49 // indirect github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect - github.com/magiconair/properties v1.8.5 // indirect - github.com/mitchellh/mapstructure v1.4.1 // indirect github.com/nicksnyder/go-i18n/v2 v2.4.0 // indirect - github.com/pelletier/go-toml v1.9.3 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rymdport/portal v0.2.6 // indirect - github.com/spf13/afero v1.6.0 // indirect - github.com/spf13/cast v1.3.1 // indirect - github.com/spf13/jwalterweatherman v1.1.0 // indirect - github.com/spf13/pflag v1.0.5 // indirect github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c // indirect github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef // indirect github.com/stretchr/testify v1.8.4 // indirect - github.com/subosito/gotenv v1.2.0 // indirect github.com/yuin/goldmark v1.7.1 // indirect golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect golang.org/x/net v0.25.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.16.0 // indirect - gopkg.in/ini.v1 v1.62.0 // indirect - gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 3f86d5d..b29548f 100644 --- a/go.sum +++ b/go.sum @@ -189,7 +189,6 @@ github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/b github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= -github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ= @@ -205,7 +204,6 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1 github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e h1:LvL4XsI70QxOGHed6yhQtAU34Kx3Qq2wwBzGFKY8zKk= github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e/go.mod h1:kLgvv7o6UM+0QSf0QjAse3wReFDsb9qbZJdfexWlrQw= -github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -214,7 +212,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= @@ -226,7 +223,6 @@ github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4 github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= -github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag= github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= @@ -238,7 +234,6 @@ github.com/nicksnyder/go-i18n/v2 v2.4.0/go.mod h1:nxYSZE9M0bf3Y70gPQjN9ha7XNHX7g github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc= -github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ= github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/profile v1.7.0 h1:hnbDkaNWPCLMO9wGLdBFTIZvzDrDfBM2072E1S9gJkA= @@ -259,20 +254,13 @@ github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxr github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= -github.com/spf13/afero v1.6.0 h1:xoax2sJ2DT8S8xA2paPFjDCScCNeWsg75VG0DLRreiY= github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I= -github.com/spf13/cast v1.3.1 h1:nFm6S0SMdyzrzcmThSipiEubIDy8WEXKNZ0UOgiRpng= github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= github.com/spf13/cobra v1.2.1/go.mod h1:ExllRjgxM/piMAM+3tAZvg8fsklGAf3tPfi+i8t68Nk= -github.com/spf13/jwalterweatherman v1.1.0 h1:ue6voC5bR5F8YxI5S67j9i582FU4Qvo2bmqnqMYADFk= github.com/spf13/jwalterweatherman v1.1.0/go.mod h1:aNWZUN0dPAAO/Ljvb5BEdw96iTZ0EXowPYD95IqWIGo= -github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.8.1 h1:Kq1fyeebqsBfbjZj4EL7gj2IO0mMaiyjYUWcUsl2O44= github.com/spf13/viper v1.8.1/go.mod h1:o0Pch8wJ9BVSWGQMbra6iw0oQ5oktSIBaujf1rJH9Ns= github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c h1:km8GpoQut05eY3GiYWEedbTT0qnSxrCjsVbb7yKY1KE= github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c/go.mod h1:cNQ3dwVJtS5Hmnjxy6AgTPd0Inb3pW05ftPSX7NZO7Q= @@ -288,7 +276,6 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -647,7 +634,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= -gopkg.in/ini.v1 v1.62.0 h1:duBzk771uxoUuOlyRLkHsygud9+5lrlGjdFBb4mSKDU= gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 64c3f82..60e67dd 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,12 @@ package main import ( "fyne.io/fyne/v2/app" + cfg "github.com/slotopol/balance/config" "github.com/slotopol/balance/ui" ) func main() { - var a = app.NewWithID("slotopol.balance") + var a = app.NewWithID(cfg.AppID) ui.Lifecycle(a) var frame = &ui.Frame{} frame.CreateWindow(a) diff --git a/task/build-win-x64.sh b/task/build-win-x64.sh index 1926415..e88b043 100644 --- a/task/build-win-x64.sh +++ b/task/build-win-x64.sh @@ -2,7 +2,7 @@ # This script compiles project for Windows amd64. wd=$(realpath -s "$(dirname "$0")/..") -cp -ruv "$wd/confdata/"* "$GOPATH/bin/config" +cp -ruv "$wd/appdata/"* "$HOME/AppData/Roaming/fyne/slotopol.balance" go env -w GOOS=windows GOARCH=amd64 CGO_ENABLED=1 go build -o "$GOPATH/bin/balance_win_x64.exe" -v -tags="" $wd diff --git a/ui/frame.go b/ui/frame.go index 222f4f2..04acfb4 100644 --- a/ui/frame.go +++ b/ui/frame.go @@ -123,6 +123,7 @@ func (p *SigninPage) Create() { {Text: "Email", Widget: p.email, HintText: "A valid registered email address"}, {Text: "Secret", Widget: p.secret, HintText: "Password for authorization"}, }, + SubmitText: "Sign-in", } p.signinPage = container.NewStack( diff --git a/ui/res.go b/ui/res.go index 83f6adb..082443b 100644 --- a/ui/res.go +++ b/ui/res.go @@ -11,9 +11,6 @@ import ( // icons var ( - //go:embed icon/login.svg - loginRes []byte - //go:embed icon/logout.svg logoutRes []byte @@ -95,10 +92,6 @@ var ( // icon resources var ( - loginIconRes = theme.NewThemedResource(&fyne.StaticResource{ - StaticName: "login", - StaticContent: loginRes, - }) logoutIconRes = theme.NewThemedResource(&fyne.StaticResource{ StaticName: "logout", StaticContent: logoutRes, diff --git a/ui/startup.go b/ui/startup.go index fa98dbe..0b1c977 100644 --- a/ui/startup.go +++ b/ui/startup.go @@ -1,6 +1,7 @@ package ui import ( + "errors" "fmt" "log" "time" @@ -47,24 +48,28 @@ func (f *Frame) MakeClubList() (err error) { } func (f *Frame) MakeUserList() (err error) { - if err = cfg.ReadUserList(); err != nil { - log.Printf("failure on reading userlist, using default: %s\n", err.Error()) - err = nil // skip this error + var users []api.User + if users, err = api.ReqUserIs(cfg.UserList); err != nil { + return } - for i, email := range cfg.UserList { - var user api.User - if user, err = api.ReqSignIs(email); err != nil { - return - } + var save bool + var i int + for _, user := range users { + var email = cfg.UserList[i] if user.UID == 0 { cfg.UserList = append(cfg.UserList[:i], cfg.UserList[i+1:]...) log.Printf("user with email '%s' presents in yaml list but absent in server database, skipped", email) + save = true continue } api.Users[email] = &user + i++ } go f.RefreshLoop() log.Printf("users list ready, %d users", len(api.Users)) + if save { + cfg.SaveUserList() + } return } @@ -110,13 +115,46 @@ func (f *Frame) StartupChain() { } } -func (f *Frame) CreateWindow(a fyne.App) { - var errCred error - if errCred = cfg.ReadCredentials(); errCred != nil { - log.Printf("failure on reading credentials, using default: %s\n", errCred.Error()) - return +func (f *Frame) submitSignin() { + if api.Admin.Email != f.email.Text { + var err error + if api.Admin, err = api.ReqSignIn(f.email.Text, f.secret.Text); err != nil { + var msg string + var aerr api.AjaxErr + if errors.As(err, &aerr) { + msg = aerr.What + } else { + msg = err.Error() + } + f.errmsg.SetText(fmt.Sprintf("can not sign in with given credentials, %s", msg)) + return + } + log.Printf("signed as '%s'", cfg.Credentials.Email) + + var save = cfg.Credentials.Addr != f.host.Text || + cfg.Credentials.Email != f.email.Text || + cfg.Credentials.Secret != f.secret.Text + if save { + cfg.Credentials.Addr = f.host.Text + cfg.Credentials.Email = f.email.Text + cfg.Credentials.Secret = f.secret.Text + if err = cfg.SaveCredentials(); err != nil { + log.Printf("can not save credentials: %s", err.Error()) + } + } } + f.SigninPage.form.OnCancel = func() { + f.Window.SetContent(f.mainPage) + } + f.SigninPage.form.Refresh() + f.Window.SetContent(f.mainPage) + f.loginTxt.SetText(cfg.Credentials.Email) + + go f.StartupChain() +} + +func (f *Frame) CreateWindow(a fyne.App) { f.MainPage.Create() f.SigninPage.Create() @@ -127,27 +165,7 @@ func (f *Frame) CreateWindow(a fyne.App) { w.SetContent(f.signinPage) f.Window = w - var submit = func() { - f.loginTxt.SetText(cfg.Credentials.Email) - w.SetContent(f.mainPage) - go f.StartupChain() - f.SigninPage.form.OnCancel = func() { - w.SetContent(f.mainPage) - } - f.SigninPage.form.Refresh() - } - f.SigninPage.form.OnSubmit = func() { - var err error - if api.Admin, err = api.ReqSignIn(f.email.Text, f.secret.Text); err != nil { - f.errmsg.SetText(fmt.Sprintf("can not sign in with given credentials, %s", err.Error())) - return - } - cfg.Credentials.Addr = f.host.Text - cfg.Credentials.Email = f.email.Text - cfg.Credentials.Secret = f.secret.Text - log.Printf("signed as '%s'", cfg.Credentials.Email) - submit() - } + f.SigninPage.form.OnSubmit = f.submitSignin f.SigninPage.form.Refresh() f.userTable.SetColumnWidth(0, 180) // email f.userTable.SetColumnWidth(1, 100) // wallet @@ -156,6 +174,6 @@ func (f *Frame) CreateWindow(a fyne.App) { f.userTable.ExtendBaseWidget(f.userTable) if cfg.Credentials.Addr != "" && cfg.Credentials.Email != "" { - //submit() + f.submitSignin() } }