296 lines
9.0 KiB
Dart
296 lines
9.0 KiB
Dart
import 'dart:convert';
|
|
import 'dart:developer';
|
|
import 'dart:io';
|
|
import 'package:get/get.dart';
|
|
import 'package:onufitness/constants/api_endpoints.dart';
|
|
import 'package:onufitness/constants/api_enum_constant.dart';
|
|
import 'package:onufitness/models/general/get_agora_user_trm_token_response_model.dart';
|
|
import 'package:onufitness/services/api_services/base_api_services.dart';
|
|
import 'package:onufitness/services/local_storage_services/shared_services.dart';
|
|
|
|
class AgoraTokenController extends GetxController {
|
|
AgoraUserAndRtmTokenResponseModel agoraToken =
|
|
AgoraUserAndRtmTokenResponseModel();
|
|
RxBool isTokenGenerating = false.obs;
|
|
|
|
Future<bool> getAgoraUserAndRrmToken() async {
|
|
isTokenGenerating(true);
|
|
var ret = false;
|
|
try {
|
|
var response = await ApiBase.getRequest(
|
|
extendedURL: ApiUrl.fetchAgoraUserAndRtmToken,
|
|
);
|
|
agoraToken = agoraUserAndRtmTokenResponseModelFromJson(response.body);
|
|
log(
|
|
"Agora Token API Statuscode-----------------> ${response.statusCode}",
|
|
);
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
await SharedServices.setAgoraToken(agoraToken);
|
|
ret = true;
|
|
} else {
|
|
ret = false;
|
|
}
|
|
} catch (e) {
|
|
ret = false;
|
|
log("Exception: $e");
|
|
}
|
|
isTokenGenerating(false);
|
|
return ret;
|
|
}
|
|
|
|
//.......... Get RTC token.......................................................................
|
|
RxString rTCtoken = "".obs;
|
|
Future<bool> getRTCtoken({
|
|
required String channelName,
|
|
required String role,
|
|
String? tempId,
|
|
String? receivrId,
|
|
String? callType,
|
|
}) async {
|
|
isTokenGenerating(true);
|
|
var ret = false;
|
|
try {
|
|
var apiBody = {"channelName": channelName, "role": role};
|
|
if (tempId != null) {
|
|
apiBody['tempId'] = tempId;
|
|
}
|
|
if (callType != null) {
|
|
apiBody['callType'] = callType.toString();
|
|
}
|
|
if (receivrId != null) {
|
|
apiBody['receiverId'] = receivrId;
|
|
}
|
|
log("RTC API response body : ${apiBody.toString()}");
|
|
var response = await ApiBase.postRequest(
|
|
extendedURL: ApiUrl.getAgoraRtcToken,
|
|
body: apiBody,
|
|
);
|
|
log("RTC From API----> ${response.body}");
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
rTCtoken.value = jsonDecode(response.body)['data']['agoraRtcToken'];
|
|
|
|
ret = true;
|
|
} else {
|
|
ret = false;
|
|
}
|
|
} catch (e) {
|
|
ret = false;
|
|
log("Exception: $e");
|
|
}
|
|
isTokenGenerating(false);
|
|
return ret;
|
|
}
|
|
|
|
// Incoming Call Notification...........................................................................
|
|
|
|
RxString incomingCallNotificationID = "".obs;
|
|
|
|
Future<bool> incomingCallNotification({
|
|
required String channelName,
|
|
required String receiverId,
|
|
required String callType,
|
|
}) async {
|
|
var ret = false;
|
|
try {
|
|
var apiBody = {
|
|
"channelName": channelName,
|
|
"receiverId": receiverId,
|
|
"callType": callType.toString(),
|
|
};
|
|
|
|
log("incoming Call Notification request body : ${apiBody.toString()}");
|
|
var response = await ApiBase.postRequest(
|
|
extendedURL: "/api/Notifications/sent-call-notification",
|
|
body: apiBody,
|
|
);
|
|
log("incoming Call Notification response body ----> ${response.body}");
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
incomingCallNotificationID.value =
|
|
jsonDecode(response.body)['data']['notificationId'];
|
|
|
|
log("Call NotificationID : ${incomingCallNotificationID.value}");
|
|
ret = true;
|
|
} else {
|
|
ret = false;
|
|
}
|
|
} catch (e) {
|
|
ret = false;
|
|
log("Exception: $e");
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
// Cancel Notification after Call ends.........................................................
|
|
|
|
Future<bool> cancelNotification({
|
|
required String channelName,
|
|
required String receiverId,
|
|
required String? callType,
|
|
required String notificationId,
|
|
}) async {
|
|
var ret = false;
|
|
try {
|
|
var apiBody = {
|
|
"channelName": channelName,
|
|
"receiverId": receiverId,
|
|
"callType": callType.toString(),
|
|
"notificationId": notificationId,
|
|
};
|
|
|
|
log("cancelNotification request body : ${apiBody.toString()}");
|
|
var response = await ApiBase.postRequest(
|
|
extendedURL: "/api/Notifications/sent-call-cancel-notification",
|
|
body: apiBody,
|
|
);
|
|
log("cancelNotification From API----> ${response.body}");
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
final res = jsonDecode(response.body);
|
|
|
|
if (res['isSuccess'] == true) {
|
|
// cancel API only returns bool, not notificationId
|
|
if (res['data'] is Map && res['data']['notificationId'] != null) {
|
|
incomingCallNotificationID.value = res['data']['notificationId'];
|
|
} else {
|
|
incomingCallNotificationID.value = "";
|
|
}
|
|
|
|
ret = true;
|
|
} else {
|
|
ret = false;
|
|
}
|
|
} else {
|
|
ret = false;
|
|
}
|
|
} catch (e) {
|
|
ret = false;
|
|
log("Exception: $e");
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
//.......... Create Live Stream.................................................................
|
|
|
|
// Variables for create live stream
|
|
RxBool isCreatingStream = false.obs;
|
|
RxString streamChatRoomId = "".obs;
|
|
RxInt streamId = 0.obs;
|
|
|
|
Future<bool> createLiveStream({
|
|
required String streamTitle,
|
|
String? streamDescription,
|
|
int tribeIdForTribeEchoboard = 0,
|
|
}) async {
|
|
isCreatingStream(true);
|
|
var ret = false;
|
|
try {
|
|
var apiBody =
|
|
tribeIdForTribeEchoboard == 0
|
|
? {
|
|
"streamTitle": streamTitle,
|
|
"streamDescription": streamDescription,
|
|
"DeviceTypeID":
|
|
Platform.isAndroid
|
|
? ApiEnum.androidDeviceTypeID
|
|
: ApiEnum.iosDeviceTypeID,
|
|
}
|
|
: {
|
|
"streamTitle": streamTitle,
|
|
"streamDescription": streamDescription,
|
|
"DeviceTypeID":
|
|
Platform.isAndroid
|
|
? ApiEnum.androidDeviceTypeID
|
|
: ApiEnum.iosDeviceTypeID,
|
|
"tribeID": tribeIdForTribeEchoboard,
|
|
};
|
|
final apiUrl =
|
|
tribeIdForTribeEchoboard == 0
|
|
? "/api/Communications/create-live-stream"
|
|
: "/api/Communications/create-tribe-live-stream";
|
|
var response = await ApiBase.postRequest(
|
|
extendedURL: apiUrl,
|
|
body: apiBody,
|
|
);
|
|
|
|
log("Create Live Stream API Response----> ${response.body}");
|
|
log("Create Live Stream API Statuscode----> ${response.statusCode}");
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
var responseData = jsonDecode(response.body);
|
|
|
|
if (responseData['isSuccess'] == true) {
|
|
// Store the response data in observable variables
|
|
streamChatRoomId.value =
|
|
responseData['data']['streamChatRoomId'].toString();
|
|
streamId.value = responseData['data']['streamId'];
|
|
|
|
log("Stream Chat Room ID: ${streamChatRoomId.value}");
|
|
log("Stream ID: ${streamId.value}");
|
|
|
|
ret = true;
|
|
} else {
|
|
log("API returned isSuccess as false");
|
|
ret = false;
|
|
}
|
|
} else {
|
|
log("API call failed with status code: ${response.statusCode}");
|
|
ret = false;
|
|
}
|
|
} catch (e) {
|
|
ret = false;
|
|
log("Exception in createLiveStream: $e");
|
|
}
|
|
isCreatingStream(false);
|
|
return ret;
|
|
}
|
|
//.....start Scheduled Live Stream............................................................
|
|
|
|
Future<bool> startScheduledLiveStream({
|
|
required String scheduleStreamId,
|
|
}) async {
|
|
isCreatingStream(true);
|
|
var ret = false;
|
|
try {
|
|
var response = await ApiBase.postRequest(
|
|
extendedURL:
|
|
"/api/Communications/start-scheduled-live-stream?streamId=$scheduleStreamId",
|
|
body: {},
|
|
);
|
|
|
|
log("Schedule Live Stream API Response----> ${response.body}");
|
|
log("Schedule Live Stream API Statuscode----> ${response.statusCode}");
|
|
|
|
if (response.statusCode == 200 || response.statusCode == 201) {
|
|
var responseData = jsonDecode(response.body);
|
|
|
|
if (responseData['isSuccess'] == true) {
|
|
streamChatRoomId.value =
|
|
responseData['data']['streamChatRoomId'].toString();
|
|
streamId.value = responseData['data']['streamId'];
|
|
|
|
log(
|
|
"Stream Chat Room ID startScheduledLiveStream: ${streamChatRoomId.value}",
|
|
);
|
|
log("Stream ID: ${streamId.toString()}");
|
|
|
|
ret = true;
|
|
} else {
|
|
log("API returned isSuccess as false");
|
|
ret = false;
|
|
}
|
|
} else {
|
|
log("API call failed with status code: ${response.statusCode}");
|
|
ret = false;
|
|
}
|
|
} catch (e) {
|
|
ret = false;
|
|
log("Exception in createLiveStream: $e");
|
|
}
|
|
isCreatingStream(false);
|
|
return ret;
|
|
}
|
|
}
|