From bcf9a1586a0b4c31df6e3e9ce3f30726eccc1ece Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 2 Nov 2019 20:14:34 +0100 Subject: [PATCH] Add support for countdown posts --- lib/ui/tiles/post_tile.dart | 12 ++++ lib/ui/widgets/countdown_widget.dart | 84 ++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 lib/ui/widgets/countdown_widget.dart diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 580d532..1f253d9 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -15,6 +15,7 @@ import 'package:comunic/models/post.dart'; import 'package:comunic/models/user.dart'; import 'package:comunic/ui/tiles/comment_tile.dart'; import 'package:comunic/ui/widgets/account_image_widget.dart'; +import 'package:comunic/ui/widgets/countdown_widget.dart'; import 'package:comunic/ui/widgets/network_image_widget.dart'; import 'package:comunic/ui/widgets/survey_widget.dart'; import 'package:comunic/utils/date_utils.dart'; @@ -176,6 +177,10 @@ class _PostTileState extends State { postContent = _buildPostWebLink(); break; + case PostKind.COUNTDOWN: + postContent = _buildCountDownTimer(); + break; + case PostKind.SURVEY: postContent = _buildPostSurvey(); break; @@ -320,6 +325,13 @@ class _PostTileState extends State { ); } + Widget _buildCountDownTimer() { + return CountdownWidget( + startTime: widget.post.timeSent, + endTime: widget.post.timeEnd, + ); + } + /// Build post survey Widget _buildPostSurvey() { return SurveyWidget( diff --git a/lib/ui/widgets/countdown_widget.dart b/lib/ui/widgets/countdown_widget.dart new file mode 100644 index 0000000..7ff56bc --- /dev/null +++ b/lib/ui/widgets/countdown_widget.dart @@ -0,0 +1,84 @@ +import 'dart:async'; + +import 'package:comunic/utils/date_utils.dart'; +import 'package:comunic/utils/intl_utils.dart'; +import 'package:flutter/material.dart'; + +/// Countdown widget +/// +/// @author Pierre HUBERT + +class CountdownWidget extends StatefulWidget { + final int startTime; + final int endTime; + + const CountdownWidget({ + Key key, + @required this.startTime, + @required this.endTime, + }) : assert(startTime != null), + assert(endTime != null), + super(key: key); + + @override + _CountdownWidgetState createState() => _CountdownWidgetState(); +} + +class _CountdownWidgetState extends State { + int get remainingTime => widget.endTime - time(); + + int get totalDuration => (widget.endTime - widget.startTime).abs(); + + String get remainingTimeStr { + final remaining = Duration(seconds: remainingTime); + + return tr( + "%days% Days %hours% Hours %minutes% Minutes %seconds% Seconds", + args: { + "days": remaining.inDays.toString(), + "hours": (remaining.inHours % 24).toString(), + "minutes": (remaining.inMinutes % 60).toString(), + "seconds": (remaining.inSeconds % 60).toString(), + }, + ); + } + + @override + void initState() { + super.initState(); + + Timer.periodic(Duration(seconds: 1), _timerCb); + } + + void _timerCb(Timer timer) { + if (!mounted) timer.cancel(); + + setState(() {}); + } + + @override + Widget build(BuildContext context) { + return Container( + width: 300, + child: Column( + children: [ + Padding( + padding: const EdgeInsets.all(8.0), + child: Text(remainingTimeStr), + ), + Padding( + padding: const EdgeInsets.all(8.0), + child: LinearProgressIndicator( + value: remainingTime <= 0 + ? 1.0 + : 1 - (remainingTime / totalDuration), + backgroundColor: Theme.of(context).accentColor, + valueColor: AlwaysStoppedAnimation( + Theme.of(context).backgroundColor), + ), + ) + ], + ), + ); + } +}