Update Pie Chart In DeviceManagerScreen

This commit is contained in:
anhtunz
2024-12-24 11:16:00 +07:00
parent e047fe1e27
commit 77afc09d19
6 changed files with 297 additions and 29 deletions

View File

@@ -1,6 +1,8 @@
import 'dart:async';
import 'dart:convert';
import 'package:sfm_app/product/constant/app/app_constants.dart';
import 'device_model.dart';
import '../../product/base/bloc/base_bloc.dart';
import '../../product/services/api_services.dart';
@@ -18,18 +20,54 @@ class DevicesManagerBloc extends BlocBase {
StreamSink<List<Device>> get sinkAllDevices => allDevices.sink;
Stream<List<Device>> get streamAllDevices => allDevices.stream;
final deviceByState = StreamController<Map<String, List<Device>>>.broadcast();
StreamSink<Map<String, List<Device>>> get sinkDeviceByState =>
deviceByState.sink;
Stream<Map<String, List<Device>>> get streamDeviceByState =>
deviceByState.stream;
@override
void dispose() {}
void getDevice() async {
String body = await apiServices.getOwnerDevices();
if (body != "") {
Map<String, List<Device>> deviceByState = {
ApplicationConstants.OFFLINE_STATE: [],
ApplicationConstants.NORMAL_STATE: [],
ApplicationConstants.WARNING_STATE: [],
ApplicationConstants.INPROGRESS_STATE: [],
ApplicationConstants.ERROR_STATE: [],
};
if (body.isNotEmpty) {
final data = jsonDecode(body);
List<dynamic> items = data['items'];
List<Device> originalDevices = Device.fromJsonDynamicList(items);
List<Device> 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);
}
}
}