## طبقتا الميزات مُطبَّقتان معاً
AppDrawer يفحص: Features.wallet && context.tenantHas('wallet')
- Features.x ثابت const: يقرّر ما يُبنى في الـbinary. إطفاؤه يحذف الكود
فلا يُستخرج (docs/23 §5)
- tenantHas من GET /tenant/config/:slug: يقرّر ما يُعرض ممّا بُني حسب اشتراك
المستأجر (docs/38 §10)
هذا هو ربط lite/pro/max عملياً. TenantCubit يعرض النسخة المخزّنة فوراً ثم
يحدّثها: إقلاع بلا إنترنت يعرض الميزات المعروفة آخر مرة لا شاشة فارغة.
## الشاشات
الدراور · تعديل الحساب · سجلّ الرحلات · الإعدادات (مظهر + لغة) · المساعدة.
الرقم في تعديل الحساب معطّل مع سبب معروض لا مخفي: هو هوية الحساب على الخادم
وتغييره يعني حساباً آخر (docs/38 §1).
## الإشعارات
PushService معزول — الملف الوحيد الذي يعرف Firebase. التسجيل بعد إثبات
الجلسة عبر خطّاف في SessionCubit، ومع اشتراك onTokenRefresh: بدونه تتوقّف
الإشعارات بصمت بعد أسابيع. الإعداد الأصلي كان جاهزاً مسبقاً.
## تصحيحات بنيوية كشفها فحص آلي
docs/23 §1 يمنع أن تستورد ميزة من أخرى — وكانت خمس مخالفات:
1. features/history أُدمجت في features/trip — السجلّ جزء من ميزة الرحلة
2. AppDrawer صعد إلى core/ui — هيكل تطبيق لا ميزة
3. AuthFailure → core/api/api_failure.dart، ومترجمه → core/ui/failure_text.dart
4. AuthHeader → core/ui/form_header.dart
5. ProfileRepository مستقلّ عن AuthRepository: تعديل الملف ليس مصادقة
flutter analyze نظيف · الراكب 98 ملف/7,497 سطر · السائق 95 ملف/6,931 سطر.
فحوص آلية نظيفة: صفر استيراد بين ميزتين · core يستورد features من di و
router وحدهما · Firebase و intaleq_maps و geolocator و socket_io كلٌّ معزول.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
177 lines
5.6 KiB
Dart
177 lines
5.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../../../core/api/api_exception.dart';
|
|
|
|
import '../../../core/api/api_failure.dart';
|
|
|
|
import '../../../core/config.dart';
|
|
|
|
import '../data/auth_repository.dart';
|
|
|
|
import 'login_state.dart';
|
|
|
|
import 'phone_error.dart';
|
|
|
|
/// تدفّق الدخول كاملاً: الشروط ← إذن الموقع ← الهاتف ← الرمز.
|
|
///
|
|
/// الـCubit لا يعرف Flutter: لا `BuildContext` ولا تنقّل ولا `SnackBar`
|
|
/// (docs/23 §3). يُصدر حالة، والواجهة تتصرّف.
|
|
class LoginCubit extends Cubit<LoginState> {
|
|
LoginCubit(this._repo, this._prefs) : super(const LoginState()) {
|
|
_resolveInitialStep();
|
|
}
|
|
|
|
final AuthRepository _repo;
|
|
final SharedPreferences _prefs;
|
|
|
|
static const _kAgreed = 'auth.agreed_terms';
|
|
|
|
Timer? _resendTimer;
|
|
|
|
@override
|
|
Future<void> close() {
|
|
_resendTimer?.cancel();
|
|
return super.close();
|
|
}
|
|
|
|
// ── البوابتان ──────────────────────────────────────────────────────────
|
|
|
|
void _resolveInitialStep() {
|
|
if (_prefs.getBool(_kAgreed) != true) {
|
|
emit(state.copyWith(step: LoginStep.agreement));
|
|
return;
|
|
}
|
|
emit(state.copyWith(step: LoginStep.permission, agreed: true));
|
|
unawaited(checkLocationPermission());
|
|
}
|
|
|
|
void toggleAgreement(bool value) => emit(state.copyWith(agreed: value));
|
|
|
|
Future<void> acceptAgreement() async {
|
|
if (!state.agreed) return;
|
|
await _prefs.setBool(_kAgreed, true);
|
|
emit(state.copyWith(step: LoginStep.permission));
|
|
await checkLocationPermission();
|
|
}
|
|
|
|
/// تُستدعى أيضاً عند **العودة من إعدادات النظام**: بلا إعادة الفحص تبقى
|
|
/// الشاشة عالقة بعد أن يمنح المستخدم الإذن يدوياً (حالة حافة من docs/39 §2).
|
|
Future<void> checkLocationPermission() async {
|
|
final status = await Permission.locationWhenInUse.status;
|
|
if (status.isGranted || status.isLimited) {
|
|
emit(state.copyWith(step: LoginStep.phone));
|
|
return;
|
|
}
|
|
emit(state.copyWith(
|
|
permissionPermanentlyDenied: status.isPermanentlyDenied,
|
|
));
|
|
}
|
|
|
|
Future<void> requestLocationPermission() async {
|
|
final status = await Permission.locationWhenInUse.request();
|
|
if (status.isGranted || status.isLimited) {
|
|
emit(state.copyWith(step: LoginStep.phone));
|
|
return;
|
|
}
|
|
emit(state.copyWith(
|
|
permissionPermanentlyDenied: status.isPermanentlyDenied,
|
|
));
|
|
}
|
|
|
|
Future<void> openSystemSettings() => openAppSettings();
|
|
|
|
// ── الهاتف ─────────────────────────────────────────────────────────────
|
|
|
|
void phoneChanged(String value) {
|
|
emit(state.copyWith(
|
|
phone: value,
|
|
clearPhoneError: true,
|
|
clearFailure: true,
|
|
));
|
|
}
|
|
|
|
Future<void> submitPhone() async {
|
|
final error = validatePhone(state.phone);
|
|
if (error != null) {
|
|
emit(state.copyWith(phoneError: error));
|
|
return;
|
|
}
|
|
emit(state.copyWith(status: LoginStatus.submitting, clearFailure: true));
|
|
try {
|
|
await _repo.sendOtp(state.phone.trim());
|
|
emit(state.copyWith(status: LoginStatus.idle, step: LoginStep.otp));
|
|
_startResendCountdown();
|
|
} on ApiException catch (e) {
|
|
emit(state.copyWith(
|
|
status: LoginStatus.failure,
|
|
failure: e.toAuthFailure(),
|
|
));
|
|
}
|
|
}
|
|
|
|
void editPhone() {
|
|
_resendTimer?.cancel();
|
|
emit(state.copyWith(
|
|
step: LoginStep.phone,
|
|
status: LoginStatus.idle,
|
|
resendIn: 0,
|
|
clearFailure: true,
|
|
));
|
|
}
|
|
|
|
// ── الرمز ──────────────────────────────────────────────────────────────
|
|
|
|
Future<void> resendOtp() async {
|
|
if (!state.canResend) return;
|
|
emit(state.copyWith(status: LoginStatus.submitting, clearFailure: true));
|
|
try {
|
|
await _repo.sendOtp(state.phone.trim());
|
|
emit(state.copyWith(status: LoginStatus.idle));
|
|
_startResendCountdown();
|
|
} on ApiException catch (e) {
|
|
emit(state.copyWith(
|
|
status: LoginStatus.failure,
|
|
failure: e.toAuthFailure(),
|
|
));
|
|
// حتى عند الرفض بحدّ المعدّل نبدأ العدّاد — وإلا ضغط المستخدم مجدداً
|
|
// فوراً وعمّق الحظر.
|
|
_startResendCountdown();
|
|
}
|
|
}
|
|
|
|
Future<void> submitOtp(String code) async {
|
|
if (code.length != AppConfig.otpLength) return;
|
|
emit(state.copyWith(status: LoginStatus.submitting, clearFailure: true));
|
|
try {
|
|
final user = await _repo.verifyOtp(phone: state.phone.trim(), code: code);
|
|
_resendTimer?.cancel();
|
|
emit(state.copyWith(
|
|
status: LoginStatus.success,
|
|
step: LoginStep.done,
|
|
user: user,
|
|
));
|
|
} on ApiException catch (e) {
|
|
emit(state.copyWith(
|
|
status: LoginStatus.failure,
|
|
failure: e.toAuthFailure(),
|
|
));
|
|
}
|
|
}
|
|
|
|
void _startResendCountdown() {
|
|
_resendTimer?.cancel();
|
|
emit(state.copyWith(resendIn: AppConfig.otpResendCooldown.inSeconds));
|
|
_resendTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
|
|
final next = state.resendIn - 1;
|
|
if (next <= 0) timer.cancel();
|
|
emit(state.copyWith(resendIn: next < 0 ? 0 : next));
|
|
});
|
|
}
|
|
}
|