Skip to content

Commit

Permalink
Fix firebase problem (#15)
Browse files Browse the repository at this point in the history
* Add print statements

* Solve bug of too frequent firebase fetches

* Fix small bug

* Fix null update problem in presentation screen
  • Loading branch information
paul019 authored Sep 19, 2024
1 parent 1cd67be commit 1edb26e
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 216 deletions.
4 changes: 4 additions & 0 deletions lib/screens/presentation_screen/presentation_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class _PresentationScreenState extends State<PresentationScreen> {
});
}
activeAgendaItemId = widget.activeAgendaItem?.id;
} else {
setState(() {
activeAgendaItem = null;
});
}

final settings = Provider.of<SettingsModel?>(context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:async';

import 'package:confetti/confetti.dart';
import 'package:flutter/material.dart';
import 'package:integration_bee_helper/models/agenda_item_model/agenda_item_knockout.dart';
Expand All @@ -13,7 +11,6 @@ import 'package:integration_bee_helper/screens/presentation_screen/widgets/timer
import 'package:integration_bee_helper/screens/presentation_screen/widgets/title_view.dart';
import 'package:integration_bee_helper/services/basic_services/intl_service.dart';
import 'package:integration_bee_helper/widgets/current_integral_stream.dart';
import 'package:just_audio/just_audio.dart';

class PresentationScreenKnockout extends StatefulWidget {
final AgendaItemModelKnockout activeAgendaItem;
Expand All @@ -36,12 +33,6 @@ class _PresentationScreenKnockoutState
extends State<PresentationScreenKnockout> {
String agendaItemId = '';

late Timer timer;
Duration timeLeft = Duration.zero;
bool timerRed = false;

late final AudioPlayer player;

late final ConfettiController competitor1ConfettiController;
late final ConfettiController competitor2ConfettiController;

Expand Down Expand Up @@ -78,11 +69,6 @@ class _PresentationScreenKnockoutState
}
}

DateTime? get timerStopsAt => widget.activeAgendaItem.timer.timerStopsAt;

Duration? get pausedTimerDuration =>
widget.activeAgendaItem.timer.pausedTimerDuration;

void initialize() async {
agendaItemId = widget.activeAgendaItem.id;

Expand All @@ -93,111 +79,16 @@ class _PresentationScreenKnockoutState
void initState() {
initialize();

player = AudioPlayer();

competitor1ConfettiController =
ConfettiController(duration: const Duration(seconds: 10));
competitor2ConfettiController =
ConfettiController(duration: const Duration(seconds: 10));

const timerInterval = Duration(milliseconds: 250);
const timeWarningDuration = Duration(seconds: 30);

timer = Timer.periodic(timerInterval, (timer) {
switch (problemPhase) {
case ProblemPhase.idle:
competitor1ConfettiController.stop();
competitor2ConfettiController.stop();

if (widget.activeAgendaItem.currentIntegralType ==
IntegralType.regular) {
setState(() {
timeLeft = widget.activeAgendaItem.timeLimitPerIntegral;
timerRed = false;
});
} else {
setState(() {
timeLeft = widget.activeAgendaItem.timeLimitPerSpareIntegral;
timerRed = false;
});
}
break;
case ProblemPhase.showProblem:
competitor1ConfettiController.stop();
competitor2ConfettiController.stop();

if (pausedTimerDuration != null) {
setState(() {
timeLeft = pausedTimerDuration!;
timerRed = pausedTimerDuration! <
timeWarningDuration + const Duration(seconds: 1);
});
} else if (timerStopsAt == null) {
setState(() {
timeLeft = Duration.zero;
timerRed = false;
});
} else {
final difference = timerStopsAt!.difference(DateTime.now());

setState(() {
timeLeft = difference.isNegative ? Duration.zero : difference;
timerRed =
difference < timeWarningDuration + const Duration(seconds: 1);
});

if (difference < timeWarningDuration + const Duration(seconds: 1) &&
difference + timerInterval >
timeWarningDuration + const Duration(seconds: 1)) {
playWarningSound();
}
if (difference < Duration.zero &&
difference + timerInterval > Duration.zero) {
playTimeUpSound();
}
}
break;
case ProblemPhase.showSolution:
case ProblemPhase.showSolutionAndWinner:
if (widget.activeAgendaItem.currentWinner == Score.competitor1) {
if (competitor1ConfettiController.state !=
ConfettiControllerState.playing) {
competitor1ConfettiController.play();
}
} else if (widget.activeAgendaItem.currentWinner ==
Score.competitor2) {
if (competitor2ConfettiController.state !=
ConfettiControllerState.playing) {
competitor2ConfettiController.play();
}
}

setState(() {
timeLeft = Duration.zero;
timerRed = false;
});

break;
}
});

super.initState();
}

void playWarningSound() {
if (widget.isPreview) return;
player.setAsset('sound/time_warning.mp3').then((_) => player.play());
}

void playTimeUpSound() {
if (widget.isPreview) return;
player.setAsset('sound/time_up.mp3').then((_) => player.play());
}

@override
void dispose() {
timer.cancel();
player.dispose();
competitor1ConfettiController.dispose();
competitor2ConfettiController.dispose();
super.dispose();
Expand All @@ -218,10 +109,11 @@ class _PresentationScreenKnockoutState
alignment: Alignment.center,
children: [
TimerView(
timeLeft: timeLeft,
timerRed: timerRed,
paused: pausedTimerDuration != null,
activeAgendaItem: widget.activeAgendaItem,
size: widget.size,
isPreview: widget.isPreview,
competitor1ConfettiController: competitor1ConfettiController,
competitor2ConfettiController: competitor2ConfettiController,
),
ScoreView(
competitor1Name: widget.activeAgendaItem.competitor1Name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:integration_bee_helper/models/agenda_item_model/agenda_item_qualification.dart';
import 'package:integration_bee_helper/models/integral_model/current_integral_wrapper.dart';
Expand All @@ -12,7 +10,6 @@ import 'package:integration_bee_helper/screens/presentation_screen/widgets/timer
import 'package:integration_bee_helper/screens/presentation_screen/widgets/title_view.dart';
import 'package:integration_bee_helper/services/basic_services/intl_service.dart';
import 'package:integration_bee_helper/services/integrals_service/integrals_service.dart';
import 'package:just_audio/just_audio.dart';
import 'package:provider/provider.dart';

class PresentationScreenQualification extends StatefulWidget {
Expand All @@ -36,12 +33,6 @@ class _PresentationScreenQualificationState
extends State<PresentationScreenQualification> {
String agendaItemId = '';

late Timer timer;
Duration timeLeft = Duration.zero;
bool timerRed = false;

late final AudioPlayer player;

String get uid => widget.activeAgendaItem.uid;
ProblemPhase get problemPhase => widget.activeAgendaItem.problemPhase;
List<String> get integralsCodes => widget.activeAgendaItem.integralsCodes;
Expand Down Expand Up @@ -76,89 +67,9 @@ class _PresentationScreenQualificationState
void initState() {
initialize();

player = AudioPlayer();

const timerInterval = Duration(milliseconds: 250);
const timeWarningDuration = Duration(seconds: 30);

timer = Timer.periodic(timerInterval, (timer) {
switch (problemPhase) {
case ProblemPhase.idle:
if (widget.activeAgendaItem.currentIntegralType ==
IntegralType.regular) {
setState(() {
timeLeft = widget.activeAgendaItem.timeLimitPerIntegral;
timerRed = false;
});
} else {
setState(() {
timeLeft = widget.activeAgendaItem.timeLimitPerSpareIntegral;
timerRed = false;
});
}
break;
case ProblemPhase.showProblem:
if (pausedTimerDuration != null) {
setState(() {
timeLeft = pausedTimerDuration!;
timerRed = pausedTimerDuration! <
timeWarningDuration + const Duration(seconds: 1);
});
} else if (timerStopsAt == null) {
setState(() {
timeLeft = Duration.zero;
timerRed = false;
});
} else {
final difference = timerStopsAt!.difference(DateTime.now());

setState(() {
timeLeft = difference.isNegative ? Duration.zero : difference;
timerRed =
difference < timeWarningDuration + const Duration(seconds: 1);
});

if (difference < timeWarningDuration + const Duration(seconds: 1) &&
difference + timerInterval >
timeWarningDuration + const Duration(seconds: 1)) {
playWarningSound();
}
if (difference < Duration.zero &&
difference + timerInterval > Duration.zero) {
playTimeUpSound();
}
}
break;
case ProblemPhase.showSolution:
case ProblemPhase.showSolutionAndWinner:
setState(() {
timeLeft = Duration.zero;
timerRed = false;
});
break;
}
});

super.initState();
}

void playWarningSound() {
if (widget.isPreview) return;
player.setAsset('time_warning.mp3').then((_) => player.play());
}

void playTimeUpSound() {
if (widget.isPreview) return;
player.setAsset('time_up.mp3').then((_) => player.play());
}

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

@override
Widget build(BuildContext context) {
if (agendaItemId != widget.activeAgendaItem.id) {
Expand All @@ -180,10 +91,9 @@ class _PresentationScreenQualificationState
alignment: Alignment.center,
children: [
TimerView(
timeLeft: timeLeft,
timerRed: timerRed,
paused: pausedTimerDuration != null,
activeAgendaItem: widget.activeAgendaItem,
size: widget.size,
isPreview: widget.isPreview,
),
IntegralView(
currentIntegral: currentIntegral,
Expand All @@ -201,7 +111,8 @@ class _PresentationScreenQualificationState
),
NamesView(
competitorNames: widget.activeAgendaItem.competitorNames,
problemName: problemName ?? MyIntl.of(context).exerciseNumber(1),
problemName:
problemName ?? MyIntl.of(context).exerciseNumber(1),
size: widget.size,
),
],
Expand Down
Loading

0 comments on commit 1edb26e

Please sign in to comment.