This commit is contained in:
@@ -4,10 +4,14 @@ import (
|
||||
"cloudsave/cmd/gui/window/about"
|
||||
"cloudsave/cmd/gui/window/credential"
|
||||
"cloudsave/cmd/gui/window/loading"
|
||||
|
||||
syncdialog "cloudsave/cmd/gui/window/sync"
|
||||
"cloudsave/pkg/data"
|
||||
"cloudsave/pkg/remote"
|
||||
"cloudsave/pkg/remote/client"
|
||||
"cloudsave/pkg/repository"
|
||||
"cloudsave/pkg/sync"
|
||||
"cloudsave/pkg/tools/iterator"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -19,6 +23,17 @@ import (
|
||||
"fyne.io/fyne/v2/widget"
|
||||
)
|
||||
|
||||
type (
|
||||
ExtraLabel struct {
|
||||
*widget.Label
|
||||
id string
|
||||
}
|
||||
)
|
||||
|
||||
func (e *ExtraLabel) SetID(id string) {
|
||||
e.id = id
|
||||
}
|
||||
|
||||
func Make(a fyne.App, d *data.Service) fyne.Window {
|
||||
w := a.NewWindow("CloudSave")
|
||||
w.Resize(fyne.NewSize(1000, 700))
|
||||
@@ -38,12 +53,17 @@ func Make(a fyne.App, d *data.Service) fyne.Window {
|
||||
return len(games)
|
||||
},
|
||||
func() fyne.CanvasObject {
|
||||
return widget.NewLabel("template")
|
||||
return &ExtraLabel{Label: widget.NewLabel("")}
|
||||
},
|
||||
func(i widget.ListItemID, o fyne.CanvasObject) {
|
||||
o.(*widget.Label).SetText(games[i].Name)
|
||||
o.(*ExtraLabel).SetText(games[i].Name)
|
||||
o.(*ExtraLabel).SetID(games[i].ID)
|
||||
})
|
||||
|
||||
list.OnSelected = func(id widget.ListItemID) {
|
||||
fmt.Println(id)
|
||||
}
|
||||
|
||||
toolbar := widget.NewToolbar(
|
||||
widget.NewToolbarAction(theme.FolderNewIcon(), func() {
|
||||
folderSelection := dialog.NewFolderOpen(func(lu fyne.ListableURI, err error) {
|
||||
@@ -121,53 +141,85 @@ func Make(a fyne.App, d *data.Service) fyne.Window {
|
||||
return w
|
||||
}
|
||||
|
||||
func doSync(d *data.Service, w fyne.Window) error {
|
||||
remotes, err := remote.All()
|
||||
func doSync(d *data.Service, w fyne.Window) {
|
||||
rs, err := remote.All()
|
||||
if err != nil {
|
||||
return err
|
||||
d := dialog.NewError(fmt.Errorf("failed to load datastore: %w", err), w)
|
||||
d.Show()
|
||||
return
|
||||
}
|
||||
|
||||
i := 0
|
||||
nextCh := make(chan struct{})
|
||||
doneCh := make(chan struct{})
|
||||
it := iterator.New(rs)
|
||||
if it.IsEmpty() {
|
||||
dialog.NewInformation("Sync", "no remote configured", w).Show()
|
||||
return
|
||||
}
|
||||
dialog := credential.Make(it.Value().URL, syncing(it, d, w), w)
|
||||
dialog.Show()
|
||||
}
|
||||
|
||||
var cd *credential.CredentialDialog
|
||||
func syncing(it *iterator.Iterator[remote.Remote], d *data.Service, w fyne.Window) func(bool, string, string) {
|
||||
return func(b bool, username, password string) {
|
||||
if b {
|
||||
r := it.Value()
|
||||
|
||||
go func() {
|
||||
nextCh <- struct{}{}
|
||||
}()
|
||||
cli := client.New(r.URL, username, password)
|
||||
|
||||
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()
|
||||
if err := cli.Ping(); err != nil {
|
||||
dialog.NewError(fmt.Errorf("failed to connect to the server: %w", err), w).Show()
|
||||
return
|
||||
}
|
||||
|
||||
syncDialog := syncdialog.Make(0, w)
|
||||
syncDialog.Show()
|
||||
|
||||
s := sync.NewSyncer(cli, d)
|
||||
|
||||
var errg error
|
||||
s.SetStateCallback(func(s sync.State, g repository.Metadata) {
|
||||
switch s {
|
||||
case sync.FetchingMetdata:
|
||||
fyne.Do(func() {
|
||||
syncDialog.UpdateLabel(fmt.Sprintf("%s: fetching metadata from repository", g.Name))
|
||||
})
|
||||
case sync.Pushing:
|
||||
fyne.Do(func() {
|
||||
syncDialog.UpdateLabel(fmt.Sprintf("%s: pushing data to the server", g.Name))
|
||||
})
|
||||
case sync.Pulling:
|
||||
fyne.Do(func() {
|
||||
syncDialog.UpdateLabel(fmt.Sprintf("%s: pull data from the server", g.Name))
|
||||
})
|
||||
case sync.UpToDate:
|
||||
fyne.Do(func() {
|
||||
syncDialog.UpdateLabel(fmt.Sprintf("%s: already up-to-date", g.Name))
|
||||
})
|
||||
case sync.Pushed:
|
||||
fyne.Do(func() {
|
||||
syncDialog.UpdateLabel(fmt.Sprintf("%s: pushed", g.Name))
|
||||
})
|
||||
case sync.Pulled:
|
||||
fyne.Do(func() {
|
||||
syncDialog.UpdateLabel(fmt.Sprintf("%s: pulled", g.Name))
|
||||
})
|
||||
case sync.Done:
|
||||
fyne.Do(func() {
|
||||
syncDialog.Dismiss()
|
||||
if errg == nil {
|
||||
dialog.NewInformation("Sync", "The sync ended sucessfully!", w).Show()
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
s.SetErrorCallback(func(err error, g repository.Metadata) {
|
||||
errg = err
|
||||
fyne.Do(func() {
|
||||
dialog.NewError(err, w).Show()
|
||||
})
|
||||
})
|
||||
|
||||
go s.Sync()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user