Update: 2026-05-07 18:16:10

This commit is contained in:
Hamza-Ayed
2026-05-07 18:16:10 +03:00
parent 01956fa714
commit 8ee3557109
5 changed files with 976 additions and 155 deletions

View File

@@ -0,0 +1,43 @@
import 'dart:io';
import 'package:dio/dio.dart';
import '../network/dio_client.dart';
import '../utils/logger.dart';
class VoiceAssistantService {
final Dio _dio = DioClient().client;
Future<Map<String, dynamic>> processVoiceCommand(File audioFile) async {
final fileName = audioFile.uri.pathSegments.isNotEmpty
? audioFile.uri.pathSegments.last
: 'voice_command.m4a';
final formData = FormData.fromMap({
'audio': await MultipartFile.fromFile(audioFile.path, filename: fileName),
});
final response = await _dio.post(
'voice/transcribe',
data: formData,
options: Options(contentType: 'multipart/form-data'),
);
if (response.data is! Map) {
throw Exception('استجابة غير متوقعة من الخادم');
}
final body = Map<String, dynamic>.from(response.data as Map);
if (body['success'] != true) {
throw Exception((body['message'] ?? 'فشل تنفيذ الأمر الصوتي').toString());
}
final payload = body['data'];
if (payload is! Map) {
throw Exception('بيانات المساعد الصوتي ناقصة');
}
AppLogger.print('Voice API success: ${body['message']}');
return Map<String, dynamic>.from(payload);
}
}

View File

