From e29920b7575715fe8425dabb9ea81ddecc2a22e6 Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Sun, 3 Nov 2024 17:07:25 +0100 Subject: [PATCH] fixed films initialization --- lib/application_wrapper.dart | 62 ++++++++++++++++--------------- lib/providers/films_provider.dart | 11 +++++- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/lib/application_wrapper.dart b/lib/application_wrapper.dart index ed83604..7722226 100644 --- a/lib/application_wrapper.dart +++ b/lib/application_wrapper.dart @@ -39,7 +39,6 @@ class _ApplicationWrapperState extends State { late final bool hasLightSensor; final filmsStorageService = FilmsStorageService(); - final filmsProviderKey = GlobalKey(); late final Future _initFuture; @@ -51,28 +50,28 @@ class _ApplicationWrapperState extends State { @override Widget build(BuildContext context) { - return FilmsProvider( - key: filmsProviderKey, - filmsStorageService: filmsStorageService, - child: FutureBuilder( - future: _initFuture, - builder: (context, snapshot) { - if (snapshot.error != null) { - return Center(child: Text(snapshot.error!.toString())); - } else if (snapshot.connectionState == ConnectionState.done) { - return ServicesProvider( - analytics: const LightmeterAnalytics(api: LightmeterAnalyticsFirebase()), - caffeineService: const CaffeineService(), - environment: widget.env.copyWith(hasLightSensor: hasLightSensor), - hapticsService: const HapticsService(), - lightSensorService: const LightSensorService(LocalPlatform()), - permissionsService: const PermissionsService(), - userPreferencesService: userPreferencesService, - volumeEventsService: const VolumeEventsService(LocalPlatform()), - child: RemoteConfigProvider( - remoteConfigService: remoteConfigService, - child: EquipmentProfileProvider( - storageService: iapStorageService, + return FutureBuilder( + future: _initFuture, + builder: (context, snapshot) { + if (snapshot.error != null) { + return Center(child: Text(snapshot.error!.toString())); + } else if (snapshot.connectionState == ConnectionState.done) { + return ServicesProvider( + analytics: const LightmeterAnalytics(api: LightmeterAnalyticsFirebase()), + caffeineService: const CaffeineService(), + environment: widget.env.copyWith(hasLightSensor: hasLightSensor), + hapticsService: const HapticsService(), + lightSensorService: const LightSensorService(LocalPlatform()), + permissionsService: const PermissionsService(), + userPreferencesService: userPreferencesService, + volumeEventsService: const VolumeEventsService(LocalPlatform()), + child: RemoteConfigProvider( + remoteConfigService: remoteConfigService, + child: EquipmentProfileProvider( + storageService: iapStorageService, + child: FilmsProvider( + filmsStorageService: filmsStorageService, + onInitialized: _onFilmsProviderInitialized, child: UserPreferencesProvider( hasLightSensor: hasLightSensor, userPreferencesService: userPreferencesService, @@ -80,12 +79,12 @@ class _ApplicationWrapperState extends State { ), ), ), - ); - } + ), + ); + } - return const SizedBox(); - }, - ), + return const SizedBox(); + }, ); } @@ -94,13 +93,16 @@ class _ApplicationWrapperState extends State { SharedPreferences.getInstance(), const LightSensorService(LocalPlatform()).hasSensor(), remoteConfigService.activeAndFetchFeatures(), - filmsStorageService.init().then((_) => filmsProviderKey.currentState?.init()), + filmsStorageService.init(), ]).then((value) { final sharedPrefs = (value[0] as SharedPreferences?)!; iapStorageService = IAPStorageService(sharedPrefs); userPreferencesService = UserPreferencesService(sharedPrefs); hasLightSensor = value[1] as bool? ?? false; - FlutterNativeSplash.remove(); }); } + + void _onFilmsProviderInitialized() { + FlutterNativeSplash.remove(); + } } diff --git a/lib/providers/films_provider.dart b/lib/providers/films_provider.dart index 6fa8f33..6bc3c0d 100644 --- a/lib/providers/films_provider.dart +++ b/lib/providers/films_provider.dart @@ -5,10 +5,12 @@ import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; class FilmsProvider extends StatefulWidget { final FilmsStorageService filmsStorageService; + final VoidCallback? onInitialized; final Widget child; const FilmsProvider({ required this.filmsStorageService, + this.onInitialized, required this.child, super.key, }); @@ -28,6 +30,12 @@ class FilmsProviderState extends State { Film get _selectedFilm => customFilms[_selectedId]?.film ?? predefinedFilms[_selectedId]?.film ?? const FilmStub(); + @override + void initState() { + super.initState(); + _init(); + } + @override Widget build(BuildContext context) { return Films( @@ -38,12 +46,13 @@ class FilmsProviderState extends State { ); } - Future init() async { + Future _init() async { _selectedId = widget.filmsStorageService.selectedFilmId; predefinedFilms.addAll(await widget.filmsStorageService.getPredefinedFilms()); customFilms.addAll(await widget.filmsStorageService.getCustomFilms()); _discardSelectedIfNotIncluded(); if (mounted) setState(() {}); + widget.onInitialized?.call(); } /* Both type of films **/