forked from firka/student-legacy
fixed msg sending and added error handler
This commit is contained in:
parent
fdc6209656
commit
f46610314d
@ -156,6 +156,7 @@ class KretaClient {
|
||||
if (res == null) throw "Login error";
|
||||
|
||||
if (json) {
|
||||
print(jsonDecode(res.body));
|
||||
return jsonDecode(res.body);
|
||||
} else {
|
||||
return res.body;
|
||||
@ -168,6 +169,69 @@ class KretaClient {
|
||||
}
|
||||
}
|
||||
|
||||
Future<dynamic> sendFilesAPI(
|
||||
String url, {
|
||||
Map<String, String>? headers,
|
||||
bool autoHeader = true,
|
||||
Map<String, String>? body,
|
||||
}) async {
|
||||
Map<String, String> headerMap;
|
||||
|
||||
if (headers != null) {
|
||||
headerMap = headers;
|
||||
} else {
|
||||
headerMap = {};
|
||||
}
|
||||
|
||||
try {
|
||||
http.StreamedResponse? res;
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (autoHeader) {
|
||||
if (!headerMap.containsKey("authorization") && accessToken != null) {
|
||||
headerMap["authorization"] = "Bearer $accessToken";
|
||||
}
|
||||
if (!headerMap.containsKey("user-agent") && userAgent != null) {
|
||||
headerMap["user-agent"] = "$userAgent";
|
||||
}
|
||||
if (!headerMap.containsKey("content-type")) {
|
||||
headerMap["content-type"] = "multipart/form-data";
|
||||
}
|
||||
if (url.contains('kommunikacio/uzenetek')) {
|
||||
headerMap["X-Uzenet-Lokalizacio"] = "hu-HU";
|
||||
}
|
||||
}
|
||||
|
||||
var request = http.MultipartRequest("POST", Uri.parse(url));
|
||||
|
||||
// request.files.add(value)
|
||||
|
||||
request.fields.addAll(body ?? {});
|
||||
request.headers.addAll(headers ?? {});
|
||||
|
||||
res = await request.send();
|
||||
|
||||
if (res.statusCode == 401) {
|
||||
await refreshLogin();
|
||||
headerMap.remove("authorization");
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (res == null) throw "Login error";
|
||||
|
||||
print(res.statusCode);
|
||||
|
||||
return res.statusCode;
|
||||
} on http.ClientException catch (error) {
|
||||
print(
|
||||
"ERROR: KretaClient.postAPI ($url) ClientException: ${error.message}");
|
||||
} catch (error) {
|
||||
print("ERROR: KretaClient.postAPI ($url) ${error.runtimeType}: $error");
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> refreshLogin() async {
|
||||
if (_loginRefreshing) return;
|
||||
_loginRefreshing = true;
|
||||
|
@ -156,8 +156,9 @@ class SendRecipient {
|
||||
});
|
||||
|
||||
factory SendRecipient.fromJson(Map json, SendRecipientType type) {
|
||||
print(json);
|
||||
return SendRecipient(
|
||||
id: int.parse(json['oktatasiAzonosito'] ?? '0'),
|
||||
id: int.parse(json['kretaAzonosito'] ?? '0'),
|
||||
kretaId: json['kretaAzonosito'],
|
||||
name: json['nev'],
|
||||
type: type,
|
||||
@ -166,7 +167,7 @@ class SendRecipient {
|
||||
}
|
||||
|
||||
Object get kretaJson => {
|
||||
'azonosito': id ?? 0,
|
||||
'azonosito': kretaId ?? 0,
|
||||
'kretaAzonosito': kretaId ?? 0,
|
||||
'nev': name ?? 'Teszt Lajos',
|
||||
'tipus': {
|
||||
|
@ -1,5 +1,5 @@
|
||||
// ignore_for_file: use_build_context_synchronously
|
||||
|
||||
import 'dart:convert';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:filcnaplo/api/providers/user_provider.dart';
|
||||
@ -209,7 +209,7 @@ class MessageProvider with ChangeNotifier {
|
||||
}
|
||||
|
||||
// send message
|
||||
Future<void> sendMessage({
|
||||
Future<String?> sendMessage({
|
||||
required List<SendRecipient> recipients,
|
||||
String subject = "Nincs tárgy",
|
||||
required String messageText,
|
||||
@ -235,20 +235,34 @@ class MessageProvider with ChangeNotifier {
|
||||
// }
|
||||
recipientList.addAll(recipients.map((e) => e.kretaJson));
|
||||
|
||||
Object body = {
|
||||
Map body = {
|
||||
"cimzettLista": recipientList,
|
||||
"csatolmanyok": [],
|
||||
"azonosito": Random().nextInt(10000) + 10000,
|
||||
"azonosito": (Random().nextInt(10000) + 10000),
|
||||
"feladoNev": user.name,
|
||||
"feladoTitulus": user.role == Role.parent ? "Szülő" : "Diák",
|
||||
"kuldesDatum": DateTime.now().toIso8601String(),
|
||||
"targy": subject,
|
||||
"szoveg": messageText,
|
||||
"elozoUzenetAzonosito": 0,
|
||||
// "elozoUzenetAzonosito": 0,
|
||||
};
|
||||
|
||||
// send the message
|
||||
await Provider.of<KretaClient>(_context, listen: false)
|
||||
.postAPI(KretaAPI.sendMessage, autoHeader: true, body: body);
|
||||
Map<String, String> headers = {
|
||||
"content-type": "application/json",
|
||||
};
|
||||
|
||||
var res = await Provider.of<KretaClient>(_context, listen: false).postAPI(
|
||||
KretaAPI.sendMessage,
|
||||
autoHeader: true,
|
||||
json: true,
|
||||
body: json.encode(body),
|
||||
headers: headers,
|
||||
);
|
||||
|
||||
if (res!['hibakod'] == 'UzenetKuldesEngedelyRule') {
|
||||
return 'send_permission_error';
|
||||
}
|
||||
|
||||
return 'successfully_sent';
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
// ignore_for_file: use_build_context_synchronously
|
||||
|
||||
import 'package:dropdown_button2/dropdown_button2.dart';
|
||||
import 'package:filcnaplo/theme/colors/colors.dart';
|
||||
import 'package:filcnaplo_kreta_api/models/message.dart';
|
||||
import 'package:filcnaplo_kreta_api/providers/message_provider.dart';
|
||||
import 'package:filcnaplo_mobile_ui/common/custom_snack_bar.dart';
|
||||
// import 'package:filcnaplo_mobile_ui/common/custom_snack_bar.dart';
|
||||
import 'package:filcnaplo_mobile_ui/common/material_action_button.dart';
|
||||
import 'package:filcnaplo_mobile_ui/pages/messages/send_message/send_message.i18n.dart';
|
||||
@ -190,11 +193,22 @@ class SendMessageSheetState extends State<SendMessageSheet> {
|
||||
? _subjectController.text
|
||||
: 'Nincs tárgy';
|
||||
|
||||
messageProvider.sendMessage(
|
||||
var res = await messageProvider.sendMessage(
|
||||
recipients: selectedRecipients,
|
||||
subject: subjectText,
|
||||
messageText: _messageController.text,
|
||||
);
|
||||
|
||||
// do after send
|
||||
if (res == 'send_permission_error') {
|
||||
ScaffoldMessenger.of(context).showSnackBar(CustomSnackBar(
|
||||
content: Text('cant_send'.i18n), context: context));
|
||||
}
|
||||
|
||||
if (res == 'successfully_sent') {
|
||||
ScaffoldMessenger.of(context).showSnackBar(CustomSnackBar(
|
||||
content: Text('sent'.i18n), context: context));
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
|
@ -11,6 +11,7 @@ extension Localization on String {
|
||||
"message_subject": "Subject...",
|
||||
"message_text": "Message text...",
|
||||
"select_recipient": "Add Recipient",
|
||||
"cant_send": "You can't send a message to one of the recipients!",
|
||||
},
|
||||
"hu_hu": {
|
||||
"recipients": "Címzettek",
|
||||
@ -20,6 +21,7 @@ extension Localization on String {
|
||||
"message_subject": "Tárgy...",
|
||||
"message_text": "Üzenet szövege...",
|
||||
"select_recipient": "Címzett hozzáadása",
|
||||
"cant_send": "Az egyik címzettnek nem küldhetsz üzenetet!",
|
||||
},
|
||||
"de_de": {
|
||||
"recipients": "Empfänger",
|
||||
@ -29,6 +31,7 @@ extension Localization on String {
|
||||
"message_subject": "Betreff...",
|
||||
"message_text": "Nachrichtentext...",
|
||||
"select_recipient": "Empfänger hinzufügen",
|
||||
"cant_send": "Neki nem küldhetsz üzenetet!",
|
||||
},
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user