25-10-2/1
This commit is contained in:
@@ -1,15 +1,19 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:sefer_driver/controller/auth/captin/login_captin_controller.dart';
|
||||
import 'package:sefer_driver/views/home/on_boarding_page.dart';
|
||||
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 '../../views/auth/captin/login_captin.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 {
|
||||
@@ -20,73 +24,116 @@ class SplashScreenController extends GetxController
|
||||
|
||||
String packageInfo = '';
|
||||
|
||||
Future<void> _getPackageInfo() async {
|
||||
final info = await PackageInfo.fromPlatform();
|
||||
packageInfo = info.version;
|
||||
box.write(BoxName.packagInfo, packageInfo);
|
||||
update();
|
||||
}
|
||||
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
_getPackageInfo();
|
||||
_animationController = AnimationController(
|
||||
vsync: this,
|
||||
duration: const Duration(milliseconds: 1500), // Reduced duration
|
||||
)..forward();
|
||||
|
||||
animation =
|
||||
CurvedAnimation(parent: _animationController, curve: Curves.easeOut);
|
||||
|
||||
startTimer();
|
||||
_startProgressTimer();
|
||||
_setupAnimations();
|
||||
_initializeAndNavigate();
|
||||
}
|
||||
|
||||
void _startProgressTimer() {
|
||||
Log.print(
|
||||
'box.read(BoxName.phoneDriver): ${box.read(BoxName.phoneDriver)}');
|
||||
Log.print(
|
||||
'box.read(BoxName.phoneVerified): ${box.read(BoxName.phoneVerified)}');
|
||||
const totalTime = 3000; // 5 seconds in milliseconds
|
||||
const interval = 50; // Update every 50ms
|
||||
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) async {
|
||||
Timer.periodic(const Duration(milliseconds: interval), (timer) {
|
||||
elapsed += interval;
|
||||
progress.value = (elapsed / totalTime).clamp(0.0, 1.0);
|
||||
|
||||
if (elapsed >= totalTime) {
|
||||
progress.value = (elapsed / totalMilliseconds).clamp(0.0, 1.0);
|
||||
if (elapsed >= totalMilliseconds) {
|
||||
timer.cancel();
|
||||
// Get.off(SyrianCardAI());
|
||||
box.read(BoxName.onBoarding) == null
|
||||
? Get.off(() => OnBoardingPage())
|
||||
: box.read(BoxName.phoneDriver) != null &&
|
||||
box.read(BoxName.phoneVerified).toString() == '1'
|
||||
? await Get.put(LoginDriverController())
|
||||
.loginWithGoogleCredential(
|
||||
box.read(BoxName.driverID).toString(),
|
||||
box.read(BoxName.emailDriver).toString())
|
||||
: Get.off(() => LoginCaptin());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void startTimer() async {
|
||||
Timer(const Duration(seconds: 5), () async {
|
||||
// box.read(BoxName.onBoarding) == null
|
||||
// ? Get.off(() => OnBoardingPage())
|
||||
// : box.read(BoxName.email) != null &&
|
||||
// box.read(BoxName.phone) != null &&
|
||||
// box.read(BoxName.isVerified) == '1'
|
||||
// // ? Get.off(() => const MapPagePassenger())
|
||||
// ? await Get.put(LoginController()).loginUsingCredentials(
|
||||
// box.read(BoxName.passengerID).toString(),
|
||||
// box.read(BoxName.email).toString(),
|
||||
// )
|
||||
// : Get.off(() => LoginPage());
|
||||
});
|
||||
/// 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 {
|
||||
final onBoardingShown = box.read(BoxName.onBoarding) != null;
|
||||
if (!onBoardingShown) {
|
||||
return OnBoardingPage();
|
||||
}
|
||||
|
||||
final isDriverDataAvailable = box.read(BoxName.phoneDriver) != null;
|
||||
final isPhoneVerified = box.read(BoxName.phoneVerified).toString() == '1';
|
||||
|
||||
if (isDriverDataAvailable && isPhoneVerified) {
|
||||
Log.print('Attempting to log in with stored credentials...');
|
||||
final loginController = Get.put(LoginDriverController());
|
||||
|
||||
// Assume loginWithGoogleCredential handles its own navigation on success.
|
||||
await loginController.loginWithGoogleCredential(
|
||||
box.read(BoxName.driverID).toString(),
|
||||
box.read(BoxName.emailDriver).toString(),
|
||||
);
|
||||
|
||||
// *** FIX: Return null to signify that navigation has been handled. ***
|
||||
return null;
|
||||
} else {
|
||||
Log.print('No valid driver session found. Navigating to login page.');
|
||||
return LoginCaptin();
|
||||
}
|
||||
} catch (e) {
|
||||
Log.print("Error during navigation logic: $e");
|
||||
// Fallback to the login page in case of any error.
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user