Skip to content

Commit

Permalink
#22: Countdown timer on events page
Browse files Browse the repository at this point in the history
  • Loading branch information
hjtappe committed Dec 11, 2024
1 parent abde0f7 commit 9d7428d
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 18 deletions.
90 changes: 90 additions & 0 deletions lib/components/app_bar_countdown.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:iccm_eu_app/data/dataProviders/events_provider.dart';
import 'package:iccm_eu_app/utils/countdown_functions.dart';
import 'package:iccm_eu_app/utils/date_functions.dart';
import 'package:provider/provider.dart';

class AppBarCountdown extends StatefulWidget {
const AppBarCountdown({super.key});

@override
State<AppBarCountdown> createState() => _AppBarCountdownState();
}

class _AppBarCountdownState extends State<AppBarCountdown> {
Timer? _timer;
late DateTime? _nextEventTime = DateTime.now();
Duration _remainingDuration = Duration.zero;

@override
void initState() {
super.initState();
_startTimer();
}

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

void _startTimer() {
int seconds = 1;
if (_nextEventTime!.difference(DateTime.now()).inDays > 0) {
seconds = 60;
}

// Set state already now, not only in 1 second.
setState(() {
if (_nextEventTime == null) {
_remainingDuration = Duration.zero;
} else {
_remainingDuration = _nextEventTime!.difference(DateTime.now());
}
});

_timer = Timer.periodic(Duration(seconds: seconds), (timer) {
setState(() {
if (_nextEventTime == null) {
_remainingDuration = Duration.zero;
} else {
_remainingDuration = _nextEventTime!.difference(DateTime.now());
}
});
});
}

@override
Widget build(BuildContext context) {
final EventsProvider eventsProvider = Provider.of<EventsProvider>(context);
_nextEventTime = eventsProvider.nextStartTime();
int colorIndex = CountdownFunctions.colors.length - 1;
if (_nextEventTime != null) {
colorIndex = min(CountdownFunctions.colors.length - 1,
_remainingDuration.inMinutes);
}
return Container(
padding: const EdgeInsets.all(4),
decoration: BoxDecoration(
color: CountdownFunctions.colors[colorIndex]
.primary,
borderRadius: BorderRadius.circular(4),
border: Border.all(
color: Theme.of(context).secondaryHeaderColor,
width: 2.0,
),
),
child: Text(DateFunctions.formatDuration(_remainingDuration),
style: TextStyle(
color: CountdownFunctions
.colors[colorIndex].secondary,
),
textAlign: TextAlign.center,
),
);
}

}
4 changes: 2 additions & 2 deletions lib/data/testData/test_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ class TestData {
startTime.isBefore(now.add(Duration(hours: 1, minutes: 1)))) {
// ----------------------------------------
// Modify the duration around now here
// duration = Duration(hours: 0, minutes: 6);
duration = Duration(hours: 0, minutes: 30);
duration = Duration(hours: 0, minutes: 6);
// duration = Duration(hours: 0, minutes: 30);
// ----------------------------------------
parallel = 2;
} else {
Expand Down
28 changes: 13 additions & 15 deletions lib/pages/countdown_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter/material.dart';
import 'package:iccm_eu_app/data/appProviders/preferences_provider.dart';
import 'package:iccm_eu_app/data/dataProviders/events_provider.dart';
import 'package:iccm_eu_app/data/dataProviders/rooms_provider.dart';
import 'package:iccm_eu_app/data/model/colors_data.dart';
import 'package:iccm_eu_app/utils/countdown_functions.dart';
import 'package:iccm_eu_app/utils/date_functions.dart';

import 'package:provider/provider.dart';
Expand All @@ -27,15 +27,6 @@ class CountdownPageState extends State<CountdownPage> {
late DateTime? _nextEventTime = DateTime.now();
Duration _remainingDuration = Duration.zero;

final List<ColorsData> _countdownColors = [
ColorsData(primary: Colors.red.shade900, secondary: Colors.white),
ColorsData(primary: Colors.red.shade600, secondary: Colors.white),
ColorsData(primary: Colors.deepOrange.shade300, secondary: Colors.white),
ColorsData(primary: Colors.yellow.shade700, secondary: Colors.white),
ColorsData(primary: Colors.green.shade900, secondary: Colors.white),
ColorsData(primary: Colors.grey.shade900, secondary: Colors.white),
];

@override
void initState() {
super.initState();
Expand Down Expand Up @@ -90,10 +81,15 @@ class CountdownPageState extends State<CountdownPage> {
_currentEvents = eventsProvider.currentEvents(room: _selectedRoom);
_upcomingEvents = eventsProvider.nextEvents(room: _selectedRoom);
_nextEventTime = eventsProvider.nextStartTime(room: _selectedRoom);
int colorIndex = _countdownColors.length - 1;
int colorIndex = CountdownFunctions.colors.length - 1;
if (_nextEventTime != null) {
colorIndex =
min(_countdownColors.length - 1, _remainingDuration.inMinutes);
colorIndex = min(CountdownFunctions.colors.length - 1,
_remainingDuration.inMinutes);
}
List<String> roomNames = roomsProvider.items().map(
(item) => item.name).toList();
if (! roomNames.contains(_selectedRoom)) {
_selectedRoom = null;
}
return Scaffold(
backgroundColor: Colors.black, // Set Scaffold background to black
Expand Down Expand Up @@ -198,7 +194,8 @@ class CountdownPageState extends State<CountdownPage> {
.width * 0.9,
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: _countdownColors[colorIndex].primary,
color: CountdownFunctions.colors[colorIndex]
.primary,
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Colors.white,
Expand All @@ -219,7 +216,8 @@ class CountdownPageState extends State<CountdownPage> {
Text(
DateFunctions.formatDuration(_remainingDuration),
style: TextStyle(
color: _countdownColors[colorIndex].secondary,
color: CountdownFunctions
.colors[colorIndex].secondary,
),
)
: Text(''),
Expand Down
14 changes: 13 additions & 1 deletion lib/pages/events_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_week_view/flutter_week_view.dart';
import 'package:iccm_eu_app/components/app_bar_countdown.dart';
import 'package:iccm_eu_app/components/event_list_tile.dart';
import 'package:iccm_eu_app/data/appProviders/preferences_provider.dart';
import 'package:iccm_eu_app/data/dataProviders/events_provider.dart';
Expand Down Expand Up @@ -46,7 +47,18 @@ class _EventsPageState extends State<EventsPage> {
builder: (context, futureEvents, child) {
return Scaffold(
appBar: AppBar(
title: const Text('Events'),
title: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text('Events'),
Flexible( // Center the remaining space
child: AppBarCountdown(),
),
Flexible(
child: Text(''),
)
],
),
automaticallyImplyLeading: false,
backgroundColor: Theme
.of(context)
Expand Down
13 changes: 13 additions & 0 deletions lib/utils/countdown_functions.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import 'package:flutter/material.dart';
import 'package:iccm_eu_app/data/model/colors_data.dart';

class CountdownFunctions {
static final List<ColorsData> colors = [
ColorsData(primary: Colors.red.shade900, secondary: Colors.white),
ColorsData(primary: Colors.red.shade600, secondary: Colors.black),
ColorsData(primary: Colors.deepOrange.shade300, secondary: Colors.black),
ColorsData(primary: Colors.yellow.shade700, secondary: Colors.black),
ColorsData(primary: Colors.green.shade900, secondary: Colors.white),
ColorsData(primary: Colors.grey.shade900, secondary: Colors.white),
];
}

0 comments on commit 9d7428d

Please sign in to comment.