1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-10-22 22:43:22 +00:00

Managed to send reports

This commit is contained in:
Pierre HUBERT 2022-03-18 18:29:25 +01:00
parent 80a1c4e0c4
commit ec16984b8a
3 changed files with 122 additions and 25 deletions

View File

@ -0,0 +1,38 @@
import 'package:comunic/enums/report_target_type.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/models/report_target.dart';
import 'package:comunic/models/server_config.dart';
/// Reports Helper
///
/// @author Pierre Hubert
enum ReportResult {
Success,
ErrorAlreadyReported,
Error,
}
class ReportHelper {
/// Send a new report to the server
static Future<ReportResult> sendReport({
required ReportCause cause,
required ReportTarget target,
required String comment,
}) async {
final response = await APIRequest.withLogin("reports/create", args: {
"cause": cause.id,
"target_type": target.type.apiId,
"target_id": target.id.toString(),
"comment": comment
}).exec();
if (response.isOK) return ReportResult.Success;
print("Failed to send report: ${response.content}");
if (response.code == 409) return ReportResult.ErrorAlreadyReported;
return ReportResult.Error;
}
}

View File

@ -2,7 +2,7 @@ import 'package:comunic/enums/report_target_type.dart';
class ReportTarget {
final ReportTargetType type;
final int targetId;
final int id;
const ReportTarget(this.type, this.targetId);
const ReportTarget(this.type, this.id);
}

View File

@ -1,7 +1,11 @@
import 'package:comunic/helpers/report_helper.dart';
import 'package:comunic/helpers/server_config_helper.dart';
import 'package:comunic/models/report_target.dart';
import 'package:comunic/models/server_config.dart';
import 'package:comunic/ui/widgets/dialogs/cancel_dialog_button.dart';
import 'package:comunic/ui/dialogs/alert_dialog.dart';
import 'package:comunic/ui/routes/main_route/main_route.dart';
import 'package:comunic/ui/widgets/comunic_back_button_widget.dart';
import 'package:comunic/ui/widgets/safe_state.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:flutter/material.dart';
@ -12,11 +16,11 @@ Future<void> showReportDialog({
required BuildContext ctx,
required ReportTarget target,
}) async {
await showDialog(
context: ctx,
builder: (ctx) => _ReportDialog(
target: target,
));
MainController.of(ctx)!.push(
_ReportDialog(target: target),
canShowAsDialog: true,
hideNavBar: true,
);
}
class _ReportDialog extends StatefulWidget {
@ -28,28 +32,31 @@ class _ReportDialog extends StatefulWidget {
State<_ReportDialog> createState() => _ReportDialogState();
}
class _ReportDialogState extends State<_ReportDialog> {
class _ReportDialogState extends SafeState<_ReportDialog> {
var _cause = srvConfig!.reportPolicy!.causes.length - 1;
var _status = _Status.ChooseCause;
final _commentController = TextEditingController();
List<ReportCause> get _causes => srvConfig!.reportPolicy!.causes;
@override
Widget build(BuildContext context) {
return AlertDialog(
return Scaffold(
appBar: AppBar(
leading: ComunicBackButton(),
title: Text(tr("Report abuse")!),
content: Container(width: 100, height: 200, child: _buildContent()),
actions: _status == _Status.Sending
? []
: [
CancelDialogButton(),
MaterialButton(
onPressed: null,
child: Text(
(_status == _Status.ChooseCause ? tr("Next")! : tr("Send")!)
.toUpperCase()),
)
],
IconButton(
onPressed: _goNextStep,
icon: Icon(Icons.check),
),
]),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: _buildContent(),
),
);
}
@ -60,7 +67,8 @@ class _ReportDialogState extends State<_ReportDialog> {
if (_status == _Status.ChooseCause)
return Column(
children: [
Text(tr("Please choose the reason of your report (you can scroll to access all reasons):")!),
Text(tr("Please choose the reason of your report:")!),
SizedBox(height: 15),
Expanded(
child: ListView.builder(
itemBuilder: (c, i) => RadioListTile(
@ -75,6 +83,57 @@ class _ReportDialogState extends State<_ReportDialog> {
],
);
throw Exception("todo");
if (_status == _Status.GiveComment) {
return Column(
children: [
Text("You can optionally describe the reason of your report:"),
SizedBox(height: 15),
TextField(
controller: _commentController,
decoration: InputDecoration(hintText: tr("Reason of report")),
minLines: 5,
maxLines: 5,
maxLength: srvConfig!.reportPolicy!.maxCommentLength,
)
],
);
}
throw Exception("Unknown status!");
}
void _goNextStep() async {
if (_status == _Status.ChooseCause) {
setState(() => _status = _Status.GiveComment);
return;
}
// Send report
try {
setState(() => _status = _Status.Sending);
final result = await ReportHelper.sendReport(
cause: _causes[_cause],
target: widget.target,
comment: _commentController.value.text,
);
// In case of success
if (result == ReportResult.Success) {
await alert(context,
tr("Report successfully saved. Thank you for your contribution!"));
MainController.of(context)!.popPage();
} else if (result == ReportResult.ErrorAlreadyReported) {
await alert(
context, tr("You have already sent a report for this resource!"));
} else {
await alert(context, tr("Failed to send report!"));
}
} catch (e, s) {
print("$e $s");
alert(context, tr("Failed to send report!"));
} finally {
setState(() => _status = _Status.GiveComment);
}
}
}