This commit is contained in:
mohammad
2024-11-25 12:47:13 +03:00
parent 7ffc67651c
commit c5474c1b10
4 changed files with 93 additions and 220 deletions

View File

@ -41,7 +41,6 @@ class SosBloc extends Bloc<SosEvent, SosState> {
on<SaveNameEvent>(saveName); on<SaveNameEvent>(saveName);
on<ToggleUpdateEvent>(_toggleUpdate); on<ToggleUpdateEvent>(_toggleUpdate);
on<ToggleHelpfulEvent>(_toggleHelpful); on<ToggleHelpfulEvent>(_toggleHelpful);
// on<ToggleWaterLeakAlarmEvent>(_toggleWaterLeakAlarm);
} }
final TextEditingController nameController = final TextEditingController nameController =
@ -49,34 +48,11 @@ class SosBloc extends Bloc<SosEvent, SosState> {
bool isSaving = false; bool isSaving = false;
bool editName = false; bool editName = false;
final FocusNode focusNode = FocusNode(); final FocusNode focusNode = FocusNode();
Timer? _timer;
bool enableAlarm = false; bool enableAlarm = false;
bool closingReminder = false; bool closingReminder = false;
bool waterAlarm = false;
SosModel deviceStatus = SosModel deviceStatus =
SosModel(sosContactState: 'sos', batteryPercentage: 0); SosModel(sosContactState: 'sos', batteryPercentage: 0);
void _fetchStatus(SosInitial event, Emitter<SosState> emit) async {
emit(SosLoadingState());
try {
var response = await DevicesAPI.getDeviceStatus(sosId);
List<StatusModel> 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( DeviceInfoModel deviceInfo = DeviceInfoModel(
activeTime: 0, activeTime: 0,
category: "", category: "",
@ -110,29 +86,6 @@ class SosBloc extends Bloc<SosEvent, SosState> {
), ),
); );
void _onSearchFaq(SearchFaqEvent event, Emitter<SosState> emit) {
emit(SosLoadingState());
List<QuestionModel> _faqQuestions = faqQuestions.where((question) {
return question.question
.toLowerCase()
.contains(event.query.toLowerCase());
}).toList();
emit(FaqSearchState(filteredFaqQuestions: _faqQuestions));
}
void _changeName(ChangeNameEvent event, Emitter<SosState> 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( void _toggleLowBattery(
ToggleEnableAlarmEvent event, Emitter<SosState> emit) async { ToggleEnableAlarmEvent event, Emitter<SosState> emit) async {
emit(LoadingNewSate(sosSensor: deviceStatus)); emit(LoadingNewSate(sosSensor: deviceStatus));
@ -189,6 +142,8 @@ class SosBloc extends Bloc<SosEvent, SosState> {
} }
} }
//========================= Device Info & Status =============================
static String deviceName = ''; static String deviceName = '';
fetchDeviceInfo(SosInitialDeviseInfo event, Emitter<SosState> emit) async { fetchDeviceInfo(SosInitialDeviseInfo event, Emitter<SosState> emit) async {
@ -203,48 +158,29 @@ class SosBloc extends Bloc<SosEvent, SosState> {
} }
} }
// Demo list of FAQ questions using the QuestionModel class void _fetchStatus(SosInitial event, Emitter<SosState> emit) async {
final List<QuestionModel> 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<void> _onSosInitial(
SosInitialQuestion event, Emitter<SosState> emit) async {
emit(SosLoadingState()); emit(SosLoadingState());
// SosModel sosModel = await fetchSosData(sosId); // Define this function as needed try {
emit(FaqLoadedState(filteredFaqQuestions: faqQuestions)); var response = await DevicesAPI.getDeviceStatus(sosId);
List<StatusModel> 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<DeviceModel> allDevices = []; List<DeviceModel> allDevices = [];
List<SubSpaceModel> roomsList = []; List<SubSpaceModel> roomsList = [];
void _fetchRoomsAndDevices( void _fetchRoomsAndDevices(
FetchRoomsEvent event, Emitter<SosState> emit) async { FetchRoomsEvent event, Emitter<SosState> emit) async {
try { try {
@ -279,9 +215,6 @@ class SosBloc extends Bloc<SosEvent, SosState> {
_hasSelectionChanged = false; _hasSelectionChanged = false;
add(AssignRoomEvent(roomId: roomId, unit: unit, context: event.context)); add(AssignRoomEvent(roomId: roomId, unit: unit, context: event.context));
emit(SaveSelectionSuccessState()); emit(SaveSelectionSuccessState());
// Navigator.pushNamedAndRemoveUntil(
// event.context, Routes.homeRoute, (route) => false);
var cubit = HomeCubit.getInstance(); var cubit = HomeCubit.getInstance();
cubit.updatePageIndex(1); cubit.updatePageIndex(1);
Navigator.pushReplacementNamed(event.context, Routes.homeRoute); Navigator.pushReplacementNamed(event.context, Routes.homeRoute);
@ -290,7 +223,6 @@ class SosBloc extends Bloc<SosEvent, SosState> {
void _assignDevice(AssignRoomEvent event, Emitter<SosState> emit) async { void _assignDevice(AssignRoomEvent event, Emitter<SosState> emit) async {
try { try {
// Map<String, bool> roomDevicesId = {};
emit(SosLoadingState()); emit(SosLoadingState());
await HomeManagementAPI.assignDeviceToRoom( await HomeManagementAPI.assignDeviceToRoom(
@ -339,11 +271,6 @@ class SosBloc extends Bloc<SosEvent, SosState> {
roomDevicesId[e.uuid!] = false; roomDevicesId[e.uuid!] = false;
} }
}); });
// emit(FetchDeviceByRoomIdState(
// roomDevices: devicesList,
// allDevices: allDevices,
// roomDevicesId: roomDevicesId,
// roomId: event.roomId));
} catch (e) { } catch (e) {
emit(const SosFailedState(errorMessage: 'Something went wrong')); emit(const SosFailedState(errorMessage: 'Something went wrong'));
return; return;
@ -352,46 +279,7 @@ class SosBloc extends Bloc<SosEvent, SosState> {
Map<String, List<DeviceModel>> devicesByRoom = {}; Map<String, List<DeviceModel>> devicesByRoom = {};
// Your existing function //======================= setting name ======================================
void _fetchDevicesByRoomId(
FetchDevicesByRoomIdEvent event, Emitter<SosState> emit) async {
try {
Map<String, bool> 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<String> 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;
}
}
Future<void> saveName(SaveNameEvent event, Emitter<SosState> emit) async { Future<void> saveName(SaveNameEvent event, Emitter<SosState> emit) async {
if (_validateInputs()) return; if (_validateInputs()) return;
@ -432,6 +320,20 @@ class SosBloc extends Bloc<SosEvent, SosState> {
return null; return null;
} }
void _changeName(ChangeNameEvent event, Emitter<SosState> 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; bool enableUpdate = false;
void _toggleUpdate(ToggleUpdateEvent event, Emitter<SosState> emit) async { void _toggleUpdate(ToggleUpdateEvent event, Emitter<SosState> emit) async {
@ -444,8 +346,57 @@ class SosBloc extends Bloc<SosEvent, SosState> {
} }
} }
bool isHelpful = false; //============================ Question and faq ============================
final List<QuestionModel> 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<void> _onSosInitial(
SosInitialQuestion event, Emitter<SosState> emit) async {
emit(SosLoadingState());
emit(FaqLoadedState(filteredFaqQuestions: faqQuestions));
}
void _onSearchFaq(SearchFaqEvent event, Emitter<SosState> emit) {
emit(SosLoadingState());
List<QuestionModel> _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<SosState> emit) async { void _toggleHelpful(ToggleHelpfulEvent event, Emitter<SosState> emit) async {
try { try {
emit(SosLoadingState()); emit(SosLoadingState());
@ -456,6 +407,8 @@ class SosBloc extends Bloc<SosEvent, SosState> {
} }
} }
//===================== delete Device ===================================
deleteDevice(DeleteDeviceEvent event, Emitter<SosState> emit) async { deleteDevice(DeleteDeviceEvent event, Emitter<SosState> emit) async {
try { try {
emit(SosLoadingState()); emit(SosLoadingState());

View File

@ -11,21 +11,8 @@ abstract class SosEvent extends Equatable {
List<Object> get props => []; List<Object> get props => [];
} }
class SosLoading extends SosEvent {} class DeleteDeviceEvent extends SosEvent {}
class DeleteDeviceEvent extends SosLoading {}
class SosSwitch extends SosEvent {
final String switchD;
final String deviceId;
final String productId;
const SosSwitch(
{required this.switchD, this.deviceId = '', this.productId = ''});
@override
List<Object> get props => [switchD, deviceId, productId];
}
class SosUpdated extends SosEvent {}
class SosInitialDeviseInfo extends SosEvent {} class SosInitialDeviseInfo extends SosEvent {}
class SosInitial extends SosEvent { class SosInitial extends SosEvent {
@ -36,15 +23,6 @@ class ReportLogsInitial extends SosEvent {
const ReportLogsInitial(); const ReportLogsInitial();
} }
class SosChangeStatus extends SosEvent {}
class GetCounterEvent extends SosEvent {
final String deviceCode;
const GetCounterEvent({required this.deviceCode});
@override
List<Object> get props => [deviceCode];
}
class ToggleEnableAlarmEvent extends SosEvent { class ToggleEnableAlarmEvent extends SosEvent {
final bool isLowBatteryEnabled; final bool isLowBatteryEnabled;
@ -63,44 +41,6 @@ class ToggleClosingReminderEvent extends SosEvent {
List<Object> get props => [isClosingReminderEnabled]; List<Object> get props => [isClosingReminderEnabled];
} }
class ToggleSosAlarmEvent extends SosEvent {
final bool isSosAlarmEnabled;
const ToggleSosAlarmEvent(this.isSosAlarmEnabled);
@override
List<Object> get props => [isSosAlarmEnabled];
}
class SetCounterValue extends SosEvent {
final Duration duration;
final String deviceCode;
const SetCounterValue({required this.duration, required this.deviceCode});
@override
List<Object> get props => [duration, deviceCode];
}
class StartTimer extends SosEvent {
final int duration;
const StartTimer(this.duration);
@override
List<Object> get props => [duration];
}
class TickTimer extends SosEvent {
final int remainingTime;
const TickTimer(this.remainingTime);
@override
List<Object> get props => [remainingTime];
}
class StopTimer extends SosEvent {}
class OnClose extends SosEvent {}
class SaveNameEvent extends SosEvent {} class SaveNameEvent extends SosEvent {}
class ChangeNameEvent extends SosEvent { class ChangeNameEvent extends SosEvent {
@ -127,7 +67,6 @@ class FetchRoomsEvent extends SosEvent {
List<Object> get props => [unit]; List<Object> get props => [unit];
} }
class SelectOptionEvent extends SosEvent { class SelectOptionEvent extends SosEvent {
dynamic selectedOption; dynamic selectedOption;
SelectOptionEvent({ SelectOptionEvent({
@ -160,6 +99,7 @@ class AssignRoomEvent extends SosEvent {
context, context,
]; ];
} }
class UnassignRoomEvent extends SosEvent { class UnassignRoomEvent extends SosEvent {
final String roomId; final String roomId;
final String deviceId; final String deviceId;
@ -172,25 +112,9 @@ class UnassignRoomEvent extends SosEvent {
List<Object> get props => [roomId, unit]; List<Object> 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<Object> get props => [unit, roomId];
}
class LoadingDeviceInfo extends SosEvent { class LoadingDeviceInfo extends SosEvent {
DeviceInfoModel deviceInfo; DeviceInfoModel deviceInfo;
LoadingDeviceInfo({required this.deviceInfo}); LoadingDeviceInfo({required this.deviceInfo});
@override @override
List<Object> get props => [deviceInfo]; List<Object> get props => [deviceInfo];
@ -204,4 +128,4 @@ class ToggleUpdateEvent extends SosEvent {
class ToggleHelpfulEvent extends SosEvent { class ToggleHelpfulEvent extends SosEvent {
final bool? isHelpful; final bool? isHelpful;
const ToggleHelpfulEvent({this.isHelpful}); const ToggleHelpfulEvent({this.isHelpful});
} }

View File

@ -12,12 +12,8 @@ class SosState extends Equatable {
List<Object> get props => []; List<Object> get props => [];
} }
class SosInitialState extends SosState {}
class SosLoadingState extends SosState {} class SosLoadingState extends SosState {}
class SaveState extends SosState {} class SaveState extends SosState {}
class LoadingSosDeviceInfo extends SosState { class LoadingSosDeviceInfo extends SosState {

View File

@ -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. # 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 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: environment:
sdk: ">=3.0.6 <4.0.0" sdk: ">=3.0.6 <4.0.0"