241 lines
8.2 KiB
Dart
241 lines
8.2 KiB
Dart
// ignore_for_file: deprecated_member_use
|
|
|
|
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:android_intent_plus/android_intent.dart';
|
|
import 'package:bot_toast/bot_toast.dart';
|
|
import 'package:didvan/config/theme_data.dart';
|
|
import 'package:didvan/firebase_options.dart';
|
|
import 'package:didvan/models/notification_message.dart';
|
|
import 'package:didvan/providers/media.dart';
|
|
import 'package:didvan/providers/theme.dart';
|
|
import 'package:didvan/providers/user.dart';
|
|
import 'package:didvan/routes/route_generator.dart';
|
|
import 'package:didvan/routes/routes.dart';
|
|
import 'package:didvan/services/app_home_widget/home_widget_repository.dart';
|
|
import 'package:didvan/services/media/media.dart';
|
|
import 'package:didvan/services/notification/firebase_api.dart';
|
|
import 'package:didvan/services/notification/notification_service.dart';
|
|
import 'package:didvan/utils/my_custom_scroll_behavior.dart';
|
|
import 'package:didvan/views/ai/ai_state.dart';
|
|
import 'package:didvan/views/ai/bot_assistants_state.dart';
|
|
import 'package:didvan/views/ai/history_ai_chat_state.dart';
|
|
import 'package:didvan/views/podcasts/podcasts_state.dart';
|
|
import 'package:didvan/views/podcasts/studio_details/studio_details_state.dart';
|
|
import 'package:firebase_core/firebase_core.dart';
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:home_widget/home_widget.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:flutter_downloader/flutter_downloader.dart';
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
|
|
|
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
|
|
|
// @pragma('vm:entry-point')
|
|
// Future _initPushNotification(RemoteMessage message) async {
|
|
// if (!kIsWeb) {
|
|
// await NotificationService.startListeningNotificationEvents();
|
|
// }
|
|
// await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
|
// // if (kDebugMode) {
|
|
// print("background: ${NotificationData.fromJson(message.data).toJson()}");
|
|
// // }
|
|
// try {
|
|
// NotificationService.showFirebaseNotification(message);
|
|
// } catch (e) {
|
|
// // if (kDebugMode) {
|
|
// print(e);
|
|
// // }
|
|
// }
|
|
// }
|
|
|
|
@pragma('vm:entry-point')
|
|
Future<void> _backgroundCallbackHomeWidget(Uri? uri) async {
|
|
await HomeWidget.saveWidgetData("uri", uri!.host);
|
|
AndroidIntent intent = const AndroidIntent(
|
|
action: 'android.intent.action.RUN',
|
|
package: 'com.didvan.didvanapp',
|
|
componentName: 'com.didvan.didvanapp.MainActivity',
|
|
);
|
|
await intent.launch();
|
|
return;
|
|
}
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
try {
|
|
if (!kIsWeb) {
|
|
HomeWidget.registerBackgroundCallback(_backgroundCallbackHomeWidget);
|
|
HomeWidget.registerInteractivityCallback(_backgroundCallbackHomeWidget);
|
|
await NotificationService.initializeNotification();
|
|
try {
|
|
if (Platform.isAndroid) {
|
|
await FlutterDownloader.initialize(
|
|
debug:
|
|
true, // optional: set to false to disable printing logs to console (default: true)
|
|
ignoreSsl:
|
|
true // option: set to false to disable working with http links (default: false)
|
|
);
|
|
}
|
|
} catch (e) {
|
|
e.printError();
|
|
}
|
|
}
|
|
|
|
// FirebaseMessaging.onBackgroundMessage(_initPushNotification);
|
|
await Firebase.initializeApp(
|
|
options: DefaultFirebaseOptions.currentPlatform);
|
|
await FirebaseApi().initNotification();
|
|
} catch (e) {
|
|
debugPrint;
|
|
}
|
|
|
|
await SentryFlutter.init(
|
|
(options) {
|
|
options.dsn =
|
|
'https://a4cfcaa7d67471240d295c25c968d91d@o4508585857384448.ingest.de.sentry.io/4508585886548048';
|
|
// Set tracesSampleRate to 1.0 to capture 100% of transactions for tracing.
|
|
// We recommend adjusting this value in production.
|
|
options.tracesSampleRate = 1.0;
|
|
// The sampling rate for profiling is relative to tracesSampleRate
|
|
// Setting to 1.0 will profile 100% of sampled transactions:
|
|
options.profilesSampleRate = 1.0;
|
|
},
|
|
appRunner: () => runApp(const Didvan()),
|
|
);
|
|
}
|
|
|
|
class Didvan extends StatefulWidget {
|
|
const Didvan({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<Didvan> createState() => _DidvanState();
|
|
}
|
|
|
|
class _DidvanState extends State<Didvan> with WidgetsBindingObserver {
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
// if (!kIsWeb) {
|
|
// NotificationService.startListeningNotificationEvents();
|
|
// }
|
|
WidgetsBinding.instance.addObserver(this);
|
|
// NotificationService.startListeningNotificationEvents();
|
|
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
// MediaService.audioPlayer.dispose();
|
|
if (MediaService.currentPodcast != null) {
|
|
MediaService.audioPlayer.dispose();
|
|
}
|
|
super.dispose();
|
|
}
|
|
|
|
bool b = true;
|
|
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) async {
|
|
if (!kIsWeb) {
|
|
if (state == AppLifecycleState.resumed) {
|
|
var r = await HomeWidget.getWidgetData("cRoute", defaultValue: '');
|
|
if (r!.toString() != Routes.splash) {
|
|
await HomeWidgetRepository.decideWhereToGo();
|
|
|
|
NotificationMessage? data = HomeWidgetRepository.data;
|
|
|
|
if (data != null) {
|
|
await HomeWidgetRepository.decideWhereToGoNotif();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MultiProvider(
|
|
providers: [
|
|
ChangeNotifierProvider<PodcastsState>(
|
|
create: (context) => PodcastsState(),
|
|
),
|
|
ChangeNotifierProvider<MediaProvider>(
|
|
create: (context) => MediaProvider(),
|
|
),
|
|
ChangeNotifierProvider<UserProvider>(
|
|
create: (context) => UserProvider(),
|
|
),
|
|
ChangeNotifierProvider<ThemeProvider>(
|
|
create: (context) => ThemeProvider(),
|
|
),
|
|
ChangeNotifierProvider<StudioDetailsState>(
|
|
create: (context) => StudioDetailsState(),
|
|
),
|
|
ChangeNotifierProvider<HistoryAiChatState>(
|
|
create: (context) => HistoryAiChatState(),
|
|
),
|
|
ChangeNotifierProvider<AiState>(
|
|
create: (context) => AiState(),
|
|
),
|
|
ChangeNotifierProvider<BotAssistantsState>(
|
|
create: (context) => BotAssistantsState(),
|
|
),
|
|
],
|
|
child: Consumer<ThemeProvider>(
|
|
builder: (context, themeProvider, child) => Container(
|
|
color: Theme.of(context).colorScheme.surface,
|
|
child: SafeArea(
|
|
child: MaterialApp(
|
|
scrollBehavior: MyCustomScrollBehavior(),
|
|
navigatorKey: navigatorKey,
|
|
debugShowCheckedModeBanner: false,
|
|
title: 'Didvan',
|
|
theme: LightThemeConfig.themeData.copyWith(
|
|
bottomSheetTheme: const BottomSheetThemeData(
|
|
surfaceTintColor: Colors.transparent,
|
|
backgroundColor: Colors.transparent),
|
|
textTheme: LightThemeConfig.themeData.textTheme.apply(
|
|
fontFamily: themeProvider.fontFamily,
|
|
)),
|
|
darkTheme: DarkThemeConfig.themeData.copyWith(
|
|
bottomSheetTheme: const BottomSheetThemeData(
|
|
surfaceTintColor: Colors.transparent,
|
|
backgroundColor: Colors.transparent),
|
|
textTheme: DarkThemeConfig.themeData.textTheme.apply(
|
|
fontFamily: themeProvider.fontFamily,
|
|
)),
|
|
color: LightThemeConfig.themeData.primaryColor,
|
|
themeMode: themeProvider.themeMode,
|
|
onGenerateRoute: (settings) =>
|
|
RouteGenerator.generateRoute(settings),
|
|
builder: BotToastInit(),
|
|
//1. call BotToastInit
|
|
navigatorObservers: [BotToastNavigatorObserver()],
|
|
initialRoute: "/",
|
|
localizationsDelegates: const [
|
|
GlobalCupertinoLocalizations.delegate,
|
|
GlobalMaterialLocalizations.delegate,
|
|
GlobalWidgetsLocalizations.delegate,
|
|
],
|
|
supportedLocales: const [
|
|
Locale("fa", "IR"),
|
|
],
|
|
locale: const Locale("fa", "IR"),
|
|
)),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|