Skip to content

Commit 697e3b5

Browse files
committed
conf: Set up Hive and HydratedBloc.
1 parent ea15a6a commit 697e3b5

5 files changed

Lines changed: 197 additions & 48 deletions

File tree

lib/data/constants/strings.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class AppStrings {
2+
static const String hiveBoxName = 'app_box';
3+
}

lib/main.dart

Lines changed: 74 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,95 @@ import 'package:firebase_crashlytics/firebase_crashlytics.dart';
55
import 'package:firebase_messaging/firebase_messaging.dart';
66
import 'package:flutter/foundation.dart';
77
import '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';
812
import 'package:sentry_flutter/sentry_flutter.dart';
913

1014
import '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
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import 'package:flutter/material.dart';
2+
3+
class Codephile extends StatelessWidget {
4+
const Codephile({Key? key}) : super(key: key);
5+
6+
@override
7+
Widget build(BuildContext context) {
8+
return const MaterialApp(
9+
debugShowCheckedModeBanner: false,
10+
);
11+
}
12+
13+
static Future<Widget> run() async {
14+
// TODO(BURG3R5): Initialize services.
15+
// TODO(BURG3R5): Wrap `Codephile` widget with `RepositoryProvider`s.
16+
return const Codephile();
17+
}
18+
}

pubspec.lock

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,20 @@ packages:
338338
url: "https://pub.dartlang.org"
339339
source: hosted
340340
version: "2.1.0"
341+
hive:
342+
dependency: transitive
343+
description:
344+
name: hive
345+
url: "https://pub.dartlang.org"
346+
source: hosted
347+
version: "2.0.5"
348+
hive_flutter:
349+
dependency: "direct main"
350+
description:
351+
name: hive_flutter
352+
url: "https://pub.dartlang.org"
353+
source: hosted
354+
version: "1.1.0"
341355
http:
342356
dependency: transitive
343357
description:
@@ -359,6 +373,13 @@ packages:
359373
url: "https://pub.dartlang.org"
360374
source: hosted
361375
version: "4.0.0"
376+
hydrated_bloc:
377+
dependency: "direct main"
378+
description:
379+
name: hydrated_bloc
380+
url: "https://pub.dartlang.org"
381+
source: hosted
382+
version: "8.0.0"
362383
io:
363384
dependency: transitive
364385
description:
@@ -485,6 +506,62 @@ packages:
485506
url: "https://pub.dartlang.org"
486507
source: hosted
487508
version: "1.8.0"
509+
path_provider:
510+
dependency: transitive
511+
description:
512+
name: path_provider
513+
url: "https://pub.dartlang.org"
514+
source: hosted
515+
version: "2.0.8"
516+
path_provider_android:
517+
dependency: transitive
518+
description:
519+
name: path_provider_android
520+
url: "https://pub.dartlang.org"
521+
source: hosted
522+
version: "2.0.11"
523+
path_provider_ios:
524+
dependency: transitive
525+
description:
526+
name: path_provider_ios
527+
url: "https://pub.dartlang.org"
528+
source: hosted
529+
version: "2.0.7"
530+
path_provider_linux:
531+
dependency: transitive
532+
description:
533+
name: path_provider_linux
534+
url: "https://pub.dartlang.org"
535+
source: hosted
536+
version: "2.1.5"
537+
path_provider_macos:
538+
dependency: transitive
539+
description:
540+
name: path_provider_macos
541+
url: "https://pub.dartlang.org"
542+
source: hosted
543+
version: "2.0.5"
544+
path_provider_platform_interface:
545+
dependency: transitive
546+
description:
547+
name: path_provider_platform_interface
548+
url: "https://pub.dartlang.org"
549+
source: hosted
550+
version: "2.0.3"
551+
path_provider_windows:
552+
dependency: transitive
553+
description:
554+
name: path_provider_windows
555+
url: "https://pub.dartlang.org"
556+
source: hosted
557+
version: "2.0.5"
558+
platform:
559+
dependency: transitive
560+
description:
561+
name: platform
562+
url: "https://pub.dartlang.org"
563+
source: hosted
564+
version: "3.1.0"
488565
plugin_platform_interface:
489566
dependency: transitive
490567
description:
@@ -499,6 +576,13 @@ packages:
499576
url: "https://pub.dartlang.org"
500577
source: hosted
501578
version: "1.5.0"
579+
process:
580+
dependency: transitive
581+
description:
582+
name: process
583+
url: "https://pub.dartlang.org"
584+
source: hosted
585+
version: "4.2.4"
502586
provider:
503587
dependency: transitive
504588
description:
@@ -602,6 +686,13 @@ packages:
602686
url: "https://pub.dartlang.org"
603687
source: hosted
604688
version: "1.1.0"
689+
synchronized:
690+
dependency: transitive
691+
description:
692+
name: synchronized
693+
url: "https://pub.dartlang.org"
694+
source: hosted
695+
version: "3.0.0"
605696
term_glyph:
606697
dependency: transitive
607698
description:
@@ -665,6 +756,13 @@ packages:
665756
url: "https://pub.dartlang.org"
666757
source: hosted
667758
version: "2.3.3"
759+
xdg_directories:
760+
dependency: transitive
761+
description:
762+
name: xdg_directories
763+
url: "https://pub.dartlang.org"
764+
source: hosted
765+
version: "0.2.0"
668766
yaml:
669767
dependency: transitive
670768
description:
@@ -674,4 +772,4 @@ packages:
674772
version: "3.1.0"
675773
sdks:
676774
dart: ">=2.14.0 <3.0.0"
677-
flutter: ">=1.20.0"
775+
flutter: ">=2.5.0"

pubspec.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ dependencies:
1717
sdk: flutter
1818
flutter_bloc: ^8.0.1
1919
freezed_annotation: ^1.1.0
20-
json_annotation: ^4.3.0
20+
hive_flutter: ^1.1.0
21+
hydrated_bloc: ^8.0.0
22+
json_annotation: ^4.4.0
2123
sentry_flutter: ^6.2.2
2224

2325
dev_dependencies:

0 commit comments

Comments
 (0)