Skip to content

Commit

Permalink
Make Quiz Timer functionable, add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
SriramPatibanda authored and nayakastha committed Aug 11, 2021
1 parent 5ab8406 commit 86f0c4a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
34 changes: 34 additions & 0 deletions aptiche/lib/views/quiz/quiz_controller.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
import 'dart:async';
import 'package:aptiche/services/graphql.dart';
import 'package:aptiche/utils/enums.dart';
import 'package:get/get.dart';

class QuizController extends GetxController {
// Variables

/// Finds the [GraphQLService] using [Getx] dependency injection
final GraphQLService _graphQLService = Get.find();

/// Stores the current value of the radio(selected option) of the question.
Rx<ChoicesEnum> radioGroupValue = ChoicesEnum.NON.obs;

/// Late initialises [Timer].
late Timer _timer;

/// Stores the value of the timer.
Rx<Duration> timeout = const Duration(hours: 1).obs;

// Functions

/// A void function which controls the behaviour of the timer(`timeout`)
void startTimeout() {
const Duration oneSec = Duration(seconds: 1);
_timer = Timer.periodic(oneSec, (Timer timer) {
if (timeout.value.inSeconds == 0) {
timer.cancel();
} else {
timeout.value = timeout.value - oneSec;
}
});
}

@override
void dispose() {
_timer.cancel();
super.dispose();
}

/// Changes [radioGroupValue] to the selected option.
void selectOption(ChoicesEnum newValue) {
radioGroupValue.value = newValue;
}

/// A future that fetches a list of [Question] objects for the Quiz.
Future<void> getQuestionsByQuiz(List<String> ids) async {
await _graphQLService.getQuestionsByQuiz(ids);
}
Expand Down
20 changes: 12 additions & 8 deletions aptiche/lib/widgets/quiz/quiz_timer.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import 'package:aptiche/utils/ui_scaling.dart';
import 'package:aptiche/views/quiz/quiz_controller.dart';
import 'package:flutter/material.dart';
import 'package:get/state_manager.dart';

class QuizTimer extends StatelessWidget {
class QuizTimer extends GetView<QuizController> {
const QuizTimer({
Key? key,
}) : super(key: key);

@override
Widget build(BuildContext context) {
return Container(
color: Colors.grey,
color: Theme.of(context).accentColor,
height: SizeConfig.safeBlockVertical! * 3.5,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
Expand All @@ -22,12 +24,14 @@ class QuizTimer extends StatelessWidget {
SizedBox(
width: SizeConfig.safeBlockHorizontal! * 1,
),
Text(
'59:47',
style: Theme.of(context)
.textTheme
.bodyText1!
.copyWith(color: Colors.white),
Obx(
() => Text(
'${controller.timeout.value.inMinutes.remainder(60)} : ${controller.timeout.value.inSeconds.remainder(60)}',
style: Theme.of(context)
.textTheme
.bodyText1!
.copyWith(color: Colors.white),
),
),
],
),
Expand Down

0 comments on commit 86f0c4a

Please sign in to comment.