Files
Client/lib/api/marianumconnect/queries/submit_feedback/submit_feedback.dart
T

64 lines
2.0 KiB
Dart

import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:dio/dio.dart';
import 'package:package_info_plus/package_info_plus.dart';
import '../../errors/marianumconnect_error.dart';
import '../../marianumconnect_api.dart';
import '../../marianumconnect_endpoint.dart';
/// Submits user feedback to MarianumConnect (`POST me/feedback`, bearer-auth).
/// The optional screenshot is sent base64-encoded. Replaces the legacy mhsl.eu
/// `server/feedback` endpoint — the user no longer needs to be sent explicitly,
/// the bearer token identifies them.
class SubmitFeedback {
final Dio _dio;
SubmitFeedback({Dio? dio}) : _dio = dio ?? MarianumConnectApi.dio();
Future<void> run({
required String message,
Uint8List? screenshot,
String screenshotContentType = 'image/png',
}) async {
try {
final package = await PackageInfo.fromPlatform();
final screenshotBase64 = screenshot != null ? base64Encode(screenshot) : null;
await _dio.post<void>(
MarianumConnectEndpoint.resolve('me/feedback'),
data: {
'message': message,
'screenshot': ?screenshotBase64,
'screenshotContentType': screenshot != null ? screenshotContentType : null,
'platform': _platform(),
'appVersion': package.version,
'appBuild': int.tryParse(package.buildNumber),
'deviceModel': await _deviceModel(),
},
);
} on DioException catch (e) {
throw mapMarianumConnectError(e);
}
}
static String? _platform() {
if (Platform.isAndroid) return 'android';
if (Platform.isIOS) return 'ios';
return null;
}
static Future<String?> _deviceModel() async {
try {
final info = DeviceInfoPlugin();
if (Platform.isAndroid) return (await info.androidInfo).model;
if (Platform.isIOS) return (await info.iosInfo).utsname.machine;
} catch (_) {
// Device-Plugin nicht verfügbar (z.B. Tests).
}
return null;
}
}