feat(api_service): Update try-catch funtion and handle exception update(loading_animation): Update loading animation using Lottie
143 lines
4.5 KiB
Dart
143 lines
4.5 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import '../feature/devices/device_model.dart';
|
|
import '../product/base/bloc/base_bloc.dart';
|
|
import '../product/constant/app/app_constants.dart';
|
|
import '../product/services/api_services.dart';
|
|
|
|
import '../product/shared/shared_snack_bar.dart';
|
|
import '../product/utils/device_utils.dart';
|
|
|
|
class DevicesManagerBloc extends BlocBase {
|
|
APIServices apiServices = APIServices();
|
|
|
|
final userRole = StreamController<String>.broadcast();
|
|
StreamSink<String> get sinkUserRole => userRole.sink;
|
|
Stream<String> get streamUserRole => userRole.stream;
|
|
|
|
final allDevices = StreamController<List<Device>>.broadcast();
|
|
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;
|
|
|
|
final tagStates = StreamController<List<int>>.broadcast();
|
|
StreamSink<List<int>> get sinkTagStates => tagStates.sink;
|
|
Stream<List<int>> get streamTagStates => tagStates.stream;
|
|
|
|
@override
|
|
void dispose() {}
|
|
|
|
// void getDevice() async {
|
|
// String body = await apiServices.getOwnerDevices();
|
|
// 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);
|
|
// }
|
|
// }
|
|
|
|
void getDeviceByState(BuildContext context,int state) async {
|
|
try {
|
|
sinkTagStates.add([state]);
|
|
|
|
Map<String, List<Device>> deviceByState = {
|
|
ApplicationConstants.OFFLINE_STATE: [],
|
|
ApplicationConstants.NORMAL_STATE: [],
|
|
ApplicationConstants.WARNING_STATE: [],
|
|
ApplicationConstants.INPROGRESS_STATE: [],
|
|
ApplicationConstants.ERROR_STATE: [],
|
|
};
|
|
|
|
List<Device> devices = [];
|
|
List<Device> originalDevices = [];
|
|
if (state != -2) {
|
|
originalDevices =
|
|
await apiServices.getOwnerDeviceByState({"state": state.toString()});
|
|
} else {
|
|
originalDevices = await apiServices.getOwnerDevices();
|
|
}
|
|
|
|
List<Device> publicDevices = [];
|
|
|
|
for(var device in originalDevices){
|
|
if(device.visibility == "PUBLIC"){
|
|
publicDevices.add(device);
|
|
}
|
|
}
|
|
devices = (state != -2)
|
|
? DeviceUtils.instance.sortDeviceAZByName(publicDevices)
|
|
: DeviceUtils.instance.sortDeviceByState(publicDevices);
|
|
|
|
if (state == -2) {
|
|
for (var device in publicDevices) {
|
|
String stateKey = _getStateKey(device.state!);
|
|
deviceByState[stateKey]!.add(device);
|
|
}
|
|
sinkDeviceByState.add(deviceByState);
|
|
}
|
|
|
|
sinkAllDevices.add(devices);
|
|
} catch (e) {
|
|
if (!context.mounted) return;
|
|
showErrorTopSnackBarCustom(
|
|
context, e.toString());
|
|
}
|
|
|
|
}
|
|
|
|
String _getStateKey(int state) {
|
|
switch (state) {
|
|
case -1:
|
|
return ApplicationConstants.OFFLINE_STATE;
|
|
case 0:
|
|
return ApplicationConstants.NORMAL_STATE;
|
|
case 1:
|
|
return ApplicationConstants.WARNING_STATE;
|
|
case 2:
|
|
return ApplicationConstants.INPROGRESS_STATE;
|
|
default:
|
|
return ApplicationConstants.ERROR_STATE;
|
|
}
|
|
}
|
|
}
|