push tab to run and handling automation

This commit is contained in:
ashrafzarkanisala
2024-11-24 23:07:03 +03:00
parent 87c47a74ce
commit 0c555cda83
13 changed files with 295 additions and 154 deletions

View File

@ -19,6 +19,7 @@ class StatefulTextField extends StatefulWidget {
this.icon,
this.hintColor,
required this.onChanged,
this.isRequired,
});
final String title;
@ -34,6 +35,7 @@ class StatefulTextField extends StatefulWidget {
final IconData? icon;
final Color? hintColor;
final Function(String)? onChanged;
final bool? isRequired;
@override
State<StatefulTextField> createState() => _StatefulTextFieldState();
@ -62,6 +64,7 @@ class _StatefulTextFieldState extends State<StatefulTextField> {
icon: widget.icon,
hintColor: widget.hintColor,
onChanged: widget.onChanged,
isRequired: widget.isRequired,
);
}
}
@ -82,6 +85,7 @@ class CustomTextField extends StatelessWidget {
this.icon,
this.hintColor,
required this.onChanged,
this.isRequired,
});
final String title;
@ -97,6 +101,7 @@ class CustomTextField extends StatelessWidget {
final IconData? icon;
final Color? hintColor;
final Function(String)? onChanged;
final bool? isRequired;
@override
Widget build(BuildContext context) {
@ -123,6 +128,7 @@ class CustomTextField extends StatelessWidget {
child: TextFormField(
controller: controller,
style: const TextStyle(color: Colors.black),
decoration: InputDecoration(
hintText: hintText,
hintStyle: TextStyle(
@ -140,6 +146,16 @@ class CustomTextField extends StatelessWidget {
onChanged: (value) {
onChanged!(value);
},
/// required validator
validator: (value) {
if (isRequired == true) {
if (value == null || value.isEmpty) {
return 'This field is required';
}
}
return null;
},
),
),
),

View File

@ -88,31 +88,32 @@ class DeviceManagementPage extends StatelessWidget with HelperResponsiveLayout {
);
}),
rightBody: const NavigateHomeGridView(),
scaffoldBody: BlocBuilder<SwitchTabsBloc, SwitchTabsState>(
builder: (context, state) {
if (state is SelectedTabState && state.selectedTab) {
return const RoutinesView();
}
if (state is ShowCreateRoutineState && state.showCreateRoutine) {
return const CreateNewRoutineView();
}
scaffoldBody: CreateNewRoutineView(),
// BlocBuilder<SwitchTabsBloc, SwitchTabsState>(
// builder: (context, state) {
// if (state is SelectedTabState && state.selectedTab) {
// return const RoutinesView();
// }
// if (state is ShowCreateRoutineState && state.showCreateRoutine) {
// return const CreateNewRoutineView();
// }
return BlocBuilder<DeviceManagementBloc, DeviceManagementState>(
builder: (context, deviceState) {
if (deviceState is DeviceManagementLoading) {
return const Center(child: CircularProgressIndicator());
} else if (deviceState is DeviceManagementLoaded ||
deviceState is DeviceManagementFiltered) {
final devices = (deviceState as dynamic).devices ??
(deviceState as DeviceManagementFiltered).filteredDevices;
// return BlocBuilder<DeviceManagementBloc, DeviceManagementState>(
// builder: (context, deviceState) {
// if (deviceState is DeviceManagementLoading) {
// return const Center(child: CircularProgressIndicator());
// } else if (deviceState is DeviceManagementLoaded ||
// deviceState is DeviceManagementFiltered) {
// final devices = (deviceState as dynamic).devices ??
// (deviceState as DeviceManagementFiltered).filteredDevices;
return DeviceManagementBody(devices: devices);
} else {
return const Center(child: Text('Error fetching Devices'));
}
},
);
}),
// return DeviceManagementBody(devices: devices);
// } else {
// return const Center(child: Text('Error fetching Devices'));
// }
// },
// );
// }),
),
);
}

