mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-08-26 01:39:40 +00:00
refactor: consolidate device loading and error handling in SceneDevicesBody and implement SceneDevicesList widget
This commit is contained in:
@ -1,6 +1,5 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/device_manager_bloc/device_manager_bloc.dart';
|
||||
import 'package:syncrow_app/features/devices/bloc/device_manager_bloc/device_manager_state.dart';
|
||||
import 'package:syncrow_app/features/devices/model/subspace_model.dart';
|
||||
@ -8,20 +7,17 @@ import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_bloc.dart'
|
||||
import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_state.dart';
|
||||
import 'package:syncrow_app/features/scene/enum/create_scene_enum.dart';
|
||||
import 'package:syncrow_app/features/scene/model/scene_settings_route_arguments.dart';
|
||||
import 'package:syncrow_app/features/scene/widgets/scene_list_tile.dart';
|
||||
import 'package:syncrow_app/features/scene/widgets/scene_devices/scene_devices_list.dart';
|
||||
import 'package:syncrow_app/features/shared_widgets/app_loading_indicator.dart';
|
||||
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
|
||||
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_large.dart';
|
||||
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
|
||||
import 'package:syncrow_app/navigation/routing_constants.dart';
|
||||
import 'package:syncrow_app/utils/context_extension.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||
|
||||
class SceneDevicesBody extends StatelessWidget {
|
||||
const SceneDevicesBody({
|
||||
super.key,
|
||||
required this.tabController,
|
||||
required this.rooms,
|
||||
super.key,
|
||||
});
|
||||
|
||||
final TabController tabController;
|
||||
@ -44,6 +40,8 @@ class SceneDevicesBody extends StatelessWidget {
|
||||
controller: tabController,
|
||||
dividerColor: Colors.transparent,
|
||||
indicatorColor: Colors.transparent,
|
||||
isScrollable: true,
|
||||
tabAlignment: TabAlignment.start,
|
||||
tabs: rooms.map((e) {
|
||||
final isStateTabSelected = state is TabSelected;
|
||||
final isSelected = isStateTabSelected && state.roomId == e.id;
|
||||
@ -61,8 +59,6 @@ class SceneDevicesBody extends StatelessWidget {
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
isScrollable: true,
|
||||
tabAlignment: TabAlignment.start,
|
||||
),
|
||||
Expanded(
|
||||
child: TabBarView(
|
||||
@ -86,47 +82,29 @@ class SceneDevicesBody extends StatelessWidget {
|
||||
) {
|
||||
return BlocBuilder<DeviceManagerBloc, DeviceManagerState>(
|
||||
builder: (context, state) {
|
||||
if (state.loading && state.devices == null) {
|
||||
return const AppLoadingIndicator();
|
||||
} else if (state.devices != null && state.devices!.isNotEmpty) {
|
||||
return ListView.builder(
|
||||
itemCount: state.devices!.length,
|
||||
itemBuilder: (context, index) {
|
||||
final device = state.devices?[index];
|
||||
return DefaultContainer(
|
||||
child: SceneListTile(
|
||||
minLeadingWidth: 40,
|
||||
leadingWidget: SvgPicture.asset(device?.icon ?? ''),
|
||||
titleWidget: BodyMedium(
|
||||
text: device?.name ?? '',
|
||||
style: context.titleSmall.copyWith(
|
||||
color: ColorsManager.secondaryTextColor,
|
||||
fontWeight: FontWeight.w400,
|
||||
fontSize: 20,
|
||||
),
|
||||
),
|
||||
trailingWidget: const Icon(
|
||||
Icons.arrow_forward_ios_rounded,
|
||||
color: ColorsManager.greyColor,
|
||||
size: 16,
|
||||
),
|
||||
onPressed: () => Navigator.pushNamed(
|
||||
context,
|
||||
Routes.deviceFunctionsRoute,
|
||||
arguments: {
|
||||
"device": device,
|
||||
"isAutomationDeviceStatus": isAutomationDeviceStatus
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
} else if (state.error != null) {
|
||||
return const Center(child: Text('Something went wrong'));
|
||||
} else {
|
||||
return const SizedBox();
|
||||
}
|
||||
final isLoading = state.loading && state.devices == null;
|
||||
final hasData =
|
||||
state.devices != null && (state.devices?.isNotEmpty ?? false);
|
||||
final hasError = state.error != null;
|
||||
|
||||
final widgets = <bool, Widget>{
|
||||
isLoading: const AppLoadingIndicator(),
|
||||
hasError: Center(child: Text('${state.error}')),
|
||||
hasData: SceneDevicesList(
|
||||
devices: state.devices ?? [],
|
||||
isAutomationDeviceStatus: isAutomationDeviceStatus,
|
||||
),
|
||||
};
|
||||
|
||||
final validWidgetEntry = widgets.entries.firstWhere(
|
||||
(entry) => entry.key == true,
|
||||
orElse: () => MapEntry(
|
||||
true,
|
||||
Center(child: Text('This subspace has no devices')),
|
||||
),
|
||||
);
|
||||
|
||||
return validWidgetEntry.value;
|
||||
},
|
||||
);
|
||||
}
|
||||
|
@ -0,0 +1,66 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_svg/flutter_svg.dart';
|
||||
import 'package:syncrow_app/features/devices/model/device_model.dart';
|
||||
import 'package:syncrow_app/features/scene/widgets/scene_list_tile.dart';
|
||||
import 'package:syncrow_app/features/shared_widgets/default_container.dart';
|
||||
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_medium.dart';
|
||||
import 'package:syncrow_app/navigation/routing_constants.dart';
|
||||
import 'package:syncrow_app/utils/context_extension.dart';
|
||||
import 'package:syncrow_app/utils/resource_manager/color_manager.dart';
|
||||
|
||||
class SceneDevicesList extends StatelessWidget {
|
||||
const SceneDevicesList({
|
||||
required this.isAutomationDeviceStatus,
|
||||
required this.devices,
|
||||
super.key,
|
||||
});
|
||||
final List<DeviceModel> devices;
|
||||
final bool isAutomationDeviceStatus;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView.builder(
|
||||
itemCount: devices.length,
|
||||
itemBuilder: (context, index) => _buildDeviceTile(context, devices[index]),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildDeviceTile(
|
||||
BuildContext context,
|
||||
DeviceModel device,
|
||||
) {
|
||||
return DefaultContainer(
|
||||
child: SceneListTile(
|
||||
minLeadingWidth: 40,
|
||||
leadingWidget: SvgPicture.asset(device.icon ?? ''),
|
||||
titleWidget: BodyMedium(
|
||||
text: device.name ?? '',
|
||||
style: context.titleSmall.copyWith(
|
||||
color: ColorsManager.secondaryTextColor,
|
||||
fontWeight: FontWeight.w400,
|
||||
fontSize: 20,
|
||||
),
|
||||
),
|
||||
trailingWidget: const Icon(
|
||||
Icons.arrow_forward_ios_rounded,
|
||||
color: ColorsManager.greyColor,
|
||||
size: 16,
|
||||
),
|
||||
onPressed: () => _navigateToDeviceFunctions(context, device),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _navigateToDeviceFunctions(
|
||||
BuildContext context,
|
||||
DeviceModel device,
|
||||
) {
|
||||
Navigator.of(context).pushNamed(
|
||||
Routes.deviceFunctionsRoute,
|
||||
arguments: {
|
||||
"device": device,
|
||||
"isAutomationDeviceStatus": isAutomationDeviceStatus,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user