Skip to content

Commit

Permalink
frame.
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarzlichtbezirk committed Nov 17, 2024
1 parent e7e85ee commit b0be259
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 84 deletions.
2 changes: 0 additions & 2 deletions core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"time"
)

var admin AuthResp

type (
ArgSignIs struct {
XMLName xml.Name `json:"-" yaml:"-" xml:"arg"`
Expand Down
26 changes: 26 additions & 0 deletions core/res.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
_ "embed"
"math/rand/v2"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
Expand Down Expand Up @@ -203,3 +204,28 @@ var (
StaticContent: underlay18Res,
}
)

var underlays = []*fyne.StaticResource{
underlay01ImgRes,
underlay02ImgRes,
underlay03ImgRes,
underlay04ImgRes,
underlay05ImgRes,
underlay06ImgRes,
underlay07ImgRes,
underlay08ImgRes,
underlay09ImgRes,
underlay10ImgRes,
underlay11ImgRes,
underlay12ImgRes,
underlay13ImgRes,
underlay14ImgRes,
underlay15ImgRes,
underlay16ImgRes,
underlay17ImgRes,
underlay18ImgRes,
}

func AnyUnderlay() *fyne.StaticResource {
return underlays[rand.N(len(underlays))]
}
62 changes: 33 additions & 29 deletions core/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
cfg "github.com/slotopol/balance/config"
)

func MakeSignIn() (err error) {
var admin AuthResp

func (f *Frame) MakeSignIn() (err error) {
if err = cfg.ReadCredentials(); err != nil {
log.Printf("failure on reading credentials, using default: %s\n", err.Error())
err = nil // skip this error
Expand All @@ -21,12 +23,12 @@ func MakeSignIn() (err error) {
if admin, err = ApiSignIn(cfg.Credentials.Email, cfg.Credentials.Secret); err != nil {
return
}
loginTxt.SetText(fmt.Sprintf(cfg.Credentials.Email))
f.loginTxt.SetText(fmt.Sprintf(cfg.Credentials.Email))
log.Printf("signed as '%s'", cfg.Credentials.Email)
return
}

func MakeClubList() (err error) {
func (f *Frame) MakeClubList() (err error) {
var cl RetClubList
if cl, err = ApiClubList(); err != nil {
return
Expand All @@ -38,9 +40,9 @@ func MakeClubList() (err error) {
Clubs[item.Name] = item.CID
tabs[i] = container.NewTabItem(item.Name, widget.NewLabel(""))
}
clubtabs.SetItems(tabs)
f.clubTabs.SetItems(tabs)

clubtabs.OnSelected = func(tab *container.TabItem) {
f.clubTabs.OnSelected = func(tab *container.TabItem) {
var err error

var ok bool
Expand All @@ -51,15 +53,15 @@ func MakeClubList() (err error) {
return
}

RefreshContent()
f.RefreshContent()
}
clubtabs.OnSelected(clubtabs.Selected())
f.clubTabs.OnSelected(f.clubTabs.Selected())

log.Printf("clubs list ready, %d clubs", len(Clubs))
return
}

func MakeUserList() (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
Expand All @@ -77,25 +79,31 @@ func MakeUserList() (err error) {
user.props = map[uint64]Props{} // make new empty map
Users[email] = user
}
go RefreshLoop()
go f.RefreshLoop()
log.Printf("users list ready, %d users", len(Users))
return
}

func RefreshLoop() {
func (f *Frame) RefreshLoop() {
var c = time.Tick(Cfg.PropUpdateTick)
for range c {
if Foreground {
RefreshContent()
f.RefreshContent()
}
}
}

func WaitToken() (err error) {
for {
var t time.Time
if t, err = time.Parse(admin.Expire, time.RFC3339); err != nil {
return
for {
if t, err = time.Parse(admin.Expire, time.RFC3339); err != nil {
return
}
if !t.IsZero() {
break
}
<-time.After(5 * time.Minute)
}
// get tokens before expire
<-time.After(time.Until(t.Add(-15 * time.Second)))
Expand All @@ -105,32 +113,28 @@ func WaitToken() (err error) {
}
}

func StartupChain() {
func (f *Frame) StartupChain() {
var chain = [](func() error){
MakeSignIn,
MakeClubList,
MakeUserList,
f.MakeSignIn,
f.MakeClubList,
f.MakeUserList,
}
for _, f := range chain {
if err := f(); err != nil {
for _, step := range chain {
if err := step(); err != nil {
log.Printf("startup chain does not complete: %s\n", err.Error())
return
}
}
}

func CreateMainWindow(a fyne.App) fyne.Window {
var w = a.NewWindow("Balance")
func (f *Frame) CreateWindow(a fyne.App) {
f.MainPage.Create()

w.SetContent(mainPage)
go StartupChain()
go f.StartupChain()
go WaitToken()

userlist.SetColumnWidth(0, 180) // email
userlist.SetColumnWidth(1, 100) // wallet
userlist.SetColumnWidth(2, 50) // mtrp
userlist.SetColumnWidth(3, 150) // access
userlist.ExtendBaseWidget(userlist)
var w = a.NewWindow("Balance")
w.Resize(fyne.NewSize(540, 640))
return w
w.SetContent(f.mainPage)
f.Window = w
}
114 changes: 63 additions & 51 deletions core/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package core
import (
"fmt"
"log"
"math/rand/v2"
"strconv"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -79,65 +78,73 @@ func (l FitLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
return minSize
}

var underlays = []*fyne.StaticResource{
underlay01ImgRes,
underlay02ImgRes,
underlay03ImgRes,
underlay04ImgRes,
underlay05ImgRes,
underlay06ImgRes,
underlay07ImgRes,
underlay08ImgRes,
underlay09ImgRes,
underlay10ImgRes,
underlay11ImgRes,
underlay12ImgRes,
underlay13ImgRes,
underlay14ImgRes,
underlay15ImgRes,
underlay16ImgRes,
underlay17ImgRes,
underlay18ImgRes,
type Frame struct {
fyne.Window
MainPage
}

var (
type MainPage struct {
// Backgroud image
underlay = &canvas.Image{
Resource: underlays[rand.N(len(underlays))],
underlay *canvas.Image

// Toolbar buttons
useraddBut *widget.ToolbarAction
userdelBut *widget.ToolbarAction
walletBut *widget.ToolbarAction
mrtpBut *widget.ToolbarAction
accessBut *widget.ToolbarAction
bankBut *widget.ToolbarAction
logoutBut *widget.ToolbarAction
loginTxt *ToolbarLabel

// Toolbar frame
toolbar *widget.Toolbar

// Table with users
clubTabs *container.AppTabs
userTable *widget.Table
mainPage *fyne.Container
}

var colhdr = []string{"email", "wallet", "MRTP", "access"}

func (p *MainPage) Create() {
// Backgroud image
p.underlay = &canvas.Image{
Resource: AnyUnderlay(),
FillMode: canvas.ImageFillContain,
Translucency: 0.85,
}

// Toolbar buttons
useraddBut = widget.NewToolbarAction(useraddIconRes, func() { fmt.Println("useradd") })
userdelBut = widget.NewToolbarAction(userdelIconRes, func() { fmt.Println("userdel") })
walletBut = widget.NewToolbarAction(walletIconRes, func() { fmt.Println("wallet") })
mrtpBut = widget.NewToolbarAction(percentIconRes, func() { fmt.Println("mrtp") })
accessBut = widget.NewToolbarAction(accessIconRes, func() { fmt.Println("access") })
bankBut = widget.NewToolbarAction(bankIconRes, func() { fmt.Println("bank") })
logoutBut = widget.NewToolbarAction(logoutIconRes, func() { fmt.Println("logout") })
loginTxt = NewToolbarLabel("not logined yet")
p.useraddBut = widget.NewToolbarAction(useraddIconRes, func() { fmt.Println("useradd") })
p.userdelBut = widget.NewToolbarAction(userdelIconRes, func() { fmt.Println("userdel") })
p.walletBut = widget.NewToolbarAction(walletIconRes, func() { fmt.Println("wallet") })
p.mrtpBut = widget.NewToolbarAction(percentIconRes, func() { fmt.Println("mrtp") })
p.accessBut = widget.NewToolbarAction(accessIconRes, func() { fmt.Println("access") })
p.bankBut = widget.NewToolbarAction(bankIconRes, func() { fmt.Println("bank") })
p.logoutBut = widget.NewToolbarAction(logoutIconRes, func() { fmt.Println("logout") })
p.loginTxt = NewToolbarLabel("not logined yet")

// Toolbar frame
toolbar = widget.NewToolbar(
useraddBut,
userdelBut,
p.toolbar = widget.NewToolbar(
p.useraddBut,
p.userdelBut,
widget.NewToolbarSeparator(),
walletBut,
mrtpBut,
accessBut,
p.walletBut,
p.mrtpBut,
p.accessBut,
widget.NewToolbarSeparator(),
bankBut,
p.bankBut,
widget.NewToolbarSpacer(),
widget.NewToolbarSeparator(),
loginTxt,
logoutBut,
p.loginTxt,
p.logoutBut,
)

// Table with users
clubtabs = &container.AppTabs{}
colhdr = []string{"email", "wallet", "MRTP", "access"}
userlist = &widget.Table{
p.clubTabs = &container.AppTabs{}
p.userTable = &widget.Table{
Length: func() (int, int) { return len(cfg.UserList), 4 },
CreateCell: func() fyne.CanvasObject {
var label = widget.NewLabel("")
Expand Down Expand Up @@ -197,25 +204,30 @@ var (
ShowHeaderRow: true,
ShowHeaderColumn: true,
}
p.userTable.SetColumnWidth(0, 180) // email
p.userTable.SetColumnWidth(1, 100) // wallet
p.userTable.SetColumnWidth(2, 50) // mtrp
p.userTable.SetColumnWidth(3, 150) // access
p.userTable.ExtendBaseWidget(p.userTable)

// Main page
mainPage = container.NewStack(
container.New(FitLayout{}, underlay),
p.mainPage = container.NewStack(
container.New(FitLayout{}, p.underlay),
container.NewBorder(
container.NewVBox(toolbar, clubtabs),
container.NewVBox(p.toolbar, p.clubTabs),
nil, nil, nil,
userlist),
p.userTable),
)
)
}

// Refreshes visible content of users list. Fetches data from server
// if cached data has timeout is over.
func RefreshContent() {
func (p *MainPage) RefreshContent() {
var err error

userlist.Refresh()
p.userTable.Refresh()

var label = clubtabs.Selected().Content.(*widget.Label)
var label = p.clubTabs.Selected().Content.(*widget.Label)
var bank, fund, deposit = "N/A", "N/A", "N/A"
if cural&ALclub != 0 {
var info RetClubInfo
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func main() {
var a = app.NewWithID("slotopol.balance")
core.Lifecycle(a)
var w = core.CreateMainWindow(a)
w.ShowAndRun()
var frame = &core.Frame{}
frame.CreateWindow(a)
frame.ShowAndRun()
}

0 comments on commit b0be259

Please sign in to comment.