refactor: rename tab change events and states for consistency, and to match blocs naming convention.

This commit is contained in:
Faris Armoush
2025-04-16 16:07:30 +03:00
parent f25b4dbf6d
commit e4768c95aa
6 changed files with 37 additions and 22 deletions

View File

@ -1,5 +1,3 @@
import 'dart:async';
import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.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_bloc.dart';
import 'package:syncrow_app/features/devices/bloc/device_manager_bloc/device_manager_event.dart'; import 'package:syncrow_app/features/devices/bloc/device_manager_bloc/device_manager_event.dart';
@ -7,19 +5,27 @@ import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_event.dart
import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_state.dart'; import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_state.dart';
class TabBarBloc extends Bloc<TabBarEvent, TabBarState> { class TabBarBloc extends Bloc<TabBarEvent, TabBarState> {
final DeviceManagerBloc deviceManagerBloc; TabBarBloc(this.deviceManagerBloc) : super(const TabBarInitialState()) {
TabBarBloc(this.deviceManagerBloc) : super(const Initial()) { on<TabBarTabChangedEvent>(_onTabBarTabChangedEvent);
on<TabChanged>(_handleTabChanged);
} }
FutureOr<void> _handleTabChanged( final DeviceManagerBloc deviceManagerBloc;
TabChanged event, Emitter<TabBarState> emit) {
void _onTabBarTabChangedEvent(
TabBarTabChangedEvent event,
Emitter<TabBarState> emit,
) {
if (event.roomId == "-1") { if (event.roomId == "-1") {
deviceManagerBloc.add(FetchAllDevices()); deviceManagerBloc.add(FetchAllDevices());
} else { } else {
deviceManagerBloc.add(FetchDevicesByRoomId(event.roomId,event.unit)); deviceManagerBloc.add(FetchDevicesByRoomId(event.roomId, event.unit));
} }
emit(TabSelected(
roomId: event.roomId, selectedTabIndex: event.selectedIndex)); emit(
TabBarTabSelectedState(
roomId: event.roomId,
selectedTabIndex: event.selectedIndex,
),
);
} }
} }

View File

@ -4,10 +4,14 @@ abstract class TabBarEvent {
const TabBarEvent(); const TabBarEvent();
} }
class TabChanged extends TabBarEvent { class TabBarTabChangedEvent extends TabBarEvent {
const TabBarTabChangedEvent({
required this.selectedIndex,
required this.roomId,
required this.unit,
});
final int selectedIndex; final int selectedIndex;
final String roomId; final String roomId;
final SpaceModel unit; final SpaceModel unit;
const TabChanged(
{required this.selectedIndex, required this.roomId, required this.unit});
} }

View File

@ -2,12 +2,16 @@ abstract class TabBarState {
const TabBarState(); const TabBarState();
} }
class Initial extends TabBarState { class TabBarInitialState extends TabBarState {
const Initial(); const TabBarInitialState();
} }
class TabSelected extends TabBarState { class TabBarTabSelectedState extends TabBarState {
const TabBarTabSelectedState({
required this.roomId,
required this.selectedTabIndex,
});
final int selectedTabIndex; final int selectedTabIndex;
final String roomId; final String roomId;
const TabSelected({required this.roomId, required this.selectedTabIndex});
} }

View File

@ -64,7 +64,7 @@ class _SceneRoomsTabBarDevicesViewState extends State<SceneRoomsTabBarDevicesVie
final index = _tabController.index; final index = _tabController.index;
context.read<TabBarBloc>().add( context.read<TabBarBloc>().add(
TabChanged( TabBarTabChangedEvent(
selectedIndex: index, selectedIndex: index,
roomId: rooms[index].id ?? '', roomId: rooms[index].id ?? '',
unit: selectedSpace, unit: selectedSpace,

View File

@ -31,7 +31,7 @@ class SceneDevicesBody extends StatelessWidget {
SceneDevicesBodyTabBar( SceneDevicesBodyTabBar(
tabController: tabController, tabController: tabController,
rooms: rooms, rooms: rooms,
selectedRoomId: state is TabSelected ? state.roomId : '-1', selectedRoomId: state is TabBarTabSelectedState ? state.roomId : '-1',
), ),
Expanded( Expanded(
child: TabBarView( child: TabBarView(

View File

@ -3,8 +3,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/model/community_model.dart'; import 'package:syncrow_app/features/app_layout/model/community_model.dart';
import 'package:syncrow_app/features/app_layout/model/space_model.dart'; import 'package:syncrow_app/features/app_layout/model/space_model.dart';
import 'package:syncrow_app/features/app_layout/view/app_layout.dart'; import 'package:syncrow_app/features/app_layout/view/app_layout.dart';
import 'package:syncrow_app/features/auth/view/otp_view.dart';
import 'package:syncrow_app/features/auth/view/login_view.dart'; import 'package:syncrow_app/features/auth/view/login_view.dart';
import 'package:syncrow_app/features/auth/view/otp_view.dart';
import 'package:syncrow_app/features/auth/view/sign_up_view.dart'; import 'package:syncrow_app/features/auth/view/sign_up_view.dart';
import 'package:syncrow_app/features/dashboard/view/dashboard_view.dart'; import 'package:syncrow_app/features/dashboard/view/dashboard_view.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_bloc.dart';
@ -17,11 +17,12 @@ import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_bloc.dart'
import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_event.dart'; import 'package:syncrow_app/features/scene/bloc/tab_change/tab_change_event.dart';
import 'package:syncrow_app/features/scene/view/device_functions_view.dart'; import 'package:syncrow_app/features/scene/view/device_functions_view.dart';
import 'package:syncrow_app/features/scene/view/scene_auto_settings.dart'; import 'package:syncrow_app/features/scene/view/scene_auto_settings.dart';
import 'package:syncrow_app/features/scene/view/scene_tasks_view.dart';
import 'package:syncrow_app/features/scene/view/scene_rooms_tabbar.dart'; import 'package:syncrow_app/features/scene/view/scene_rooms_tabbar.dart';
import 'package:syncrow_app/features/scene/view/scene_tasks_view.dart';
import 'package:syncrow_app/features/scene/view/scene_view.dart'; import 'package:syncrow_app/features/scene/view/scene_view.dart';
import 'package:syncrow_app/features/scene/view/smart_automation_select_route.dart'; import 'package:syncrow_app/features/scene/view/smart_automation_select_route.dart';
import 'package:syncrow_app/features/splash/view/splash_view.dart'; import 'package:syncrow_app/features/splash/view/splash_view.dart';
import 'routing_constants.dart'; import 'routing_constants.dart';
class Router { class Router {
@ -88,7 +89,7 @@ class Router {
BlocProvider( BlocProvider(
create: (BuildContext context) => create: (BuildContext context) =>
TabBarBloc(context.read<DeviceManagerBloc>()) TabBarBloc(context.read<DeviceManagerBloc>())
..add(TabChanged( ..add(TabBarTabChangedEvent(
selectedIndex: 0, selectedIndex: 0,
roomId: '-1', roomId: '-1',
unit: SpaceModel( unit: SpaceModel(