Files
cloudsave/cmd/gui/window/mainwindow/mainwindow.go
Aurélie DELHAIE 5f7ca22b8f
Some checks failed
CloudSave/pipeline/head There was a failure building this commit
wip
2025-09-11 17:41:57 +02:00

174 lines
3.6 KiB
Go

package mainwindow
import (
"cloudsave/cmd/gui/window/about"
"cloudsave/cmd/gui/window/credential"
"cloudsave/cmd/gui/window/loading"
"cloudsave/pkg/data"
"cloudsave/pkg/remote"
"cloudsave/pkg/remote/client"
"cloudsave/pkg/sync"
"fmt"
"os"
"path/filepath"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
func Make(a fyne.App, d *data.Service) fyne.Window {
w := a.NewWindow("CloudSave")
w.Resize(fyne.NewSize(1000, 700))
w.CenterOnScreen()
games, err := d.AllGames()
if err != nil {
d := dialog.NewError(fmt.Errorf("failed to load datastore: %w", err), w)
d.Show()
d.SetOnClosed(func() {
os.Exit(1)
})
}
list := widget.NewList(
func() int {
return len(games)
},
func() fyne.CanvasObject {
return widget.NewLabel("template")
},
func(i widget.ListItemID, o fyne.CanvasObject) {
o.(*widget.Label).SetText(games[i].Name)
})
toolbar := widget.NewToolbar(
widget.NewToolbarAction(theme.FolderNewIcon(), func() {
folderSelection := dialog.NewFolderOpen(func(lu fyne.ListableURI, err error) {
if err != nil {
d := dialog.NewError(fmt.Errorf("failed to open window: %w", err), w)
d.Show()
return
}
if lu == nil {
return
}
confirmDialog := dialog.NewConfirm("Add", "Do you want to add and scan '"+lu.Path()+"'?", func(accepted bool) {
if !accepted {
return
}
name := filepath.Base(lu.Path())
loadingDialog := loading.Make("Scanning directory", w)
loadingDialog.Show()
go func() {
gameID, err := d.Add(name, lu.Path(), "")
if err != nil {
fyne.Do(func() {
d := dialog.NewError(fmt.Errorf("failed to create metadata the directory: %w", err), w)
d.Show()
loadingDialog.Hide()
})
return
}
_, err = d.Scan(gameID)
if err != nil {
fyne.Do(func() {
d := dialog.NewError(fmt.Errorf("failed to scan the directory: %w", err), w)
d.Show()
loadingDialog.Hide()
})
return
}
games, err = d.AllGames()
if err != nil {
fyne.Do(func() {
d := dialog.NewError(fmt.Errorf("failed to load datastore: %w", err), w)
d.Show()
loadingDialog.Hide()
})
return
}
fyne.Do(func() {
list.Refresh()
loadingDialog.Hide()
})
}()
}, w)
confirmDialog.Show()
}, w)
folderSelection.Show()
}),
widget.NewToolbarSeparator(),
widget.NewToolbarAction(theme.UploadIcon(), func() {
doSync(d, w)
}),
widget.NewToolbarSpacer(),
widget.NewToolbarAction(theme.HelpIcon(), func() {
aboutWindow := about.Make(a)
aboutWindow.Show()
}),
)
content := container.NewBorder(toolbar, nil, nil, nil, list)
w.SetContent(content)
return w
}
func doSync(d *data.Service, w fyne.Window) error {
remotes, err := remote.All()
if err != nil {
return err
}
i := 0
nextCh := make(chan struct{})
doneCh := make(chan struct{})
var cd *credential.CredentialDialog
go func() {
nextCh <- struct{}{}
}()
for {
select {
case <-doneCh:
return nil
case <-nextCh:
{
cd = credential.Make(remotes[i].URL, func(v bool) {
if !v {
return
}
username, password := cd.Credentials()
cli := client.New(remotes[i].URL, username, password)
sync.NewSyncer(cli, d)
fmt.Println(i)
if i < len(remotes) {
go func() {
nextCh <- struct{}{}
}()
} else {
go func() {
doneCh <- struct{}{}
}()
}
}, w)
cd.Show()
}
}
}
}