From ba07247ec40116cd93f93f676482bf123bc4ccd5 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 25 Apr 2020 08:23:52 +0200 Subject: [PATCH] Can create countdown timers --- lib/helpers/posts_helper.dart | 5 +++ lib/models/new_post.dart | 5 ++- lib/ui/widgets/post_create_form_widget.dart | 49 +++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/helpers/posts_helper.dart b/lib/helpers/posts_helper.dart index f78eee2..237735e 100644 --- a/lib/helpers/posts_helper.dart +++ b/lib/helpers/posts_helper.dart @@ -153,6 +153,11 @@ class PostsHelper { type: MediaType.parse("application/pdf"))); break; + case PostKind.COUNTDOWN: + request.addInt( + "time-end", (post.timeEnd.millisecondsSinceEpoch / 1000).floor()); + break; + default: throw Exception("Unsupported post type :" + post.kind.toString()); break; diff --git a/lib/models/new_post.dart b/lib/models/new_post.dart index b528b4b..d4f27fa 100644 --- a/lib/models/new_post.dart +++ b/lib/models/new_post.dart @@ -17,6 +17,7 @@ class NewPost { final File image; final List pdf; final PostKind kind; + final DateTime timeEnd; const NewPost({ @required this.target, @@ -26,11 +27,13 @@ class NewPost { @required this.kind, @required this.image, @required this.pdf, + @required this.timeEnd, }) : assert(target != null), assert(targetID != null), assert(visibility != null), assert(content != null), assert(kind != PostKind.TEXT || content.length > 3), assert(kind != PostKind.IMAGE || image != null), - assert(kind != PostKind.PDF || pdf != null); + assert(kind != PostKind.PDF || pdf != null), + assert(kind != PostKind.COUNTDOWN || timeEnd != null); } diff --git a/lib/ui/widgets/post_create_form_widget.dart b/lib/ui/widgets/post_create_form_widget.dart index 8e260d2..702d2f9 100644 --- a/lib/ui/widgets/post_create_form_widget.dart +++ b/lib/ui/widgets/post_create_form_widget.dart @@ -49,11 +49,14 @@ class _PostCreateFormWidgetState extends State { PostVisibilityLevel _postVisibilityLevel; File _postImage; List _postPDF; + DateTime _timeEnd; bool get hasImage => _postImage != null; bool get hasPDF => _postPDF != null; + bool get hasTimeEnd => _timeEnd != null; + bool get canSubmitForm => !_isCreating && _postTextController.text.length > 5 || postKind != PostKind.TEXT; @@ -63,6 +66,8 @@ class _PostCreateFormWidgetState extends State { return PostKind.IMAGE; else if (hasPDF) return PostKind.PDF; + else if (hasTimeEnd) + return PostKind.COUNTDOWN; else return PostKind.TEXT; } @@ -116,6 +121,13 @@ class _PostCreateFormWidgetState extends State { onTap: _pickPDFForPost, ), + // Add countdown timer + _PostOptionWidget( + icon: Icons.timer, + selected: postKind == PostKind.COUNTDOWN, + onTap: _pickCountdownTime, + ), + Expanded( child: Container(), ), @@ -174,6 +186,7 @@ class _PostCreateFormWidgetState extends State { setState(() { _postImage = null; _postPDF = null; + _timeEnd = null; }); } @@ -211,6 +224,41 @@ class _PostCreateFormWidgetState extends State { } } + /// Pick countdown time + Future _pickCountdownTime() async { + final initialDate = + _timeEnd == null ? DateTime.now().add(Duration(minutes: 1)) : _timeEnd; + + // Pick date + final newDate = await showDatePicker( + context: context, + initialDate: initialDate, + firstDate: DateTime.now(), + lastDate: DateTime.now().add(Duration(days: 5000))); + + if (newDate == null) return; + + // Pick time + final newTime = await showTimePicker( + context: context, + initialTime: TimeOfDay.fromDateTime(initialDate), + ); + + if (newTime == null) return; + + // Apply new selection + _resetPostSelection(); + setState(() { + _timeEnd = newDate.add(Duration( + hours: newTime.hour - newDate.hour, + minutes: newTime.minute - newDate.minute)); + + print(newDate.toString()); + print(newTime); + print(_timeEnd); + }); + } + /// Submit new post Future _submitForm() async { if (!canSubmitForm) @@ -227,6 +275,7 @@ class _PostCreateFormWidgetState extends State { kind: postKind, image: _postImage, pdf: _postPDF, + timeEnd: _timeEnd, )); setState(() => _isCreating = false);