Skip to content

Commit

Permalink
fix attend quiz error page
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabi-02 committed Apr 17, 2024
1 parent fe39ff1 commit 6fb7d64
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions frontend/lib/pages/quiz/attend_quiz_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
late String _formId;
late String _userId;
late String _alias;
late QuizForm _form;
QuizForm? _form;

String _aliasError = '';

Expand Down Expand Up @@ -93,7 +93,7 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
_loading = false;
_fetchResult = 'success';
});
await fetchForm();
// await fetchForm();
}
} on http.ClientException {
setState(() {
Expand Down Expand Up @@ -237,7 +237,7 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
_socketChannel!.stream.listen((event) {
var data = jsonDecode(event);
if (data["formStatus"] == "FINISHED") {
_form.status = FormStatus.fromString(data["formStatus"]);
_form!.status = FormStatus.fromString(data["formStatus"]);
_value = null;
_voted = false;
_userHasAnsweredCorrectly = false;
Expand All @@ -246,20 +246,20 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
if (data["action"] == "FORM_STATUS_CHANGED") {
var form = QuizForm.fromJson(data["form"]);
setState(() {
_form.status = FormStatus.fromString(data["formStatus"]);
_form!.status = FormStatus.fromString(data["formStatus"]);
_value = null;
_voted = false;
_form.currentQuestionIndex = form.currentQuestionIndex;
_form.currentQuestionFinished = form.currentQuestionFinished;
_form!.currentQuestionIndex = form.currentQuestionIndex;
_form!.currentQuestionFinished = form.currentQuestionFinished;
_scoreboard = getScoreboard(data["form"]);
});
}
if (data["action"] == "CLOSED_QUESTION" ||
data["action"] == "OPENED_NEXT_QUESTION") {
var form = QuizForm.fromJson(data["form"]);
setState(() {
_form.currentQuestionIndex = form.currentQuestionIndex;
_form.currentQuestionFinished = form.currentQuestionFinished;
_form!.currentQuestionIndex = form.currentQuestionIndex;
_form!.currentQuestionFinished = form.currentQuestionFinished;
});
if (data["action"] == "OPENED_NEXT_QUESTION") {
setState(() {
Expand Down Expand Up @@ -299,7 +299,7 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
}, onError: (error) {
//TODO: Should there be another error handling for this?
setState(() {
_form.status = FormStatus.error;
_form!.status = FormStatus.error;
});
});
}
Expand Down Expand Up @@ -401,27 +401,9 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
child: CircularProgressIndicator(),
),
);
} else if (_fetchResult == 'success') {

final int totalQuestions = _form.questions.length;
final double progress = (_form.currentQuestionIndex + 1) / totalQuestions;

final appBarWithProgress = AppBar(
title: Text(_form.name,
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
backgroundColor: Theme.of(context).colorScheme.primary,
bottom: PreferredSize(
preferredSize: const Size.fromHeight(10.0),
child: SizedBox(
height: 10.0,
child: LinearProgressIndicator(
value: progress,
backgroundColor: Colors.grey[300],
valueColor: AlwaysStoppedAnimation<Color>(Theme.of(context).colorScheme.secondary),
),
),
),
);
}

if (_fetchResult == 'success') {

final appBar = AppBar(
title: const Text(
Expand All @@ -437,6 +419,7 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
body: ChooseAlias(
onAliasSubmitted: (chosenAlias) async {
setState(() {
_loading = true;
_alias = chosenAlias;
});
bool success = await participate();
Expand All @@ -452,6 +435,26 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
);
}

final int totalQuestions = _form!.questions.length;
final double progress = (_form!.currentQuestionIndex + 1) / totalQuestions;

final appBarWithProgress = AppBar(
title: Text(_form!.name,
style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold)),
backgroundColor: Theme.of(context).colorScheme.primary,
bottom: PreferredSize(
preferredSize: const Size.fromHeight(10.0),
child: SizedBox(
height: 10.0,
child: LinearProgressIndicator(
value: progress,
backgroundColor: Colors.grey[300],
valueColor: AlwaysStoppedAnimation<Color>(Theme.of(context).colorScheme.secondary),
),
),
),
);

if (_form?.status == FormStatus.finished) {
return Scaffold(
appBar: appBar,
Expand Down Expand Up @@ -528,7 +531,7 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
);
}

if (_form.status != FormStatus.started) {
if (_form!.status != FormStatus.started) {
return Scaffold(
appBar: appBar,
body: Center(
Expand Down Expand Up @@ -556,7 +559,7 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
);
}

final element = _form.questions[_form.currentQuestionIndex];
final element = _form!.questions[_form!.currentQuestionIndex];

return Scaffold(
appBar: appBarWithProgress,
Expand All @@ -569,7 +572,7 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
child: Column(
children: <Widget>[
const SizedBox(height: 16),
Text(element!.name,
Text(element.name,
style: const TextStyle(
fontSize: 24, fontWeight: FontWeight.bold)),
Text(element.description,
Expand Down Expand Up @@ -612,7 +615,7 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
],
),
),
if (!_form.currentQuestionFinished && !_voted)
if (!_form!.currentQuestionFinished && !_voted)
ElevatedButton(
child: const Text('Senden'),
onPressed: () {
Expand All @@ -631,7 +634,7 @@ class _AttendQuizPageState extends AuthState<AttendQuizPage> {
});
},
),
if (!_form.currentQuestionFinished && _voted)
if (!_form!.currentQuestionFinished && _voted)
Container(
margin: const EdgeInsets.only(
top: 0.0,
Expand Down

0 comments on commit 6fb7d64

Please sign in to comment.