161 lines
5.9 KiB
Dart
Executable File
161 lines
5.9 KiB
Dart
Executable File
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:sefer_driver/controller/auth/captin/login_captin_controller.dart';
|
|
import 'package:sefer_driver/views/auth/captin/login_captin.dart';
|
|
import 'package:sefer_driver/views/home/on_boarding_page.dart';
|
|
|
|
import '../../constant/box_name.dart';
|
|
import '../../main.dart';
|
|
import '../../print.dart';
|
|
import '../functions/encrypt_decrypt.dart';
|
|
import '../functions/secure_storage.dart';
|
|
|
|
// Assuming you have a home page to navigate to after successful login.
|
|
// If not, you might need to adjust the navigation target.
|
|
// import 'package:sefer_driver/views/home/home_page.dart';
|
|
|
|
class SplashScreenController extends GetxController
|
|
with GetTickerProviderStateMixin {
|
|
late AnimationController _animationController;
|
|
late Animation<double> animation;
|
|
final progress = 0.0.obs;
|
|
Timer? _progressTimer;
|
|
|
|
String packageInfo = '';
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
_setupAnimations();
|
|
_initializeAndNavigate();
|
|
}
|
|
|
|
void _setupAnimations() {
|
|
_animationController = AnimationController(
|
|
vsync: this,
|
|
duration: const Duration(milliseconds: 1500),
|
|
);
|
|
animation =
|
|
CurvedAnimation(parent: _animationController, curve: Curves.easeInOut);
|
|
_animationController.forward();
|
|
}
|
|
|
|
/// This is the core function that initializes the app.
|
|
/// It runs two tasks simultaneously and navigates only when necessary.
|
|
Future<void> _initializeAndNavigate() async {
|
|
// Start getting package info, no need to wait for it.
|
|
_getPackageInfo();
|
|
|
|
const minSplashDurationMs = 4000;
|
|
_animateProgressBar(minSplashDurationMs);
|
|
|
|
// Define the two concurrent tasks
|
|
final minDuration =
|
|
Future.delayed(const Duration(milliseconds: minSplashDurationMs));
|
|
final navigationTargetFuture = _getNavigationTarget();
|
|
|
|
// Wait for both tasks to complete
|
|
await Future.wait([minDuration, navigationTargetFuture]);
|
|
|
|
// The future now returns a nullable Widget (Widget?)
|
|
final Widget? targetPage = await navigationTargetFuture;
|
|
|
|
// *** FIX: Only navigate if the targetPage is not null. ***
|
|
// This prevents navigating again if the login function already handled it.
|
|
if (targetPage != null) {
|
|
Get.off(() => targetPage,
|
|
transition: Transition.fadeIn,
|
|
duration: const Duration(milliseconds: 500));
|
|
} else {
|
|
Log.print(
|
|
"Navigation was handled internally by the login process. Splash screen will not navigate.");
|
|
}
|
|
}
|
|
|
|
/// Animates the progress bar over a given duration.
|
|
void _animateProgressBar(int totalMilliseconds) {
|
|
const interval = 50;
|
|
int elapsed = 0;
|
|
_progressTimer?.cancel();
|
|
_progressTimer =
|
|
Timer.periodic(const Duration(milliseconds: interval), (timer) {
|
|
elapsed += interval;
|
|
progress.value = (elapsed / totalMilliseconds).clamp(0.0, 1.0);
|
|
if (elapsed >= totalMilliseconds) {
|
|
timer.cancel();
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Determines the correct page to navigate to, or returns null if navigation
|
|
/// is expected to be handled by an internal process (like login).
|
|
Future<Widget?> _getNavigationTarget() async {
|
|
try {
|
|
// ... (التحقق من OnBoarding)
|
|
|
|
final isDriverDataAvailable = box.read(BoxName.phoneDriver) != null;
|
|
// final isPhoneVerified = box.read(BoxName.phoneVerified).toString() == '1'; // <-- ⛔️ تم حذف هذا السطر
|
|
|
|
final loginController = Get.put(LoginDriverController());
|
|
|
|
// ✅ --- (الحل) ---
|
|
// تم حذف التحقق من "isPhoneVerified"
|
|
// هذا يسمح لـ "loginWithGoogleCredential" بتحديد الحالة والتوجيه الصحيح
|
|
// (إلى Home أو DriverVerificationScreen أو PhoneNumberScreen)
|
|
if (isDriverDataAvailable) {
|
|
Log.print('المستخدم مسجل. جارٍ تهيئة الجلسة...');
|
|
|
|
// الخطوة 1: ضمان جلب الـ JWT أولاً
|
|
// (هذا هو الكود الذي كان في main.dart)
|
|
final AppInitializer initializer = AppInitializer();
|
|
await initializer.initializeApp();
|
|
await EncryptionHelper.initialize();
|
|
// انتظر حتى ينتهي جلب الـ JWT
|
|
|
|
Log.print('تم جلب الـ JWT. جارٍ تسجيل الدخول ببيانات جوجل...');
|
|
|
|
// الخطوة 2: الآن قم بتسجيل الدخول وأنت متأكد أن الـ JWT موجود
|
|
// يجب تعديل "loginWithGoogleCredential" لتعيد "bool" (نجاح/فشل)
|
|
await loginController.loginWithGoogleCredential(
|
|
box.read(BoxName.driverID).toString(),
|
|
box.read(BoxName.emailDriver).toString(),
|
|
);
|
|
|
|
// إذا نجح تسجيل الدخول (سواء لـ Home أو لـ DriverVerification)
|
|
// فإن "loginWithGoogleCredential" تقوم بالتوجيه بنفسها
|
|
// ونحن نُرجع "null" هنا لمنع "SplashScreen" من التوجيه مرة أخرى.
|
|
} else {
|
|
Log.print('مستخدم غير مسجل. اذهب لصفحة الدخول.');
|
|
return LoginCaptin();
|
|
}
|
|
} catch (e) {
|
|
Log.print("Error during navigation logic: $e");
|
|
// أي خطأ فادح (مثل خطأ في جلب الـ JWT) سيعيدك للدخول
|
|
return LoginCaptin();
|
|
}
|
|
}
|
|
|
|
Future<void> _getPackageInfo() async {
|
|
try {
|
|
final info = await PackageInfo.fromPlatform();
|
|
packageInfo = info.version;
|
|
await box.write(BoxName.packagInfo, packageInfo);
|
|
update(); // To update any UI element that might be listening
|
|
} catch (e) {
|
|
Log.print("Could not get package info: $e");
|
|
packageInfo = '1.0.0'; // Default value
|
|
await box.write(BoxName.packagInfo, packageInfo);
|
|
}
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
_progressTimer?.cancel();
|
|
_animationController.dispose();
|
|
super.onClose();
|
|
}
|
|
}
|