167 lines
5.7 KiB
Dart
167 lines
5.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
import '../../../core/di/injection.dart';
|
|
import '../../../core/theme/app_colors.dart';
|
|
import '../../../l10n/app_strings.dart';
|
|
import '../../../shared/widgets/prediqo_logo.dart';
|
|
import '../../app_update/app_update_coordinator.dart';
|
|
import '../../auth/bloc/auth_bloc.dart';
|
|
|
|
/// Minimum branding dwell before leaving splash (real session still required).
|
|
const Duration kSplashMinBrandingDelay = Duration(milliseconds: 1200);
|
|
|
|
/// Semver-ish compare: true when [latest] is strictly newer than [current].
|
|
bool splashIsNewerVersion(String latest, String current) {
|
|
List<int> parts(String v) =>
|
|
v.split('.').map((e) => int.tryParse(e.replaceAll(RegExp(r'[^0-9]'), '')) ?? 0).toList();
|
|
final a = parts(latest);
|
|
final b = parts(current);
|
|
final len = a.length > b.length ? a.length : b.length;
|
|
for (var i = 0; i < len; i++) {
|
|
final x = i < a.length ? a[i] : 0;
|
|
final y = i < b.length ? b[i] : 0;
|
|
if (x > y) return true;
|
|
if (x < y) return false;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// True while splash must still wait for a real session check result.
|
|
bool splashShouldWaitForAuth(AuthStatus status) =>
|
|
status == AuthStatus.loading || status == AuthStatus.initial;
|
|
|
|
/// Branded splash while session and light bootstrap checks run.
|
|
///
|
|
/// Never navigates on a guessed/fake session — only after auth leaves
|
|
/// [AuthStatus.initial]/[AuthStatus.loading], and not before [kSplashMinBrandingDelay].
|
|
class SplashScreen extends StatefulWidget {
|
|
const SplashScreen({super.key});
|
|
|
|
@override
|
|
State<SplashScreen> createState() => _SplashScreenState();
|
|
}
|
|
|
|
class _SplashScreenState extends State<SplashScreen> {
|
|
bool _navigated = false;
|
|
bool _updateAvailable = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_bootstrap();
|
|
}
|
|
|
|
Future<void> _bootstrap() async {
|
|
final minDelay = Future<void>.delayed(kSplashMinBrandingDelay);
|
|
final versionCheck = _checkAppUpdate();
|
|
|
|
final authBloc = context.read<AuthBloc>();
|
|
// Belt-and-suspenders: factory usually fires AuthCheckRequested; ensure it runs.
|
|
if (authBloc.state.status == AuthStatus.initial) {
|
|
authBloc.add(const AuthCheckRequested());
|
|
}
|
|
|
|
Future<void> waitAuth() async {
|
|
if (splashShouldWaitForAuth(authBloc.state.status)) {
|
|
await authBloc.stream.firstWhere(
|
|
(s) => !splashShouldWaitForAuth(s.status),
|
|
);
|
|
}
|
|
}
|
|
|
|
await Future.wait([minDelay, versionCheck, waitAuth()]);
|
|
if (!mounted || _navigated) return;
|
|
// Route only from the real post-check auth state (never invent a session).
|
|
_navigated = true;
|
|
final signedIn = authBloc.state.isSignedIn;
|
|
context.go(signedIn ? '/home' : '/welcome');
|
|
}
|
|
|
|
Future<void> _checkAppUpdate() async {
|
|
if (!sl.isRegistered<AppUpdateCoordinator>()) return;
|
|
try {
|
|
await sl<AppUpdateCoordinator>().onColdStart();
|
|
if (!mounted) return;
|
|
final last = sl<AppUpdateCoordinator>().lastResult;
|
|
setState(() => _updateAvailable = last?.updateAvailable == true);
|
|
} catch (_) {}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final s = context.strings;
|
|
final isDark = Theme.of(context).brightness == Brightness.dark;
|
|
|
|
return Scaffold(
|
|
key: const Key('splash_screen'),
|
|
body: Container(
|
|
width: double.infinity,
|
|
height: double.infinity,
|
|
decoration: BoxDecoration(gradient: AppColors.heroGradient(isDark)),
|
|
child: SafeArea(
|
|
child: Column(
|
|
children: [
|
|
const Spacer(flex: 2),
|
|
PrediqoLogo(
|
|
size: 88,
|
|
showWordmark: false,
|
|
)
|
|
.animate()
|
|
.fadeIn(duration: 500.ms)
|
|
.scale(begin: const Offset(0.85, 0.85), end: const Offset(1, 1)),
|
|
const SizedBox(height: 20),
|
|
Text(
|
|
s.appName,
|
|
style: Theme.of(context).textTheme.headlineLarge?.copyWith(
|
|
fontWeight: FontWeight.w900,
|
|
letterSpacing: 0.5,
|
|
),
|
|
).animate().fadeIn(delay: 120.ms, duration: 400.ms),
|
|
const SizedBox(height: 10),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 40),
|
|
child: Text(
|
|
s.tagline,
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
|
color: AppColors.textSecondary,
|
|
),
|
|
),
|
|
).animate().fadeIn(delay: 200.ms, duration: 400.ms),
|
|
if (_updateAvailable) ...[
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
s.updateAvailableHint,
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context).textTheme.labelSmall?.copyWith(
|
|
color: AppColors.warning,
|
|
),
|
|
),
|
|
],
|
|
const Spacer(flex: 2),
|
|
const SizedBox(
|
|
width: 28,
|
|
height: 28,
|
|
child: CircularProgressIndicator(
|
|
strokeWidth: 2.5,
|
|
color: AppColors.primary,
|
|
),
|
|
).animate().fadeIn(delay: 300.ms),
|
|
const SizedBox(height: 12),
|
|
Text(
|
|
s.splashLoading,
|
|
style: Theme.of(context).textTheme.labelMedium?.copyWith(
|
|
color: AppColors.textMuted,
|
|
),
|
|
),
|
|
const Spacer(),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|