127 lines
5.7 KiB
Dart
127 lines
5.7 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import '../../data/dashboard_repository.dart';
|
|
import 'dashboard_state.dart';
|
|
|
|
// English: DashboardCubit manages the active tab and data fetching.
|
|
// Arabic: يدير ملف الكيوبيت الباب النشط وجلب البيانات المخصصة له.
|
|
//
|
|
// English: In the web client, AlpineJS switches active tabs and calls fetch functions.
|
|
// Arabic: في عميل الويب، تبدل مكتبة ألبين الباب النشط وتستدعي دوال الجلب.
|
|
//
|
|
// English: Here, this Cubit replicates that behavior by capturing changes and emitting updated states.
|
|
// Arabic: هنا، يكرر هذا الكيوبيت هذا السلوك من خلال رصد التغييرات وإرسال حالات محدثة.
|
|
class DashboardCubit extends Cubit<DashboardState> {
|
|
final DashboardRepository _repository;
|
|
|
|
DashboardCubit(this._repository)
|
|
: super(const DashboardState(
|
|
activeTab: DashboardTab.whatsapp,
|
|
isLoading: false,
|
|
));
|
|
|
|
// English: Switch the active dashboard view and fetch the required API data.
|
|
// Arabic: تبديل عرض لوحة التحكم النشط وجلب بيانات واجهة برمجة التطبيقات المطلوبة.
|
|
Future<void> changeTab(DashboardTab tab) async {
|
|
// English: Update tab state first to give instant visual feedback in UI.
|
|
// Arabic: تحديث حالة الباب أولاً لتقديم استجابة مرئية فورية في الواجهة.
|
|
emit(state.copyWith(activeTab: tab, isLoading: true));
|
|
|
|
try {
|
|
switch (tab) {
|
|
case DashboardTab.whatsapp:
|
|
final status = await _repository.getWhatsAppStatus();
|
|
emit(state.copyWith(whatsappStatus: status, isLoading: false));
|
|
break;
|
|
case DashboardTab.billing:
|
|
final plans = await _repository.getPlans();
|
|
emit(state.copyWith(plans: plans, isLoading: false));
|
|
break;
|
|
case DashboardTab.contacts:
|
|
final contacts = await _repository.getContacts();
|
|
emit(state.copyWith(contacts: contacts, isLoading: false));
|
|
break;
|
|
case DashboardTab.chatbot:
|
|
final rules = await _repository.getChatbotRules();
|
|
emit(state.copyWith(chatbotRules: rules, isLoading: false));
|
|
break;
|
|
case DashboardTab.superAdmin:
|
|
final adminStats = await _repository.getAdminStats();
|
|
emit(state.copyWith(superAdminStats: adminStats, isLoading: false));
|
|
break;
|
|
default:
|
|
// English: Placeholder views do not hit any backend API.
|
|
// Arabic: شاشات الحجز المؤقتة لا تتصل بأي واجهة برمجة تطبيقات.
|
|
emit(state.copyWith(isLoading: false));
|
|
break;
|
|
}
|
|
} catch (e) {
|
|
final cleanMsg = e.toString().replaceAll('Exception: ', '');
|
|
emit(state.copyWith(errorMessage: cleanMsg, isLoading: false));
|
|
}
|
|
}
|
|
|
|
// English: Action trigger to reload current active tab data.
|
|
// Arabic: مشغل الإجراء لإعادة تحميل بيانات الباب النشط الحالي.
|
|
Future<void> refreshCurrentTab() async {
|
|
await changeTab(state.activeTab);
|
|
}
|
|
|
|
// English: Add a new contact and automatically reload the updated contacts list.
|
|
// Arabic: إضافة جهة اتصال جديدة وإعادة تحميل قائمة جهات الاتصال المحدثة تلقائياً.
|
|
Future<bool> addContact(String name, String phone) async {
|
|
emit(state.copyWith(isLoading: true));
|
|
try {
|
|
await _repository.addContact(name, phone);
|
|
final contacts = await _repository.getContacts();
|
|
emit(state.copyWith(contacts: contacts, isLoading: false));
|
|
return true;
|
|
} catch (e) {
|
|
final cleanMsg = e.toString().replaceAll('Exception: ', '');
|
|
emit(state.copyWith(errorMessage: cleanMsg, isLoading: false));
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// English: Request a new WhatsApp QR connection session and refresh status.
|
|
// Arabic: طلب جلسة اتصال واتساب جديدة برمز استجابة سريع وتحديث الحالة.
|
|
Future<void> requestWhatsAppQr() async {
|
|
emit(state.copyWith(isLoading: true));
|
|
try {
|
|
await _repository.requestWhatsAppQr();
|
|
final status = await _repository.getWhatsAppStatus();
|
|
emit(state.copyWith(whatsappStatus: status, isLoading: false));
|
|
} catch (e) {
|
|
final cleanMsg = e.toString().replaceAll('Exception: ', '');
|
|
emit(state.copyWith(errorMessage: cleanMsg, isLoading: false));
|
|
}
|
|
}
|
|
|
|
// English: Disconnect active WhatsApp connection and refresh status.
|
|
// Arabic: قطع اتصال الواتساب النشط وتحديث الحالة.
|
|
Future<void> disconnectWhatsApp() async {
|
|
emit(state.copyWith(isLoading: true));
|
|
try {
|
|
await _repository.disconnectWhatsApp();
|
|
final status = await _repository.getWhatsAppStatus();
|
|
emit(state.copyWith(whatsappStatus: status, isLoading: false));
|
|
} catch (e) {
|
|
final cleanMsg = e.toString().replaceAll('Exception: ', '');
|
|
emit(state.copyWith(errorMessage: cleanMsg, isLoading: false));
|
|
}
|
|
}
|
|
|
|
// English: Approve a pending company subscription billing and reload admin stats.
|
|
// Arabic: الموافقة على اشتراك شركة معلق وإعادة تحميل إحصائيات المدير العام.
|
|
Future<void> approveBilling(int companyId) async {
|
|
emit(state.copyWith(isLoading: true));
|
|
try {
|
|
await _repository.approveBilling(companyId);
|
|
final stats = await _repository.getAdminStats();
|
|
emit(state.copyWith(superAdminStats: stats, isLoading: false));
|
|
} catch (e) {
|
|
final cleanMsg = e.toString().replaceAll('Exception: ', '');
|
|
emit(state.copyWith(errorMessage: cleanMsg, isLoading: false));
|
|
}
|
|
}
|
|
}
|