-
Notifications
You must be signed in to change notification settings - Fork 0
/
students.go
266 lines (242 loc) · 6.63 KB
/
students.go
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package gui
import (
"fmt"
"image/color"
"math"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/Tom5521/EduTrack/assets"
"github.com/Tom5521/EduTrack/internal/pkg/sizes"
"github.com/Tom5521/EduTrack/internal/pkg/tools"
"github.com/Tom5521/EduTrack/pkg/conf"
"github.com/Tom5521/EduTrack/pkg/data"
"github.com/Tom5521/EduTrack/pkg/widgets"
"github.com/Tom5521/EduTrack/pkg/wins"
)
type StudentForm struct {
Edit struct {
Enable bool
Student *data.Student
}
Add bool
}
func (ui ui) GetStudentsList(students *[]data.Student) *widget.List {
list := widget.NewList(
func() int {
return len(*students)
},
func() fyne.CanvasObject {
return &widget.Label{}
},
func(i widget.ListItemID, o fyne.CanvasObject) {
s := *students
o.(*widget.Label).SetText(s[i].Name)
},
)
return list
}
func (ui *ui) LoadStudentInfo(s *data.Student) {
// 1: Light | 0: Dark
var currentColor color.Color
if fyne.CurrentApp().Settings().ThemeVariant() == 1 {
currentColor = color.Black
} else {
currentColor = color.White
}
itemLabel := func(inputText any, limit int) *canvas.Text {
text := fmt.Sprint(inputText)
if len(text) > limit {
text = text[:limit] + "..."
}
label := canvas.NewText(text, currentColor)
if conf.Config.Lang == "es" {
label.TextSize = 18
}
if conf.Config.Lang == "en" {
label.TextSize = 20
}
return label
}
tagLabel := func(inputText any) *canvas.Text {
text := fmt.Sprint(inputText)
label := itemLabel(po.Get(text), 90)
label.TextSize = 20
label.TextStyle.Bold = true
return label
}
var nhbx = container.NewHBox
image := tools.LoadProfileImg(s.ImageFilePath)
// Name Label
nameCont := nhbx(tagLabel("Name:"), itemLabel(s.Name, 21))
// Age Label
ageCont := nhbx(tagLabel("Age:"), itemLabel(s.Age, 20))
// DNI Label
dniCont := nhbx(tagLabel("DNI:"), itemLabel(s.DNI, 20))
// Phone Label
phoneCont := nhbx(tagLabel("Phone Number:"), itemLabel(s.PhoneNumber, 20))
dataContainer := container.NewVBox(nameCont, ageCont, dniCont, phoneCont)
showRecordsBt := widget.NewButton(po.Get("Show Student Records"), func() {
ui.StudentRecordsMainWin(s)
})
showCoursesBt := widget.NewButton(po.Get("Show Student Courses"), func() {
ui.StudentCoursesMainWin(s)
})
const gridNumber int = 1
content := container.NewVBox(
image,
dataContainer,
container.NewAdaptiveGrid(
gridNumber,
showRecordsBt,
showCoursesBt,
),
)
ui.StudentTab.Objects = []fyne.CanvasObject{content}
ui.StudentTab.Refresh()
}
func getAgeEntry(app fyne.App, ageEntry *widget.Entry) uint {
text := ageEntry.Text
ret := uint(atoi(text))
if (text != "0") && (ret == math.MaxUint) || (atoi(text) == -1) {
wins.ErrWin(app, po.Get("Overflow in age entry"))
ret = math.MaxUint
}
return ret
}
func (ui *ui) StudentForm(c StudentForm) {
studentToEdit := c.Edit.Student
var imagePath = func() string {
if c.Edit.Enable {
return studentToEdit.ImageFilePath
}
return ""
}()
getWinTitle := func() string {
var winTitle string
if c.Add {
winTitle = po.Get("Add a student")
}
if c.Edit.Enable {
winTitle = po.Get("Edit %s", studentToEdit.Name)
}
return winTitle
}
w := ui.App.NewWindow(getWinTitle())
w.Resize(sizes.FormSize)
// Initialize form fields
nameEntry := widget.NewEntry()
ageEntry := widget.NewEntry()
dniEntry := widget.NewEntry()
phoneEntry := widget.NewEntry()
imagePathLabel := widget.NewLabel(imagePath)
if c.Edit.Enable {
nameEntry.SetText(studentToEdit.Name)
ageEntry.SetText(itoa(studentToEdit.Age))
dniEntry.SetText(studentToEdit.DNI)
phoneEntry.SetText(studentToEdit.PhoneNumber)
}
imageButton := widget.NewButton(po.Get("Select Image"), func() {
wins.ImagePicker(&imagePath)
imagePathLabel.SetText(imagePath)
})
deleteImgBtn := widget.NewButton(po.Get("Delete Current Image"), func() {
imagePath = ""
imagePathLabel.SetText(imagePath)
})
const gridNumber int = 2
form := widgets.NewForm(
&widget.FormItem{Text: po.Get("Name:"), Widget: nameEntry, HintText: po.Get("First and last name")},
widget.NewFormItem(po.Get("DNI:"), dniEntry),
widget.NewFormItem(po.Get("Age:"), ageEntry),
widget.NewFormItem(po.Get("Phone Number:"), phoneEntry),
widget.NewFormItem(po.Get("Image Path"), imagePathLabel),
)
form.CustomItems = container.NewAdaptiveGrid(gridNumber, imageButton, deleteImgBtn)
form.SubmitText = po.Get("Submit")
form.CancelText = po.Get("Cancel")
form.OnCancel = func() {
w.Close()
}
form.OnSubmit = func() {
n := data.Student{
Name: nameEntry.Text,
Age: getAgeEntry(ui.App, ageEntry),
DNI: dniEntry.Text,
PhoneNumber: phoneEntry.Text,
ImageFilePath: imagePath,
}
if !validateStudentForm(n) {
wins.ErrWin(ui.App, po.Get("Some value in this form is empty"))
return
}
if c.Edit.Enable {
if dniEntry.Text != studentToEdit.DNI {
if validateStudentDNI(dniEntry.Text, data.StudentDNIs()) {
wins.ErrWin(ui.App, po.Get("The DNI already exists"))
return
}
}
err := studentToEdit.Edit(&n)
if err != nil {
wins.ErrWin(ui.App, err.Error())
}
}
if c.Add {
if validateStudentDNI(dniEntry.Text, data.StudentDNIs()) {
wins.ErrWin(ui.App, po.Get("The DNI already exists"))
return
}
// Add a new student
err := data.AddStudent(&n)
if err != nil {
wins.ErrWin(ui.App, err.Error())
}
}
ui.StudentList.Refresh()
var idToLoad uint
if c.Add {
idToLoad = n.ID
}
if c.Edit.Enable {
idToLoad = studentToEdit.ID
}
i := data.StudentIndexByID(idToLoad)
if i == -1 {
wins.ErrWin(ui.App, po.Get("Student not found!"))
return
}
s := data.Students[i]
ui.LoadStudentInfo(&s)
w.Close()
}
w.SetContent(form)
w.Show()
}
func (ui *ui) StudentDetailsWin(s *data.Student) {
w := ui.App.NewWindow(po.Get("Details of %s", s.Name))
w.Resize(sizes.FormSize)
form := widgets.NewForm(
widget.NewFormItem(po.Get("Name:"), widget.NewLabel(s.Name)),
widget.NewFormItem(po.Get("Age:"), widget.NewLabel(itoa(s.Age))),
widget.NewFormItem(po.Get("DNI:"), widget.NewLabel(s.DNI)),
widget.NewFormItem(po.Get("Phone Number:"), widget.NewLabel(s.PhoneNumber)),
)
form.CustomItems = container.NewVBox(
widget.NewButton(po.Get("Show student courses"), func() { ui.StudentCoursesMainWin(s) }),
widget.NewButton(po.Get("Show student records"), func() { ui.StudentRecordsMainWin(s) }),
)
form.SubmitText = po.Get("Close")
form.OnSubmit = func() {
w.Close()
}
w.SetContent(form)
w.Show()
}
func (ui ui) TemplateUser() *fyne.Container {
image := canvas.NewImageFromResource(assets.UserTemplate)
image.SetMinSize(sizes.ProfileSize)
content := container.NewVBox(image)
return content
}