@@ -5,67 +5,95 @@ import 'package:firebase_crashlytics/firebase_crashlytics.dart';
55import 'package:firebase_messaging/firebase_messaging.dart' ;
66import 'package:flutter/foundation.dart' ;
77import 'package:flutter/material.dart' ;
8+ import 'package:flutter/services.dart' ;
9+ import 'package:hive_flutter/hive_flutter.dart' ;
10+ import 'package:hydrated_bloc/hydrated_bloc.dart' ;
11+ import 'package:path_provider/path_provider.dart' ;
812import 'package:sentry_flutter/sentry_flutter.dart' ;
913
1014import 'data/config/config.dart' ;
15+ import 'data/constants/strings.dart' ;
16+ import 'presentation/core/main_app.dart' ;
1117
12- Future <void > main () async {
18+ void main () {
19+ // Zone for Sentry
1320 runZonedGuarded (
1421 () async {
1522 // Necessary if you intend to initialize in an async function.
1623 WidgetsFlutterBinding .ensureInitialized ();
1724
18- // Firebase section
19- await Firebase .initializeApp ();
20- FirebaseCrashlytics .instance.setCrashlyticsCollectionEnabled (true );
21- FlutterError .onError = FirebaseCrashlytics .instance.recordFlutterError;
22- FirebaseMessaging .onMessage.listen (
23- (message) {
24- final notification = message.notification;
25- if (notification != null ) {
26- debugPrint (
27- 'title: ${notification .title } \t body: ${notification .body }' );
28- }
29- },
30- );
31- FirebaseMessaging .onMessageOpenedApp.listen (
32- (message) {
33- final notification = message.notification;
34- if (notification != null ) {
35- debugPrint (
36- 'title: ${notification .title } \t body: ${notification .body }' ,
37- );
38- }
39- },
40- );
25+ _initOrientations ();
26+ await _initFirebase ();
27+ await _initSentry ();
28+ await _initHive ();
29+ final hydratedStorage = await _initHydratedBloc ();
4130
42- // Sentry section
43- await SentryFlutter .init (
44- (SentryFlutterOptions options) {
45- options
46- ..dsn = Environment .sentryDSN
47- ..tracesSampleRate = 1.0
48- ..reportPackages = true
49- ..debug = kDebugMode;
50- },
31+ // Zone for Hydrated Bloc.
32+ HydratedBlocOverrides .runZoned (
33+ () async => runApp (await Codephile .run ()),
34+ storage: hydratedStorage,
5135 );
36+ },
37+ _onError,
38+ );
39+ }
40+
41+ void _initOrientations () {
42+ SystemChrome .setPreferredOrientations (
43+ < DeviceOrientation > [DeviceOrientation .portraitUp],
44+ );
45+ }
46+
47+ Future <void > _initFirebase () async {
48+ await Firebase .initializeApp ();
49+ FirebaseCrashlytics .instance.setCrashlyticsCollectionEnabled (true );
50+ FlutterError .onError = FirebaseCrashlytics .instance.recordFlutterError;
51+ FirebaseMessaging .onMessage.listen (
52+ (message) {
53+ final notification = message.notification;
54+ if (notification != null ) {
55+ debugPrint (
56+ 'title: ${notification .title } \t body: ${notification .body }' );
57+ }
58+ },
59+ );
60+ FirebaseMessaging .onMessageOpenedApp.listen (
61+ (message) {
62+ final notification = message.notification;
63+ if (notification != null ) {
64+ debugPrint (
65+ 'title: ${notification .title } \t body: ${notification .body }' ,
66+ );
67+ }
68+ },
69+ );
70+ }
5271
53- runApp (const Codephile ());
72+ Future <void > _initSentry () async {
73+ await SentryFlutter .init (
74+ (SentryFlutterOptions options) {
75+ options
76+ ..dsn = Environment .sentryDSN
77+ ..tracesSampleRate = 1.0
78+ ..reportPackages = true
79+ ..debug = kDebugMode;
5480 },
55- (Object exception, StackTrace stack) async => Sentry .captureException (
56- exception,
57- stackTrace: stack,
58- ),
5981 );
6082}
6183
62- class Codephile extends StatelessWidget {
63- const Codephile ({Key ? key}) : super (key: key);
84+ Future <void > _initHive () async {
85+ await Hive .initFlutter ();
86+ await Hive .openBox (AppStrings .hiveBoxName);
87+ }
88+
89+ Future <Storage > _initHydratedBloc () async {
90+ return HydratedStorage .build (
91+ storageDirectory: kIsWeb
92+ ? HydratedStorage .webStorageDirectory
93+ : await getApplicationSupportDirectory (),
94+ );
95+ }
6496
65- @override
66- Widget build (BuildContext context) {
67- return const MaterialApp (
68- debugShowCheckedModeBanner: false ,
69- );
70- }
97+ void _onError (Object exception, StackTrace stacktrace) async {
98+ Sentry .captureException (exception, stackTrace: stacktrace);
7199}
0 commit comments