Skip to content

Commit

Permalink
Add Offline Mode Task
Browse files Browse the repository at this point in the history
  • Loading branch information
gmochid committed Nov 9, 2023
1 parent 54226c8 commit 46572c1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {

quiz = await quizService.getWeeklyQuizById(quizId);

problemIdList = quiz.problems[challengeGroup]!;
// ignore: unnecessary_null_comparison
if (problemIdList == null) {
final _problemIdList = quiz.problems[challengeGroup];
if (_problemIdList == null) {
throw Exception('Task set for `$challengeGroup` is not found');
}
problemIdList = _problemIdList;
if (problemIdList.isEmpty) {
throw Exception('Task set for `$challengeGroup` is empty');
}
Expand All @@ -78,6 +78,9 @@ class QuizExerciseCubit extends Cubit<QuizExerciseState> {
answers: [],
);

// Fetch all quiz data for it to be available when offline
await quizExerciseRepository.getListQuizExercise(problemIdList);

currentProblem =
await quizExerciseRepository.getQuizExercise(problemIdList.first);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ class _QuizExercisePageState extends State<QuizExercisePage> {
@override
void initState() {
final cubit = context.read<QuizExerciseCubit>();
if (cubit.quizParticipantId != widget.quizParticipantId) {
cubit.initialize(
quizId: widget.quizId,
quizParticipantId: widget.quizParticipantId,
challengeGroup: widget.challengeGroup);
}
// if (cubit.quizParticipantId != widget.quizParticipantId) {
cubit.initialize(
quizId: widget.quizId,
quizParticipantId: widget.quizParticipantId,
challengeGroup: widget.challengeGroup);
// }
super.initState();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,24 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/foundation.dart';
import 'package:injectable/injectable.dart';

import '../../../../services/firebase_service.dart';
import '../model/quiz_exercise.dart';
import '../model/quiz_exercise_attempt.dart';

@injectable
class QuizExerciseRepository {
FirebaseFirestore db = FirebaseFirestore.instance;

QuizExerciseRepository() {
db.settings = FirebaseService.settings;
}

Future<List<QuizExercise>> getListQuizExercise(
List<String> taskIdList,
) async {
final quizExerciseList = <QuizExercise>[];
try {
final result =
await FirebaseFirestore.instance.collection('task_set').get();
final result = await db.collection('task_set').get();
final taskResult =
result.docs.where((element) => taskIdList.contains(element.id));
for (final element in taskResult) {
Expand All @@ -33,10 +39,8 @@ class QuizExerciseRepository {

Future<QuizExercise> getQuizExercise(String taskId) async {
try {
final result = await FirebaseFirestore.instance
.collection('task_set')
.where('id', isEqualTo: taskId)
.get();
final result =
await db.collection('task_set').where('id', isEqualTo: taskId).get();
if (result.docs.isEmpty) {
throw Exception('Task ID not found');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';

import '../../../../core/bases/enum/button_type.dart';
import '../../../../core/bases/widgets/atoms/button.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ class _QuizRegistrationPageState extends State<QuizRegistrationPage> {
Widget quizCard(WeeklyQuizParticipation weeklyQuizParticipant, String date,
String score, String level, BuildContext context) {
return InkWell(
onTap: () {
onTap: () async {
Future future;
if (weeklyQuizParticipant.attempts.length >=
weeklyQuizParticipant.quiz_max_attempts) {
context.push(
future = context.push(
Uri(
path: '/quiz_result',
queryParameters: {
Expand All @@ -59,7 +60,7 @@ class _QuizRegistrationPageState extends State<QuizRegistrationPage> {
).toString(),
);
} else if (weeklyQuizParticipant.attempts.isNotEmpty) {
context.push(
future = context.push(
Uri(
path: '/quiz_result',
queryParameters: {
Expand All @@ -68,7 +69,7 @@ class _QuizRegistrationPageState extends State<QuizRegistrationPage> {
).toString(),
);
} else {
context.push(
future = context.push(
Uri(
path: '/quiz_exercise',
queryParameters: {
Expand All @@ -79,6 +80,11 @@ class _QuizRegistrationPageState extends State<QuizRegistrationPage> {
).toString(),
);
}

await future;
await context
.read<QuizRegistrationCubit>()
.fetchParticipantWeeklyQuiz();
},
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 18),
Expand Down
4 changes: 4 additions & 0 deletions app/lib/services/firebase_service.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';

Expand All @@ -13,6 +14,9 @@ class FirebaseService {
AppLogger.d('Initialized app $app');
}

static const Settings settings = Settings(
persistenceEnabled: true, cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED);

static FirebaseAuth auth() {
return FirebaseAuth.instanceFor(app: app);
}
Expand Down
2 changes: 1 addition & 1 deletion app/lib/services/quiz_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class QuizService {
String currentUserUID = FirebaseService.auth().currentUser!.uid;

QuizService() {
db.settings = const Settings(cacheSizeBytes: Settings.CACHE_SIZE_UNLIMITED);
db.settings = FirebaseService.settings;
}

// week => running_weekly_quiz or next_weekly_quiz
Expand Down

0 comments on commit 46572c1

Please sign in to comment.