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
|
package sh.lajo.buddy
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
@Composable
fun EducationScreen(repository: EducationRepository = remember { EducationRepository() }) {
var lectures by remember { mutableStateOf<List<EducationLecture>>(emptyList()) }
var selectedLecture by remember { mutableStateOf<EducationLecture?>(null) }
var showingQuiz by remember { mutableStateOf(false) }
var isLoading by remember { mutableStateOf(true) }
LaunchedEffect(Unit) {
lectures = repository.fetchLectures()
isLoading = false
}
if (isLoading) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
CircularProgressIndicator()
}
} else if (showingQuiz && selectedLecture != null) {
QuizScreen(quiz = selectedLecture!!.quiz, onBack = { showingQuiz = false })
} else if (selectedLecture != null) {
LectureDetailScreen(
lecture = selectedLecture!!,
onBack = { selectedLecture = null },
onStartQuiz = { showingQuiz = true }
)
} else {
LectureListScreen(lectures = lectures, onLectureClick = { selectedLecture = it })
}
}
@Composable
fun LectureListScreen(lectures: List<EducationLecture>, onLectureClick: (EducationLecture) -> Unit) {
LazyColumn(
modifier = Modifier.fillMaxSize(),
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
item {
Text(
text = "Edukacija",
style = MaterialTheme.typography.headlineMedium,
modifier = Modifier.padding(bottom = 16.dp)
)
}
items(lectures) { lecture ->
ElevatedCard(
onClick = { onLectureClick(lecture) },
modifier = Modifier.fillMaxWidth()
) {
Text(
text = lecture.title,
modifier = Modifier.padding(16.dp),
style = MaterialTheme.typography.titleLarge
)
}
}
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LectureDetailScreen(
lecture: EducationLecture,
onBack: () -> Unit,
onStartQuiz: () -> Unit
) {
Scaffold(
topBar = {
TopAppBar(
title = { Text(lecture.title) },
navigationIcon = {
IconButton(onClick = onBack) {
Icon(Icons.Default.ArrowBack, contentDescription = "Back")
}
}
)
}
) { paddingValues ->
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(paddingValues),
contentPadding = PaddingValues(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
lecture.lectureItems.forEach { item ->
item {
Text(text = item.title, style = MaterialTheme.typography.headlineSmall)
}
items(item.lectureElements) { element ->
when (element.type) {
"text" -> element.text?.let { Text(text = it, style = MaterialTheme.typography.bodyLarge) }
"image" -> element.imageUrl?.let {
AsyncImage(
model = it,
contentDescription = null,
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 8.dp)
)
}
}
}
item {
HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp))
}
}
item {
Button(
onClick = onStartQuiz,
modifier = Modifier
.fillMaxWidth()
.padding(top = 16.dp)
) {
Text("Započni kviz")
}
}
}
}
}
@Composable
fun QuizScreen(quiz: Quiz, onBack: () -> Unit) {
var currentQuestionIndex by remember { mutableIntStateOf(0) }
var selectedAnswerIndex by remember { mutableStateOf<Int?>(null) }
var score by remember { mutableIntStateOf(0) }
var showResult by remember { mutableStateOf(false) }
val currentQuestion = quiz.questions.getOrNull(currentQuestionIndex)
if (showResult || currentQuestion == null) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text(text = "Kviz završen!", style = MaterialTheme.typography.headlineMedium)
Spacer(modifier = Modifier.height(8.dp))
Text(text = "Tvoj rezultat: $score / ${quiz.questions.size}", style = MaterialTheme.typography.titleLarge)
Spacer(modifier = Modifier.height(24.dp))
Button(onClick = onBack) {
Text("Povratak na predavanje")
}
}
} else {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
Text(
text = "Pitanje ${currentQuestionIndex + 1} od ${quiz.questions.size}",
style = MaterialTheme.typography.labelMedium
)
Spacer(modifier = Modifier.height(8.dp))
Text(text = currentQuestion.question, style = MaterialTheme.typography.headlineSmall)
Spacer(modifier = Modifier.height(24.dp))
currentQuestion.answers.forEachIndexed { index, answer ->
val isSelected = selectedAnswerIndex == index
OutlinedCard(
onClick = { selectedAnswerIndex = index },
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
colors = CardDefaults.outlinedCardColors(
containerColor = if (isSelected) MaterialTheme.colorScheme.primaryContainer else MaterialTheme.colorScheme.surface
),
border = CardDefaults.outlinedCardBorder().run {
if (isSelected) copy(width = 2.dp) else this
}
) {
Text(
text = answer,
modifier = Modifier.padding(16.dp),
style = MaterialTheme.typography.bodyLarge
)
}
}
Spacer(modifier = Modifier.weight(1f))
Button(
onClick = {
// correctAnswer in JSON is "1" which refers to index 1 (second answer)
val correctIndex = currentQuestion.correctAnswer.toIntOrNull() ?: 0
if (selectedAnswerIndex == correctIndex) {
score++
}
if (currentQuestionIndex < quiz.questions.size - 1) {
currentQuestionIndex++
selectedAnswerIndex = null
} else {
showResult = true
}
},
enabled = selectedAnswerIndex != null,
modifier = Modifier.fillMaxWidth()
) {
Text(if (currentQuestionIndex < quiz.questions.size - 1) "Sljedeće pitanje" else "Završi kviz")
}
}
}
}
|