diff --git a/lib/helpers/survey_helper.dart b/lib/helpers/survey_helper.dart index f2fbb13..c458bef 100644 --- a/lib/helpers/survey_helper.dart +++ b/lib/helpers/survey_helper.dart @@ -8,6 +8,13 @@ import 'package:meta/meta.dart'; /// @author Pierre HUBERT class SurveyHelper { + /// Get information about a single survey + static Future getSurveyInfo(int postID) async => + apiToSurvey((await APIRequest.withLogin("surveys/get_info") + .addInt("postID", postID) + .execWithThrow()) + .getObject()); + /// Cancel the response of a user to a survey Future cancelResponse(Survey survey) async { return (await APIRequest( diff --git a/lib/models/post.dart b/lib/models/post.dart index f60fe86..1ec6ae9 100644 --- a/lib/models/post.dart +++ b/lib/models/post.dart @@ -34,7 +34,7 @@ class Post implements LikeElement { bool userLike; final UserAccessLevels access; final CommentsList comments; - final Survey survey; + Survey survey; Post( {@required this.id, diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 6748a73..41af5d5 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -341,6 +341,7 @@ class _PostTileState extends State { Widget _buildPostSurvey() { return SurveyWidget( survey: widget.post.survey, + onUpdated: (s) => setState(() => widget.post.survey = s), ); } diff --git a/lib/ui/widgets/survey_widget.dart b/lib/ui/widgets/survey_widget.dart index b299927..52d85da 100644 --- a/lib/ui/widgets/survey_widget.dart +++ b/lib/ui/widgets/survey_widget.dart @@ -13,9 +13,14 @@ import 'package:pie_chart/pie_chart.dart'; class SurveyWidget extends StatefulWidget { final Survey survey; + final Function(Survey) onUpdated; - const SurveyWidget({Key key, @required this.survey}) - : assert(survey != null), + const SurveyWidget({ + Key key, + @required this.survey, + @required this.onUpdated, + }) : assert(survey != null), + assert(onUpdated != null), super(key: key); @override @@ -160,6 +165,14 @@ class _SurveyWidgetState extends SafeState { }); } + /// Reload survey information + /// + /// Throws in case of failure + Future _reloadSurvey() async { + final newSurvey = await SurveyHelper.getSurveyInfo(widget.survey.postID); + widget.onUpdated(newSurvey); + } + /// Create a new choice void _createNewChoices() async { try { @@ -176,7 +189,7 @@ class _SurveyWidgetState extends SafeState { await SurveyHelper.createNewChoice(survey.postID, newChoice); - // TODO : reload survey + await _reloadSurvey(); } catch (e, s) { print("Could not create new survey choice! $e\n$s"); showSimpleSnack(