mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 10:27:08 +02:00
118 lines
4.3 KiB
Dart
118 lines
4.3 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart'; // Import this
|
|
import 'package:provider/provider.dart';
|
|
import 'package:shadcn_ui/shadcn_ui.dart';
|
|
import 'package:supplements/logging.dart';
|
|
|
|
import 'providers/settings_provider.dart';
|
|
import 'providers/simple_sync_provider.dart';
|
|
import 'providers/supplement_provider.dart';
|
|
import 'screens/home_screen.dart';
|
|
import 'services/notification_router.dart';
|
|
import 'services/simple_notification_service.dart';
|
|
|
|
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
|
|
|
|
// Top-level function to handle notification responses in the background
|
|
@pragma('vm:entry-point')
|
|
void notificationTapBackground(NotificationResponse notificationResponse) {
|
|
// handle action here
|
|
printLog('Background notification action tapped: ${notificationResponse.actionId}');
|
|
NotificationRouter.instance.handleNotificationResponse(notificationResponse);
|
|
}
|
|
|
|
void main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
// Initialize SimpleNotificationService early
|
|
await SimpleNotificationService.instance.initialize(
|
|
onDidReceiveBackgroundNotificationResponse: notificationTapBackground,
|
|
);
|
|
|
|
final settingsProvider = SettingsProvider();
|
|
await settingsProvider.initialize();
|
|
|
|
runApp(MyApp(settingsProvider: settingsProvider));
|
|
}
|
|
|
|
class MyApp extends StatelessWidget {
|
|
final SettingsProvider settingsProvider;
|
|
|
|
const MyApp({super.key, required this.settingsProvider});
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return MultiProvider(
|
|
providers: [
|
|
ChangeNotifierProvider(
|
|
create: (context) => SupplementProvider()..initialize(context),
|
|
),
|
|
ChangeNotifierProvider.value(
|
|
value: settingsProvider,
|
|
),
|
|
ChangeNotifierProvider(
|
|
create: (context) => SimpleSyncProvider(),
|
|
),
|
|
],
|
|
child: Consumer2<SettingsProvider, SimpleSyncProvider>(
|
|
builder: (context, settingsProvider, syncProvider, child) {
|
|
// Set up the sync completion callback to refresh supplement data
|
|
// and initialize auto-sync integration
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
final supplementProvider = context.read<SupplementProvider>();
|
|
|
|
// Initialize notification router with the app's navigator
|
|
// This is done here because navigatorKey is only available after MaterialApp is built
|
|
NotificationRouter.instance.initialize(navigatorKey);
|
|
|
|
// If the app was launched via a notification, route to the proper dialog
|
|
// This needs to be called after the router is initialized with the navigatorKey
|
|
SimpleNotificationService.instance.getLaunchDetails().then((details) {
|
|
NotificationRouter.instance.handleAppLaunchDetails(details);
|
|
});
|
|
|
|
// Set up sync completion callback
|
|
syncProvider.setOnSyncCompleteCallback(() async {
|
|
if (kDebugMode) {
|
|
printLog('Sync completed, refreshing UI data...');
|
|
}
|
|
await supplementProvider.loadSupplements();
|
|
await supplementProvider.loadTodayIntakes();
|
|
if (kDebugMode) {
|
|
printLog('UI data refreshed after sync');
|
|
}
|
|
});
|
|
|
|
// Initialize auto-sync service
|
|
syncProvider.initializeAutoSync(settingsProvider);
|
|
|
|
// Set up auto-sync callback for data changes
|
|
supplementProvider.setOnDataChangedCallback(() {
|
|
syncProvider.triggerAutoSyncIfEnabled();
|
|
});
|
|
});
|
|
|
|
return ShadApp(
|
|
navigatorKey: navigatorKey,
|
|
title: 'Supplements Tracker',
|
|
theme: ShadThemeData(
|
|
brightness: Brightness.light,
|
|
colorScheme: const ShadZincColorScheme.light(),
|
|
),
|
|
darkTheme: ShadThemeData(
|
|
brightness: Brightness.dark,
|
|
colorScheme: const ShadZincColorScheme.dark(),
|
|
),
|
|
themeMode: settingsProvider.themeMode,
|
|
home: ShadSonner(
|
|
child: const HomeScreen(),
|
|
),
|
|
debugShowCheckedModeBanner: false,
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|