Skip to content

Commit

Permalink
Merge pull request #70 from ia-toki/quiz-start-page
Browse files Browse the repository at this point in the history
Quiz Start Page
  • Loading branch information
gmochid authored Nov 10, 2023
2 parents 1e2c0fe + 6067565 commit aa76145
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 67 deletions.
2 changes: 2 additions & 0 deletions app/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'features/onboarding/presentation/bloc/user_initialization_bloc.dart';
import 'features/quiz_exercise/presentation/bloc/quiz_exercise_cubit.dart';
import 'features/quiz_registration/presentation/bloc/quiz_registration_cubit.dart';
import 'features/quiz_result/presentation/bloc/quiz_result_cubit.dart';
import 'features/quiz_start/presentation/bloc/quiz_start_cubit.dart';
import 'services/di.dart';
import 'services/router_service.dart';

Expand All @@ -30,6 +31,7 @@ class App extends StatelessWidget {
),
BlocProvider(create: (context) => QuizExerciseCubit()),
BlocProvider(create: (context) => QuizResultCubit()),
BlocProvider(create: (context) => QuizStartCubit()),
BlocProvider(create: (context) => QuizRegistrationCubit()),
BlocProvider(create: (context) => get<UserRegisterBloc>()),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'dart:async';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../models/quiz_participation.dart';
import '../../../../models/weekly_quiz.dart';
import '../../../../services/quiz_service.dart';
import '../model/quiz_exercise.dart';
Expand All @@ -18,11 +19,11 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
QuizExerciseCubit() : super(QuizExerciseInitialState());

late WeeklyQuiz quiz;
late WeeklyQuizParticipation participation;
late QuizExercise currentProblem;
int currentProblemIndex = 0;
late List<String> problemIdList;
late QuizExerciseAttempt attempt;
late String challengeGroup;
String? quizParticipantId;

String selectedAnswer = '';
Expand All @@ -33,43 +34,35 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
late QuizService quizService;
late QuizExerciseRepository quizExerciseRepository;

