Skip to content

Commit

Permalink
update .
Browse files Browse the repository at this point in the history
  • Loading branch information
lambiengcode committed Oct 6, 2024
1 parent cc523cc commit 673f691
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 54 deletions.
2 changes: 2 additions & 0 deletions lib/constants/api_enpoints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class ApiEndpoints {
static const String meetingConversations = 'meetings/conversations';
static const String meetingMembers = 'meetings/members';
static const String acceptInvite = 'meetings/members/accept';
static const String startRecord = 'meetings/record/start';
static const String stopRecord = 'meetings/record/stop';

// Chats
static const String chats = 'chats';
Expand Down
32 changes: 23 additions & 9 deletions lib/core/api/base/base_remote_data.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'dart:async';
import 'dart:convert' as convert;
import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:injectable/injectable.dart';
Expand Down Expand Up @@ -82,21 +84,14 @@ class BaseRemoteData {
Future<Response<dynamic>> postRoute(
String gateway, {
Map<String, dynamic>? body,
String? query,
Map<String, dynamic>? queryParameters,
}) async {
try {
final Map<String, String> paramsObject = {};
if (query != null) {
query.split('&').forEach((element) {
paramsObject[element.split('=')[0].toString()] =
element.split('=')[1].toString();
});
}
final Response response = await dio.post(
gateway,
data: body == null ? {} : convert.jsonEncode(body),
options: getOptions(),
queryParameters: query == null ? null : paramsObject,
queryParameters: queryParameters,
);

return response;
Expand Down Expand Up @@ -263,6 +258,7 @@ class BaseRemoteData {
connectTimeout: 10.seconds,
receiveTimeout: 10.seconds,
sendTimeout: 10.seconds,
responseDecoder: _responseDecoder,
),
);

Expand All @@ -272,4 +268,22 @@ class BaseRemoteData {
.then((client) => dio = client),
]);
}

FutureOr<String?> _responseDecoder(
List<int> responseBytes,
RequestOptions options,
ResponseBody responseBody,
) {
final encoding = (responseBody.headers["content-encoding"] ?? ['']).first;
switch (encoding) {
case "":
return utf8.decode(responseBytes);
case "gzip":
return utf8.decode(gzip.decode(responseBytes));
default:
throw Exception(
"unsupported encoding /$encoding/ used in response body",
);
}
}
}
5 changes: 4 additions & 1 deletion lib/core/api/chat/datasources/chat_remote_datasource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ class ChatRemoteDataSourceImpl extends ChatRemoteDataSource {
final String key = map['key'];
final List<Meeting> conversationsDecrypt = [];
for (final Meeting conversation in conversations) {
if (conversation.latestMessage == null) continue;
if (conversation.latestMessage == null) {
conversationsDecrypt.add(conversation);
continue;
}

final String decrypt = await EncryptAES().decryptAES256(
cipherText: conversation.latestMessage?.data ?? "",
Expand Down
26 changes: 26 additions & 0 deletions lib/core/api/meetings/datasources/meeting_remote_datesource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ abstract class MeetingRemoteDataSource {
required Meeting meeting,
});
Future<Meeting?> getInfoMeeting(int code);
Future<int?> startRecord(int roomId);
Future<bool> stopRecord(int roomId);
}

@LazySingleton(as: MeetingRemoteDataSource)
Expand Down Expand Up @@ -115,4 +117,28 @@ class MeetingRemoteDataSourceImpl extends MeetingRemoteDataSource {

return response.statusCode == StatusCode.ok;
}

@override
Future<int?> startRecord(int roomId) async {
final Response response = await _remoteData.postRoute(
ApiEndpoints.startRecord,
queryParameters: {"code": roomId},
);

if (response.statusCode == StatusCode.created) {
return response.data['id'];
}

return null;
}

@override
Future<bool> stopRecord(int roomId) async {
final Response response = await _remoteData.postRoute(
ApiEndpoints.stopRecord,
queryParameters: {"code": roomId},
);

return response.statusCode == StatusCode.created;
}
}
12 changes: 12 additions & 0 deletions lib/core/api/meetings/repositories/meeting_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ abstract class MeetingRepository {
CreateMeetingParams params,
);
Future<Meeting?> getInfoMeeting(int code);
Future<int?> startRecord(int roomId);
Future<bool> stopRecord(int roomId);
}

@LazySingleton(as: MeetingRepository)
Expand Down Expand Up @@ -117,4 +119,14 @@ class MeetingRepositoryImpl extends MeetingRepository {

return meeting.copyWith(participants: participants);
}

@override
Future<int?> startRecord(int roomId) async {
return await _remoteDataSource.startRecord(roomId);
}

