import 'dart:io'; import 'package:easy_localization/easy_localization.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:mohem_flutter_app/api/etqan_ovr_api_client.dart'; import 'package:mohem_flutter_app/classes/utils.dart'; import 'package:mohem_flutter_app/models/etqan_ovr/etqan_get_employee_incident_report.dart'; import 'package:mohem_flutter_app/models/etqan_ovr/etqan_get_requests_model.dart'; import 'package:mohem_flutter_app/models/etqan_ovr/etqan_getprojects_model.dart'; import 'package:speech_to_text/speech_recognition_result.dart'; import 'package:speech_to_text/speech_to_text.dart'; class EtqanOvrProviderModel with ChangeNotifier, DiagnosticableTreeMixin { // Home screen state List? getEtqanProjectsList; List? getEtqanEmployeeRequestsList; List? getEtqanEmployeeIncidnetRequest; bool isLoading = false; String? _ticketId; // Speech to text state final SpeechToText _speechToText = SpeechToText(); bool _speechEnabled = false; bool _isListening = false; // Getters for speech state bool get speechEnabled => _speechEnabled; bool get isListening => _isListening; // Getter for ticketId String get getTicketId => _ticketId ?? ''; // Setter for ticketId void setTicketId(String ticketId) { _ticketId = ticketId; notifyListeners(); } // Create request screen state EtqanGetProjectsResponse? selectedProject; String? selectedEmployeeNumber; File? attachment; String? attachmentBase64; String description = ''; bool isAnonymous = false; /// Initialize speech recognition - call this once when screen loads Future initSpeech() async { _speechEnabled = await _speechToText.initialize( onStatus: (String status) { // Update listening state when speech recognition status changes if (status == 'done' || status == 'notListening') { _isListening = false; notifyListeners(); } }, ); notifyListeners(); } /// Start listening for speech input Future startListening(String localeId, TextEditingController controller) async { await _speechToText.listen( onResult: (SpeechRecognitionResult result) { description = result.recognizedWords; controller.text = result.recognizedWords; controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length)); notifyListeners(); }, localeId: localeId, listenFor: const Duration(seconds: 30), pauseFor: const Duration(seconds: 3), partialResults: true, listenMode: ListenMode.confirmation, ); _isListening = true; notifyListeners(); } /// Stop listening for speech input Future stopListening() async { await _speechToText.stop(); _isListening = false; notifyListeners(); } /// Toggle listening state Future toggleListening(String localeId, TextEditingController controller) async { if (_isListening) { await stopListening(); } else { await startListening(localeId, controller); } } // Fetch Etqan Projects Future fetchEtqanProjects(BuildContext context) async { isLoading = true; notifyListeners(); Utils.showLoading(context); getEtqanProjectsList?.clear(); getEtqanProjectsList = await EtqanApiClient().getEtqanProjects(); Utils.hideLoading(context); isLoading = false; notifyListeners(); } // Fetch Etqan Employee Requests Future fetchEtqanEmployeeRequests(BuildContext context) async { isLoading = true; notifyListeners(); Utils.showLoading(context); getEtqanEmployeeRequestsList?.clear(); getEtqanEmployeeRequestsList = await EtqanApiClient().getEmployeeEtqanRequests(); Utils.hideLoading(context); isLoading = false; notifyListeners(); } // Fetch Etqan Employee Incident Report Future fetchEtqanEmployeeIncidentRequests(BuildContext context, String ticketNo) async { isLoading = true; notifyListeners(); Utils.showLoading(context); getEtqanEmployeeIncidnetRequest?.clear(); getEtqanEmployeeIncidnetRequest = await EtqanApiClient().getEtqanIncidentRequests(ticketNo); Utils.hideLoading(context); isLoading = false; notifyListeners(); } // Set selected project void setSelectedProject(EtqanGetProjectsResponse? project) { selectedProject = project; notifyListeners(); } // Set selected employee number void setSelectedEmployeeNumber(String? employeeNumber) { selectedEmployeeNumber = employeeNumber; notifyListeners(); } // Set attachment with base64 void setAttachment(File file, String base64) { attachment = file; attachmentBase64 = base64; notifyListeners(); } // Remove attachment void removeAttachment() { attachment = null; attachmentBase64 = null; notifyListeners(); } // Set description void setDescription(String value) { description = value; notifyListeners(); } // Validate description bool isDescriptionValid() { return description.trim().isNotEmpty; } // Set isAnonymous void setIsAnonymous(bool value) { isAnonymous = value; notifyListeners(); } // Clear create request form void clearCreateRequestForm() { selectedProject = null; selectedEmployeeNumber = null; attachment = null; attachmentBase64 = null; description = ''; isAnonymous = false; notifyListeners(); } // Submit request Future?> submitRequest(BuildContext context) async { try { Utils.showLoading(context); Map? response = await EtqanApiClient().createRequest(description, selectedProject?.projectId, attachmentBase64, isAnonymous); Utils.hideLoading(context); if (response != null) { getEtqanEmployeeRequestsList = await EtqanApiClient().getEmployeeEtqanRequests(); if (getEtqanEmployeeRequestsList != null) { getEtqanEmployeeRequestsList!.sort((EtqanGetEmployeeOvrRequestsResponse a, EtqanGetEmployeeOvrRequestsResponse b) { if (a.createdDate == null && b.createdDate == null) return 0; if (a.createdDate == null) return 1; if (b.createdDate == null) return -1; try { DateFormat dateFormat = DateFormat("dd-MMM-yyyy hh:mm a"); DateTime dateA = dateFormat.parse(a.createdDate!); DateTime dateB = dateFormat.parse(b.createdDate!); return dateA.compareTo(dateB); } catch (e) { return (b.createdDate ?? '').compareTo(a.createdDate ?? ''); } }); } return response; } return null; } catch (ex) { Utils.hideLoading(context); Utils.handleException(ex, context, null); return null; } } }