Passed
Push — main ( f3935e...19af13 )
by Rafael
01:35
created

ui.*FileRepoManagerDialog.fileSystemNames   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
dl 0
loc 10
rs 10
c 0
b 0
f 0
nop 0
1
package ui
2
3
import (
4
	"fmt"
5
6
	"fyne.io/fyne/v2"
7
	"fyne.io/fyne/v2/container"
8
	"fyne.io/fyne/v2/data/binding"
9
	"fyne.io/fyne/v2/layout"
10
	"fyne.io/fyne/v2/widget"
11
	"github.com/rchargel/hdfs-explorer/files"
12
	"github.com/rchargel/hdfs-explorer/log"
13
)
14
15
type FileRepoManagerDialog struct {
16
	Title          string
17
	popup          *widget.PopUp
18
	fileSystemRepo files.FileSystemRepository
19
	fsList         binding.DataList
20
	name           binding.String
21
	description    binding.String
22
	addresses      binding.String
23
}
24
25
func NewFileRepoManagerDialog() *FileRepoManagerDialog {
26
	repository, err := files.GetFileSystemRepository()
27
28
	if err != nil {
29
		log.Error.Fatal(err)
30
	}
31
	dialog := &FileRepoManagerDialog{}
32
	dialog.Title = "Connections"
33
	dialog.fileSystemRepo = repository
34
	fileSystems := binding.BindStringList(dialog.fileSystemNames())
35
	dialog.fsList = fileSystems
36
	dialog.name = binding.BindString(nil)
37
	dialog.description = binding.BindString(nil)
38
	dialog.addresses = binding.BindString(nil)
39
40
	return dialog
41
}
42
43
func (d *FileRepoManagerDialog) fileSystemNames() *[]string {
44
	list, err := d.fileSystemRepo.List()
45
	if err != nil {
46
		ShowFatalError(err)
47
	}
48
	names := make([]string, len(list))
49
	for i, fileSystem := range list {
50
		names[i] = fileSystem.Name
51
	}
52
	return &names
53
}
54
55
func (f *FileRepoManagerDialog) Open() {
56
	if f.popup == nil {
57
		fsList := f.fileSystemList()
58
		form := f.createForm()
59
		content := container.NewHBox(fsList, form)
60
		newButton := widget.NewButton("New", func() {
61
			val := fmt.Sprintf("Next %d", f.fsList.Length()+1)
62
			f.fsList.(binding.ExternalStringList).Append(val)
63
		})
64
		f.popup = NewCustomDialog(
65
			f.Title,
66
			content,
67
			newButton,
68
		)
69
	}
70
	f.popup.Show()
71
}
72
73
func (f *FileRepoManagerDialog) createForm() *fyne.Container {
74
	nameLabel := widget.NewLabel("Name: ")
75
	nameEntry := widget.NewEntryWithData(f.name)
76
	descriptionLabel := widget.NewLabel("Description: ")
77
	descriptionEntry := widget.NewEntryWithData(f.description)
78
	descriptionEntry.MultiLine = true
79
	addressLabel := widget.NewLabel("Addresses (new line sperated):")
80
	addressEntry := widget.NewEntryWithData(f.addresses)
81
	addressEntry.MultiLine = true
82
83
	return container.NewVBox(
84
		nameLabel,
85
		nameEntry,
86
		descriptionLabel,
87
		descriptionEntry,
88
		addressLabel,
89
		addressEntry,
90
	)
91
}
92
93
func (f *FileRepoManagerDialog) fileSystemList() *fyne.Container {
94
	list := widget.NewListWithData(
95
		f.fsList,
96
		func() fyne.CanvasObject {
97
			return widget.NewLabel("Connections")
98
		},
99
		func(i binding.DataItem, o fyne.CanvasObject) {
100
			o.(*widget.Label).Bind(i.(binding.String))
101
		},
102
	)
103
	return container.New(layout.NewMaxLayout(), list)
104
}
105