mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
Extracted pagination data into a generic DTO.
This commit is contained in:
@ -0,0 +1,45 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class PaginatedDataModel<T> extends Equatable {
|
||||
const PaginatedDataModel({
|
||||
required this.data,
|
||||
required this.page,
|
||||
required this.size,
|
||||
required this.hasNext,
|
||||
required this.totalItems,
|
||||
required this.totalPages,
|
||||
});
|
||||
|
||||
final List<T> data;
|
||||
final int page;
|
||||
final int size;
|
||||
final bool hasNext;
|
||||
final int totalItems;
|
||||
final int totalPages;
|
||||
|
||||
factory PaginatedDataModel.fromJson(
|
||||
Map<String, dynamic> json,
|
||||
T Function(Map<String, dynamic>) fromJsonT,
|
||||
) {
|
||||
return PaginatedDataModel<T>(
|
||||
data: (json['data'] as List<dynamic>? ?? [])
|
||||
.map((e) => fromJsonT(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
page: json['page'] as int? ?? 1,
|
||||
size: json['size'] as int? ?? 25,
|
||||
hasNext: json['hasNext'] as bool? ?? false,
|
||||
totalItems: json['totalItem'] as int? ?? 0,
|
||||
totalPages: json['totalPage'] as int? ?? 0,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
data,
|
||||
page,
|
||||
size,
|
||||
hasNext,
|
||||
totalItems,
|
||||
totalPages,
|
||||
];
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/communities_pagination_model.dart';
|
||||
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';
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:syncrow_web/pages/common/bloc/project_manager.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/communities_pagination_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
||||
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';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
@ -30,7 +30,7 @@ class RemoteCommunitiesService implements CommunitiesService {
|
||||
},
|
||||
expectedResponseModel: (json) {
|
||||
final data = json as Map<String, dynamic>;
|
||||
return CommunitiesPaginationModel.fromJson(data);
|
||||
return CommunitiesPaginationModel.fromJson(data, CommunityModel.fromJson);
|
||||
},
|
||||
);
|
||||
|
||||
|
@ -1,69 +0,0 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
||||
|
||||
class CommunitiesPaginationModel extends Equatable {
|
||||
const CommunitiesPaginationModel({
|
||||
required this.communities,
|
||||
required this.page,
|
||||
required this.size,
|
||||
required this.hasNext,
|
||||
required this.totalItems,
|
||||
required this.totalPages,
|
||||
});
|
||||
|
||||
final List<CommunityModel> communities;
|
||||
final int page;
|
||||
final int size;
|
||||
final bool hasNext;
|
||||
final int totalItems;
|
||||
final int totalPages;
|
||||
|
||||
const CommunitiesPaginationModel.empty()
|
||||
: communities = const [],
|
||||
page = 1,
|
||||
size = 25,
|
||||
hasNext = false,
|
||||
totalItems = 0,
|
||||
totalPages = 0;
|
||||
|
||||
factory CommunitiesPaginationModel.fromJson(Map<String, dynamic> json) {
|
||||
return CommunitiesPaginationModel(
|
||||
communities: (json['data'] as List<dynamic>? ?? [])
|
||||
.map((e) => CommunityModel.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
page: json['page'] as int? ?? 1,
|
||||
size: json['size'] as int? ?? 25,
|
||||
hasNext: json['hasNext'] as bool? ?? false,
|
||||
totalItems: json['totalItem'] as int? ?? 0,
|
||||
totalPages: json['totalPage'] as int? ?? 0,
|
||||
);
|
||||
}
|
||||
|
||||
CommunitiesPaginationModel copyWith({
|
||||
List<CommunityModel>? communities,
|
||||
int? page,
|
||||
int? size,
|
||||
bool? hasNext,
|
||||
int? totalItems,
|
||||
int? totalPages,
|
||||
}) {
|
||||
return CommunitiesPaginationModel(
|
||||
communities: communities ?? this.communities,
|
||||
page: page ?? this.page,
|
||||
size: size ?? this.size,
|
||||
hasNext: hasNext ?? this.hasNext,
|
||||
totalItems: totalItems ?? this.totalItems,
|
||||
totalPages: totalPages ?? this.totalPages,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
communities,
|
||||
page,
|
||||
size,
|
||||
hasNext,
|
||||
totalItems,
|
||||
totalPages,
|
||||
];
|
||||
}
|
@ -1,6 +1,9 @@
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/communities_pagination_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/main_module/shared/models/paginated_data_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/params/load_communities_param.dart';
|
||||
|
||||
typedef CommunitiesPaginationModel = PaginatedDataModel<CommunityModel>;
|
||||
|
||||
abstract class CommunitiesService {
|
||||
Future<CommunitiesPaginationModel> getCommunity(LoadCommunitiesParam param);
|
||||
}
|
||||
|
@ -36,7 +36,7 @@ class CommunitiesBloc extends Bloc<CommunitiesEvent, CommunitiesState> {
|
||||
emit(
|
||||
CommunitiesState(
|
||||
status: CommunitiesStatus.success,
|
||||
communities: paginationResponse.communities,
|
||||
communities: paginationResponse.data,
|
||||
hasNext: paginationResponse.hasNext,
|
||||
currentPage: paginationResponse.page,
|
||||
searchQuery: event.param.search,
|
||||
@ -67,7 +67,7 @@ class CommunitiesBloc extends Bloc<CommunitiesEvent, CommunitiesState> {
|
||||
final paginationResponse = await _communitiesService.getCommunity(param);
|
||||
|
||||
final updatedCommunities = List<CommunityModel>.from(state.communities)
|
||||
..addAll(paginationResponse.communities);
|
||||
..addAll(paginationResponse.data);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
|
Reference in New Issue
Block a user