Add device icons based on device type and update network exception handling

- Add logic to set device icons based on device type in DeviceModel.fromJson method
- Update network exception handling to parse HTML response in ServerFailure class
- Add html package as a dependency for parsing HTML responses
-Added Devices Icons by updateing device types mapping
This commit is contained in:
Mohammad Salameh
2024-04-02 15:11:20 +03:00
parent 5dc4f96a71
commit 81625cd50e
8 changed files with 76 additions and 30 deletions

View File

@ -1,4 +1,5 @@
import 'package:syncrow_app/features/devices/model/function_model.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/utils/resource_manager/constants.dart';
class DeviceModel {
@ -56,13 +57,37 @@ class DeviceModel {
}
factory DeviceModel.fromJson(Map<String, dynamic> json) {
print(
'type : ${json['productId']} => ${devicesTypesMap[json['productId']]}');
String icon = '';
DeviceType type = devicesTypesMap[json['productId']] ?? DeviceType.Other;
if (type == DeviceType.LightBulb) {
icon = Assets.iconsLight;
} else if (type == DeviceType.CeilingSensor ||
type == DeviceType.WallSensor) {
icon = Assets.iconsSensors;
} else if (type == DeviceType.AC) {
icon = Assets.iconsAC;
} else if (type == DeviceType.DoorLock) {
icon = Assets.iconsDoorLock;
} else if (type == DeviceType.Curtain) {
icon = Assets.iconsCurtain;
} else if (type == DeviceType.ThreeGang) {
icon = Assets.iconsLight;
} else if (type == DeviceType.Gateway) {
icon = Assets.iconsGateway;
} else {
icon = Assets.iconsLogo;
}
return DeviceModel(
activeTime: json['activeTime'],
category: json['category'],
categoryName: json['categoryName'],
createTime: json['createTime'],
gatewayId: json['gatewayId'],
icon: json['icon'],
icon: icon,
id: json['id'],
ip: json['ip'],
lat: double.tryParse(json['lat']),

View File

@ -22,7 +22,6 @@ class DevicesViewBody extends StatelessWidget {
builder: (context, state) {
return BlocBuilder<DevicesCubit, DevicesState>(
builder: (context, state) {
//TODO : move to NavigationCubit
if (state is DevicesLoading) {
return const Center(child: CircularProgressIndicator());
} else {

View File

@ -21,18 +21,6 @@ class RoomPageSwitch extends StatelessWidget {
Widget build(BuildContext context) {
return InkWell(
onTap: () {
//Navigate to the chosen category view without animation
// Navigator.push(
// context,
// CustomPageRoute(
// builder: (context) {
// return DevicesCubit.get(context)
// .chosenCategoryView!;
// },
// ),
// );
if (device.productType == DeviceType.AC) {
Navigator.push(
context,

View File

@ -61,10 +61,7 @@ class WizartSwitches extends StatelessWidget {
DevicesCubit.allCategories![index].icon!,
fit: BoxFit.contain,
),
CustomSwitch(
category:
DevicesCubit.allCategories![index],
),
// CustomSwitch(
],
),
Expanded(

View File

@ -1,4 +1,5 @@
import 'package:dio/dio.dart';
import 'package:html/parser.dart' as parser;
abstract class Failure {
final String errMessage;
@ -28,8 +29,12 @@ class ServerFailure extends Failure {
return ServerFailure("Bad certificate!");
case DioExceptionType.badResponse:
return ServerFailure.fromResponse(dioError.response!.statusCode!,
dioError.response!.data!["message"]);
{
var document = parser.parse(dioError.response!.data.toString());
var message = document.body!.text;
return ServerFailure.fromResponse(
dioError.response!.statusCode!, message);
}
case DioExceptionType.cancel:
return ServerFailure("The request to ApiServer was canceled");

View File

@ -29,17 +29,32 @@ enum DeviceType {
Other,
}
// Map<String, DeviceType> devicesTypesMap = {
// "AC": DeviceType.AC,
// "LB": DeviceType.LightBulb,
// "DL": DeviceType.DoorLock,
// "WC": DeviceType.Curtain,
// "WB": DeviceType.Blind,
// "3G": DeviceType.ThreeGang,
// "GW": DeviceType.Gateway,
// "CPS": DeviceType.CeilingSensor,
// "WPS": DeviceType.WallSensor,
// "Other": DeviceType.Other,
// };
//AC wzdcrqh0
// GW wp8ticoo2bhumwgb
// CPS d3ci7gcn
// DL awu7anehyu5q1iu8
// WPS awarhusb
// 3G 1a6vgvyi
Map<String, DeviceType> devicesTypesMap = {
"AC": DeviceType.AC,
"LB": DeviceType.LightBulb,
"DL": DeviceType.DoorLock,
"WC": DeviceType.Curtain,
"WB": DeviceType.Blind,
"3G": DeviceType.ThreeGang,
"GW": DeviceType.Gateway,
"CPS": DeviceType.CeilingSensor,
"WPS": DeviceType.WallSensor,
"Other": DeviceType.Other,
"wzdcrqh0": DeviceType.AC,
"wp8ticoo2bhumwgb": DeviceType.Gateway,
"d3ci7gcn": DeviceType.CeilingSensor,
"awu7anehyu5q1iu8": DeviceType.DoorLock,
"awarhusb": DeviceType.WallSensor,
"1a6vgvyi": DeviceType.ThreeGang,
};
Map<DeviceType, List<FunctionModel>> devicesFunctionsMap = {
DeviceType.AC: [

View File

@ -97,6 +97,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.3"
csslib:
dependency: transitive
description:
name: csslib
sha256: "706b5707578e0c1b4b7550f64078f0a0f19dec3f50a178ffae7006b0a9ca58fb"
url: "https://pub.dev"
source: hosted
version: "1.0.0"
cupertino_icons:
dependency: "direct main"
description:
@ -357,6 +365,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "7.6.7"
html:
dependency: "direct main"
description:
name: html
sha256: "3a7812d5bcd2894edf53dfaf8cd640876cf6cef50a8f238745c8b8120ea74d3a"
url: "https://pub.dev"
source: hosted
version: "0.15.4"
http:
dependency: transitive
description:

View File

@ -36,6 +36,7 @@ dependencies:
firebase_analytics: ^10.8.7
firebase_crashlytics: ^3.4.16
smooth_page_indicator: ^1.1.0
html: ^0.15.4
dev_dependencies:
flutter_test: