mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-08-26 04:19:41 +00:00
matched community and space models with API.
This commit is contained in:
@ -4,57 +4,44 @@ import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/params/load_communities_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/services/communities_service.dart';
|
||||
|
||||
class DebouncedCommunitiesService implements CommunitiesService {
|
||||
DebouncedCommunitiesService({
|
||||
required CommunitiesService communitiesService,
|
||||
this.debounceDuration = const Duration(milliseconds: 400),
|
||||
}) : _communitiesService = communitiesService;
|
||||
final class DebouncedCommunitiesService implements CommunitiesService {
|
||||
DebouncedCommunitiesService(
|
||||
this._decoratee, {
|
||||
this.debounceDuration = const Duration(milliseconds: 500),
|
||||
});
|
||||
|
||||
final CommunitiesService _communitiesService;
|
||||
final CommunitiesService _decoratee;
|
||||
final Duration debounceDuration;
|
||||
|
||||
Timer? _debounceTimer;
|
||||
String _lastSearchQuery = '';
|
||||
Completer<CommunitiesPaginationModel>? _completer;
|
||||
|
||||
@override
|
||||
Future<CommunitiesPaginationModel> getCommunity(
|
||||
LoadCommunitiesParam param,
|
||||
) async {
|
||||
if (param.search.isNotEmpty) {
|
||||
return _getDebouncedCommunity(param);
|
||||
}
|
||||
|
||||
return _communitiesService.getCommunity(param);
|
||||
}
|
||||
|
||||
Future<CommunitiesPaginationModel> _getDebouncedCommunity(
|
||||
LoadCommunitiesParam param,
|
||||
) async {
|
||||
final completer = Completer<CommunitiesPaginationModel>();
|
||||
|
||||
_debounceTimer?.cancel();
|
||||
|
||||
_lastSearchQuery = param.search;
|
||||
if (_completer != null && !_completer!.isCompleted) {
|
||||
_completer!.completeError(Exception('Request cancelled by newer request'));
|
||||
}
|
||||
|
||||
_completer = Completer<CommunitiesPaginationModel>();
|
||||
final currentCompleter = _completer!;
|
||||
|
||||
_debounceTimer = Timer(debounceDuration, () async {
|
||||
try {
|
||||
if (_lastSearchQuery == param.search) {
|
||||
final result = await _communitiesService.getCommunity(param);
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(result);
|
||||
}
|
||||
} else {
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(const CommunitiesPaginationModel.empty());
|
||||
}
|
||||
final result = await _decoratee.getCommunity(param);
|
||||
if (!currentCompleter.isCompleted) {
|
||||
currentCompleter.complete(result);
|
||||
}
|
||||
} catch (error) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.completeError(error);
|
||||
if (!currentCompleter.isCompleted) {
|
||||
currentCompleter.completeError(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return completer.future;
|
||||
return currentCompleter.future;
|
||||
}
|
||||
}
|
||||
|
@ -15,10 +15,9 @@ class RemoteCommunitiesService implements CommunitiesService {
|
||||
static const _defaultErrorMessage = 'Failed to load communities';
|
||||
|
||||
@override
|
||||
Future<CommunitiesPaginationModel> getCommunity(LoadCommunitiesParam param) async {
|
||||
final projectUuid = await ProjectManager.getProjectUUID();
|
||||
if (projectUuid == null) throw APIException('Project UUID is not set');
|
||||
|
||||
Future<CommunitiesPaginationModel> getCommunity(
|
||||
LoadCommunitiesParam param,
|
||||
) async {
|
||||
try {
|
||||
final response = await _httpService.get(
|
||||
path: await _makeUrl(),
|
||||
@ -26,10 +25,12 @@ class RemoteCommunitiesService implements CommunitiesService {
|
||||
'page': param.page,
|
||||
'size': param.size,
|
||||
'includeSpaces': param.includeSpaces,
|
||||
if (param.search.isNotEmpty) 'search': param.search,
|
||||
if (param.search.isNotEmpty && param.search != 'null')
|
||||
'search': param.search,
|
||||
},
|
||||
expectedResponseModel: (json) {
|
||||
return CommunitiesPaginationModel.fromJson(json as Map<String, dynamic>);
|
||||
final data = json as Map<String, dynamic>;
|
||||
return CommunitiesPaginationModel.fromJson(data);
|
||||
},
|
||||
);
|
||||
|
||||
@ -48,6 +49,9 @@ class RemoteCommunitiesService implements CommunitiesService {
|
||||
Future<String> _makeUrl() async {
|
||||
final projectUuid = await ProjectManager.getProjectUUID();
|
||||
if (projectUuid == null) throw APIException('Project UUID is required');
|
||||
return ApiEndpoints.getCommunityList.replaceAll('{projectId}', projectUuid);
|
||||
return ApiEndpoints.getCommunityListv2.replaceAll(
|
||||
'{projectId}',
|
||||
projectUuid,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user