@override
Future<bool> stopRecord(int roomId) async {
return await _remoteDataSource.stopRecord(roomId);
}
}
10 changes: 9 additions & 1 deletion lib/flutter_waterbus_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class WaterbusSdk {
Future<void> initializeApp({
required String wsUrl,
required String apiUrl,
required String privateMessageKey,
String apiKey = 'waterbus@2024',
String privateMessageKey = '', // Disable message encrypted if empty
}) async {
WaterbusSdk.wsUrl = wsUrl;
WaterbusSdk.apiUrl = apiUrl;
Expand Down Expand Up @@ -107,6 +107,14 @@ class WaterbusSdk {
return await _sdk.getRoomInfo(code);
}

Future<int?> startRecord() async {
return await _sdk.startRecord();
}

Future<bool> stopRecord() async {
return await _sdk.stopRecord();
}

Future<void> leaveRoom() async {
await _sdk.leaveRoom();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/types/models/meeting_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class Meeting {
status: (int.tryParse(map['status'].toString()) ?? 0).getChatStatusEnum,
createdAt: DateTime.parse(map['createdAt']).toLocal(),
latestJoinedAt:
DateTime.parse(map['latestJoinedAt'] ?? map['createdAt']).toLocal(),
DateTime.parse(map['latestMessageCreatedAt'] ?? map['createdAt'])
.toLocal(),
latestMessage: map['latestMessage'] != null &&
map['latestMessage'] is Map<String, dynamic>
? MessageModel.fromMap(map['latestMessage'])
Expand Down
56 changes: 14 additions & 42 deletions lib/utils/http/dio_transformer.dart
Original file line number Diff line number Diff line change
@@ -1,55 +1,27 @@
library dio_flutter_transformer;

import 'dart:async';
import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';

import 'package:dio/dio.dart';
/// Dio has already implemented a [SyncTransformer], and as the default
/// [Transformer]. If you want to custom the transformation of
/// request/response data, you can provide a [Transformer] by your self, and
/// replace the [SyncTransformer] by setting the [dio.Transformer].
///
/// [FlutterTransformer] is especially for flutter, by which the json decoding
/// will be in background with [compute] function.
/// FlutterTransformer optimized for performance.
/// JSON decoding/encoding will only be offloaded to a separate isolate
/// if the data is above a certain size threshold.
/// FlutterTransformer
class FlutterTransformer extends SyncTransformer {
static const int _computeThreshold = 200 * 1024; // 200 KB threshold

FlutterTransformer()
: super(
jsonDecodeCallback: _parseJson,
jsonEncodeCallback: _parseString,
);
}

/// Determines if data should be processed in the main thread or offloaded.
bool _shouldUseCompute(String text) {
return text.length > FlutterTransformer._computeThreshold;
}

/// Parses and decodes a JSON string. Offloads to a separate isolate if data is large.
Future<Map<String, dynamic>> _parseJson(String text) {
if (_shouldUseCompute(text)) {
return compute(_parseAndDecode, text);
} else {
return Future.value(_parseAndDecode(text));
}
}

/// Encodes an object to a JSON string. Offloads to a separate isolate if data is large.
Future<String> _parseString(Object obj) {
final String jsonString = _parseAndEncode(obj);
if (jsonString.length > FlutterTransformer._computeThreshold) {
return compute(_parseAndEncode, obj);
} else {
return Future.value(jsonString);
}
FlutterTransformer() : super(jsonDecodeCallback: _parseJson);
}

/// Decodes a JSON string into a Map.
Map<String, dynamic> _parseAndDecode(String response) {
// Must be top-level function
_parseAndDecode(String response) {
return jsonDecode(response);
}

/// Encodes an object into a JSON string.
String _parseAndEncode(Object obj) {
return jsonEncode(obj);
_parseJson(String text) {
return compute(_parseAndDecode, text);
}
18 changes: 18 additions & 0 deletions lib/waterbus_sdk_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,24 @@ class SdkCore extends WaterbusSdkInterface {
return await _meetingRepository.getInfoMeeting(code);
}

@override
Future<int?> startRecord() async {
final String? meetingId = _rtcManager.roomId;

if (meetingId == null) return null;

return await _meetingRepository.startRecord(int.parse(meetingId));
}

@override
Future<bool> stopRecord() async {
final String? meetingId = _rtcManager.roomId;

if (meetingId == null) return false;

return await _meetingRepository.stopRecord(int.parse(meetingId));
}

@override
Future<void> leaveRoom() async {
try {
Expand Down
2 changes: 2 additions & 0 deletions lib/waterbus_sdk_interface.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ abstract class WaterbusSdkInterface {
required int? userId,
});
Future<Meeting?> getRoomInfo(int code);
Future<int?> startRecord();
Future<bool> stopRecord();
Future<void> leaveRoom();

// WebRTC
Expand Down

0 comments on commit 673f691

Please sign in to comment.