## الثيم مقرَّب من سيرو (قرار المالك 2026-08-05) - البذرة صارت كحليّ سيرو #1A2340 بدل الأزرق العام، والثانوي #8C9CF8 - fromSeed وحدها تنتج من بذرة داكنة أسطحاً باهتة، فثُبّت الأساسي والثانوي والأسطح صراحةً وتُرك الباقي للمشتقّ — شكل سيرو مع تباين M3 - الأدوار الدلالية بأرقام سيرو الميدانية: نجاح #22C55E · تحذير #F59E0B · خطأ #EF4444، وبطاقة الليلي #2C2C2E ## مفتاح الدولة عاد core/ui/phone_field.dart — النمط المجرَّب في سيرو، لكن بلا intl_phone_field: تلك الحزمة تحمل 240 دولة ونحتاج أربعاً. CountryCode صريح ومقصور على أسواقنا. - الاختيار من ورقة سفلية لا قائمة منسدلة: أهداف لمس أكبر وأربع دول لا مئتان - الترتيب يبدأ بحزمة دولة المستأجر — الأردنيّ يرى الأردن أولاً بلا بحث - الرقم يُرسل كاملاً بالمفتاح بلا + (962790000001) كما يقبله الخادم - التحقّق صار بطول الدولة لا برقم ثابت: jo/sy تسع خانات، eg/iq عشر ## العراق - CountryCode.iraq (+964) في قائمة الهاتف - قنوات دفعه: زين كاش · كي كارد · نقد ⚠️ الباك إند لا يعرف حزمة `iq`: default-tariffs.ts فيه jo/sy/eg فقط، فمستأجر عراقي يُزوَّد بلا أي تعرفة. موثَّق في الكود — وهو شغل خادم يسبق أي إطلاق عراقي. flutter analyze نظيف في التطبيقين. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
213 lines
7.0 KiB
Dart
213 lines
7.0 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:geolocator/geolocator.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 '../../../core/payments/country_codes.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 geoStatus = await Geolocator.checkPermission();
|
|
if (geoStatus == LocationPermission.always ||
|
|
geoStatus == LocationPermission.whileInUse) {
|
|
emit(state.copyWith(
|
|
step: LoginStep.phone,
|
|
permissionPermanentlyDenied: false,
|
|
));
|
|
return;
|
|
}
|
|
|
|
final status = await Permission.locationWhenInUse.status;
|
|
if (status.isGranted || status.isLimited) {
|
|
emit(state.copyWith(
|
|
step: LoginStep.phone,
|
|
permissionPermanentlyDenied: false,
|
|
));
|
|
return;
|
|
}
|
|
|
|
emit(state.copyWith(
|
|
permissionPermanentlyDenied:
|
|
geoStatus == LocationPermission.deniedForever ||
|
|
status.isPermanentlyDenied,
|
|
));
|
|
}
|
|
|
|
Future<void> requestLocationPermission() async {
|
|
final geoStatus = await Geolocator.requestPermission();
|
|
if (geoStatus == LocationPermission.always ||
|
|
geoStatus == LocationPermission.whileInUse) {
|
|
emit(state.copyWith(
|
|
step: LoginStep.phone,
|
|
permissionPermanentlyDenied: false,
|
|
));
|
|
return;
|
|
}
|
|
|
|
final status = await Permission.locationWhenInUse.request();
|
|
if (status.isGranted || status.isLimited) {
|
|
emit(state.copyWith(
|
|
step: LoginStep.phone,
|
|
permissionPermanentlyDenied: false,
|
|
));
|
|
return;
|
|
}
|
|
|
|
emit(state.copyWith(
|
|
permissionPermanentlyDenied:
|
|
geoStatus == LocationPermission.deniedForever ||
|
|
status.isPermanentlyDenied,
|
|
));
|
|
}
|
|
|
|
Future<void> openSystemSettings() => openAppSettings();
|
|
|
|
// ── الهاتف ─────────────────────────────────────────────────────────────
|
|
|
|
/// يُنادى عند الإقلاع بحزمة دولة المستأجر — فيرى الأردنيُّ الأردن أولاً.
|
|
void applyTenantCountry(String countryPack) {
|
|
if (state.phone.isNotEmpty) return; // لا نغيّر اختياراً واعياً للمستخدم
|
|
emit(state.copyWith(country: CountryCode.fromPack(countryPack)));
|
|
}
|
|
|
|
void countryChanged(CountryCode country) =>
|
|
emit(state.copyWith(country: country, clearPhoneError: true));
|
|
|
|
void phoneChanged(String value) {
|
|
emit(state.copyWith(
|
|
phone: value,
|
|
clearPhoneError: true,
|
|
clearFailure: true,
|
|
));
|
|
}
|
|
|
|
Future<void> submitPhone() async {
|
|
final error = validatePhone(state.phone, state.country);
|
|
if (error != null) {
|
|
emit(state.copyWith(phoneError: error));
|
|
return;
|
|
}
|
|
emit(state.copyWith(status: LoginStatus.submitting, clearFailure: true));
|
|
try {
|
|
await _repo.sendOtp(state.e164);
|
|
emit(state.copyWith(status: LoginStatus.idle, step: LoginStep.otp));
|
|
_startResendCountdown();
|
|
} on ApiException catch (e) {
|
|
emit(state.copyWith(
|
|
status: LoginStatus.failure,
|
|
failure: e.toApiFailure(),
|
|
));
|
|
}
|
|
}
|
|
|
|
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.e164);
|
|
emit(state.copyWith(status: LoginStatus.idle));
|
|
_startResendCountdown();
|
|
} on ApiException catch (e) {
|
|
emit(state.copyWith(
|
|
status: LoginStatus.failure,
|
|
failure: e.toApiFailure(),
|
|
));
|
|
// حتى عند الرفض بحدّ المعدّل نبدأ العدّاد — وإلا ضغط المستخدم مجدداً
|
|
// فوراً وعمّق الحظر.
|
|
_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.e164, 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.toApiFailure(),
|
|
));
|
|
}
|
|
}
|
|
|
|
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));
|
|
});
|
|
}
|
|
}
|