Fix(bugs):

Can't logout
ui display when user doesn't has device
This commit is contained in:
anhtunz
2025-05-26 11:58:19 +07:00
parent f80e234b1d
commit 01ae020374
10 changed files with 417 additions and 278 deletions

View File

@@ -1,6 +1,7 @@
// ignore_for_file: use_build_context_synchronously
import 'package:flutter/material.dart';
import 'package:sfm_app/bloc/devices_manager_bloc.dart';
import 'package:sfm_app/product/shared/shared_snack_bar.dart';
import '../../product/utils/response_status_utils.dart';
import '../../product/constant/enums/role_enums.dart';
@@ -10,7 +11,7 @@ import '../../product/constant/icon/icon_constants.dart';
import '../../product/extension/context_extension.dart';
import '../../product/services/language_services.dart';
addNewDevice(BuildContext context, String role) async {
addNewDevice(BuildContext context, String role, DevicesManagerBloc deviceManagerBloc) async {
TextEditingController extIDController = TextEditingController(text: "");
TextEditingController deviceNameController = TextEditingController(text: "");
ScaffoldMessenger.of(context).showSnackBar(
@@ -76,7 +77,7 @@ addNewDevice(BuildContext context, String role) async {
Colors.white);
ScaffoldMessenger.of(context).hideCurrentSnackBar();
} else {
addDevices(context, role, extID, deviceName);
addDevices(context, role, extID, deviceName, deviceManagerBloc);
ScaffoldMessenger.of(context).hideCurrentSnackBar();
}
},
@@ -90,7 +91,7 @@ addNewDevice(BuildContext context, String role) async {
}
void addDevices(
BuildContext context, String role, String extID, String deviceName) async {
BuildContext context, String role, String extID, String deviceName, DevicesManagerBloc deviceManagerBloc) async {
APIServices apiServices = APIServices();
Map<String, dynamic> body = {};
if (role == RoleEnums.ADMIN.name) {
@@ -101,6 +102,7 @@ void addDevices(
statusCode,
appLocalization(context).notification_create_device_success,
appLocalization(context).notification_create_device_failed);
deviceManagerBloc.getDeviceByState(-2);
} else {
body = {"ext_id": extID};
int statusCode = await apiServices.registerDevice(body);
@@ -109,5 +111,7 @@ void addDevices(
statusCode,
appLocalization(context).notification_add_device_success,
appLocalization(context).notification_device_not_exist);
deviceManagerBloc.getDeviceByState(-2);
}
}

View File

@@ -29,7 +29,7 @@ class _DevicesManagerScreenState extends State<DevicesManagerScreen> {
late DevicesManagerBloc devicesManagerBloc;
String role = "Undefine";
APIServices apiServices = APIServices();
List<Device> devices = [];
// List<Device> devices = [];
Timer? getAllDevicesTimer;
List<Widget> tags = [];
int tagIndex = -2;
@@ -58,36 +58,60 @@ class _DevicesManagerScreenState extends State<DevicesManagerScreen> {
body: StreamBuilder<List<int>>(
stream: devicesManagerBloc.streamTagStates,
builder: (context, tagSnapshot) {
return SafeArea(
child: StreamBuilder<List<Device>>(
stream: devicesManagerBloc.streamAllDevices,
initialData: devices,
builder: (context, allDeviceSnapshot) {
if (allDeviceSnapshot.data?.isEmpty ?? devices.isEmpty) {
devicesManagerBloc
.getDeviceByState(tagSnapshot.data?[0] ?? -2);
return const Center(child: CircularProgressIndicator());
} else {
if (tagSnapshot.data!.isNotEmpty) {
tagIndex = tagSnapshot.data![0];
devicesManagerBloc.sinkTagStates.add([tagIndex]);
}
return SingleChildScrollView(
child: Column(
children: [
if (tagSnapshot.hasData &&
tagSnapshot.data!.isNotEmpty &&
tagSnapshot.data![0] != -2)
TagState(
state: tagSnapshot.data![0],
devicesManagerBloc: devicesManagerBloc,
),
SizedBox(height: context.lowValue),
StreamBuilder<String>(
stream: devicesManagerBloc.streamUserRole,
initialData: role,
builder: (context, roleSnapshot) {
return SizedBox(
return StreamBuilder<String>(
stream: devicesManagerBloc.streamUserRole,
initialData: role,
builder: (context, roleSnapshot) {
return SafeArea(
child: StreamBuilder<List<Device>>(
stream: devicesManagerBloc.streamAllDevices,
builder: (context, allDeviceSnapshot) {
if(allDeviceSnapshot.data == null){
devicesManagerBloc
.getDeviceByState(tagSnapshot.data?[0] ?? -2);
return const Center(child: CircularProgressIndicator());
}
if (allDeviceSnapshot.data!.isEmpty) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Container(
width: 100,
height: 100,
decoration: BoxDecoration(
border: Border.all(color: Theme.of(context).colorScheme.primary),
borderRadius: BorderRadius.circular(50)
),
child: IconButton(onPressed: (){
ScaffoldMessenger.of(context)
.clearSnackBars();
addNewDevice(context,
roleSnapshot.data ?? role, devicesManagerBloc);
}, iconSize: 50, icon: const Icon(Icons.add),),),
SizedBox(height: context.mediumValue,),
Text(appLocalization(context).dont_have_device, style: context.responsiveBodyMediumWithBold,)
],
),
);
} else {
if (tagSnapshot.data!.isNotEmpty) {
tagIndex = tagSnapshot.data![0];
devicesManagerBloc.sinkTagStates.add([tagIndex]);
}
return SingleChildScrollView(
child: Column(
children: [
if (tagSnapshot.hasData &&
tagSnapshot.data!.isNotEmpty &&
tagSnapshot.data![0] != -2)
TagState(
state: tagSnapshot.data![0],
devicesManagerBloc: devicesManagerBloc,
),
SizedBox(height: context.lowValue),
SizedBox(
height: getTableHeight(allDeviceSnapshot.data?.length ?? 1),
child: PaginatedDataTable2(
wrapInCard: false,
@@ -108,7 +132,7 @@ class _DevicesManagerScreenState extends State<DevicesManagerScreen> {
),
columns: [
if (roleSnapshot.data ==
RoleEnums.ADMIN.name ||
RoleEnums.ADMIN.name ||
roleSnapshot.data ==
RoleEnums.USER.name)
DataColumn2(
@@ -149,23 +173,23 @@ class _DevicesManagerScreenState extends State<DevicesManagerScreen> {
// log('Chuyen page: $pageIndex');
},
rowsPerPage:
(allDeviceSnapshot.data?.length ?? 1) < 6
? (allDeviceSnapshot.data?.length ??
0)
: 5,
(allDeviceSnapshot.data?.length ?? 1) < 6
? (allDeviceSnapshot.data?.length ??
0)
: 5,
actions: [
if (roleSnapshot.data ==
RoleEnums.USER.name ||
RoleEnums.USER.name ||
roleSnapshot.data ==
RoleEnums.ADMIN.name)
IconButton(
style: ButtonStyle(
backgroundColor:
WidgetStateProperty.all<Color>(
Colors.green),
WidgetStateProperty.all<Color>(
Colors.green),
iconColor:
WidgetStateProperty.all<Color>(
WidgetStateProperty.all<Color>(
Colors.white,
),
),
@@ -173,49 +197,49 @@ class _DevicesManagerScreenState extends State<DevicesManagerScreen> {
ScaffoldMessenger.of(context)
.clearSnackBars();
addNewDevice(context,
roleSnapshot.data ?? role);
roleSnapshot.data ?? role, devicesManagerBloc);
},
icon: IconConstants.instance
.getMaterialIcon(Icons.add))
],
source: DeviceSource(
devices: allDeviceSnapshot.data ?? devices,
devices: allDeviceSnapshot.data ?? [],
context: context,
devicesBloc: devicesManagerBloc,
role: role,
),
),
);
},
),
SizedBox(height: context.lowValue),
Text(
appLocalization(context).overview_message,
style: context.responsiveBodyLargeWithBold
),
StreamBuilder<Map<String, List<Device>>>(
stream: devicesManagerBloc.streamDeviceByState,
builder: (context, devicesByStateSnapshot) {
if (devicesByStateSnapshot.data == null) {
devicesManagerBloc.getDeviceByState(
tagSnapshot.data?[0] ?? -2);
return const Center(
child: CircularProgressIndicator());
} else {
return SharedPieChart(
deviceByState:
devicesByStateSnapshot.data ?? {},
devicesManagerBloc: devicesManagerBloc,
);
}
},
),
],
),
SizedBox(height: context.lowValue),
Text(
appLocalization(context).overview_message,
style: context.responsiveBodyLargeWithBold
),
StreamBuilder<Map<String, List<Device>>>(
stream: devicesManagerBloc.streamDeviceByState,
builder: (context, devicesByStateSnapshot) {
if (devicesByStateSnapshot.data == null) {
devicesManagerBloc.getDeviceByState(
tagSnapshot.data?[0] ?? -2);
return const Center(
child: CircularProgressIndicator());
} else {
return SharedPieChart(
deviceByState:
devicesByStateSnapshot.data ?? {},
devicesManagerBloc: devicesManagerBloc,
);
}
},
),
],
),
);
}
},
),
);
}
},
),
);
}
);
}),
);