fix lifecycle issues

This commit is contained in:
Fabian Freund
2025-10-14 18:11:35 +02:00
parent 13d7082a7e
commit 08b4dc3c70
+48 -52
View File
@@ -41,44 +41,11 @@ import 'package:weblibre/features/web_feed/utils/fetch_entrypoint.dart';
import 'package:weblibre/presentation/hooks/on_initialization.dart'; import 'package:weblibre/presentation/hooks/on_initialization.dart';
import 'package:weblibre/presentation/main_app.dart'; import 'package:weblibre/presentation/main_app.dart';
void main() async { class _MainWidget extends HookConsumerWidget {
WidgetsFlutterBinding.ensureInitialized(); const _MainWidget();
FlutterError.onError = (e) { @override
logger.e(e.toString(), error: e.exception, stackTrace: e.stack); Widget build(BuildContext context, WidgetRef ref) {
};
PlatformDispatcher.instance.onError = (error, stack) {
logger.e('Unhandled Error', error: error, stackTrace: stack);
return true;
};
await BackgroundFetch.registerHeadlessTask(backgroundFetch);
if (kDebugMode) {
final serviceProtocolInfo = await Service.getInfo();
logger.d('VM: ${serviceProtocolInfo.serverUri}');
}
//Ensure everything is ready
await Future.delayed(Duration.zero);
GeckoLoggingService.setUp((level, message) {
logger.log(switch (level) {
LogLevel.debug => Level.debug,
LogLevel.info => Level.info,
LogLevel.warn => Level.warning,
LogLevel.error => Level.error,
}, message);
});
await HomeWidget.setAppGroupId('weblibre');
runApp(
ProviderScope(
observers: const [ErrorObserver()],
child: HookConsumer(
builder: (context, ref, child) {
final rootKey = ref.watch(appStateKeyProvider); final rootKey = ref.watch(appStateKeyProvider);
final pauseTime = useRef<DateTime?>(null); final pauseTime = useRef<DateTime?>(null);
@@ -87,23 +54,22 @@ void main() async {
case AppLifecycleState.resumed: case AppLifecycleState.resumed:
if (pauseTime.value != null && if (pauseTime.value != null &&
DateTime.now().difference(pauseTime.value!) > DateTime.now().difference(pauseTime.value!) >
const Duration(minutes: 10)) { const Duration(minutes: 15)) {
//Rebuild widget tree after long time of inactivity //Rebuild widget tree after long time of inactivity
ref.read(appStateKeyProvider.notifier).reset(); ref.read(appStateKeyProvider.notifier).reset();
logger.i('UI reset');
} }
pauseTime.value = null; pauseTime.value = null;
case AppLifecycleState.detached: case AppLifecycleState.detached:
case AppLifecycleState.inactive: case AppLifecycleState.inactive:
case AppLifecycleState.hidden: case AppLifecycleState.hidden:
case AppLifecycleState.paused: case AppLifecycleState.paused:
pauseTime.value = DateTime.now(); pauseTime.value ??= DateTime.now();
} }
}); });
final themeMode = ref.watch( final themeMode = ref.watch(
generalSettingsWithDefaultsProvider.select( generalSettingsWithDefaultsProvider.select((value) => value.themeMode),
(value) => value.themeMode,
),
); );
useOnInitialization(() async { useOnInitialization(() async {
@@ -117,9 +83,7 @@ void main() async {
engineSettings.addonCollection, engineSettings.addonCollection,
); );
await ref await ref.read(appInitializationServiceProvider.notifier).initialize();
.read(appInitializationServiceProvider.notifier)
.initialize();
await BackgroundFetch.configure( await BackgroundFetch.configure(
BackgroundFetchConfig( BackgroundFetchConfig(
@@ -170,10 +134,7 @@ void main() async {
return MainApp( return MainApp(
key: rootKey, key: rootKey,
theme: ThemeData( theme: ThemeData(useMaterial3: true, colorScheme: lightColorScheme),
useMaterial3: true,
colorScheme: lightColorScheme,
),
darkTheme: ThemeData( darkTheme: ThemeData(
useMaterial3: true, useMaterial3: true,
colorScheme: darkColorScheme, colorScheme: darkColorScheme,
@@ -182,8 +143,43 @@ void main() async {
); );
}, },
); );
}, }
), }
),
void main() async {
WidgetsFlutterBinding.ensureInitialized();
FlutterError.onError = (e) {
logger.e(e.toString(), error: e.exception, stackTrace: e.stack);
};
PlatformDispatcher.instance.onError = (error, stack) {
logger.e('Unhandled Error', error: error, stackTrace: stack);
return true;
};
await BackgroundFetch.registerHeadlessTask(backgroundFetch);
if (kDebugMode) {
final serviceProtocolInfo = await Service.getInfo();
logger.d('VM: ${serviceProtocolInfo.serverUri}');
}
//Ensure everything is ready
await Future.delayed(Duration.zero);
GeckoLoggingService.setUp((level, message) {
logger.log(switch (level) {
LogLevel.debug => Level.debug,
LogLevel.info => Level.info,
LogLevel.warn => Level.warning,
LogLevel.error => Level.error,
}, message);
});
await HomeWidget.setAppGroupId('weblibre');
runApp(
const ProviderScope(observers: [ErrorObserver()], child: _MainWidget()),
); );
} }