-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.go
279 lines (256 loc) · 8.22 KB
/
helper.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
267
268
269
270
271
272
273
274
275
276
277
278
279
package yaac_frontend_pages
import (
"errors"
"fmt"
"image/color"
"io"
"log"
"regexp"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/storage"
"fyne.io/fyne/v2/widget"
yaac_backend_database "github.com/DHBW-SE-2023/YAAC/internal/backend/database"
yaac_shared "github.com/DHBW-SE-2023/YAAC/internal/shared"
"gocv.io/x/gocv"
"gorm.io/gorm"
)
var lastView fyne.CanvasObject
/*
RefreshSelection is responsible for refreshing the selection text on changes by using the SelectionTracker struct for student and course view
*/
func RefreshSelection(selection *SelectionTracker) string {
return fmt.Sprintf("%s - %s", selection.courseName.Text, selection.secondary.Text)
}
/*
ReturnHeader will return the canvas.Text objet of each page
*/
func ReturnHeader(pageTitle string) *canvas.Text {
title := canvas.NewText(pageTitle, color.Black)
title.Alignment = fyne.TextAlignCenter
title.TextSize = 28
title.TextStyle = fyne.TextStyle{Bold: true}
return title
}
/*
ReturnAttendanceTable returns the configured attendanceTable as well the tableHeader for the courses and student view
*/
func ReturnAttendanceTable(header1 string, header2 string) (*fyne.Container, *fyne.Container) {
tableHeader := container.NewGridWithColumns(2)
tableHeader.Add(widget.NewLabel(header1))
tableHeader.Add(widget.NewLabel(header2))
attendanceTable := container.NewVBox()
return tableHeader, attendanceTable
}
/*
ReturnCourseDropdown returns the configured courseDropdown passing the SelectionTracker Struct, selection Label, dependingDropdown
and source(course|student) since they will be necessary for change handling for use in course and student view.
*/
func ReturnCourseDropdown(selectionTracker *SelectionTracker, selection *widget.Label, dependingDropdown *widget.SelectEntry, source string) *widget.Select {
courses, _ := myMVVM.Courses()
var courseNames []string
for _, element := range courses {
courseNames = append(courseNames, element.Name)
}
courseDropdown := widget.NewSelect(courseNames,
func(s string) {
selectionTracker.courseName.SetText(s)
selection.SetText(RefreshSelection(selectionTracker))
if source == "course" {
dependingDropdown.SetOptions([]string{})
RefreshDateDropdown(dependingDropdown, s)
} else {
dependingDropdown.SetOptions([]string{})
RefreshStudentDropdown(dependingDropdown, s)
}
dependingDropdown.SetText("")
dependingDropdown.Enable()
})
courseDropdown.Selected = "Kursauswahl"
return courseDropdown
}
/*
MapBooleans will map the booleans to be displayed accordingly on the frontend
*/
func MapBooleans(b bool) string {
if bool(b) {
return "Anwesend"
}
return "Abwesend"
}
/*
GetImageByDate requests the latest list regarding a defined date from the dataset
*/
func GetImageByDate(title string, date time.Time) []byte {
selectedCourse, _ := myMVVM.CourseByName(title)
list, _ := myMVVM.LatestList(yaac_shared.Course{Model: gorm.Model{ID: selectedCourse.ID}}, date)
return list.Image
}
/*
ReturnNonAttending extracts the non attending students given a certain list of attendances(List).
*/
func ReturnNonAttending(attendance []yaac_shared.Attendance) []string {
var returnNonAttending []string
for _, element := range attendance {
if !element.IsAttending {
students, _ := myMVVM.Students(yaac_shared.Student{Model: gorm.Model{ID: element.StudentID}})
if len(students) > 0 {
returnNonAttending = append(returnNonAttending, fmt.Sprintf("%s %s", students[0].FirstName, students[0].LastName))
} else {
continue
}
}
}
return returnNonAttending
}
/*
OpenImageUpload opens a customized FileDialog Window with the possibility to pass optional parameters.
- Optional[0]=Course Name
- Optional[1]=List Date
These values will be passed to the function ShowFileDialog to handle the actual file upload.
*/
func OpenImageUpload(w fyne.Window, optional ...string) {
courseEntry := widget.NewEntry()
content := container.NewVBox()
customDialog := dialog.NewCustomWithoutButtons("Listen Upload", content, w)
if len(optional) != 0 {
courseEntry.Text = optional[0]
} else {
courseEntry.PlaceHolder = "TIK22,TIT22...."
}
fileUpload := widget.NewButton("Bild suchen..", func() {
if len(optional) > 1 {
ShowFileDialog(w, courseEntry.Text, optional[1])
} else {
ShowFileDialog(w, courseEntry.Text)
}
})
ValidateCourseInput(courseEntry, fileUpload)
fileUpload.Disable()
exitButton := widget.NewButton("Zurück", func() {
customDialog.Hide()
})
content.Add(widget.NewLabel("Geben sie das Kürzel des betroffenen Kurses ein:"))
content.Add(courseEntry)
content.Add(container.NewGridWithColumns(2, exitButton, fileUpload))
customDialog.Show()
}
/*
ValidateCourseInput will validate the current input regarding the courseEntry on length and syn
*/
func ValidateCourseInput(courseEntry *widget.Entry, fileUpload *widget.Button) {
courseEntry.Validator = func(s string) error {
re, _ := regexp.Compile(`\bT[A-Z]{2}\d{2}\b`)
if !re.MatchString(s) {
fileUpload.Disable()
return errors.New("die Eingabe entspricht keinem validen Kurs")
} else {
fileUpload.Enable()
}
return nil
}
courseEntry.OnChanged = func(s string) {
if len(s) > 10 {
s = s[0:10]
courseEntry.SetText(s)
}
}
}
/*
Show file selection dialog
*/
func ShowFileDialog(w fyne.Window, courseName string, optional ...string) {
fd := dialog.NewFileOpen(func(reader fyne.URIReadCloser, err error) {
if err != nil {
dialog.ShowError(fmt.Errorf("fehler während der dateiauswhl.\n%w", err), w)
return
}
if reader == nil {
log.Println("Cancelled")
return
}
img := LoadImage(w, reader)
if len(optional) != 0 {
InsertList(w, img, courseName, optional[0])
} else {
InsertList(w, img, courseName)
}
}, w)
fd.SetFilter(storage.NewExtensionFileFilter([]string{".png", ".jpg", ".jpeg"}))
fd.Show()
}
/*
Read file from fs
*/
func LoadImage(w fyne.Window, f fyne.URIReadCloser) []byte {
data, err := io.ReadAll(f)
if err != nil {
dialog.ShowError(fmt.Errorf("fehler beim öffnen der datei.\nIst die datei in einem anderen programm offen?%w", err), w)
return nil
}
return data
}
/*
InsertList will finally construct a yaac_shared.AttendanceList object from the previously gathered functions and push it on the database.
*/
func InsertList(w fyne.Window, img []byte, courseName string, optional ...string) {
// Run improg on list
course, err := myMVVM.CourseByName(courseName)
if err != nil {
dlg := dialog.NewError(fmt.Errorf("error extracting course from database.\nCourse likely does not exist.\n%w", err), w)
dlg.SetOnClosed(func() {
AskCreateCourse(w, img, courseName, optional...)
})
dlg.Show()
return
}
loading := dialog.NewCustomWithoutButtons("In Bearbeitung...", widget.NewProgressBarInfinite(), w)
loading.Show()
_, err = myMVVM.UploadImage(img, &course)
if err != nil {
loading.Hide()
if err.Error() == "record not found" {
dialog.ShowError(errors.New("fehler angegebener kurs entspriht nicht kursname der liste"), w)
return
} else {
dialog.ShowError(fmt.Errorf("fehler bei der automatischen Anwesenheitserkennung.\n%w", err), w)
return
}
}
loading.Hide()
dialog.ShowInformation("Liste erfolgreich hochgeladen", fmt.Sprintf("%s %s %s", "Ihre Liste für den Kurs", courseName, "wurde erfolgreich hochgeladen!"), w)
LoadOverviewWidgets(w, overviewGrid)
}
/*
Ask if the user wants to create a new course with the given name an create it, if user agrees
*/
func AskCreateCourse(w fyne.Window, img []byte, courseName string, optional ...string) {
// Ask to create course
dialog.ShowConfirm(fmt.Sprintf("Wollen sie %s als neuen Kurs anlegen?", courseName), "", func(create bool) {
if create {
_, err := myMVVM.InsertCourse(yaac_backend_database.Course{Name: courseName})
if err != nil {
dialog.ShowError(fmt.Errorf("fehler beim erstellen des kurses.\n%w", err), w)
} else {
InsertList(w, img, courseName)
}
}
}, w)
}
/*
RotateImage will rotate images if necessary so they can be displayed vertically
*/
func RotateImage(img []byte) *canvas.Image {
rotated := gocv.NewMat()
imgMat, _ := gocv.IMDecode(img, gocv.IMReadAnyColor)
gocv.Rotate(imgMat, &rotated, gocv.Rotate90Clockwise)
imgNew, _ := imgMat.ToImage()
image := canvas.NewImageFromImage(imgNew)
return image
}
/*
ShowSuccessDialog
*/