mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-09 22:57:21 +00:00
Created communities blocs, services, models, and params.
This commit is contained in:
@ -0,0 +1,20 @@
|
||||
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/http_service.dart';
|
||||
|
||||
class RemoteCommunitiesService implements CommunitiesService {
|
||||
const RemoteCommunitiesService(this._httpService);
|
||||
|
||||
final HTTPService _httpService;
|
||||
|
||||
@override
|
||||
Future<List<CommunityModel>> getCommunity(LoadCommunitiesParam param) async {
|
||||
return _httpService.get(
|
||||
path: '/api/communities/',
|
||||
expectedResponseModel: (json) => (json as List<dynamic>)
|
||||
.map((e) => CommunityModel.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/space_model.dart';
|
||||
|
||||
class CommunityModel extends Equatable {
|
||||
final String uuid;
|
||||
final String name;
|
||||
final List<SpaceModel> spaces;
|
||||
|
||||
const CommunityModel({
|
||||
required this.uuid,
|
||||
required this.name,
|
||||
required this.spaces,
|
||||
});
|
||||
|
||||
factory CommunityModel.fromJson(Map<String, dynamic> json) {
|
||||
return CommunityModel(
|
||||
uuid: json['uuid'] as String,
|
||||
name: json['name'] as String,
|
||||
spaces: (json['spaces'] as List<dynamic>)
|
||||
.map((e) => SpaceModel.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [uuid, name, spaces];
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class SpaceModel extends Equatable {
|
||||
final String uuid;
|
||||
final String spaceName;
|
||||
final String icon;
|
||||
final List<SpaceModel> children;
|
||||
|
||||
const SpaceModel({
|
||||
required this.uuid,
|
||||
required this.spaceName,
|
||||
required this.icon,
|
||||
required this.children,
|
||||
});
|
||||
|
||||
factory SpaceModel.fromJson(Map<String, dynamic> json) {
|
||||
return SpaceModel(
|
||||
uuid: json['uuid'] as String,
|
||||
spaceName: json['spaceName'] as String,
|
||||
icon: json['icon'] as String,
|
||||
children: (json['children'] as List<dynamic>?)
|
||||
?.map((e) => SpaceModel.fromJson(e as Map<String, dynamic>))
|
||||
.toList() ??
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [uuid, spaceName, icon, children];
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
class LoadCommunitiesParam {
|
||||
const LoadCommunitiesParam();
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
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';
|
||||
|
||||
abstract class CommunitiesService {
|
||||
Future<List<CommunityModel>> getCommunity(LoadCommunitiesParam param);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.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';
|
||||
|
||||
part 'communities_event.dart';
|
||||
part 'communities_state.dart';
|
||||
|
||||
class CommunitiesBloc extends Bloc<CommunitiesEvent, CommunitiesState> {
|
||||
final CommunitiesService _communitiesService;
|
||||
|
||||
CommunitiesBloc({
|
||||
required CommunitiesService communitiesService,
|
||||
}) : _communitiesService = communitiesService,
|
||||
super(CommunitiesInitial()) {
|
||||
on<LoadCommunities>(_onGetCommunity);
|
||||
}
|
||||
|
||||
Future<void> _onGetCommunity(
|
||||
LoadCommunities event,
|
||||
Emitter<CommunitiesState> emit,
|
||||
) async {
|
||||
try {
|
||||
emit(CommunitiesLoading());
|
||||
final communities = await _communitiesService.getCommunity(event.param);
|
||||
emit(CommunitiesLoaded(communities));
|
||||
} on APIException catch (e) {
|
||||
emit(CommunitiesFailure(e.message));
|
||||
} catch (e) {
|
||||
emit(CommunitiesFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
part of 'communities_bloc.dart';
|
||||
|
||||
sealed class CommunitiesEvent extends Equatable {
|
||||
const CommunitiesEvent();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
class LoadCommunities extends CommunitiesEvent {
|
||||
const LoadCommunities(this.param);
|
||||
|
||||
final LoadCommunitiesParam param;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [param];
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
part of 'communities_bloc.dart';
|
||||
|
||||
sealed class CommunitiesState extends Equatable {
|
||||
const CommunitiesState();
|
||||
|
||||
@override
|
||||
List<Object?> get props => [];
|
||||
}
|
||||
|
||||
final class CommunitiesInitial extends CommunitiesState {}
|
||||
|
||||
final class CommunitiesLoading extends CommunitiesState {}
|
||||
|
||||
final class CommunitiesLoaded extends CommunitiesState {
|
||||
final List<CommunityModel> communities;
|
||||
|
||||
const CommunitiesLoaded(this.communities);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [communities];
|
||||
}
|
||||
|
||||
final class CommunitiesFailure extends CommunitiesState {
|
||||
final String message;
|
||||
|
||||
const CommunitiesFailure(this.message);
|
||||
|
||||
@override
|
||||
List<Object?> get props => [message];
|
||||
}
|
Reference in New Issue
Block a user