2024-12-21 19:41:00 +01:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'package:logger/logger.dart';
|
|
|
|
import 'pages/login.dart';
|
|
|
|
import 'pages/home.dart';
|
|
|
|
|
|
|
|
void main() {
|
2024-12-21 22:36:41 +01:00
|
|
|
runApp(const MyApp());
|
2024-12-21 19:41:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatelessWidget {
|
2024-12-21 22:36:41 +01:00
|
|
|
const MyApp({super.key});
|
|
|
|
|
2024-12-21 19:41:00 +01:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
title: 'Ping Pong Tracker',
|
|
|
|
theme: ThemeData.dark(useMaterial3: true).copyWith(
|
|
|
|
colorScheme: ColorScheme.dark(
|
|
|
|
primary: Colors.blue.shade900,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
home: EntryPoint(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EntryPoint extends StatelessWidget {
|
|
|
|
final Logger _logger = Logger();
|
|
|
|
|
|
|
|
Future<Widget> _getInitialPage() async {
|
|
|
|
_logger.d("Fetching token from SharedPreferences");
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
final token = prefs.getString('token');
|
|
|
|
_logger.d("Token value from SharedPreferences: $token");
|
|
|
|
|
|
|
|
if (token == null || token.isEmpty) {
|
|
|
|
_logger.d("No token found. Returning LoginPage.");
|
|
|
|
return LoginPage();
|
|
|
|
} else {
|
|
|
|
_logger.d("Token found. Returning HomePage.");
|
|
|
|
return HomePage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
_logger.d("Building EntryPoint UI with FutureBuilder");
|
|
|
|
return FutureBuilder<Widget>(
|
|
|
|
future: _getInitialPage(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
|
|
_logger.d("Waiting for initial page resolution.");
|
2024-12-21 22:36:41 +01:00
|
|
|
return const Scaffold(
|
2024-12-21 19:41:00 +01:00
|
|
|
body: Center(child: CircularProgressIndicator()),
|
|
|
|
);
|
|
|
|
} else if (snapshot.hasError) {
|
|
|
|
_logger.e("Error while resolving initial page: ${snapshot.error}");
|
|
|
|
return LoginPage(); // Fallback to LoginPage on error
|
|
|
|
} else {
|
|
|
|
_logger.d("Navigation target resolved: ${snapshot.data.runtimeType}");
|
|
|
|
return snapshot.data!;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|