-
Notifications
You must be signed in to change notification settings - Fork 0
/
verification.go
176 lines (161 loc) · 6.47 KB
/
verification.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
package yaac_frontend_pages
import (
"fmt"
"image/color"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
yaac_shared "github.com/DHBW-SE-2023/YAAC/internal/shared"
"gorm.io/gorm"
)
func VerificationScreen(w fyne.Window, img []byte, course int, courseTable *fyne.Container, optional ...time.Time) fyne.CanvasObject {
header := ReturnVerificationHeader()
description := canvas.NewText("Vergleichen sie die links dargestellte Unterschriftenliste mit den erfassten heutigen Anwesenheiten:", color.Black)
description.TextSize = 16
description.TextStyle = fyne.TextStyle{Bold: true}
image := RotateImage(img)
var widgetList *fyne.Container
if len(optional) > 0 {
widgetList = LoadVerificationWidgets(course, optional[0])
} else {
widgetList = LoadVerificationWidgets(course, time.Now())
}
confirmButton := ReturnConfirmButton(w, widgetList, optional, course, courseTable)
exitButton := ReturnExitButton(w)
verificationList := container.NewVBox(description, widgetList, container.NewCenter(container.NewGridWithRows(1, confirmButton, exitButton)))
contentBox := container.NewAdaptiveGrid(2, image, container.NewPadded(container.NewPadded(container.NewVScroll(verificationList))))
return container.NewBorder(header, nil, nil, nil, contentBox)
}
/*
ReturnVerificationHeader returns the configured VerificationHeader including navBar extern layout
*/
func ReturnVerificationHeader() *fyne.Container {
headerFrame := canvas.NewRectangle(color.White)
logo := canvas.NewImageFromResource(yaac_shared.ResourceDHBWPng)
logo.FillMode = canvas.ImageFillContain
logo.SetMinSize(fyne.NewSize(200, 200))
title := ReturnHeader("Anwesenheitsprüfung")
// header := container.NewGridWrap(fyne.NewSize(200, 200), logo, title)
header := container.NewBorder(nil, nil, logo, nil, title)
return container.NewStack(headerFrame, header)
}
/*
ReturnConfirmButton returns the configured VerificationHeader passing the window,widgetList, optional params []time.Time,courseID and courseTable.
These values will be used to update the attendancees when the button gets clicked
*/
func ReturnConfirmButton(w fyne.Window, widgetList *fyne.Container, optional []time.Time, course int, courseTable *fyne.Container) *widget.Button {
confirmButton := widget.NewButton("Bestätigen", func() {
var attendances []bool
for _, obj := range widgetList.Objects {
if widget, ok := obj.(*VerificationWidget); ok {
attendances = append(attendances, widget.attending.Checked)
}
}
if len(optional) > 0 {
UpdateList(w, attendances, course, optional[0])
} else {
UpdateList(w, attendances, course, time.Now())
}
ReturnToPreviousPage(w, course, optional, courseTable)
})
return confirmButton
}
/*
UpdateList updates all new attendancies by initializing a AttedanceList Object as soon as the confirmButton gets clicked.
*/
func UpdateList(w fyne.Window, attendances []bool, course int, date time.Time) {
list, _ := myMVVM.AllAttendanceListInRangeByCourse(yaac_shared.Course{Model: gorm.Model{ID: uint(course)}}, date.AddDate(0, 0, -31), date.Add(24*time.Hour))
attendanceList := ReturnUpdatedAttendancies(attendances, list)
_, err := myMVVM.UpdateList(yaac_shared.AttendanceList{
ID: list[0].ID,
CreatedAt: list[0].CreatedAt,
CourseID: uint(course),
Attendancies: attendanceList,
Image: list[0].Image,
ReceivedAt: list[0].ReceivedAt,
})
if err != nil {
dialog.ShowError(fmt.Errorf("fehler beim aktualisieren der informationen.\n%w", err), w)
return
} else {
dialog.ShowInformation("Ihre Liste wurde erfolgreich aktualisiert!", "", w)
}
}
/*
ReturnUpdatedAttendancies provides UpdateList all updatedAttendancies by using the VerificationWidgets Checkbox values and construting
an Attendance object for each.
*/
func ReturnUpdatedAttendancies(attendances []bool, list []yaac_shared.AttendanceList) []yaac_shared.Attendance {
var attendanceList []yaac_shared.Attendance
for i := 0; i < len(list[0].Attendancies); i++ {
att := yaac_shared.Attendance{
StudentID: list[0].Attendancies[i].StudentID,
IsAttending: attendances[i],
AttendanceListID: list[0].ID,
}
attendanceList = append(attendanceList, att)
}
return attendanceList
}
/*
ReturnToPreviousPage executes a command to return to the previousPage passing the window, courseId,optional params []time.Time
and courseTable. Depending on the fact if optional params are passed or not the command decides where to navigate to.
*/
func ReturnToPreviousPage(w fyne.Window, course int, optional []time.Time, courseTable *fyne.Container) {
if lastView != nil {
w.SetContent(lastView)
if len(optional) > 0 {
courses, _ := myMVVM.Courses()
var selectedCourse string
for _, element := range courses {
if element.ID == uint(course) {
selectedCourse = element.Name
}
}
courseTable.RemoveAll()
RefreshCourseAttendancy(courseTable, selectedCourse, optional[0].Format("2006-01-02"))
} else {
LoadOverviewWidgets(w, overviewGrid)
}
}
}
/*
ReturnExit returns the configured exitButton to exit the current Page and return to lastView.
*/
func ReturnExitButton(w fyne.Window) *widget.Button {
exitButton := widget.NewButton("Abbrechen", func() {
if lastView != nil {
w.SetContent(lastView)
}
})
return exitButton
}
/*
GetAttendancies returns all attendancies using the courseID and date for the selected List. Returning the studentNames and isAttending
values in two seperate lists for LoadVerificationWidgets to use.
*/
func GetAttendancies(course int, date time.Time) ([]string, []bool) {
attendancies, _ := myMVVM.AllAttendanceListInRangeByCourse(yaac_shared.Course{Model: gorm.Model{ID: uint(course)}}, date.AddDate(0, 0, -31), date.Add(24*time.Hour))
var students []string
var attendance []bool
for _, element := range attendancies[0].Attendancies {
student, _ := myMVVM.Students(yaac_shared.Student{Model: gorm.Model{ID: element.StudentID}})
students = append(students, fmt.Sprintf("%s %s", student[0].FirstName, student[0].LastName))
attendance = append(attendance, element.IsAttending)
}
return students, attendance
}
/*
LoadVerificationWidgets loads all attendancies as VerificationWidgets into a container.
*/
func LoadVerificationWidgets(course int, date time.Time) *fyne.Container {
students, attendance := GetAttendancies(course, date)
studentList := container.NewVBox()
for i := 0; i < len(students); i++ {
studentList.Add(NewVerificationWidget(students[i], attendance[i], students, attendance))
}
return studentList
}