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 (res == null) throw "Login error";
|
||||||
|
|
||||||
if (json) {
|
if (json) {
|
||||||
|
print(jsonDecode(res.body));
|
||||||
return jsonDecode(res.body);
|
return jsonDecode(res.body);
|
||||||
} else {
|
} else {
|
||||||
return res.body;
|
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 {
|
Future<void> refreshLogin() async {
|
||||||
if (_loginRefreshing) return;
|
if (_loginRefreshing) return;
|
||||||
_loginRefreshing = true;
|
_loginRefreshing = true;
|
||||||
|
@ -156,8 +156,9 @@ class SendRecipient {
|
|||||||
});
|
});
|
||||||
|
|
||||||
factory SendRecipient.fromJson(Map json, SendRecipientType type) {
|
factory SendRecipient.fromJson(Map json, SendRecipientType type) {
|
||||||
|
print(json);
|
||||||
return SendRecipient(
|
return SendRecipient(
|
||||||
id: int.parse(json['oktatasiAzonosito'] ?? '0'),
|
id: int.parse(json['kretaAzonosito'] ?? '0'),
|
||||||
kretaId: json['kretaAzonosito'],
|
kretaId: json['kretaAzonosito'],
|
||||||
name: json['nev'],
|
name: json['nev'],
|
||||||
type: type,
|
type: type,
|
||||||
@ -166,7 +167,7 @@ class SendRecipient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Object get kretaJson => {
|
Object get kretaJson => {
|
||||||
'azonosito': id ?? 0,
|
'azonosito': kretaId ?? 0,
|
||||||
'kretaAzonosito': kretaId ?? 0,
|
'kretaAzonosito': kretaId ?? 0,
|
||||||
'nev': name ?? 'Teszt Lajos',
|
'nev': name ?? 'Teszt Lajos',
|
||||||
'tipus': {
|
'tipus': {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// ignore_for_file: use_build_context_synchronously
|
// ignore_for_file: use_build_context_synchronously
|
||||||
|
import 'dart:convert';
|
||||||
import 'dart:math';
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:filcnaplo/api/providers/user_provider.dart';
|
import 'package:filcnaplo/api/providers/user_provider.dart';
|
||||||
@ -209,7 +209,7 @@ class MessageProvider with ChangeNotifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// send message
|
// send message
|
||||||
Future<void> sendMessage({
|
Future<String?> sendMessage({
|
||||||
required List<SendRecipient> recipients,
|
required List<SendRecipient> recipients,
|
||||||
String subject = "Nincs tárgy",
|
String subject = "Nincs tárgy",
|
||||||
required String messageText,
|
required String messageText,
|
||||||
@ -235,20 +235,34 @@ class MessageProvider with ChangeNotifier {
|
|||||||
// }
|
// }
|
||||||
recipientList.addAll(recipients.map((e) => e.kretaJson));
|
recipientList.addAll(recipients.map((e) => e.kretaJson));
|
||||||
|
|
||||||
Object body = {
|
Map body = {
|
||||||
"cimzettLista": recipientList,
|
"cimzettLista": recipientList,
|
||||||
"csatolmanyok": [],
|
"csatolmanyok": [],
|
||||||
"azonosito": Random().nextInt(10000) + 10000,
|
"azonosito": (Random().nextInt(10000) + 10000),
|
||||||
"feladoNev": user.name,
|
"feladoNev": user.name,
|
||||||
"feladoTitulus": user.role == Role.parent ? "Szülő" : "Diák",
|
"feladoTitulus": user.role == Role.parent ? "Szülő" : "Diák",
|
||||||
"kuldesDatum": DateTime.now().toIso8601String(),
|
"kuldesDatum": DateTime.now().toIso8601String(),
|
||||||
"targy": subject,
|
"targy": subject,
|
||||||
"szoveg": messageText,
|
"szoveg": messageText,
|
||||||
"elozoUzenetAzonosito": 0,
|
// "elozoUzenetAzonosito": 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// send the message
|
Map<String, String> headers = {
|
||||||
await Provider.of<KretaClient>(_context, listen: false)
|
"content-type": "application/json",
|
||||||
.postAPI(KretaAPI.sendMessage, autoHeader: true, body: body);
|
};
|
||||||
|
|
||||||
|
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:dropdown_button2/dropdown_button2.dart';
|
||||||
import 'package:filcnaplo/theme/colors/colors.dart';
|
import 'package:filcnaplo/theme/colors/colors.dart';
|
||||||
import 'package:filcnaplo_kreta_api/models/message.dart';
|
import 'package:filcnaplo_kreta_api/models/message.dart';
|
||||||
import 'package:filcnaplo_kreta_api/providers/message_provider.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/custom_snack_bar.dart';
|
||||||
import 'package:filcnaplo_mobile_ui/common/material_action_button.dart';
|
import 'package:filcnaplo_mobile_ui/common/material_action_button.dart';
|
||||||
import 'package:filcnaplo_mobile_ui/pages/messages/send_message/send_message.i18n.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
|
? _subjectController.text
|
||||||
: 'Nincs tárgy';
|
: 'Nincs tárgy';
|
||||||
|
|
||||||
messageProvider.sendMessage(
|
var res = await messageProvider.sendMessage(
|
||||||
recipients: selectedRecipients,
|
recipients: selectedRecipients,
|
||||||
subject: subjectText,
|
subject: subjectText,
|
||||||
messageText: _messageController.text,
|
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_subject": "Subject...",
|
||||||
"message_text": "Message text...",
|
"message_text": "Message text...",
|
||||||
"select_recipient": "Add Recipient",
|
"select_recipient": "Add Recipient",
|
||||||
|
"cant_send": "You can't send a message to one of the recipients!",
|
||||||
},
|
},
|
||||||
"hu_hu": {
|
"hu_hu": {
|
||||||
"recipients": "Címzettek",
|
"recipients": "Címzettek",
|
||||||
@ -20,6 +21,7 @@ extension Localization on String {
|
|||||||
"message_subject": "Tárgy...",
|
"message_subject": "Tárgy...",
|
||||||
"message_text": "Üzenet szövege...",
|
"message_text": "Üzenet szövege...",
|
||||||
"select_recipient": "Címzett hozzáadása",
|
"select_recipient": "Címzett hozzáadása",
|
||||||
|
"cant_send": "Az egyik címzettnek nem küldhetsz üzenetet!",
|
||||||
},
|
},
|
||||||
"de_de": {
|
"de_de": {
|
||||||
"recipients": "Empfänger",
|
"recipients": "Empfänger",
|
||||||
@ -29,6 +31,7 @@ extension Localization on String {
|
|||||||
"message_subject": "Betreff...",
|
"message_subject": "Betreff...",
|
||||||
"message_text": "Nachrichtentext...",
|
"message_text": "Nachrichtentext...",
|
||||||
"select_recipient": "Empfänger hinzufügen",
|
"select_recipient": "Empfänger hinzufügen",
|
||||||
|
"cant_send": "Neki nem küldhetsz üzenetet!",
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user