import 'dart:async'; import 'dart:convert'; import 'package:sfm_app/product/constant/app/app_constants.dart'; import '../feature/devices/device_model.dart'; import '../product/base/bloc/base_bloc.dart'; import '../product/services/api_services.dart'; import '../product/utils/device_utils.dart'; class DevicesManagerBloc extends BlocBase { APIServices apiServices = APIServices(); final userRole = StreamController.broadcast(); StreamSink get sinkUserRole => userRole.sink; Stream get streamUserRole => userRole.stream; final allDevices = StreamController>.broadcast(); StreamSink> get sinkAllDevices => allDevices.sink; Stream> get streamAllDevices => allDevices.stream; final deviceByState = StreamController>>.broadcast(); StreamSink>> get sinkDeviceByState => deviceByState.sink; Stream>> get streamDeviceByState => deviceByState.stream; @override void dispose() {} void getDevice() async { String body = await apiServices.getOwnerDevices(); Map> deviceByState = { ApplicationConstants.OFFLINE_STATE: [], ApplicationConstants.NORMAL_STATE: [], ApplicationConstants.WARNING_STATE: [], ApplicationConstants.INPROGRESS_STATE: [], ApplicationConstants.ERROR_STATE: [], }; if (body.isNotEmpty) { final data = jsonDecode(body); List items = data['items']; List originalDevices = Device.fromJsonDynamicList(items); List devices = DeviceUtils.instance.sortDeviceByState(originalDevices); for (var device in devices) { String stateKey; switch (device.state) { case -1: stateKey = ApplicationConstants.OFFLINE_STATE; break; case 0: stateKey = ApplicationConstants.NORMAL_STATE; break; case 1: stateKey = ApplicationConstants.WARNING_STATE; break; case 2: stateKey = ApplicationConstants.INPROGRESS_STATE; break; default: stateKey = ApplicationConstants.ERROR_STATE; break; } deviceByState[stateKey]!.add(device); } sinkAllDevices.add(devices); sinkDeviceByState.add(deviceByState); } } }