diff --git a/lib/features/devices/bloc/sos_bloc/sos_bloc.dart b/lib/features/devices/bloc/sos_bloc/sos_bloc.dart index b41356c..7690953 100644 --- a/lib/features/devices/bloc/sos_bloc/sos_bloc.dart +++ b/lib/features/devices/bloc/sos_bloc/sos_bloc.dart @@ -41,7 +41,6 @@ class SosBloc extends Bloc { on(saveName); on(_toggleUpdate); on(_toggleHelpful); - // on(_toggleWaterLeakAlarm); } final TextEditingController nameController = @@ -49,34 +48,11 @@ class SosBloc extends Bloc { bool isSaving = false; bool editName = false; final FocusNode focusNode = FocusNode(); - Timer? _timer; bool enableAlarm = false; bool closingReminder = false; - bool waterAlarm = false; SosModel deviceStatus = SosModel(sosContactState: 'sos', batteryPercentage: 0); - void _fetchStatus(SosInitial event, Emitter emit) async { - emit(SosLoadingState()); - try { - var response = await DevicesAPI.getDeviceStatus(sosId); - List statusModelList = []; - for (var status in response['status']) { - statusModelList.add(StatusModel.fromJson(status)); - } - deviceStatus = SosModel.fromJson( - statusModelList, - ); - emit(UpdateState(sensor: deviceStatus)); - - Future.delayed(const Duration(milliseconds: 500)); - // _listenToChanges(); - } catch (e) { - emit(SosFailedState(errorMessage: e.toString())); - return; - } - } - DeviceInfoModel deviceInfo = DeviceInfoModel( activeTime: 0, category: "", @@ -110,29 +86,6 @@ class SosBloc extends Bloc { ), ); - void _onSearchFaq(SearchFaqEvent event, Emitter emit) { - emit(SosLoadingState()); - List _faqQuestions = faqQuestions.where((question) { - return question.question - .toLowerCase() - .contains(event.query.toLowerCase()); - }).toList(); - emit(FaqSearchState(filteredFaqQuestions: _faqQuestions)); - } - - void _changeName(ChangeNameEvent event, Emitter emit) { - emit(SosLoadingState()); - editName = event.value!; - if (editName) { - Future.delayed(const Duration(milliseconds: 500), () { - focusNode.requestFocus(); - }); - } else { - focusNode.unfocus(); - } - emit(NameEditingState(editName: editName)); - } - void _toggleLowBattery( ToggleEnableAlarmEvent event, Emitter emit) async { emit(LoadingNewSate(sosSensor: deviceStatus)); @@ -189,6 +142,8 @@ class SosBloc extends Bloc { } } +//========================= Device Info & Status ============================= + static String deviceName = ''; fetchDeviceInfo(SosInitialDeviseInfo event, Emitter emit) async { @@ -203,48 +158,29 @@ class SosBloc extends Bloc { } } - // Demo list of FAQ questions using the QuestionModel class - final List faqQuestions = [ - QuestionModel( - id: 1, - question: 'How does an SOS emergency button work?', - answer: - 'The SOS emergency button sends an alert to your contacts when pressed.', - ), - QuestionModel( - id: 2, - question: 'How long will an SOS alarm persist?', - answer: - 'The SOS alarm will persist until it is manually turned off or after a set time.', - ), - QuestionModel( - id: 3, - question: 'What should I do if the SOS button is unresponsive?', - answer: 'Try restarting the device. If it persists, contact support.', - ), - QuestionModel( - id: 4, - question: 'Can I use the SOS feature without a network connection?', - answer: - 'No, a network connection is required to send the alert to your contacts.', - ), - QuestionModel( - id: 5, - question: 'How often should I check the SOS battery?', - answer: - 'Check the SOS battery at least once a month to ensure it is operational.', - ), - ]; - Future _onSosInitial( - SosInitialQuestion event, Emitter emit) async { + void _fetchStatus(SosInitial event, Emitter emit) async { emit(SosLoadingState()); - // SosModel sosModel = await fetchSosData(sosId); // Define this function as needed - emit(FaqLoadedState(filteredFaqQuestions: faqQuestions)); + try { + var response = await DevicesAPI.getDeviceStatus(sosId); + List statusModelList = []; + for (var status in response['status']) { + statusModelList.add(StatusModel.fromJson(status)); + } + deviceStatus = SosModel.fromJson( + statusModelList, + ); + emit(UpdateState(sensor: deviceStatus)); + + Future.delayed(const Duration(milliseconds: 500)); + } catch (e) { + emit(SosFailedState(errorMessage: e.toString())); + return; + } } +//========================= assign & unassign devise to room ============================= List allDevices = []; List roomsList = []; - void _fetchRoomsAndDevices( FetchRoomsEvent event, Emitter emit) async { try { @@ -279,9 +215,6 @@ class SosBloc extends Bloc { _hasSelectionChanged = false; add(AssignRoomEvent(roomId: roomId, unit: unit, context: event.context)); emit(SaveSelectionSuccessState()); - - // Navigator.pushNamedAndRemoveUntil( - // event.context, Routes.homeRoute, (route) => false); var cubit = HomeCubit.getInstance(); cubit.updatePageIndex(1); Navigator.pushReplacementNamed(event.context, Routes.homeRoute); @@ -290,7 +223,6 @@ class SosBloc extends Bloc { void _assignDevice(AssignRoomEvent event, Emitter emit) async { try { - // Map roomDevicesId = {}; emit(SosLoadingState()); await HomeManagementAPI.assignDeviceToRoom( @@ -339,11 +271,6 @@ class SosBloc extends Bloc { roomDevicesId[e.uuid!] = false; } }); - // emit(FetchDeviceByRoomIdState( - // roomDevices: devicesList, - // allDevices: allDevices, - // roomDevicesId: roomDevicesId, - // roomId: event.roomId)); } catch (e) { emit(const SosFailedState(errorMessage: 'Something went wrong')); return; @@ -352,46 +279,7 @@ class SosBloc extends Bloc { Map> devicesByRoom = {}; -// Your existing function - void _fetchDevicesByRoomId( - FetchDevicesByRoomIdEvent event, Emitter emit) async { - try { - Map roomDevicesId = {}; - emit(SosLoadingState()); - - // Fetch devices for the specified room - final devicesList = await DevicesAPI.getDevicesByRoomId( - communityUuid: event.unit.community.uuid, - spaceUuid: event.unit.id, - roomId: event.roomId); - - // Fetch all devices accessible by the user - allDevices = await HomeManagementAPI.fetchDevicesByUserId(); - - // Map all accessible device IDs - List allDevicesIds = []; - allDevices.forEach((element) { - allDevicesIds.add(element.uuid!); - }); - - // Mark devices as accessible/inaccessible for the room and add to devicesByRoom map - devicesList.forEach((e) { - roomDevicesId[e.uuid!] = allDevicesIds.contains(e.uuid!); - }); - - // Update the devicesByRoom map with the devices in the current room - devicesByRoom[event.roomId] = devicesList; - - // emit(FetchDeviceByRoomIdState( - // roomDevices: devicesList, - // allDevices: allDevices, - // roomDevicesId: roomDevicesId, - // roomId: event.roomId)); - } catch (e) { - emit(const SosFailedState(errorMessage: 'Something went wrong')); - return; - } - } +//======================= setting name ====================================== Future saveName(SaveNameEvent event, Emitter emit) async { if (_validateInputs()) return; @@ -432,6 +320,20 @@ class SosBloc extends Bloc { return null; } + void _changeName(ChangeNameEvent event, Emitter emit) { + emit(SosLoadingState()); + editName = event.value!; + if (editName) { + Future.delayed(const Duration(milliseconds: 500), () { + focusNode.requestFocus(); + }); + } else { + focusNode.unfocus(); + } + emit(NameEditingState(editName: editName)); + } + +//============================== update setting =============================== bool enableUpdate = false; void _toggleUpdate(ToggleUpdateEvent event, Emitter emit) async { @@ -444,8 +346,57 @@ class SosBloc extends Bloc { } } - bool isHelpful = false; + //============================ Question and faq ============================ + final List faqQuestions = [ + QuestionModel( + id: 1, + question: 'How does an SOS emergency button work?', + answer: + 'The SOS emergency button sends an alert to your contacts when pressed.', + ), + QuestionModel( + id: 2, + question: 'How long will an SOS alarm persist?', + answer: + 'The SOS alarm will persist until it is manually turned off or after a set time.', + ), + QuestionModel( + id: 3, + question: 'What should I do if the SOS button is unresponsive?', + answer: 'Try restarting the device. If it persists, contact support.', + ), + QuestionModel( + id: 4, + question: 'Can I use the SOS feature without a network connection?', + answer: + 'No, a network connection is required to send the alert to your contacts.', + ), + QuestionModel( + id: 5, + question: 'How often should I check the SOS battery?', + answer: + 'Check the SOS battery at least once a month to ensure it is operational.', + ), + ]; + + Future _onSosInitial( + SosInitialQuestion event, Emitter emit) async { + emit(SosLoadingState()); + emit(FaqLoadedState(filteredFaqQuestions: faqQuestions)); + } + + void _onSearchFaq(SearchFaqEvent event, Emitter emit) { + emit(SosLoadingState()); + List _faqQuestions = faqQuestions.where((question) { + return question.question + .toLowerCase() + .contains(event.query.toLowerCase()); + }).toList(); + emit(FaqSearchState(filteredFaqQuestions: _faqQuestions)); + } + + bool isHelpful = false; void _toggleHelpful(ToggleHelpfulEvent event, Emitter emit) async { try { emit(SosLoadingState()); @@ -456,6 +407,8 @@ class SosBloc extends Bloc { } } +//===================== delete Device =================================== + deleteDevice(DeleteDeviceEvent event, Emitter emit) async { try { emit(SosLoadingState()); diff --git a/lib/features/devices/bloc/sos_bloc/sos_event.dart b/lib/features/devices/bloc/sos_bloc/sos_event.dart index 6556228..79ad27a 100644 --- a/lib/features/devices/bloc/sos_bloc/sos_event.dart +++ b/lib/features/devices/bloc/sos_bloc/sos_event.dart @@ -11,21 +11,8 @@ abstract class SosEvent extends Equatable { List get props => []; } -class SosLoading extends SosEvent {} -class DeleteDeviceEvent extends SosLoading {} +class DeleteDeviceEvent extends SosEvent {} -class SosSwitch extends SosEvent { - final String switchD; - final String deviceId; - final String productId; - const SosSwitch( - {required this.switchD, this.deviceId = '', this.productId = ''}); - - @override - List get props => [switchD, deviceId, productId]; -} - -class SosUpdated extends SosEvent {} class SosInitialDeviseInfo extends SosEvent {} class SosInitial extends SosEvent { @@ -36,15 +23,6 @@ class ReportLogsInitial extends SosEvent { const ReportLogsInitial(); } -class SosChangeStatus extends SosEvent {} - -class GetCounterEvent extends SosEvent { - final String deviceCode; - const GetCounterEvent({required this.deviceCode}); - @override - List get props => [deviceCode]; -} - class ToggleEnableAlarmEvent extends SosEvent { final bool isLowBatteryEnabled; @@ -63,44 +41,6 @@ class ToggleClosingReminderEvent extends SosEvent { List get props => [isClosingReminderEnabled]; } -class ToggleSosAlarmEvent extends SosEvent { - final bool isSosAlarmEnabled; - - const ToggleSosAlarmEvent(this.isSosAlarmEnabled); - - @override - List get props => [isSosAlarmEnabled]; -} - -class SetCounterValue extends SosEvent { - final Duration duration; - final String deviceCode; - const SetCounterValue({required this.duration, required this.deviceCode}); - @override - List get props => [duration, deviceCode]; -} - -class StartTimer extends SosEvent { - final int duration; - - const StartTimer(this.duration); - - @override - List get props => [duration]; -} - -class TickTimer extends SosEvent { - final int remainingTime; - - const TickTimer(this.remainingTime); - - @override - List get props => [remainingTime]; -} - -class StopTimer extends SosEvent {} - -class OnClose extends SosEvent {} class SaveNameEvent extends SosEvent {} class ChangeNameEvent extends SosEvent { @@ -127,7 +67,6 @@ class FetchRoomsEvent extends SosEvent { List get props => [unit]; } - class SelectOptionEvent extends SosEvent { dynamic selectedOption; SelectOptionEvent({ @@ -160,6 +99,7 @@ class AssignRoomEvent extends SosEvent { context, ]; } + class UnassignRoomEvent extends SosEvent { final String roomId; final String deviceId; @@ -172,25 +112,9 @@ class UnassignRoomEvent extends SosEvent { List get props => [roomId, unit]; } -class FetchDevicesByRoomIdEvent extends SosEvent { - final SpaceModel unit; // Represents the unit (e.g., room or space) context - final String roomId; // Unique identifier for the room - - // Constructor - FetchDevicesByRoomIdEvent({ - required this.unit, - required this.roomId, - }); - - // Adding properties for Equatable to compare - @override - List get props => [unit, roomId]; -} - - class LoadingDeviceInfo extends SosEvent { - DeviceInfoModel deviceInfo; - LoadingDeviceInfo({required this.deviceInfo}); + DeviceInfoModel deviceInfo; + LoadingDeviceInfo({required this.deviceInfo}); @override List get props => [deviceInfo]; @@ -204,4 +128,4 @@ class ToggleUpdateEvent extends SosEvent { class ToggleHelpfulEvent extends SosEvent { final bool? isHelpful; const ToggleHelpfulEvent({this.isHelpful}); -} \ No newline at end of file +} diff --git a/lib/features/devices/bloc/sos_bloc/sos_state.dart b/lib/features/devices/bloc/sos_bloc/sos_state.dart index 3f125cb..cd010cc 100644 --- a/lib/features/devices/bloc/sos_bloc/sos_state.dart +++ b/lib/features/devices/bloc/sos_bloc/sos_state.dart @@ -12,12 +12,8 @@ class SosState extends Equatable { List get props => []; } -class SosInitialState extends SosState {} - class SosLoadingState extends SosState {} - - class SaveState extends SosState {} class LoadingSosDeviceInfo extends SosState { diff --git a/pubspec.yaml b/pubspec.yaml index 5aaacfe..42bd7df 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ description: This is the mobile application project, developed with Flutter for # pub.dev using `flutter pub publish`. This is preferred for private packages. publish_to: "none" # Remove this line if you wish to publish to pub.dev -version: 1.0.8+41 +version: 1.0.9+42 environment: sdk: ">=3.0.6 <4.0.0"