121 lines
3.8 KiB
Dart
121 lines
3.8 KiB
Dart
import 'dart:developer';
|
|
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
import 'package:sfm_app/product/services/api_services.dart';
|
|
import 'product/services/notification_services.dart';
|
|
import 'product/services/theme_services.dart';
|
|
import 'product/services/language_services.dart';
|
|
import 'bloc/main_bloc.dart';
|
|
import 'product/base/bloc/base_bloc.dart';
|
|
import 'product/constant/navigation/navigation_router.dart';
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
await Firebase.initializeApp();
|
|
FirebaseMessaging
|
|
.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
|
|
NotificationServices().setupInteractMessage();
|
|
|
|
runApp(
|
|
BlocProvider(
|
|
child: const MyApp(),
|
|
blocBuilder: () => MainBloc(),
|
|
),
|
|
);
|
|
}
|
|
|
|
@pragma('vm:entry-point')
|
|
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
|
log("Full background message payload: ${message.toMap()}");
|
|
await Firebase.initializeApp();
|
|
final notificationServices = NotificationServices();
|
|
await notificationServices.initLocalNotifications();
|
|
await notificationServices.showNotification(message);
|
|
log("Background message handled: ${message.data['title']}");
|
|
}
|
|
class MyApp extends StatefulWidget {
|
|
const MyApp({super.key});
|
|
|
|
@override
|
|
State<MyApp> createState() => _MyAppState();
|
|
static void setLocale(BuildContext context, Locale newLocale) {
|
|
_MyAppState? state = context.findAncestorStateOfType<_MyAppState>();
|
|
state?.setLocale(newLocale);
|
|
}
|
|
|
|
static void setTheme(BuildContext context, ThemeData newTheme) {
|
|
_MyAppState? state = context.findAncestorStateOfType<_MyAppState>();
|
|
state?.setTheme(newTheme);
|
|
}
|
|
}
|
|
|
|
class _MyAppState extends State<MyApp> {
|
|
Locale? _locale;
|
|
ThemeData? _themeData;
|
|
late MainBloc mainBloc;
|
|
LanguageServices languageServices = LanguageServices();
|
|
ThemeServices themeServices = ThemeServices();
|
|
final NotificationServices notificationServices = NotificationServices();
|
|
APIServices apiServices = APIServices();
|
|
setLocale(Locale locale) {
|
|
_locale = locale;
|
|
mainBloc.sinkLanguage.add(_locale);
|
|
}
|
|
|
|
setTheme(ThemeData theme) {
|
|
_themeData = theme;
|
|
mainBloc.sinkTheme.add(_themeData);
|
|
}
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
mainBloc = BlocProvider.of(context);
|
|
notificationServices.initLocalNotifications();
|
|
notificationServices.firebaseInit(context);
|
|
// notificationServices.setupInteractMessage();
|
|
notificationServices.getDeviceToken().then((token){
|
|
print("Firebase Token: $token");
|
|
sendNotificationToken(token);
|
|
});
|
|
}
|
|
|
|
void sendNotificationToken (String token) async {
|
|
int statusCode = await apiServices.sendNotificationToken(token);
|
|
log("Notification Send StatusCode : $statusCode");
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
languageServices.getLocale().then((locale) => {setLocale(locale)});
|
|
themeServices.getTheme().then((theme) => {setTheme(theme)});
|
|
super.didChangeDependencies();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final router = goRouter();
|
|
return StreamBuilder<Locale?>(
|
|
stream: mainBloc.streamLanguage,
|
|
initialData: _locale,
|
|
builder: (context, languageSnapshot) {
|
|
return StreamBuilder<ThemeData?>(
|
|
stream: mainBloc.streamTheme,
|
|
initialData: _themeData,
|
|
builder: (context, themeSnapshot) {
|
|
return MaterialApp.router(
|
|
theme: themeSnapshot.data,
|
|
routerConfig: router,
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
locale: languageSnapshot.data,
|
|
);
|
|
}
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|