mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
renamed devices
module, to products
.
This commit is contained in:
@ -1,82 +0,0 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class DeviceModel extends Equatable {
|
||||
final String uuid;
|
||||
final String name;
|
||||
final String type;
|
||||
final String status;
|
||||
final String lastSeen;
|
||||
final String spaceUuid;
|
||||
final List<DeviceCapability> capabilities;
|
||||
|
||||
const DeviceModel({
|
||||
required this.uuid,
|
||||
required this.name,
|
||||
required this.type,
|
||||
required this.status,
|
||||
required this.lastSeen,
|
||||
required this.spaceUuid,
|
||||
required this.capabilities,
|
||||
});
|
||||
|
||||
factory DeviceModel.fromJson(Map<String, dynamic> json) {
|
||||
return DeviceModel(
|
||||
uuid: json['uuid'] as String,
|
||||
name: json['name'] as String,
|
||||
type: json['type'] as String,
|
||||
status: json['status'] as String,
|
||||
lastSeen: json['lastSeen'] as String,
|
||||
spaceUuid: json['spaceUuid'] as String,
|
||||
capabilities: (json['capabilities'] as List)
|
||||
.map((e) => DeviceCapability.fromJson(e as Map<String, dynamic>))
|
||||
.toList(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'uuid': uuid,
|
||||
'name': name,
|
||||
'type': type,
|
||||
'status': status,
|
||||
'lastSeen': lastSeen,
|
||||
'spaceUuid': spaceUuid,
|
||||
'capabilities': capabilities.map((e) => e.toJson()).toList(),
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props =>
|
||||
[uuid, name, type, status, lastSeen, spaceUuid, capabilities];
|
||||
}
|
||||
|
||||
class DeviceCapability extends Equatable {
|
||||
final String name;
|
||||
final String type;
|
||||
final Map<String, dynamic> config;
|
||||
|
||||
const DeviceCapability({
|
||||
required this.name,
|
||||
required this.type,
|
||||
required this.config,
|
||||
});
|
||||
|
||||
factory DeviceCapability.fromJson(Map<String, dynamic> json) {
|
||||
return DeviceCapability(
|
||||
name: json['name'] as String,
|
||||
type: json['type'] as String,
|
||||
config: json['config'] as Map<String, dynamic>,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'name': name,
|
||||
'type': type,
|
||||
'config': config,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [name, type, config];
|
||||
}
|
@ -1,6 +0,0 @@
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/models/device_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/params/load_devices_param.dart';
|
||||
|
||||
abstract class DeviceService {
|
||||
Future<List<DeviceModel>> getDevices(LoadDevicesParam param);
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/models/device_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/params/load_devices_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/services/device_service.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
|
||||
part 'device_event.dart';
|
||||
part 'device_state.dart';
|
||||
|
||||
class DeviceBloc extends Bloc<DeviceEvent, DeviceState> {
|
||||
final DeviceService _deviceService;
|
||||
|
||||
DeviceBloc(this._deviceService) : super(DeviceInitial()) {
|
||||
on<LoadDevices>(_onLoadDevices);
|
||||
}
|
||||
|
||||
Future<void> _onLoadDevices(
|
||||
LoadDevices event,
|
||||
Emitter<DeviceState> emit,
|
||||
) async {
|
||||
emit(DeviceLoading());
|
||||
try {
|
||||
final devices = await _deviceService.getDevices(event.param);
|
||||
emit(DeviceLoaded(devices));
|
||||
} on APIException catch (e) {
|
||||
emit(DeviceFailure(e.message));
|
||||
} catch (e) {
|
||||
emit(DeviceFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
part of 'device_bloc.dart';
|
||||
|
||||
sealed class DeviceEvent extends Equatable {
|
||||
const DeviceEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class LoadDevices extends DeviceEvent {
|
||||
const LoadDevices(this.param);
|
||||
|
||||
final LoadDevicesParam param;
|
||||
|
||||
@override
|
||||
List<Object> get props => [param];
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
part of 'device_bloc.dart';
|
||||
|
||||
sealed class DeviceState extends Equatable {
|
||||
const DeviceState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class DeviceInitial extends DeviceState {}
|
||||
|
||||
final class DeviceLoading extends DeviceState {}
|
||||
|
||||
final class DeviceLoaded extends DeviceState {
|
||||
final List<DeviceModel> devices;
|
||||
|
||||
const DeviceLoaded(this.devices);
|
||||
|
||||
@override
|
||||
List<Object> get props => [devices];
|
||||
}
|
||||
|
||||
final class DeviceFailure extends DeviceState {
|
||||
final String message;
|
||||
|
||||
const DeviceFailure(this.message);
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
@ -1,21 +1,19 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/models/device_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/params/load_devices_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/devices/domain/services/device_service.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/products/domain/models/product_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/products/domain/params/load_products_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/products/domain/services/products_service.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
import 'package:syncrow_web/services/api/http_service.dart';
|
||||
|
||||
class RemoteDeviceService implements DeviceService {
|
||||
final HTTPService _httpService;
|
||||
class RemoteProductsService implements ProductsService {
|
||||
const RemoteProductsService(this._httpService);
|
||||
|
||||
RemoteDeviceService({
|
||||
required HTTPService httpService,
|
||||
}) : _httpService = httpService;
|
||||
final HTTPService _httpService;
|
||||
|
||||
static const _defaultErrorMessage = 'Failed to load devices';
|
||||
|
||||
@override
|
||||
Future<List<DeviceModel>> getDevices(LoadDevicesParam param) async {
|
||||
Future<List<ProductModel>> getProducts(LoadProductsParam param) async {
|
||||
try {
|
||||
final response = await _httpService.get(
|
||||
path: 'devices',
|
||||
@ -26,7 +24,7 @@ class RemoteDeviceService implements DeviceService {
|
||||
},
|
||||
expectedResponseModel: (data) {
|
||||
return (data as List)
|
||||
.map((e) => DeviceModel.fromJson(e as Map<String, dynamic>))
|
||||
.map((e) => ProductModel.fromJson(e as Map<String, dynamic>))
|
||||
.toList();
|
||||
},
|
||||
);
|
@ -0,0 +1,21 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class ProductModel extends Equatable {
|
||||
final String uuid;
|
||||
final String name;
|
||||
|
||||
const ProductModel({
|
||||
required this.uuid,
|
||||
required this.name,
|
||||
});
|
||||
|
||||
factory ProductModel.fromJson(Map<String, dynamic> json) {
|
||||
return ProductModel(
|
||||
uuid: json['uuid'] as String,
|
||||
name: json['name'] as String,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [uuid, name];
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
class LoadDevicesParam {
|
||||
class LoadProductsParam {
|
||||
final String spaceUuid;
|
||||
final String? type;
|
||||
final String? status;
|
||||
|
||||
const LoadDevicesParam({
|
||||
const LoadProductsParam({
|
||||
required this.spaceUuid,
|
||||
this.type,
|
||||
this.status,
|
@ -0,0 +1,6 @@
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/products/domain/models/product_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/products/domain/params/load_products_param.dart';
|
||||
|
||||
abstract class ProductsService {
|
||||
Future<List<ProductModel>> getProducts(LoadProductsParam param);
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/products/domain/models/product_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/products/domain/params/load_products_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/products/domain/services/products_service.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
|
||||
part 'products_event.dart';
|
||||
part 'products_state.dart';
|
||||
|
||||
class ProductsBloc extends Bloc<ProductsEvent, ProductsState> {
|
||||
final ProductsService _deviceService;
|
||||
|
||||
ProductsBloc(this._deviceService) : super(ProductsInitial()) {
|
||||
on<LoadProducts>(_onLoadDevices);
|
||||
}
|
||||
|
||||
Future<void> _onLoadDevices(
|
||||
LoadProducts event,
|
||||
Emitter<ProductsState> emit,
|
||||
) async {
|
||||
emit(ProductsLoading());
|
||||
try {
|
||||
final devices = await _deviceService.getProducts(event.param);
|
||||
emit(ProductsLoaded(devices));
|
||||
} on APIException catch (e) {
|
||||
emit(ProductsFailure(e.message));
|
||||
} catch (e) {
|
||||
emit(ProductsFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
part of 'products_bloc.dart';
|
||||
|
||||
sealed class ProductsEvent extends Equatable {
|
||||
const ProductsEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class LoadProducts extends ProductsEvent {
|
||||
const LoadProducts(this.param);
|
||||
|
||||
final LoadProductsParam param;
|
||||
|
||||
@override
|
||||
List<Object> get props => [param];
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
part of 'products_bloc.dart';
|
||||
|
||||
sealed class ProductsState extends Equatable {
|
||||
const ProductsState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class ProductsInitial extends ProductsState {}
|
||||
|
||||
final class ProductsLoading extends ProductsState {}
|
||||
|
||||
final class ProductsLoaded extends ProductsState {
|
||||
final List<ProductModel> devices;
|
||||
|
||||
const ProductsLoaded(this.devices);
|
||||
|
||||
@override
|
||||
List<Object> get props => [devices];
|
||||
}
|
||||
|
||||
final class ProductsFailure extends ProductsState {
|
||||
final String message;
|
||||
|
||||
const ProductsFailure(this.message);
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
Reference in New Issue
Block a user