Adding Chat Repo
parent
95fec1e62d
commit
08fd442a36
@ -0,0 +1,60 @@
|
|||||||
|
import 'package:mc_common_app/classes/app_state.dart';
|
||||||
|
import 'package:mc_common_app/classes/consts.dart';
|
||||||
|
import 'package:mc_common_app/main.dart';
|
||||||
|
import 'package:mc_common_app/utils/utils.dart';
|
||||||
|
import 'package:signalr_netcore/http_connection_options.dart';
|
||||||
|
import 'package:signalr_netcore/hub_connection.dart';
|
||||||
|
import 'package:signalr_netcore/hub_connection_builder.dart';
|
||||||
|
import 'package:logging/logging.dart';
|
||||||
|
|
||||||
|
abstract class ChatRepo {
|
||||||
|
Future<HubConnection?> buildChatHubConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
class ChatRepoImp implements ChatRepo {
|
||||||
|
@override
|
||||||
|
Future<HubConnection?> buildChatHubConnection() async {
|
||||||
|
final userId = AppState().getUser.data!.userInfo!.userId ?? "";
|
||||||
|
HttpConnectionOptions httpOptions = HttpConnectionOptions(skipNegotiation: false, logMessageContent: true);
|
||||||
|
HubConnection hubConnection = HubConnectionBuilder()
|
||||||
|
.withUrl(
|
||||||
|
"${ApiConsts.chatHubUrl}?userID=$userId",
|
||||||
|
options: httpOptions,
|
||||||
|
)
|
||||||
|
.withAutomaticReconnect(
|
||||||
|
retryDelays: <int>[2000, 5000, 10000, 20000],
|
||||||
|
)
|
||||||
|
.configureLogging(
|
||||||
|
Logger("configureLogging"),
|
||||||
|
)
|
||||||
|
.build();
|
||||||
|
hubConnection.onclose(
|
||||||
|
({Exception? error}) {
|
||||||
|
logger.i("onClose");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
hubConnection.onreconnecting(
|
||||||
|
({Exception? error}) {
|
||||||
|
logger.i("onReconnecting");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
hubConnection.onreconnected(
|
||||||
|
({String? connectionId}) {
|
||||||
|
logger.i("onReconnected");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
if (hubConnection.state != HubConnectionState.Connected) {
|
||||||
|
await hubConnection.start();
|
||||||
|
logger.i("Started HubConnection");
|
||||||
|
|
||||||
|
try {
|
||||||
|
hubConnection.on("ReceiveMessageRequestOffer", (List<Object?>? arguments) {
|
||||||
|
Utils.showToast("I received ping : ${arguments.toString()}");
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
logger.i("Error in OnSendQuestionToParticipant");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hubConnection;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
import 'package:flutter/cupertino.dart';
|
||||||
|
import 'package:mc_common_app/repositories/chat_repo.dart';
|
||||||
|
import 'package:mc_common_app/utils/utils.dart';
|
||||||
|
import 'package:signalr_netcore/hub_connection.dart';
|
||||||
|
|
||||||
|
class ChatVM extends ChangeNotifier {
|
||||||
|
final ChatRepo chatRepo;
|
||||||
|
|
||||||
|
ChatVM({required this.chatRepo});
|
||||||
|
|
||||||
|
HubConnection? hubConnection;
|
||||||
|
|
||||||
|
Future<void> buildHubConnection() async {
|
||||||
|
hubConnection = await chatRepo.buildChatHubConnection();
|
||||||
|
notifyListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> onSendMessageForRequestOffer() async {
|
||||||
|
if (hubConnection == null || hubConnection!.state != HubConnectionState.Connected) {
|
||||||
|
await buildHubConnection();
|
||||||
|
}
|
||||||
|
if (hubConnection != null) {
|
||||||
|
hubConnection!.invoke(
|
||||||
|
"SendMessageRequestOffer",
|
||||||
|
args: <Object>[
|
||||||
|
// <String, dynamic>{
|
||||||
|
// "employeeNumber": AppState().memberInformationList!.eMPLOYEENUMBER ?? "",
|
||||||
|
// "employeeName": AppState().memberInformationList!.eMPLOYEENAME ?? "",
|
||||||
|
// "marathonId": AppState().getMarathonProjectId,
|
||||||
|
// "prizeId": "8577B2E8-5DD7-43F0-10DD-08DACB0AC064",
|
||||||
|
// }
|
||||||
|
],
|
||||||
|
).catchError((e) {
|
||||||
|
Utils.showToast(e.toString());
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue