mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-24 22:09:21 +00:00
Managed to send reports
This commit is contained in:
parent
80a1c4e0c4
commit
ec16984b8a
38
lib/helpers/report_helper.dart
Normal file
38
lib/helpers/report_helper.dart
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
@ -2,7 +2,7 @@ import 'package:comunic/enums/report_target_type.dart';
|
|||||||
|
|
||||||
class ReportTarget {
|
class ReportTarget {
|
||||||
final ReportTargetType type;
|
final ReportTargetType type;
|
||||||
final int targetId;
|
final int id;
|
||||||
|
|
||||||
const ReportTarget(this.type, this.targetId);
|
const ReportTarget(this.type, this.id);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
|
import 'package:comunic/helpers/report_helper.dart';
|
||||||
import 'package:comunic/helpers/server_config_helper.dart';
|
import 'package:comunic/helpers/server_config_helper.dart';
|
||||||
import 'package:comunic/models/report_target.dart';
|
import 'package:comunic/models/report_target.dart';
|
||||||
import 'package:comunic/models/server_config.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:comunic/utils/intl_utils.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
@ -12,11 +16,11 @@ Future<void> showReportDialog({
|
|||||||
required BuildContext ctx,
|
required BuildContext ctx,
|
||||||
required ReportTarget target,
|
required ReportTarget target,
|
||||||
}) async {
|
}) async {
|
||||||
await showDialog(
|
MainController.of(ctx)!.push(
|
||||||
context: ctx,
|
_ReportDialog(target: target),
|
||||||
builder: (ctx) => _ReportDialog(
|
canShowAsDialog: true,
|
||||||
target: target,
|
hideNavBar: true,
|
||||||
));
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ReportDialog extends StatefulWidget {
|
class _ReportDialog extends StatefulWidget {
|
||||||
@ -28,28 +32,31 @@ class _ReportDialog extends StatefulWidget {
|
|||||||
State<_ReportDialog> createState() => _ReportDialogState();
|
State<_ReportDialog> createState() => _ReportDialogState();
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ReportDialogState extends State<_ReportDialog> {
|
class _ReportDialogState extends SafeState<_ReportDialog> {
|
||||||
var _cause = srvConfig!.reportPolicy!.causes.length - 1;
|
var _cause = srvConfig!.reportPolicy!.causes.length - 1;
|
||||||
var _status = _Status.ChooseCause;
|
var _status = _Status.ChooseCause;
|
||||||
|
final _commentController = TextEditingController();
|
||||||
|
|
||||||
List<ReportCause> get _causes => srvConfig!.reportPolicy!.causes;
|
List<ReportCause> get _causes => srvConfig!.reportPolicy!.causes;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return AlertDialog(
|
return Scaffold(
|
||||||
title: Text(tr("Report abuse")!),
|
appBar: AppBar(
|
||||||
content: Container(width: 100, height: 200, child: _buildContent()),
|
leading: ComunicBackButton(),
|
||||||
actions: _status == _Status.Sending
|
title: Text(tr("Report abuse")!),
|
||||||
? []
|
actions: _status == _Status.Sending
|
||||||
: [
|
? []
|
||||||
CancelDialogButton(),
|
: [
|
||||||
MaterialButton(
|
IconButton(
|
||||||
onPressed: null,
|
onPressed: _goNextStep,
|
||||||
child: Text(
|
icon: Icon(Icons.check),
|
||||||
(_status == _Status.ChooseCause ? tr("Next")! : tr("Send")!)
|
),
|
||||||
.toUpperCase()),
|
]),
|
||||||
)
|
body: Padding(
|
||||||
],
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
child: _buildContent(),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,7 +67,8 @@ class _ReportDialogState extends State<_ReportDialog> {
|
|||||||
if (_status == _Status.ChooseCause)
|
if (_status == _Status.ChooseCause)
|
||||||
return Column(
|
return Column(
|
||||||
children: [
|
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(
|
Expanded(
|
||||||
child: ListView.builder(
|
child: ListView.builder(
|
||||||
itemBuilder: (c, i) => RadioListTile(
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user