@@ -1,31 +1,49 @@
import 'dart:async';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';
import 'package:record/record.dart';
import '../../../core/storage/secure_storage.dart';
import '../../../core/network/dio_client.dart';
import '../../../core/services/voice_assistant_service.dart';
import '../../../core/utils/app_snackbar.dart';
import '../../../core/utils/logger.dart';
import '../../../app/routes/app_pages.dart';
class DashboardController extends GetxController {
final SecureStorage _storage = SecureStorage();
final Dio _dio = Dio(BaseOptions(
baseUrl: 'https://musadaq.intaleqapp.com/api/v1',
connectTimeout: const Duration(seconds: 15),
receiveTimeout: const Duration(seconds: 15),
responseType: ResponseType.json,
));
final Dio _dio = DioClient().client;
final VoiceAssistantService _voiceAssistantService = VoiceAssistantService();
final AudioRecorder _audioRecorder = AudioRecorder();
Timer? _recordTimer;
var isLoading = true.obs;
var stats = {}.obs;
var recentActivities = [].obs;
var userRole = ''.obs;
final isVoiceRecording = false.obs;
final isVoiceUploading = false.obs;
final voiceRemainingSeconds = 15.obs;
final voiceStatusText = 'اضغط "ابدأ التسجيل" ثم تحدّث'.obs;
@override
void onInit() {
super.onInit();
_loadDashboardData();
}
@override
void onClose() {
_recordTimer?.cancel();
_audioRecorder.dispose();
super.onClose();
}
Future<void> _loadDashboardData() async {
try {
isLoading.value = true;
@@ -36,17 +54,15 @@ class DashboardController extends GetxController {
return;
}
_dio.options.headers['Authorization'] = 'Bearer $token';
// Fetch Stats
final statsResponse = await _dio.get('/dashboard/stats');
final statsResponse = await _dio.get('dashboard/stats');
if (statsResponse.data['success'] == true) {
stats.value = statsResponse.data['data'];
userRole.value = statsResponse.data['data']['role'] ?? '';
}
// Fetch Recent Activity
final activityResponse = await _dio.get('/dashboard/recent-activity');
final activityResponse = await _dio.get('dashboard/recent-activity');
if (activityResponse.data['success'] == true) {
recentActivities.value = activityResponse.data['data'];
}
@@ -71,133 +87,306 @@ class DashboardController extends GetxController {
}
void startVoiceAssistant() {
final textController = TextEditingController();
_resetVoiceState();
Get.bottomSheet(
Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: Get.isDarkMode ? const Color(0xFF1E1E2E) : Colors.white,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(30), topRight: Radius.circular(30)),
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 40,
height: 4,
margin: const EdgeInsets.only(bottom: 20),
decoration: BoxDecoration(
color: Colors.grey.withOpacity(0.3),
borderRadius: BorderRadius.circular(2)),
Obx(
() => Container(
padding: const EdgeInsets.all(24),
decoration: BoxDecoration(
color: Get.isDarkMode ? const Color(0xFF1E1E2E) : Colors.white,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(30),
topRight: Radius.circular(30),
),
Row(
children: [
const Icon(Icons.auto_awesome, color: Color(0xFF5EEAD4)),
const SizedBox(width: 12),
Text(
'المساعد الذكي (Grok-Beta)',
style: TextStyle(
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Container(
width: 40,
height: 4,
margin: const EdgeInsets.only(bottom: 18),
decoration: BoxDecoration(
color: Colors.grey.withValues(alpha: 0.3),
borderRadius: BorderRadius.circular(2),
),
),
Row(
children: [
const Icon(Icons.auto_awesome, color: Color(0xFF5EEAD4)),
const SizedBox(width: 12),
Text(
'المساعد الصوتي (Gemini)',
style: TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Get.isDarkMode
? Colors.white
: const Color(0xFF0F4C81)),
),
],
),
const SizedBox(height: 20),
TextField(
autofocus: true,
controller: textController,
style: TextStyle(
color: Get.isDarkMode ? Colors.white : Colors.black),
decoration: InputDecoration(
hintText: 'اكتب أمرك هنا (مثلاً: أريد رؤية الفواتير)...',
hintStyle: const TextStyle(fontSize: 14, color: Colors.grey),
filled: true,
fillColor: Get.isDarkMode
? const Color(0xFF1A1A2E)
: const Color(0xFFF1F5F9),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
borderSide: BorderSide.none),
prefixIcon: const Icon(Icons.mic_none_rounded, color: Colors.grey),
suffixIcon: IconButton(
icon:
const Icon(Icons.send_rounded, color: Color(0xFF0F4C81)),
onPressed: () => _handleVoiceCommand(textController.text),
: const Color(0xFF0F4C81),
),
),
],
),
const SizedBox(height: 18),
Text(
voiceStatusText.value,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 13,
color: Get.isDarkMode ? Colors.white70 : Colors.black87,
),
),
onSubmitted: (val) => _handleVoiceCommand(val),
),
const SizedBox(height: 12),
Text(
'ملاحظة: جاري تفعيل ميزة التعرف الصوتي المباشر...',
style:
TextStyle(fontSize: 11, color: Colors.grey.withOpacity(0.7)),
),
const SizedBox(height: 20),
],
const SizedBox(height: 14),
Text(
'${voiceRemainingSeconds.value}s',
style: TextStyle(
fontFamily: 'monospace',
fontWeight: FontWeight.bold,
fontSize: 18,
color: isVoiceRecording.value
? const Color(0xFFDC2626)
: const Color(0xFF0F4C81),
),
),
const SizedBox(height: 12),
ClipRRect(
borderRadius: BorderRadius.circular(6),
child: LinearProgressIndicator(
value: (15 - voiceRemainingSeconds.value) / 15,
minHeight: 8,
color: isVoiceRecording.value
? const Color(0xFFDC2626)
: const Color(0xFF0F4C81),
backgroundColor:
Get.isDarkMode ? Colors.white10 : const Color(0xFFE2E8F0),
),
),
const SizedBox(height: 20),
Row(
children: [
Expanded(
child: OutlinedButton.icon(
onPressed: isVoiceUploading.value
? null
: () async {
if (isVoiceRecording.value) {
await _stopAndUploadVoice(autoStopped: false);
} else {
await _startVoiceRecording();
}
},
icon: Icon(
isVoiceRecording.value
? Icons.stop_circle_outlined
: Icons.mic_none_rounded,
),
label: Text(
isVoiceRecording.value
? 'إيقاف وإرسال'
: 'ابدأ التسجيل',
),
style: OutlinedButton.styleFrom(
foregroundColor: isVoiceRecording.value
? const Color(0xFFDC2626)
: const Color(0xFF0F4C81),
side: BorderSide(
color: isVoiceRecording.value
? const Color(0xFFDC2626)
: const Color(0xFF0F4C81),
),
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
),
const SizedBox(width: 12),
Expanded(
child: ElevatedButton.icon(
onPressed:
isVoiceRecording.value || isVoiceUploading.value
? null
: () => Get.back(),
icon: const Icon(Icons.close),
label: const Text('إغلاق'),
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF0F4C81),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 14),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
),
),
],
),
if (isVoiceUploading.value) ...[
const SizedBox(height: 14),
const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2),
),
],
],
),
),
),
isScrollControlled: true,
isDismissible: false,
);
}
Future<void> _handleVoiceCommand(String text) async {
if (text.trim().isEmpty) return;
Future<void> _startVoiceRecording() async {
if (isVoiceUploading.value) return;
Get.back(); // Close bottom sheet
AppSnackbar.showWarning('جاري التحليل...', 'يتم تحليل طلبك بواسطة Grok AI');
final hasPermission = await _audioRecorder.hasPermission();
if (!hasPermission) {
AppSnackbar.showError('صلاحيات', 'الرجاء السماح بالوصول للميكروفون');
return;
}
final tempDir = await getTemporaryDirectory();
final path =
'${tempDir.path}/voice_${DateTime.now().millisecondsSinceEpoch}.m4a';
await _audioRecorder.start(
const RecordConfig(
encoder: AudioEncoder.aacLc,
sampleRate: 16000,
numChannels: 1,
bitRate: 64000,
),
path: path,
);
isVoiceRecording.value = true;
voiceRemainingSeconds.value = 15;
voiceStatusText.value = 'جاري التسجيل... تكلم الآن';
_startVoiceCountdown();
}
void _startVoiceCountdown() {
_recordTimer?.cancel();
_recordTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
final next = voiceRemainingSeconds.value - 1;
voiceRemainingSeconds.value = next;
if (next <= 0) {
timer.cancel();
_stopAndUploadVoice(autoStopped: true);
}
});
}
Future<void> _stopAndUploadVoice({required bool autoStopped}) async {
if (!isVoiceRecording.value) return;
_recordTimer?.cancel();
isVoiceRecording.value = false;
voiceStatusText.value = autoStopped
? 'تم الإيقاف التلقائي، جاري الإرسال...'
: 'جاري الإرسال...';
final recordPath = await _audioRecorder.stop();
if (recordPath == null || recordPath.isEmpty) {
voiceStatusText.value = 'فشل حفظ التسجيل الصوتي';
AppSnackbar.showError('خطأ', 'فشل حفظ التسجيل الصوتي');
return;
}
await _uploadVoiceCommand(File(recordPath));
}
Future<void> _uploadVoiceCommand(File audioFile) async {
isVoiceUploading.value = true;
try {
final token = await _storage.getToken();
final response = await _dio.post(
'/voice/parse-intent-grok',
data: {'text': text},
options: Options(headers: {'Authorization': 'Bearer $token'}),
final result =
await _voiceAssistantService.processVoiceCommand(audioFile);
final intent = Map<String, dynamic>.from(
(result['intent'] as Map?) ?? <String, dynamic>{},
);
final execution = Map<String, dynamic>.from(
(result['execution'] as Map?) ?? <String, dynamic>{},
);
if (response.data['success'] == true) {
final action = response.data['data']['action'];
final params = response.data['data']['params'];
final confirmation = response.data['data']['confirmation'] ?? 'تم!';
final action = (intent['action'] ?? '').toString();
final params = Map<String, dynamic>.from(
(intent['params'] as Map?) ?? <String, dynamic>{},
);
final confirmation = (intent['confirmation'] ??
execution['message'] ??
'تم تنفيذ الأمر الصوتي')
.toString();
final executionStatus = (execution['status'] ?? 'unknown').toString();
AppSnackbar.showSuccess('تم!', confirmation);
_executeAction(action, params);
} else {
AppSnackbar.showSuccess('المساعد الصوتي', confirmation);
if (executionStatus == 'failed') {
AppSnackbar.showError(
'خطأ', response.data['message'] ?? 'فشل تحليل الطلب');
'فشل التنفيذ',
(execution['message'] ?? 'تعذر تنفيذ الأمر داخليًا').toString(),
);
} else {
_executeAction(action, params);
}
} on DioException catch (e) {
AppLogger.error('Voice Assistant Error', e);
String errorMsg = 'حدث خطأ أثناء التواصل مع خادم الذكاء الاصطناعي';
AppLogger.error('Voice upload error', e.response?.data ?? e.message);
String errorMsg = 'تعذر إرسال التسجيل الصوتي';
if (e.response != null && e.response?.data != null) {
if (e.response?.data is Map && e.response?.data['message'] != null) {
errorMsg = e.response?.data['message'];
}
}
AppSnackbar.showError('خطأ', errorMsg);
} catch (e) {
AppLogger.error('Voice Assistant Error', e);
} catch (e, stack) {
AppLogger.error('Voice Assistant Error', e, stack);
AppSnackbar.showError('خطأ', 'حدث خطأ غير متوقع');
} finally {
isVoiceUploading.value = false;
voiceStatusText.value = 'جاهز لأمر صوتي جديد';
// Close voice sheet if still open.
if (Get.isBottomSheetOpen == true) {
Get.back();
}
}
}
void _executeAction(String action, dynamic params) {
switch (action) {
case 'list_invoices':
Get.toNamed(AppRoutes.INVOICES);
case 'search_invoice':
Get.toNamed(AppRoutes.MAIN);
AppSnackbar.showWarning(
'معلومة', 'تم فتح الواجهة الرئيسية؛ عرض نتائج تفصيلي قادم');
break;
case 'open_scanner':
Get.toNamed(AppRoutes.SCANNER);
break;
case 'navigate':
final screen = params['screen']?.toString().toLowerCase();
if (screen == 'settings') Get.toNamed(AppRoutes.SETTINGS);
if (screen == 'dashboard') Get.back();
final screen =
(params is Map ? params['screen'] : null)?.toString().toLowerCase();
if (screen == 'settings') {
Get.toNamed(AppRoutes.MAIN);
} else if (screen == 'scanner') {
Get.toNamed(AppRoutes.SCANNER);
} else {
Get.toNamed(AppRoutes.MAIN);
}
break;
case 'check_quota':
case 'check_status':
case 'get_report':
case 'export_pdf':
AppSnackbar.showWarning(
'تم التنفيذ',
'تم تنفيذ الأمر على الخادم وسيتم تحسين عرض النتائج داخل التطبيق قريباً',
);
break;
default:
AppSnackbar.showWarning(
@@ -205,6 +394,14 @@ class DashboardController extends GetxController {
}
}
void _resetVoiceState() {
_recordTimer?.cancel();
isVoiceRecording.value = false;
isVoiceUploading.value = false;
voiceRemainingSeconds.value = 15;
voiceStatusText.value = 'اضغط "ابدأ التسجيل" ثم تحدّث';
}
void refreshData() {
_loadDashboardData();
}

View File

@@ -181,10 +181,10 @@ packages:
dependency: transitive
description:
name: characters
sha256: faf38497bda5ead2a8c7615f4f7939df04333478bf32e4173fcb06d428b5716b
sha256: f71061c654a3380576a52b451dd5532377954cf9dbd272a78fc8479606670803
url: "https://pub.dev"
source: hosted
version: "1.4.1"
version: "1.4.0"
checked_yaml:
dependency: transitive
description:
@@ -748,26 +748,26 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "33e2e26bdd85a0112ec15400c8cbffea70d0f9c3407491f672a2fad47915e2de"
sha256: "6bb818ecbdffe216e81182c2f0714a2e62b593f4a4f13098713ff1685dfb6ab0"
url: "https://pub.dev"
source: hosted
version: "11.0.2"
version: "10.0.9"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "1dbc140bb5a23c75ea9c4811222756104fbcd1a27173f0c34ca01e16bea473c1"
sha256: f8b613e7e6a13ec79cfdc0e97638fddb3ab848452eff057653abd3edba760573
url: "https://pub.dev"
source: hosted
version: "3.0.10"
version: "3.0.9"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: "8d5a2d49f4a66b49744b23b018848400d23e54caf9463f4eb20df3eb8acb2eb1"
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
url: "https://pub.dev"
source: hosted
version: "3.0.2"
version: "3.0.1"
lints:
dependency: transitive
description:
@@ -836,18 +836,18 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "12956d0ad8390bbcc63ca2e1469c0619946ccb52809807067a7020d57e647aa6"
sha256: dc58c723c3c24bf8d3e2d3ad3f2f9d7bd9cf43ec6feaa64181775e60190153f2
url: "https://pub.dev"
source: hosted
version: "0.12.18"
version: "0.12.17"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9c337007e82b1889149c82ed242ed1cb24a66044e30979c44912381e9be4c48b"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.13.0"
version: "0.11.1"
matrix2d:
dependency: transitive
description:
@@ -860,10 +860,10 @@ packages:
dependency: transitive
description:
name: meta
sha256: "23f08335362185a5ea2ad3a4e597f1375e78bce8a040df5c600c8d3552ef2394"
sha256: e3641ec5d63ebf0d9b41bd43201a66e3fc79a65db5f61fc181f04cd27aab950c
url: "https://pub.dev"
source: hosted
version: "1.17.0"
version: "1.16.0"
mime:
dependency: transitive
description:
@@ -1152,6 +1152,70 @@ packages:
url: "https://pub.dev"
source: hosted
version: "3.0.2"
record:
dependency: "direct main"
description:
name: record
sha256: d5b6b334f3ab02460db6544e08583c942dbf23e3504bf1e14fd4cbe3d9409277
url: "https://pub.dev"
source: hosted
version: "6.2.0"
record_android:
dependency: transitive
description:
name: record_android
sha256: "94783f08403aed33ffb68797bf0715b0812eb852f3c7985644c945faea462ba1"
url: "https://pub.dev"
source: hosted
version: "1.5.1"
record_ios:
dependency: transitive
description:
name: record_ios
sha256: "8df7c136131bd05efc19256af29b2ba6ccc000ccc2c80d4b6b6d7a8d21a3b5a9"
url: "https://pub.dev"
source: hosted
version: "1.2.0"
record_linux:
dependency: transitive
description:
name: record_linux
sha256: c31a35cc158cd666fc6395f7f56fc054f31685571684be6b97670a27649ce5c7
url: "https://pub.dev"
source: hosted
version: "1.3.0"
record_macos:
dependency: transitive
description:
name: record_macos
sha256: "084902e63fc9c0c224c29203d6c75f0bdf9b6a40536c9d916393c8f4c4256488"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
record_platform_interface:
dependency: transitive
description:
name: record_platform_interface
sha256: "8a81dbc4e14e1272a285bbfef6c9136d070a47d9b0d1f40aa6193516253ee2f6"
url: "https://pub.dev"
source: hosted
version: "1.5.0"
record_web:
dependency: transitive
description:
name: record_web
sha256: "7e9846981c1f2d111d86f0ae3309071f5bba8b624d1c977316706f08fc31d16d"
url: "https://pub.dev"
source: hosted
version: "1.3.0"
record_windows:
dependency: transitive
description:
name: record_windows
sha256: "223258060a1d25c62bae18282c16783f28581ec19401d17e56b5205b9f039d78"
url: "https://pub.dev"
source: hosted
version: "1.0.7"
rxdart:
dependency: transitive
description:
@@ -1321,10 +1385,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "93167629bfc610f71560ab9312acdda4959de4df6fac7492c89ff0d3886f6636"
sha256: fb31f383e2ee25fbbfe06b40fe21e1e458d14080e3c67e7ba0acfde4df4e0bbd
url: "https://pub.dev"
source: hosted
version: "0.7.9"
version: "0.7.4"
timing:
dependency: transitive
description:
@@ -1353,10 +1417,10 @@ packages:
dependency: transitive
description:
name: vector_math
sha256: d530bd74fea330e6e364cda7a85019c434070188383e1cd8d9777ee586914c5b
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
url: "https://pub.dev"
source: hosted
version: "2.2.0"
version: "2.1.4"
vm_service:
dependency: transitive
description:
@@ -1438,5 +1502,5 @@ packages:
source: hosted
version: "3.1.3"
sdks:
dart: ">=3.9.0-0 <4.0.0"
dart: ">=3.8.0 <4.0.0"
flutter: ">=3.32.0"

View File

@@ -42,6 +42,7 @@ dependencies:
# ─── Voice & Audio ──────────────────────────────────
speech_to_text: ^7.3.0
record: ^6.2.0
permission_handler: ^11.3.0
# ─── Connectivity & Background ──────────────────────