summaryrefslogtreecommitdiff
path: root/interface/main.go
blob: 8f7566d59a4ee13f01cbbdd2e7e32a00fb8becd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main

import (
	"fmt"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
	"image/color"
)

type DBInterface struct {
	window fyne.Window
	db     SQLConnection
}

func insertView(window fyne.Window, db SQLConnection, table string) {
	window.SetTitle(fmt.Sprintf("Insert into table %s", table))
	columns, _ := db.getColumns(table)

	var elements []fyne.CanvasObject
	for _, val := range columns {
		label := canvas.NewText(val, color.White)
		input := widget.NewEntry()
		elements = append(elements, container.New(
			layout.NewBorderLayout(nil, nil, label, nil),
			label, input))
	}
	elements = append(elements, widget.NewButton("Вставить", func() {}))
	elements = append(elements, widget.NewButton("Назад", func() {
		showTable(window, db, table)
	}))

	mainContainer := container.NewVBox(elements...)
	window.SetContent(mainContainer)
}

func showTable(window fyne.Window, db SQLConnection, table string) {
	data, err := db.getTable(table)
	if err != nil {
		fmt.Println(err)
		return
	}
	columns, err := db.getColumns(table)

	list := widget.NewTable(
		func() (int, int) {
			return len(data), len(columns)
		},
		func() fyne.CanvasObject {
			return widget.NewLabel("wide content")
		},
		func(i widget.TableCellID, o fyne.CanvasObject) {
			o.(*widget.Label).SetText(fmt.Sprintf("%v", data[i.Row][i.Col]))
		})

	signup := widget.NewButton("Вставить", func() {
		insertView(window, db, table)
	})

	back := widget.NewButton("Назад", func() {
		adminView(window, db)
	})

	window.SetContent(container.NewBorder(nil, container.NewHBox(signup, back), nil, nil, list))
}

func adminView(window fyne.Window, db SQLConnection) {
	tables, _ := db.getTables()

	var tableBtns []fyne.CanvasObject
	tableBtns = append(tableBtns, container.NewCenter(widget.NewLabel("Таблицы")))
	for _, val := range tables {
		e := val
		tableBtns = append(
			tableBtns, widget.NewButton(
				fmt.Sprintf("Показать %s", val),
				func() {
					showTable(window, db, e)
				}))
	}

	views, _ := db.getViews()
	var viewsBtns []fyne.CanvasObject
	viewsBtns = append(viewsBtns, container.NewCenter(widget.NewLabel("Представления")))
	for _, val := range views {
		e := val
		viewsBtns = append(
			viewsBtns, widget.NewButton(
				fmt.Sprintf("Показать %s", val),
				func() {
					showTable(window, db, e)
				}))
	}

	tablesScroll := container.NewVScroll(container.NewVBox(tableBtns...))
	viewsScroll := container.NewVScroll(container.NewVBox(viewsBtns...))

	dataBox := container.NewHBox(tablesScroll, viewsScroll)
	menuBtn := widget.NewButton("В меню", func() {
		menu(window, db)
	})

	window.SetContent(container.NewBorder(nil, menuBtn, nil, nil, dataBox))
}

func userView(window fyne.Window, db SQLConnection) {

}

func menu(window fyne.Window, db SQLConnection) {
	window.SetContent(container.NewCenter(
		container.NewVBox(
			widget.NewButton("Режим Администратора", func() {
				adminView(window, db)
			}),
			widget.NewButton("Режим Пользователя", func() {
				userView(window, db)
			}),
		),
	))
}

func main() {
	myApp := app.New()
	window := myApp.NewWindow("Table Widget")
	window.Resize(fyne.NewSize(700, 480))

	db := SQLConnection{}
	err := db.init()
	if err != nil {
		fmt.Println(err)
		return
	}

	menu(window, db)
	window.ShowAndRun()
}