refactor(ui): Update some UI and fix some bugs

This commit is contained in:
anhtunz
2025-01-11 16:43:19 +07:00
parent c2c685da86
commit a17831d0ac
16 changed files with 688 additions and 464 deletions

View File

@@ -1,10 +1,9 @@
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/constant/app/app_constants.dart';
import '../product/services/api_services.dart';
import '../product/utils/device_utils.dart';
@@ -26,11 +25,57 @@ class DevicesManagerBloc extends BlocBase {
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();
// 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(int state) async {
sinkTagStates.add([state]);
Map<String, List<Device>> deviceByState = {
ApplicationConstants.OFFLINE_STATE: [],
ApplicationConstants.NORMAL_STATE: [],
@@ -39,35 +84,49 @@ class DevicesManagerBloc extends BlocBase {
ApplicationConstants.ERROR_STATE: [],
};
List<Device> devices = [];
String body;
if (state != -2) {
body =
await apiServices.getOwnerDevieByState({"state": state.toString()});
} else {
body = await apiServices.getOwnerDevices();
}
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;
devices = (state != -2)
? DeviceUtils.instance.sortDeviceAZByName(originalDevices)
: DeviceUtils.instance.sortDeviceByState(originalDevices);
if (state == -2) {
for (var device in originalDevices) {
String stateKey = _getStateKey(device.state!);
deviceByState[stateKey]!.add(device);
}
deviceByState[stateKey]!.add(device);
sinkDeviceByState.add(deviceByState);
}
sinkAllDevices.add(devices);
sinkDeviceByState.add(deviceByState);
}
sinkAllDevices.add(devices);
}
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;
}
}
}

View File

@@ -1,19 +1,22 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:sfm_app/product/base/bloc/base_bloc.dart';
import '../product/base/bloc/base_bloc.dart';
import '../product/services/api_services.dart';
import '../feature/bell/bell_model.dart';
import '../feature/settings/profile/profile_model.dart';
class MainBloc extends BlocBase {
APIServices apiServices = APIServices();
final bellBloc = StreamController<Bell>.broadcast();
StreamSink<Bell> get sinkBellBloc => bellBloc.sink;
Stream<Bell> get streamBellBloc => bellBloc.stream;
final language = StreamController<Locale?>.broadcast();
StreamSink<Locale?> get sinkLanguage => language.sink;
Stream<Locale?> get streamLanguage => language.stream;
final theme = StreamController<ThemeData?>.broadcast();
StreamSink<ThemeData?> get sinkTheme => theme.sink;
Stream<ThemeData?> get streamTheme => theme.stream;
@@ -26,6 +29,16 @@ class MainBloc extends BlocBase {
StreamSink<bool> get sinkIsVNIcon => isVNIcon.sink;
Stream<bool> get streamIsVNIcon => isVNIcon.stream;
final userProfile = StreamController<User>.broadcast();
StreamSink<User> get sinkUserProfile => userProfile.sink;
Stream<User> get streamUserProfile => userProfile.stream;
@override
void dispose() {}
void getUserProfile() async {
String data = await apiServices.getUserDetail();
User user = User.fromJson(jsonDecode(data));
sinkUserProfile.add(user);
}
}