Compare commits
2 Commits
a54ebb91ab
...
3dd5538dca
| Author | SHA1 | Date |
|---|---|---|
|
|
3dd5538dca | 4 months ago |
|
|
558691a0d1 | 4 months ago |
@ -0,0 +1,121 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:io';
|
||||||
|
import 'package:doctor_app_flutter/config/config.dart';
|
||||||
|
import 'package:doctor_app_flutter/core/service/authentication_service.dart';
|
||||||
|
import 'package:doctor_app_flutter/core/viewModel/authentication_view_model.dart';
|
||||||
|
import 'package:doctor_app_flutter/locator.dart';
|
||||||
|
import 'package:doctor_app_flutter/screens/auth/login_screen.dart';
|
||||||
|
import 'package:doctor_app_flutter/screens/auth/verification_methods_screen.dart';
|
||||||
|
import 'package:firebase_core/firebase_core.dart';
|
||||||
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
|
class SplashScreen extends StatefulWidget {
|
||||||
|
@override
|
||||||
|
_SplashScreenState createState() => _SplashScreenState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class _SplashScreenState extends State<SplashScreen> {
|
||||||
|
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
|
||||||
|
late AuthenticationViewModel _authViewModel;
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
AppGlobal.CONTEX = context;
|
||||||
|
|
||||||
|
|
||||||
|
_initializeApp();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _initializeApp() async {
|
||||||
|
try {
|
||||||
|
final String? token = await _getFirebaseToken();
|
||||||
|
|
||||||
|
_authViewModel = Provider.of<AuthenticationViewModel>(context, listen: false);
|
||||||
|
await _authViewModel.initializeApp(token: token);
|
||||||
|
|
||||||
|
_navigateBasedOnAuthState();
|
||||||
|
} catch (e) {
|
||||||
|
// Handle error and fallback to login
|
||||||
|
_navigateToLogin();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String?> _getFirebaseToken() async {
|
||||||
|
try {
|
||||||
|
await Firebase.initializeApp();
|
||||||
|
Future.delayed(Duration(seconds: 1), () {
|
||||||
|
});
|
||||||
|
if (Platform.isIOS) {
|
||||||
|
// Request permissions first
|
||||||
|
final settings = await _firebaseMessaging.requestPermission(
|
||||||
|
alert: true,
|
||||||
|
announcement: false,
|
||||||
|
badge: true,
|
||||||
|
carPlay: false,
|
||||||
|
criticalAlert: false,
|
||||||
|
provisional: false,
|
||||||
|
sound: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
debugPrint('Notification permissions: ${settings.authorizationStatus}');
|
||||||
|
|
||||||
|
// Get both FCM and APNS tokens
|
||||||
|
final fcmToken = await _firebaseMessaging.getToken();
|
||||||
|
debugPrint("FCM Token: $fcmToken");
|
||||||
|
|
||||||
|
// APNS token might not be immediately available
|
||||||
|
String? apnsToken;
|
||||||
|
int retries = 0;
|
||||||
|
while (apnsToken == null && retries < 3) {
|
||||||
|
apnsToken = await _firebaseMessaging.getAPNSToken();
|
||||||
|
if (apnsToken == null) {
|
||||||
|
await Future.delayed(Duration(milliseconds: 500));
|
||||||
|
retries++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
debugPrint("APNS Token: $apnsToken");
|
||||||
|
return apnsToken ?? fcmToken; // Fallback to FCM token if APNS is null
|
||||||
|
} else {
|
||||||
|
return await _firebaseMessaging.getToken();
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
debugPrint("Failed to get FCM token: $e");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
void _navigateBasedOnAuthState() {
|
||||||
|
if (_authViewModel.status == APP_STATUS.UNVERIFIED) {
|
||||||
|
Navigator.pushReplacement(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(builder: (_) => VerificationMethodsScreen()),
|
||||||
|
);
|
||||||
|
} else if (_authViewModel.status == APP_STATUS.UNAUTHENTICATED) {
|
||||||
|
_navigateToLogin();
|
||||||
|
} else {
|
||||||
|
_navigateToLogin();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _navigateToLogin() {
|
||||||
|
Navigator.pushReplacement(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(builder: (_) => LoginScreen()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
backgroundColor: Color(0xffF8F8F8),
|
||||||
|
body: Center(
|
||||||
|
child: Image.asset('assets/images/dr_app_logo.png'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue