210 lines
7.1 KiB
Dart
210 lines
7.1 KiB
Dart
// main.dart
|
|
import 'dart:ui';
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:provider/single_child_widget.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:tm_app/core/config/dependencies.dart';
|
|
import 'package:tm_app/core/config/web_url_strategy.dart';
|
|
import 'package:tm_app/core/services/cache_init.dart';
|
|
import 'package:tm_app/core/services/firebase_service.dart';
|
|
import 'package:tm_app/core/theme/theme_provider.dart';
|
|
import 'package:tm_app/core/utils/app_toast.dart';
|
|
import 'package:toastification/toastification.dart';
|
|
|
|
import 'core/routes/app_routes.dart';
|
|
import 'core/theme/colors.dart';
|
|
import 'core/utils/logger.dart';
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
// Initialize SVG cache
|
|
await CacheInit.init();
|
|
|
|
await SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
|
|
|
|
// Initialize the logger
|
|
appLogger.init();
|
|
|
|
// Log app start
|
|
appLogger.info('🚀 Starting TM App...');
|
|
// appLogger.logNavigation('Home', 'Login');
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
List<SingleChildWidget> providers = [
|
|
Provider(create: (context) => prefs),
|
|
...providersRemote,
|
|
];
|
|
// Configure web URL strategy if running on web
|
|
configureWebUrlStrategy();
|
|
|
|
await FirebaseService().initializeApp();
|
|
|
|
runApp(MultiProvider(providers: providers, child: const MyApp()));
|
|
}
|
|
|
|
class MyApp extends StatefulWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
State<MyApp> createState() => _MyAppState();
|
|
}
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
Future<void> setupInteractedMessage() async {
|
|
final initialMessage = await FirebaseMessaging.instance.getInitialMessage();
|
|
|
|
if (initialMessage != null) {
|
|
_handleMessage(initialMessage);
|
|
}
|
|
|
|
// if (kIsWeb) {
|
|
FirebaseMessaging.onMessage.listen((event) {
|
|
final overlayCtx = rootNavigatorKey.currentContext;
|
|
AppLogger().error('Notification received: ${event.notification?.web}');
|
|
AppLogger().error('web link: ${event.notification?.title}');
|
|
AppLogger().error('web link: ${event.notification?.web?.link}');
|
|
print('Notification received1: ${event.notification?.web.toString()}');
|
|
print(
|
|
'Notification received1: ${event.notification?.web?.analyticsLabel}',
|
|
);
|
|
|
|
print('web link: ${event.notification?.web?.link}');
|
|
print('Notification received: ${event.notification?.title}');
|
|
AppLogger().error('notif data: ${event.data}');
|
|
AppLogger().error('notif from: ${event.from}');
|
|
AppLogger().error('notif type: ${event.messageType}');
|
|
AppLogger().error('notif keys: ${event.data.keys}');
|
|
AppLogger().info('notif title: ${event.notification?.title}');
|
|
AppLogger().info('notif body: ${event.notification?.body}');
|
|
AppLogger().info('notif data: ${event.data}');
|
|
AppLogger().info('notif from: ${event.from}');
|
|
AppLogger().info('notif type: ${event.messageType}');
|
|
AppLogger().info('notif keys: ${event.data.keys}');
|
|
if (overlayCtx != null) {
|
|
if (overlayCtx.mounted) {
|
|
AppLogger().error(
|
|
'Notification received: ${event.notification?.web}',
|
|
);
|
|
AppLogger().error(
|
|
' link android: ${event.notification?.android?.link}',
|
|
);
|
|
AppLogger().error(
|
|
'android link image: ${event.notification?.android?.imageUrl}',
|
|
);
|
|
AppLogger().error(
|
|
'android link click action: ${event.notification?.android?.clickAction}',
|
|
);
|
|
AppLogger().error(
|
|
'web link image: ${event.notification?.web?.image}',
|
|
);
|
|
print('Notification received: ${event.notification?.web}');
|
|
print('web link: ${event.notification?.web?.link}');
|
|
AppToast.notification(
|
|
overlayCtx,
|
|
event.notification?.title ?? 'New Notification',
|
|
event.notification?.body ?? 'New Notification',
|
|
);
|
|
}
|
|
}
|
|
});
|
|
// }
|
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
|
|
}
|
|
|
|
void _handleMessage(RemoteMessage message) {
|
|
SharedPreferences.getInstance().then((value) {
|
|
value.setString('notif_data', message.data.toString());
|
|
value.setString('notif_from', message.from.toString());
|
|
value.setString('notif_type', message.messageType.toString());
|
|
value.setString('notif_keys', message.data.keys.toString());
|
|
value.setString('notif_title', message.notification?.title ?? '');
|
|
value.setString('notif_body', message.notification?.body ?? '');
|
|
value.setString('notif_data', message.contentAvailable.toString());
|
|
value.setString('notif_web_link', message.notification?.web?.link ?? '');
|
|
value.setString(
|
|
'notif_android_link',
|
|
message.notification?.android?.link ?? '',
|
|
);
|
|
value.setString(
|
|
'notif_web_image',
|
|
message.notification?.web?.image ?? '',
|
|
);
|
|
value.setString(
|
|
'notif_android_image',
|
|
message.notification?.android?.imageUrl ?? '',
|
|
);
|
|
value.setString(
|
|
'notif_web_click_action',
|
|
message.notification?.web?.link ?? '',
|
|
);
|
|
});
|
|
appRouter.go(
|
|
const SplashScreenRoute(navigateToNotification: true).location,
|
|
);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
setupInteractedMessage();
|
|
});
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
// Set the context for ThemeProvider to enable forced rebuilds
|
|
ThemeProvider.setContext(context);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
// Listen to theme changes
|
|
final themeProvider = context.watch<ThemeProvider>();
|
|
|
|
return ToastificationWrapper(
|
|
child: MaterialApp.router(
|
|
title: 'OppLens',
|
|
debugShowCheckedModeBanner: false,
|
|
scrollBehavior: const MaterialScrollBehavior().copyWith(
|
|
dragDevices: {
|
|
PointerDeviceKind.mouse,
|
|
PointerDeviceKind.touch,
|
|
PointerDeviceKind.stylus,
|
|
PointerDeviceKind.unknown,
|
|
},
|
|
),
|
|
theme: ThemeData(
|
|
pageTransitionsTheme: PageTransitionsTheme(
|
|
builders: {
|
|
for (final platform in TargetPlatform.values)
|
|
platform:
|
|
kIsWeb
|
|
? const FadeUpwardsPageTransitionsBuilder()
|
|
: const CupertinoPageTransitionsBuilder(),
|
|
},
|
|
),
|
|
brightness:
|
|
themeProvider.isDarkMode ? Brightness.dark : Brightness.light,
|
|
primaryColor: AppColors.primaryColor,
|
|
scaffoldBackgroundColor: AppColors.backgroundColor,
|
|
colorScheme: ColorScheme.fromSeed(
|
|
seedColor: AppColors.primaryColor,
|
|
brightness:
|
|
themeProvider.isDarkMode ? Brightness.dark : Brightness.light,
|
|
),
|
|
),
|
|
routerConfig: appRouter,
|
|
),
|
|
);
|
|
}
|
|
}
|