View File

@ -26,6 +26,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
on<SearchRoutines>(_onSearchRoutines);
on<AddSelectedIcon>(_onAddSelectedIcon);
on<CreateSceneEvent>(_onCreateScene);
on<RemoveDragCard>(_onRemoveDragCard);
// on<RemoveFunction>(_onRemoveFunction);
// on<ClearFunctions>(_onClearFunctions);
}
@ -33,7 +34,13 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
void _onAddToIfContainer(AddToIfContainer event, Emitter<RoutineState> emit) {
final updatedIfItems = List<Map<String, dynamic>>.from(state.ifItems)
..add(event.item);
isTabToRun = event.isTabToRun;
if (event.isTabToRun) {
isTabToRun = true;
isAutomation = false;
} else {
isTabToRun = false;
isAutomation = true;
}
emit(state.copyWith(ifItems: updatedIfItems));
}
@ -46,29 +53,26 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
void _onAddFunctionsToRoutine(
AddFunctionToRoutine event, Emitter<RoutineState> emit) {
try {
if (event.functions.isEmpty) return;
final currentSelectedFunctions =
Map<String, List<DeviceFunctionData>>.from(state.selectedFunctions);
if (currentSelectedFunctions.containsKey(event.uniqueCustomId)) {
currentSelectedFunctions[event.uniqueCustomId]!.addAll(event.functions);
currentSelectedFunctions[event.uniqueCustomId] =
List.from(currentSelectedFunctions[event.uniqueCustomId]!)
..addAll(event.functions);
} else {
currentSelectedFunctions[event.uniqueCustomId] = event.functions;
currentSelectedFunctions[event.uniqueCustomId] =
List.from(event.functions);
}
emit(state.copyWith(selectedFunctions: currentSelectedFunctions));
} catch (e) {
debugPrint('Error adding functions: $e');
}
}
// void _onRemoveFunction(RemoveFunction event, Emitter<RoutineState> emit) {
// final functions = List<DeviceFunctionData>.from(state.selectedFunctions)
// ..removeWhere((f) =>
// f.functionCode == event.function.functionCode &&
// f.value == event.function.value);
// emit(state.copyWith(selectedFunctions: functions));
// }
// void _onClearFunctions(ClearFunctions event, Emitter<RoutineState> emit) {
// emit(state.copyWith(selectedFunctions: []));
// }
Future<void> _onLoadScenes(
LoadScenes event, Emitter<RoutineState> emit) async {
@ -83,7 +87,9 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
} catch (e) {
emit(state.copyWith(
isLoading: false,
errorMessage: 'Failed to load scenes',
loadScenesErrorMessage: 'Failed to load scenes',
errorMessage: '',
loadAutomationErrorMessage: '',
));
}
}
@ -101,7 +107,9 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
} catch (e) {
emit(state.copyWith(
isLoading: false,
errorMessage: 'Failed to load automations',
loadAutomationErrorMessage: 'Failed to load automations',
errorMessage: '',
loadScenesErrorMessage: '',
));
}
}
@ -178,10 +186,7 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
final result = await SceneApi.createScene(createSceneModel);
if (result['success']) {
emit(state.copyWith(
isLoading: false,
errorMessage: null,
));
emit(const RoutineState());
} else {
emit(state.copyWith(
isLoading: false,
@ -195,4 +200,18 @@ class RoutineBloc extends Bloc<RoutineEvent, RoutineState> {
));
}
}
FutureOr<void> _onRemoveDragCard(
RemoveDragCard event, Emitter<RoutineState> emit) {
if (event.isFromThen) {
/// remove element from thenItems at specific index
final thenItems = List<Map<String, dynamic>>.from(state.thenItems);
thenItems.removeAt(event.index);
emit(state.copyWith(thenItems: thenItems));
} else {
final ifItems = List<Map<String, dynamic>>.from(state.ifItems);
ifItems.removeAt(event.index);
emit(state.copyWith(ifItems: ifItems));
}
}
}

View File

@ -79,4 +79,12 @@ class CreateSceneEvent extends RoutineEvent {
List<Object> get props => [];
}
class RemoveDragCard extends RoutineEvent {
final int index;
final bool isFromThen;
const RemoveDragCard({required this.index, required this.isFromThen});
@override
List<Object> get props => [index];
}
class ClearFunctions extends RoutineEvent {}

View File

@ -9,6 +9,8 @@ class RoutineState extends Equatable {
final Map<String, List<DeviceFunctionData>> selectedFunctions;
final bool isLoading;
final String? errorMessage;
final String? loadScenesErrorMessage;
final String? loadAutomationErrorMessage;
final String? routineName;
final String? selectedIcon;
@ -23,6 +25,8 @@ class RoutineState extends Equatable {
this.errorMessage,
this.routineName,
this.selectedIcon,
this.loadScenesErrorMessage,
this.loadAutomationErrorMessage,
});
RoutineState copyWith({
@ -35,6 +39,8 @@ class RoutineState extends Equatable {
String? errorMessage,
String? routineName,
String? selectedIcon,
String? loadAutomationErrorMessage,
String? loadScenesErrorMessage,
}) {
return RoutineState(
ifItems: ifItems ?? this.ifItems,
@ -46,6 +52,10 @@ class RoutineState extends Equatable {
errorMessage: errorMessage ?? this.errorMessage,
routineName: routineName ?? this.routineName,
selectedIcon: selectedIcon ?? this.selectedIcon,
loadScenesErrorMessage:
loadScenesErrorMessage ?? this.loadScenesErrorMessage,
loadAutomationErrorMessage:
loadAutomationErrorMessage ?? this.loadAutomationErrorMessage,
);
}
@ -60,5 +70,7 @@ class RoutineState extends Equatable {
errorMessage,
routineName,
selectedIcon,
loadScenesErrorMessage,
loadAutomationErrorMessage,
];
}

View File

@ -96,14 +96,13 @@ class ACHelper {
onConfirm: state.addedFunctions.isNotEmpty
? () {
/// add the functions to the routine bloc
// for (var function in state.addedFunctions) {
context.read<RoutineBloc>().add(
AddFunctionToRoutine(
state.addedFunctions,
uniqueCustomId,
),
);
//}
// Return the device data to be added to the container
Navigator.pop(context, {
'deviceId': functions.first.deviceId,

View File

@ -1,12 +1,10 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/routiens/bloc/functions_bloc/functions_bloc_bloc.dart';
import 'package:syncrow_web/pages/routiens/bloc/routine_bloc/routine_bloc.dart';
import 'package:syncrow_web/pages/routiens/models/device_functions.dart';
import 'package:syncrow_web/pages/routiens/widgets/dialog_footer.dart';
import 'package:syncrow_web/pages/routiens/widgets/dialog_header.dart';
import 'package:syncrow_web/utils/color_manager.dart';
class DelayHelper {
static Future<Map<String, dynamic>?> showDelayPickerDialog(

View File

@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_web/pages/routiens/bloc/routine_bloc/routine_bloc.dart';
import 'package:syncrow_web/pages/routiens/bloc/setting_bloc/setting_bloc.dart';
import 'package:syncrow_web/pages/routiens/bloc/setting_bloc/setting_event.dart';
import 'package:syncrow_web/pages/routiens/bloc/setting_bloc/setting_state.dart';
@ -10,13 +11,14 @@ import 'package:syncrow_web/utils/color_manager.dart';
import 'package:flutter/cupertino.dart';
class SettingHelper {
static Future<String?> showSettingDialog(
{required BuildContext context,
static Future<String?> showSettingDialog({
required BuildContext context,
String? iconId,
required bool isAutomation}) async {
}) async {
return showDialog<String>(
context: context,
builder: (BuildContext context) {
final isAutomation = context.read<RoutineBloc>().isAutomation;
return BlocProvider(
create: (_) =>
SettingBloc()..add(InitialEvent(selectedIcon: iconId ?? '')),

View File

@ -11,6 +11,9 @@ class DraggableCard extends StatelessWidget {
final String title;
final Map<String, dynamic> deviceData;
final EdgeInsetsGeometry? padding;
final void Function()? onRemove;
final bool? isFromThen;
final bool? isFromIf;
const DraggableCard({
super.key,
@ -18,6 +21,9 @@ class DraggableCard extends StatelessWidget {
required this.title,
required this.deviceData,
this.padding,
this.onRemove,
this.isFromThen,
this.isFromIf,
});
@override
@ -44,7 +50,9 @@ class DraggableCard extends StatelessWidget {
Widget _buildCardContent(
BuildContext context, List<DeviceFunctionData> deviceFunctions,
{EdgeInsetsGeometry? padding}) {
return Card(
return Stack(
children: [
Card(
color: ColorsManager.whiteColors,
child: Container(
padding: padding ?? const EdgeInsets.all(16),
@ -113,6 +121,26 @@ class DraggableCard extends StatelessWidget {
],
),
),
),
Positioned(
top: -4,
right: -6,
child: Visibility(
visible: (isFromIf ?? false) || (isFromThen ?? false),
child: IconButton(
onPressed: onRemove == null
? null
: () {
onRemove!();
},
icon: const Icon(
Icons.close,
color: ColorsManager.boxColor,
),
),
),
),
],
);
}

View File

@ -4,6 +4,7 @@ import 'package:syncrow_web/pages/routiens/bloc/routine_bloc/routine_bloc.dart';
import 'package:syncrow_web/pages/routiens/helper/dialog_helper/device_dialog_helper.dart';
import 'package:syncrow_web/pages/routiens/widgets/dragable_card.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:uuid/uuid.dart';
class IfContainer extends StatelessWidget {
const IfContainer({super.key});
@ -20,7 +21,9 @@ class IfContainer extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text('IF', style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
const Text('IF',
style:
TextStyle(fontSize: 18, fontWeight: FontWeight.bold)),
const SizedBox(height: 16),
if (context.read<RoutineBloc>().isTabToRun)
const Row(
@ -37,14 +40,23 @@ class IfContainer extends StatelessWidget {
Wrap(
spacing: 8,
runSpacing: 8,
children: state.ifItems
.map((item) => DraggableCard(
// key: Key(item['key']!),
imagePath: item['imagePath'] ?? '',
title: item['title'] ?? 'Tab to run',
deviceData: item,
))
.toList(),
children: List.generate(
state.ifItems.length,
(index) => DraggableCard(
imagePath:
state.ifItems[index]['imagePath'] ?? '',
title: state.ifItems[index]['title'] ?? '',
deviceData: state.ifItems[index],
padding: const EdgeInsets.symmetric(
horizontal: 4, vertical: 8),
isFromThen: false,
isFromIf: true,
onRemove: () {
context.read<RoutineBloc>().add(
RemoveDragCard(
index: index, isFromThen: false));
},
)),
),
],
),
@ -52,15 +64,52 @@ class IfContainer extends StatelessWidget {
},
onWillAccept: (data) => data != null,
onAccept: (data) async {
// final uniqueCustomId = const Uuid().v4();
// final mutableData = Map<String, dynamic>.from(data);
// mutableData['uniqueCustomId'] = uniqueCustomId;
// if (!context.read<RoutineBloc>().isTabToRun) {
// if (data['deviceId'] == 'tab_to_run') {
// context.read<RoutineBloc>().add(AddToIfContainer(data, true));
// } else {
// final result =
// await DeviceDialogHelper.showDeviceDialog(context, mutableData);
// if (result != null) {
// context
// .read<RoutineBloc>()
// .add(AddToIfContainer(mutableData, false));
// } else if (!['AC', '1G', '2G', '3G']
// .contains(data['productType'])) {
// context
// .read<RoutineBloc>()
// .add(AddToIfContainer(mutableData, false));
// }
// }
//}
final uniqueCustomId = const Uuid().v4();
final mutableData = Map<String, dynamic>.from(data);
mutableData['uniqueCustomId'] = uniqueCustomId;
if (!context.read<RoutineBloc>().isTabToRun) {
if (data['deviceId'] == 'tab_to_run') {
context.read<RoutineBloc>().add(AddToIfContainer(data, true));
if (mutableData['deviceId'] == 'tab_to_run') {
context
.read<RoutineBloc>()
.add(AddToIfContainer(mutableData, true));
} else {
final result = await DeviceDialogHelper.showDeviceDialog(context, data);
final result = await DeviceDialogHelper.showDeviceDialog(
context, mutableData);
if (result != null) {
context.read<RoutineBloc>().add(AddToIfContainer(result, false));
} else if (!['AC', '1G', '2G', '3G'].contains(data['productType'])) {
context.read<RoutineBloc>().add(AddToIfContainer(data, false));
context
.read<RoutineBloc>()
.add(AddToIfContainer(mutableData, false));
} else if (!['AC', '1G', '2G', '3G']
.contains(mutableData['productType'])) {
context
.read<RoutineBloc>()
.add(AddToIfContainer(mutableData, false));
}
}
}

View File

@ -43,6 +43,7 @@ class RoutineSearchAndButtons extends StatelessWidget {
boxDecoration: containerWhiteDecoration,
elevation: 0,
borderRadius: 15,
isRequired: true,
width: 450,
onChanged: (value) {
context
@ -62,9 +63,7 @@ class RoutineSearchAndButtons extends StatelessWidget {
final result =
await SettingHelper.showSettingDialog(
context: context,
isAutomation: context
.read<RoutineBloc>()
.isAutomation);
);
if (result != null) {
context
.read<RoutineBloc>()

View File

@ -32,17 +32,23 @@ class ThenContainer extends StatelessWidget {
Wrap(
spacing: 8,
runSpacing: 8,
children: state.thenItems
.map((item) => DraggableCard(
// key: Key(item['key']!),
imagePath: item['imagePath']!,
title: item['title']!,
deviceData: item,
padding: EdgeInsets.symmetric(
children: List.generate(
state.thenItems.length,
(index) => DraggableCard(
imagePath:
state.thenItems[index]['imagePath'] ?? '',
title: state.thenItems[index]['title'] ?? '',
deviceData: state.thenItems[index],
padding: const EdgeInsets.symmetric(
horizontal: 4, vertical: 8),
))
.toList(),
),
isFromThen: true,
isFromIf: false,
onRemove: () {
context.read<RoutineBloc>().add(
RemoveDragCard(
index: index, isFromThen: true));
},
))),
],
),
),

View File

@ -1,3 +1,4 @@
import 'package:flutter/material.dart';
import 'package:syncrow_web/pages/routiens/models/create_scene/create_scene_model.dart';
import 'package:syncrow_web/pages/routiens/models/icon_model.dart';
import 'package:syncrow_web/pages/routiens/models/routine_model.dart';
@ -11,6 +12,7 @@ class SceneApi {
static Future<Map<String, dynamic>> createScene(
CreateSceneModel createSceneModel) async {
try {
debugPrint('create scene model: ${createSceneModel.toMap()}');
final response = await _httpService.post(
path: ApiEndpoints.createScene,
body: createSceneModel.toMap(),
@ -19,8 +21,10 @@ class SceneApi {
return json;
},
);
debugPrint('create scene response: $response');
return response;
} catch (e) {
debugPrint(e.toString());
rethrow;
}
}