FutureOr<void> initialize(
{String? quizId,
String? quizParticipantId,
String? challengeGroup}) async {
FutureOr<void> initialize({String? quizParticipantId}) async {
try {
quizService = QuizService();
quizExerciseRepository = QuizExerciseRepository();

emit(QuizExerciseLoading());
if (quizId == null) {
throw Exception('Quiz Id null');
}
if (quizParticipantId == null) {
throw Exception('Quiz Participant Id null');
}
if (challengeGroup == null) {
throw Exception('Challenge Group null');
}
this.quizParticipantId = quizParticipantId;
this.challengeGroup = challengeGroup;

quiz = await quizService.getWeeklyQuizById(quizId);
final participation = await quizService.getWeeklyQuizParticipant(
quizParticipantId: quizParticipantId,
);

quiz = await quizService.getWeeklyQuizById(participation.quiz_id);

final _problemIdList = quiz.problems[challengeGroup];
if (_problemIdList == null) {
throw Exception('Task set for `$challengeGroup` is not found');
final list = quiz.problems[participation.challenge_group];
if (list == null) {
throw Exception(
'Task set for `${participation.challenge_group}` is not found');
}
problemIdList = _problemIdList;
problemIdList = list;
if (problemIdList.isEmpty) {
throw Exception('Task set for `$challengeGroup` is empty');
throw Exception(
'Task set for `${participation.challenge_group}` is empty');
}

// TODO(someone): fix the check logic later
// final weeklyQuizParticipant = await quizService.getWeeklyQuizParticipant(
// quizParticipantId: quizParticipantId,
// );
//
// if (weeklyQuizParticipant.attempts.isEmpty) {
attempt = QuizExerciseAttempt(
startAt: DateTime.now(),
Expand All @@ -88,7 +81,7 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
currentProblem =
await quizExerciseRepository.getQuizExercise(problemIdList.first);

final duration = quiz.duration_minute[challengeGroup];
final duration = quiz.duration_minute[participation.challenge_group];
if (duration == null) {
throw Exception('Duration for selected Challenge Group not found');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
part of '_pages.dart';

class QuizExercisePage extends StatefulWidget {
final String? quizId;
final String? challengeGroup;
final String? quizParticipantId;
QuizExercisePage(
{super.key, this.quizId, this.challengeGroup, this.quizParticipantId});
QuizExercisePage({super.key, this.quizParticipantId});

@override
State<QuizExercisePage> createState() => _QuizExercisePageState();
Expand All @@ -18,10 +15,7 @@ class _QuizExercisePageState extends State<QuizExercisePage> {
void initState() {
final cubit = context.read<QuizExerciseCubit>();
// if (cubit.quizParticipantId != widget.quizParticipantId) {
cubit.initialize(
quizId: widget.quizId,
quizParticipantId: widget.quizParticipantId,
challengeGroup: widget.challengeGroup);
cubit.initialize(quizParticipantId: widget.quizParticipantId);
// }
super.initState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,14 @@ class _QuizRegistrationPageState extends State<QuizRegistrationPage> {
String score, String level, BuildContext context) {
return InkWell(
onTap: () async {
Future future;
if (weeklyQuizParticipant.attempts.length >=
weeklyQuizParticipant.quiz_max_attempts) {
future = context.push(
Uri(
path: '/quiz_result',
queryParameters: {
'quiz_participant_id': weeklyQuizParticipant.id,
},
).toString(),
);
} else if (weeklyQuizParticipant.attempts.isNotEmpty) {
future = context.push(
Uri(
path: '/quiz_result',
queryParameters: {
'quiz_participant_id': weeklyQuizParticipant.id,
},
).toString(),
);
} else {
future = context.push(
Uri(
path: '/quiz_exercise',
queryParameters: {
'quiz_id': weeklyQuizParticipant.quiz_id,
'challenge_group': weeklyQuizParticipant.challenge_group,
'quiz_participant_id': weeklyQuizParticipant.id,
},
).toString(),
);
}

await future;
await context.push(
Uri(
path: '/quiz_start',
queryParameters: {
'quiz_participant_id': weeklyQuizParticipant.id,
},
).toString(),
);
await context
.read<QuizRegistrationCubit>()
.fetchParticipantWeeklyQuiz();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:bloc/bloc.dart';
import 'package:meta/meta.dart';

import '../../../../models/quiz_participation.dart';
import '../../../../models/weekly_quiz.dart';
import '../../../../services/quiz_service.dart';

part 'quiz_start_state.dart';

class QuizStartCubit extends Cubit<QuizStartState> {
QuizStartCubit() : super(QuizStartInitial());

bool agreement = false;
late WeeklyQuiz quiz;
late WeeklyQuizParticipation participation;

Future<void> initialize(String? quizParticipantId) async {
try {
final quizService = QuizService();

if (quizParticipantId == null) {
throw Exception('QuizParticipantId is null');
}

participation = await quizService.getWeeklyQuizParticipant(
quizParticipantId: quizParticipantId);

quiz = await quizService.getWeeklyQuizById(participation.quiz_id);

emit(QuizStartSuccess(
participation: participation, quiz: quiz, agreement: agreement));
} catch (e) {
emit(QuizStartFailed(e.toString()));
}
}

void setAgreement(bool value) {
agreement = value;
emit(QuizStartSuccess(
participation: participation, quiz: quiz, agreement: agreement));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
part of 'quiz_start_cubit.dart';

@immutable
abstract class QuizStartState {}

class QuizStartInitial extends QuizStartState {}

class QuizStartSuccess extends QuizStartState {
final WeeklyQuizParticipation participation;
final WeeklyQuiz quiz;
final bool agreement;

QuizStartSuccess(
{required this.participation,
required this.quiz,
required this.agreement});
@override
List<Object> get props => [participation, quiz, agreement];
}

class QuizStartFailed extends QuizStartState {
final String error;

QuizStartFailed(this.error);

@override
List<Object> get props => [error];
}
11 changes: 11 additions & 0 deletions app/lib/features/quiz_start/presentation/pages/_pages.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';

import '../../../../core/bases/enum/button_type.dart';
import '../../../../core/bases/widgets/atoms/button.dart';
import '../../../../core/bases/widgets/layout/bebras_scaffold.dart';
import '../../../../core/constants/assets.dart';
import '../bloc/quiz_start_cubit.dart';

part 'quiz_start_page.dart';
Loading

0 comments on commit aa76145

Please sign in to comment.