98 lines
3.4 KiB
Dart
98 lines
3.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
import 'package:marco/helpers/services/app_logger.dart';
|
|
import 'package:marco/helpers/services/localizations/language.dart';
|
|
import 'package:marco/helpers/services/navigation_services.dart';
|
|
import 'package:marco/helpers/services/storage/local_storage.dart';
|
|
import 'package:marco/helpers/theme/app_theme.dart';
|
|
import 'package:marco/helpers/theme/theme_customizer.dart';
|
|
import 'package:marco/helpers/theme/app_notifier.dart';
|
|
import 'package:marco/routes.dart';
|
|
|
|
class MyApp extends StatelessWidget {
|
|
const MyApp({super.key});
|
|
|
|
Future<String> _resolveInitialRoute() async {
|
|
try {
|
|
final token = LocalStorage.getJwtToken();
|
|
|
|
if (token == null || token.isEmpty) {
|
|
logSafe("👤 No token — going to login option");
|
|
return "/auth/login-option";
|
|
}
|
|
|
|
final bool hasMpin = LocalStorage.getIsMpin();
|
|
if (hasMpin) {
|
|
await LocalStorage.setBool("mpin_verified", false);
|
|
logSafe("🔐 MPIN found — going to MPIN auth");
|
|
return "/auth/mpin-auth";
|
|
}
|
|
|
|
logSafe("🏠 Routing to dashboard");
|
|
return "/dashboard";
|
|
} catch (e, stacktrace) {
|
|
logSafe("❌ Error resolving initial route",
|
|
level: LogLevel.error, error: e, stackTrace: stacktrace);
|
|
return "/auth/login-option";
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Consumer<AppNotifier>(
|
|
builder: (_, notifier, __) {
|
|
return FutureBuilder<String>(
|
|
future: _resolveInitialRoute(),
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const MaterialApp(
|
|
home: Scaffold(
|
|
body: Center(child: CircularProgressIndicator()),
|
|
),
|
|
);
|
|
}
|
|
|
|
if (snapshot.hasError) {
|
|
logSafe("❌ Error while determining initial route: ${snapshot.error}");
|
|
return const MaterialApp(
|
|
home: Scaffold(
|
|
body: Center(child: Text("Error determining route")),
|
|
),
|
|
);
|
|
}
|
|
|
|
final initialRoute = snapshot.data ?? "/auth/login-option";
|
|
logSafe("🚦 Launching app with initial route: $initialRoute");
|
|
|
|
return GetMaterialApp(
|
|
debugShowCheckedModeBanner: false,
|
|
theme: AppTheme.lightTheme,
|
|
darkTheme: AppTheme.darkTheme,
|
|
themeMode: ThemeCustomizer.instance.theme,
|
|
navigatorKey: NavigationService.navigatorKey,
|
|
initialRoute: initialRoute,
|
|
getPages: getPageRoute(),
|
|
builder: (context, child) {
|
|
NavigationService.registerContext(context);
|
|
return Directionality(
|
|
textDirection: AppTheme.textDirection,
|
|
child: child ?? const SizedBox(),
|
|
);
|
|
},
|
|
localizationsDelegates: const [
|
|
GlobalMaterialLocalizations.delegate,
|
|
GlobalWidgetsLocalizations.delegate,
|
|
GlobalCupertinoLocalizations.delegate,
|
|
],
|
|
supportedLocales: Language.getLocales(),
|
|
);
|
|
},
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|