1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-23 15:03:22 +00:00
comunicmobile/lib/ui/widgets/survey_widget.dart

216 lines
5.9 KiB
Dart
Raw Normal View History

2019-06-28 09:32:36 +00:00
import 'package:comunic/helpers/survey_helper.dart';
import 'package:comunic/models/survey.dart';
import 'package:comunic/models/survey_choice.dart';
import 'package:comunic/ui/widgets/safe_state.dart';
2019-06-28 09:32:36 +00:00
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/ui_utils.dart';
import 'package:flutter/material.dart';
import 'package:pie_chart/pie_chart.dart';
/// Survey widget
///
/// @author Pierre HUBERT
class SurveyWidget extends StatefulWidget {
final Survey survey;
2020-05-18 17:15:16 +00:00
final Function(Survey) onUpdated;
const SurveyWidget({
Key? key,
required this.survey,
required this.onUpdated,
2022-03-11 15:36:42 +00:00
}) : super(key: key);
2019-06-28 09:32:36 +00:00
@override
_SurveyWidgetState createState() => _SurveyWidgetState();
}
class _SurveyWidgetState extends SafeState<SurveyWidget> {
2019-06-28 09:32:36 +00:00
final SurveyHelper _helper = SurveyHelper();
Survey get survey => widget.survey;
Map<String, double> _buildDataMap() {
Map<String, double> data = Map();
widget.survey.choices.forEach((e) => data.putIfAbsent(
e.name + " (" + e.responses.toString() + ")", () => 1.0 * e.responses));
return data;
}
@override
Widget build(BuildContext context) {
return Column(
children: <Widget>[
Text(
widget.survey.question,
style: TextStyle(fontWeight: FontWeight.bold),
),
_buildUserResponse(),
2020-05-18 16:43:57 +00:00
// Offer the user to create a new choice on the survey, if possible
survey.allowNewChoicesCreation ? _buildNewChoiceLink() : Container(),
// Offer to block the creation of new responses, if possible
2020-05-18 16:24:04 +00:00
survey.canBlockNewChoicesCreation
? _buildBlockNewResponsesLink()
: Container(),
survey.hasResponses ? _buildChart() : _buildNoResponseNotice(),
2019-06-28 09:32:36 +00:00
],
);
}
Widget _buildUserResponse() {
if (survey.hasResponded) return _buildUserRespondedWidget();
return _buildUserResponseFormWidget();
}
Widget _buildUserRespondedWidget() {
return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Flexible(
child: Text(
tr("Your response: %response%",
args: {"response": survey.userResponse!.name})!,
2019-06-28 09:32:36 +00:00
textAlign: TextAlign.center,
),
),
MaterialButton(
child: Text(tr("Cancel")!.toUpperCase()),
2019-06-28 09:32:36 +00:00
onPressed: _cancelUserResponse,
)
],
);
}
Future<void> _cancelUserResponse() async {
if (!await showConfirmDialog(
context: context,
title: tr("Cancel response to survey"),
message:
tr("Do you really want to cancel your response to this survey ?"),
)) return;
if (!await _helper.cancelResponse(survey)) {
showSimpleSnack(
context, tr("Could not cancel your response to the survey !")!);
2019-06-28 09:32:36 +00:00
return;
}
setState(() {
survey.cancelUserResponse();
});
}
Widget _buildUserResponseFormWidget() {
2020-04-25 15:32:25 +00:00
return ConstrainedBox(
constraints: BoxConstraints(maxWidth: 300),
child: DropdownButton<SurveyChoice>(
isExpanded: true,
hint: Text(tr("Respond to survey")!),
2020-04-25 15:32:25 +00:00
items: survey.choices
.map(
(f) => DropdownMenuItem<SurveyChoice>(
value: f,
child: Text(f.name),
),
)
.toList(),
onChanged: _respondToSurvey,
),
2019-06-28 09:32:36 +00:00
);
}
2020-05-18 16:43:57 +00:00
Widget _buildNewChoiceLink() => Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: _createNewChoices,
child: Text(tr("Create a new choice")!),
2020-05-18 16:43:57 +00:00
),
);
Widget _buildBlockNewResponsesLink() => Padding(
padding: const EdgeInsets.all(8.0),
child: InkWell(
onTap: _blockNewChoices,
child: Text(tr("Block the creation of new responses")!),
2020-05-18 16:43:57 +00:00
),
);
Widget _buildChart() {
return PieChart(
dataMap: _buildDataMap(),
);
}
Widget _buildNoResponseNotice() {
return Padding(
padding: const EdgeInsets.all(16.0),
child: Text(tr("No response yet to this survey.")!),
);
}
2019-06-28 09:32:36 +00:00
/// Respond to survey
Future<void> _respondToSurvey(SurveyChoice? choice) async {
2019-06-28 09:32:36 +00:00
// Send the response to the server
if (!await _helper.respondToSurvey(survey: survey, choice: choice!))
2019-06-28 09:32:36 +00:00
return showSimpleSnack(
context, tr("Could not send your response to the survey!")!);
2019-06-28 09:32:36 +00:00
setState(() {
survey.setUserResponse(choice);
});
}
2020-05-18 17:15:16 +00:00
/// Reload survey information
///
/// Throws in case of failure
Future<void> _reloadSurvey() async {
final newSurvey = await SurveyHelper.getSurveyInfo(widget.survey.postID);
widget.onUpdated(newSurvey);
}
2020-05-18 16:43:57 +00:00
/// Create a new choice
void _createNewChoices() async {
try {
final newChoice = await askUserString(
context: context,
title: tr("New choice")!,
message: tr("Please specify the new choice for the survey")!,
2020-05-18 16:43:57 +00:00
defaultValue: "",
hint: tr("New choice...")!,
2020-05-18 16:43:57 +00:00
maxLength: 50,
);
if (newChoice == null || newChoice.isEmpty) return;
await SurveyHelper.createNewChoice(survey.postID, newChoice);
2020-05-18 17:15:16 +00:00
await _reloadSurvey();
2020-05-18 16:43:57 +00:00
} catch (e, s) {
print("Could not create new survey choice! $e\n$s");
showSimpleSnack(
context, tr("Could not create a new choice for this survey!")!);
2020-05-18 16:43:57 +00:00
}
}
/// Block the creation of new choices on this survey
void _blockNewChoices() async {
try {
if (!await showConfirmDialog(
context: context,
2022-03-11 15:36:42 +00:00
message: tr("Do you really want to block new choices creation?")))
return;
await SurveyHelper.blockNewChoicesCreation(survey.postID);
setState(() => survey.allowNewChoicesCreation = false);
} catch (e, s) {
print("Could not block the creation of new choices! $e\n$s");
showSimpleSnack(
context, tr("Could not block the creation of new choices!")!);
}
}
2019-06-28 09:32:36 +00:00
}