You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.4 KiB
Dart
69 lines
2.4 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:mc_common_app/classes/app_state.dart';
|
|
import 'package:mc_common_app/extensions/string_extensions.dart';
|
|
import 'package:mc_common_app/main.dart';
|
|
import 'package:mc_common_app/repositories/chat_repo.dart';
|
|
import 'package:mc_common_app/utils/enums.dart';
|
|
import 'package:mc_common_app/utils/utils.dart';
|
|
import 'package:signalr_core/signalr_core.dart';
|
|
|
|
class ChatVM extends ChangeNotifier {
|
|
final ChatRepo chatRepo;
|
|
|
|
ChatVM({required this.chatRepo});
|
|
|
|
late HubConnection hubConnection;
|
|
|
|
Future<void> buildHubConnection() async {
|
|
// if (hubConnection.state != HubConnectionState.Connected) {
|
|
try {
|
|
hubConnection = await chatRepo.getHubConnection();
|
|
await hubConnection.start();
|
|
hubConnection.on("ReceiveMessageRequestOffer", (List<Object?>? arguments) {
|
|
print("this is the offer: ${arguments.toString()}");
|
|
Utils.showToast("I received ping : ${arguments.toString()}");
|
|
});
|
|
} catch (e) {
|
|
logger.i("Error: ${e.toString()}");
|
|
}
|
|
|
|
notifyListeners();
|
|
// }
|
|
}
|
|
|
|
Future<bool> onSendMessageForRequestOffer(
|
|
{required String receiverId, required ChatMessageTypeEnum chatMessageType, required String message, required int requestId, required String offerPrice}) async {
|
|
if (hubConnection.state != HubConnectionState.connected) {
|
|
await buildHubConnection();
|
|
}
|
|
if (hubConnection.state == HubConnectionState.connected) {
|
|
final providerId = AppState().getUser.data!.userInfo!.providerId;
|
|
print("providerId: $providerId");
|
|
hubConnection.invoke(
|
|
"SendMessageRequestOffer",
|
|
args: <Object>[
|
|
<String, dynamic>{
|
|
"ReceiverUserID": receiverId,
|
|
"MessageType": chatMessageType.getIdFromChatMessageTypeEnum(),
|
|
"Message": message,
|
|
"RequestID": requestId,
|
|
"RequestOfferID": 0,
|
|
"RequestOffer": <String, dynamic>{
|
|
"RequestID": requestId,
|
|
"Price": double.parse(offerPrice),
|
|
"ServiceProviderID": providerId,
|
|
"OfferStatus": RequestOfferStatusEnum.offer.getIdFromRequestOfferStatusEnum(),
|
|
"Comment": message,
|
|
},
|
|
}
|
|
],
|
|
).catchError((e) {
|
|
logger.i("error in invoking SendMessageRequestOffer: ${e.toString()}");
|
|
Utils.showToast(e.toString());
|
|
return false;
|
|
});
|
|
}
|
|
return true;
|
|
}
|
|
}
|