Line data Source code
1 : // Copyright 2023 The terCAD team. All rights reserved.
2 : // Use of this source code is governed by a CC BY-NC-ND 4.0 license that can be found in the LICENSE file.
3 :
4 : import 'dart:async';
5 :
6 : import 'package:app_finance/_classes/controller/deep_link/deep_link_handler.dart';
7 : import 'package:app_finance/_classes/controller/fallback_localization_delegate.dart';
8 : import 'package:app_finance/_classes/herald/app_budget_positive.dart';
9 : import 'package:app_finance/_classes/herald/app_design.dart';
10 : import 'package:app_finance/_classes/herald/app_locale.dart';
11 : import 'package:app_finance/_classes/herald/app_palette.dart';
12 : import 'package:app_finance/_classes/herald/app_purchase.dart';
13 : import 'package:app_finance/_classes/herald/app_sorting.dart';
14 : import 'package:app_finance/_classes/herald/app_start_of_month.dart';
15 : import 'package:app_finance/_classes/herald/app_sync.dart';
16 : import 'package:app_finance/_classes/herald/app_theme.dart';
17 : import 'package:app_finance/_classes/herald/app_start_of_week.dart';
18 : import 'package:app_finance/_classes/herald/app_zoom.dart';
19 : import 'package:app_finance/_classes/integration/notification/notification_background.dart';
20 : import 'package:app_finance/_classes/storage/app_preferences.dart';
21 : import 'package:app_finance/_classes/structure/navigation/app_page_route.dart';
22 : import 'package:app_finance/_configs/custom_color_scheme.dart';
23 : import 'package:app_finance/_configs/custom_text_theme.dart';
24 : import 'package:app_finance/_configs/localization_delegates.dart';
25 : import 'package:app_finance/_classes/storage/app_data.dart';
26 : import 'package:app_finance/_classes/structure/navigation/app_route.dart';
27 : import 'package:app_finance/_configs/firebase_options.dart';
28 : import 'package:app_finance/l10n/app_localization.dart';
29 : import 'package:app_finance/pages/about/about_page.dart';
30 : import 'package:app_finance/pages/account/account_add_page.dart';
31 : import 'package:app_finance/pages/account/account_edit_page.dart';
32 : import 'package:app_finance/pages/account/account_view_page.dart';
33 : import 'package:app_finance/pages/account/account_page.dart';
34 : import 'package:app_finance/pages/automation/automation_page.dart';
35 : import 'package:app_finance/pages/automation/payment_add_page.dart';
36 : import 'package:app_finance/pages/automation/payment_edit_page.dart';
37 : import 'package:app_finance/pages/automation/payment_view_page.dart';
38 : import 'package:app_finance/pages/bill/bill_add_page.dart';
39 : import 'package:app_finance/pages/bill/bill_edit_page.dart';
40 : import 'package:app_finance/pages/bill/bill_page.dart';
41 : import 'package:app_finance/pages/bill/bill_scope_view_page.dart';
42 : import 'package:app_finance/pages/bill/bill_search_page.dart';
43 : import 'package:app_finance/pages/bill/bill_view_page.dart';
44 : import 'package:app_finance/pages/budget/budget_page.dart';
45 : import 'package:app_finance/pages/budget/budget_add_page.dart';
46 : import 'package:app_finance/pages/budget/budget_edit_page.dart';
47 : import 'package:app_finance/pages/budget/budget_view_page.dart';
48 : import 'package:app_finance/pages/currency/currency_add_page.dart';
49 : import 'package:app_finance/pages/currency/currency_page.dart';
50 : import 'package:app_finance/pages/goal/goal_add_page.dart';
51 : import 'package:app_finance/pages/goal/goal_edit_page.dart';
52 : import 'package:app_finance/pages/goal/goal_page.dart';
53 : import 'package:app_finance/pages/goal/goal_view_page.dart';
54 : import 'package:app_finance/pages/integration/integration_page.dart';
55 : import 'package:app_finance/pages/home/home_page.dart';
56 : import 'package:app_finance/pages/invoice/invoice_edit_page.dart';
57 : import 'package:app_finance/pages/invoice/invoice_page.dart';
58 : import 'package:app_finance/pages/invoice/invoice_search_page.dart';
59 : import 'package:app_finance/pages/invoice/invoice_transfer_page.dart';
60 : import 'package:app_finance/pages/invoice/invoice_transfer_search_page.dart';
61 : import 'package:app_finance/pages/invoice/invoice_view_page.dart';
62 : import 'package:app_finance/pages/manager/manager_page.dart';
63 : import 'package:app_finance/pages/metrics/metrics_page.dart';
64 : import 'package:app_finance/pages/security/security_page.dart';
65 : import 'package:app_finance/pages/settings/settings_page.dart';
66 : import 'package:app_finance/pages/sorting_page.dart';
67 : import 'package:app_finance/pages/start/start_page.dart';
68 : import 'package:app_finance/pages/subscription/subscription_page.dart';
69 : import 'package:app_finance/pages/tags/tags_page.dart';
70 : import 'package:firebase_analytics/firebase_analytics.dart';
71 : import 'package:firebase_core/firebase_core.dart';
72 : import 'package:firebase_crashlytics/firebase_crashlytics.dart';
73 : import 'package:flutter/foundation.dart';
74 : import 'package:material_ui/material_ui.dart';
75 : import 'package:flutter_currency_picker/flutter_currency_picker.dart';
76 : import 'package:provider/provider.dart';
77 : import 'package:shared_preferences/shared_preferences.dart';
78 :
79 : // Background import of notifications on Android, started by the service `NotificationHeadless`
80 0 : @pragma('vm:entry-point')
81 0 : Future<void> notificationMain() => NotificationBackground.run();
82 :
83 0 : void main() async {
84 : final FirebaseOptions? platform;
85 0 : if (kIsWeb || defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.android) {
86 0 : platform = DefaultFirebaseOptions.currentPlatform;
87 : } else {
88 : platform = null;
89 : }
90 0 : runZonedGuarded<Future<void>>(() async {
91 0 : WidgetsFlutterBinding.ensureInitialized();
92 :
93 : if (platform != null) {
94 0 : await _initializeFirebase(platform);
95 : }
96 :
97 0 : AppPreferences.pref = await SharedPreferences.getInstance();
98 0 : CurrencyDefaults.cache = AppPreferences.pref;
99 0 : await DeepLinkHandler.init();
100 0 : final appSync = AppSync();
101 0 : runApp(
102 0 : MultiProvider(
103 0 : providers: [
104 0 : ChangeNotifierProvider<AppSync>(
105 0 : create: (_) => appSync,
106 : ),
107 0 : ChangeNotifierProvider<AppData>(
108 0 : create: (_) => AppData(appSync),
109 : ),
110 0 : ChangeNotifierProvider<AppTheme>(
111 0 : create: (_) => AppTheme(ThemeMode.system),
112 : ),
113 0 : ChangeNotifierProvider<AppLocale>(
114 0 : create: (_) => AppLocale(),
115 : ),
116 0 : ChangeNotifierProvider<AppDesign>(
117 0 : create: (_) => AppDesign(),
118 : ),
119 0 : ChangeNotifierProvider<AppZoom>(
120 0 : create: (_) => AppZoom(),
121 : ),
122 0 : ChangeNotifierProvider<AppPalette>(
123 0 : create: (_) => AppPalette(),
124 : ),
125 0 : ChangeNotifierProvider<AppPurchase>(
126 0 : create: (_) => AppPurchase(),
127 : ),
128 0 : ChangeNotifierProvider<AppStartOfWeek>(
129 0 : create: (_) => AppStartOfWeek(),
130 : ),
131 0 : ChangeNotifierProvider<AppStartOfMonth>(
132 0 : create: (_) => AppStartOfMonth(),
133 : ),
134 0 : ChangeNotifierProvider<AppSorting>(
135 0 : create: (_) => AppSorting(),
136 : ),
137 0 : ChangeNotifierProvider<AppBudgetPositive>(
138 0 : create: (_) => AppBudgetPositive(),
139 : ),
140 : ],
141 0 : child: MyApp(platform: platform),
142 : ),
143 : );
144 0 : }, (error, stack) {
145 : try {
146 0 : if (platform != null && Firebase.apps.isNotEmpty) {
147 : if (kIsWeb) {
148 0 : FirebaseAnalytics.instance.logEvent(name: 'flutter-error', parameters: {'error': error.toString()});
149 : } else {
150 0 : FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
151 : }
152 : }
153 : } catch (e) {
154 : // Skip Firebase logging
155 : }
156 0 : FlutterError.presentError(FlutterErrorDetails(exception: error, stack: stack));
157 : // SystemNavigator.pop();
158 : });
159 : }
160 :
161 0 : Future<void> _initializeFirebase(FirebaseOptions options) async {
162 : try {
163 0 : await Firebase.initializeApp(options: options).timeout(const Duration(seconds: 10));
164 : // Enable Crashlytics only after the app is ready, otherwise any
165 : // FirebaseCrashlytics call throws [core/no-app].
166 : if (!kIsWeb) {
167 0 : await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(true);
168 : }
169 0 : FirebaseAnalytics.instance.logAppOpen();
170 0 : PlatformDispatcher.instance.onError = (error, stack) {
171 : if (kIsWeb) {
172 0 : FirebaseAnalytics.instance.logEvent(
173 : name: 'platform-error',
174 0 : parameters: {'error': error.toString(), 'trace': stack.toString()},
175 : );
176 : } else {
177 0 : FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
178 : }
179 : return true;
180 : };
181 : if (kIsWeb) {
182 0 : FlutterError.onError = (details) {
183 0 : FlutterError.presentError(details);
184 0 : FirebaseAnalytics.instance.logEvent(name: 'flutter-error', parameters: {'error': details.toString()});
185 : };
186 : } else {
187 0 : FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterFatalError;
188 : }
189 : } catch (error, stack) {
190 0 : FlutterError.presentError(FlutterErrorDetails(exception: error, stack: stack));
191 : }
192 : }
193 :
194 : class MyApp extends StatefulWidget {
195 : final FirebaseOptions? platform;
196 2 : const MyApp({super.key, this.platform});
197 :
198 6 : @override
199 6 : MyAppState createState() => MyAppState();
200 : }
201 :
202 : class MyAppState extends State<MyApp> {
203 4 : WidgetBuilder? getPage(String route, Object? arguments) {
204 : AppRoute.current = route;
205 : final args = arguments as Map<String, dynamic>?;
206 0 : final String key = args?['uuid'] ?? args?['search'] ?? ''; // TBD: deprecated
207 0 : final String uuid = args?['uuid'] ?? '';
208 0 : final String search = args?['search'] ?? '';
209 0 : final int focus = args?['focus'] ?? 1;
210 8 : if (widget.platform != null) {
211 : try {
212 0 : if (Firebase.apps.isNotEmpty) {
213 0 : FirebaseAnalytics.instance.logScreenView(
214 : screenName: route,
215 0 : parameters: args?.cast(),
216 : );
217 : }
218 : } catch (e) {
219 : // Skip Firebase logging
220 : }
221 : }
222 :
223 4 : Widget router(String route) => switch (route) {
224 4 : AppRoute.aboutRoute => AboutPage(search: key),
225 4 : AppRoute.accountRoute => const AccountPage(),
226 3 : AppRoute.accountAddRoute => const AccountAddPage(),
227 3 : AppRoute.accountViewRoute => AccountViewPage(uuid: key),
228 3 : AppRoute.accountSearchRoute => AccountPage(search: key),
229 3 : AppRoute.accountEditRoute => AccountEditPage(uuid: key),
230 3 : AppRoute.automationRoute => const AutomationPage(),
231 3 : AppRoute.automationPaymentRoute => const PaymentAddPage(),
232 3 : AppRoute.automationPaymentViewRoute => PaymentViewPage(uuid: key),
233 3 : AppRoute.automationPaymentEditRoute => PaymentEditPage(uuid: key),
234 3 : AppRoute.billRoute => const BillPage(),
235 5 : AppRoute.billAddRoute => BillAddPage(focus: focus),
236 2 : AppRoute.billViewRoute => BillViewPage(uuid: key),
237 2 : AppRoute.billEditRoute => BillEditPage(uuid: key),
238 2 : AppRoute.billSearchRoute => BillSearchPage(),
239 2 : AppRoute.billScopeViewRoute => BillScopeViewPage(search: search, uuid: uuid),
240 2 : AppRoute.budgetRoute => const BudgetPage(),
241 2 : AppRoute.budgetAddRoute => const BudgetAddPage(),
242 2 : AppRoute.budgetViewRoute => BudgetViewPage(uuid: key),
243 2 : AppRoute.budgetSearchRoute => BudgetPage(search: key),
244 2 : AppRoute.budgetEditRoute => BudgetEditPage(uuid: key),
245 2 : AppRoute.currencyRoute => const CurrencyPage(),
246 2 : AppRoute.currencyAddRoute => const CurrencyAddPage(),
247 2 : AppRoute.goalRoute => const GoalPage(),
248 1 : AppRoute.goalAddRoute => const GoalAddPage(),
249 1 : AppRoute.goalViewRoute => GoalViewPage(uuid: key),
250 1 : AppRoute.goalEditRoute => GoalEditPage(uuid: key),
251 1 : AppRoute.invoiceRoute => InvoicePage(),
252 1 : AppRoute.invoiceViewRoute => InvoiceViewPage(uuid: key),
253 1 : AppRoute.invoiceEditRoute => InvoiceEditPage(uuid: key),
254 1 : AppRoute.invoiceSearchRoute => InvoiceSearchPage(),
255 1 : AppRoute.invoiceTransferRoute => InvoiceTransferPage(),
256 1 : AppRoute.invoiceTransferSearchRoute => InvoiceTransferSearchPage(),
257 1 : AppRoute.integrationRoute => const IntegrationPage(),
258 0 : AppRoute.homeRoute => const HomePage(),
259 0 : AppRoute.managerRoute => const ManagerPage(),
260 0 : AppRoute.metricsRoute => const MetricsPage(),
261 0 : AppRoute.metricsSearchRoute => MetricsPage(search: key),
262 0 : AppRoute.securityRoute => const SecurityPage(),
263 0 : AppRoute.settingsRoute => const SettingsPage(),
264 0 : AppRoute.startRoute => const StartPage(),
265 0 : AppRoute.subscriptionRoute => const SubscriptionPage(),
266 0 : AppRoute.sortingRoute => const SortingPage(),
267 0 : AppRoute.tagsRoute => TagsPage(search: search),
268 : _ => const HomePage(),
269 : };
270 8 : return (_) => Directionality(
271 : textDirection: TextDirection.ltr,
272 4 : child: router(route),
273 : );
274 : }
275 :
276 6 : @override
277 : Widget build(BuildContext context) {
278 12 : final palette = context.watch<AppPalette>().value;
279 18 : final text = Theme.of(context).textTheme.withCustom(palette, Brightness.light);
280 18 : final textDark = Theme.of(context).textTheme.withCustom(palette, Brightness.dark);
281 18 : final sheet = Theme.of(context).bottomSheetTheme.copyWith(
282 : constraints: const BoxConstraints(maxWidth: double.infinity),
283 : );
284 6 : return MaterialApp(
285 : debugShowCheckedModeBanner: false,
286 6 : localizationsDelegates: [
287 : ...appLocalizationsDelegates,
288 6 : FallbackLocalizationDelegate(),
289 : ],
290 : supportedLocales: AppLocalizations.supportedLocales,
291 12 : locale: context.watch<AppLocale>().value,
292 6 : theme: ThemeData(
293 6 : colorScheme: const ColorScheme.light().withCustom(palette),
294 6 : floatingActionButtonTheme: const FloatingActionButtonThemeData().withCustom(palette, Brightness.light),
295 : brightness: Brightness.light,
296 : textTheme: text,
297 12 : datePickerTheme: DatePickerTheme.of(context).withCustom(palette, text, Brightness.light),
298 12 : timePickerTheme: TimePickerTheme.of(context).withCustom(palette, text, Brightness.light),
299 6 : dividerTheme: CustomDividerTheme(palette, Brightness.light),
300 : bottomSheetTheme: sheet,
301 : useMaterial3: true,
302 : ),
303 6 : darkTheme: ThemeData(
304 6 : colorScheme: const ColorScheme.dark().withCustom(palette),
305 6 : floatingActionButtonTheme: const FloatingActionButtonThemeData().withCustom(palette, Brightness.dark),
306 : brightness: Brightness.dark,
307 : textTheme: textDark,
308 12 : datePickerTheme: DatePickerTheme.of(context).withCustom(palette, textDark, Brightness.dark),
309 12 : timePickerTheme: TimePickerTheme.of(context).withCustom(palette, textDark, Brightness.dark),
310 6 : dividerTheme: CustomDividerTheme(palette, Brightness.dark),
311 : bottomSheetTheme: sheet,
312 : useMaterial3: true,
313 : ),
314 12 : themeMode: context.watch<AppTheme>().value,
315 : home: const Directionality(
316 : textDirection: TextDirection.ltr,
317 : child: HomePage(),
318 : ),
319 20 : onGenerateRoute: (settings) => AppPageRoute(builder: getPage(settings.name ?? '', settings.arguments)!),
320 : );
321 : }
322 : }
|