fixed films initialization

This commit is contained in:
Vadim 2024-11-03 17:07:25 +01:00
parent f22086e578
commit e29920b757
2 changed files with 42 additions and 31 deletions

View file

@ -39,7 +39,6 @@ class _ApplicationWrapperState extends State<ApplicationWrapper> {
late final bool hasLightSensor; late final bool hasLightSensor;
final filmsStorageService = FilmsStorageService(); final filmsStorageService = FilmsStorageService();
final filmsProviderKey = GlobalKey<FilmsProviderState>();
late final Future<void> _initFuture; late final Future<void> _initFuture;
@ -51,28 +50,28 @@ class _ApplicationWrapperState extends State<ApplicationWrapper> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FilmsProvider( return FutureBuilder(
key: filmsProviderKey, future: _initFuture,
filmsStorageService: filmsStorageService, builder: (context, snapshot) {
child: FutureBuilder( if (snapshot.error != null) {
future: _initFuture, return Center(child: Text(snapshot.error!.toString()));
builder: (context, snapshot) { } else if (snapshot.connectionState == ConnectionState.done) {
if (snapshot.error != null) { return ServicesProvider(
return Center(child: Text(snapshot.error!.toString())); analytics: const LightmeterAnalytics(api: LightmeterAnalyticsFirebase()),
} else if (snapshot.connectionState == ConnectionState.done) { caffeineService: const CaffeineService(),
return ServicesProvider( environment: widget.env.copyWith(hasLightSensor: hasLightSensor),
analytics: const LightmeterAnalytics(api: LightmeterAnalyticsFirebase()), hapticsService: const HapticsService(),
caffeineService: const CaffeineService(), lightSensorService: const LightSensorService(LocalPlatform()),
environment: widget.env.copyWith(hasLightSensor: hasLightSensor), permissionsService: const PermissionsService(),
hapticsService: const HapticsService(), userPreferencesService: userPreferencesService,
lightSensorService: const LightSensorService(LocalPlatform()), volumeEventsService: const VolumeEventsService(LocalPlatform()),
permissionsService: const PermissionsService(), child: RemoteConfigProvider(
userPreferencesService: userPreferencesService, remoteConfigService: remoteConfigService,
volumeEventsService: const VolumeEventsService(LocalPlatform()), child: EquipmentProfileProvider(
child: RemoteConfigProvider( storageService: iapStorageService,
remoteConfigService: remoteConfigService, child: FilmsProvider(
child: EquipmentProfileProvider( filmsStorageService: filmsStorageService,
storageService: iapStorageService, onInitialized: _onFilmsProviderInitialized,
child: UserPreferencesProvider( child: UserPreferencesProvider(
hasLightSensor: hasLightSensor, hasLightSensor: hasLightSensor,
userPreferencesService: userPreferencesService, userPreferencesService: userPreferencesService,
@ -80,12 +79,12 @@ class _ApplicationWrapperState extends State<ApplicationWrapper> {
), ),
), ),
), ),
); ),
} );
}
return const SizedBox(); return const SizedBox();
}, },
),
); );
} }
@ -94,13 +93,16 @@ class _ApplicationWrapperState extends State<ApplicationWrapper> {
SharedPreferences.getInstance(), SharedPreferences.getInstance(),
const LightSensorService(LocalPlatform()).hasSensor(), const LightSensorService(LocalPlatform()).hasSensor(),
remoteConfigService.activeAndFetchFeatures(), remoteConfigService.activeAndFetchFeatures(),
filmsStorageService.init().then((_) => filmsProviderKey.currentState?.init()), filmsStorageService.init(),
]).then((value) { ]).then((value) {
final sharedPrefs = (value[0] as SharedPreferences?)!; final sharedPrefs = (value[0] as SharedPreferences?)!;
iapStorageService = IAPStorageService(sharedPrefs); iapStorageService = IAPStorageService(sharedPrefs);
userPreferencesService = UserPreferencesService(sharedPrefs); userPreferencesService = UserPreferencesService(sharedPrefs);
hasLightSensor = value[1] as bool? ?? false; hasLightSensor = value[1] as bool? ?? false;
FlutterNativeSplash.remove();
}); });
} }
void _onFilmsProviderInitialized() {
FlutterNativeSplash.remove();
}
} }

View file

@ -5,10 +5,12 @@ import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
class FilmsProvider extends StatefulWidget { class FilmsProvider extends StatefulWidget {
final FilmsStorageService filmsStorageService; final FilmsStorageService filmsStorageService;
final VoidCallback? onInitialized;
final Widget child; final Widget child;
const FilmsProvider({ const FilmsProvider({
required this.filmsStorageService, required this.filmsStorageService,
this.onInitialized,
required this.child, required this.child,
super.key, super.key,
}); });
@ -28,6 +30,12 @@ class FilmsProviderState extends State<FilmsProvider> {
Film get _selectedFilm => customFilms[_selectedId]?.film ?? predefinedFilms[_selectedId]?.film ?? const FilmStub(); Film get _selectedFilm => customFilms[_selectedId]?.film ?? predefinedFilms[_selectedId]?.film ?? const FilmStub();
@override
void initState() {
super.initState();
_init();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Films( return Films(
@ -38,12 +46,13 @@ class FilmsProviderState extends State<FilmsProvider> {
); );
} }
Future<void> init() async { Future<void> _init() async {
_selectedId = widget.filmsStorageService.selectedFilmId; _selectedId = widget.filmsStorageService.selectedFilmId;
predefinedFilms.addAll(await widget.filmsStorageService.getPredefinedFilms()); predefinedFilms.addAll(await widget.filmsStorageService.getPredefinedFilms());
customFilms.addAll(await widget.filmsStorageService.getCustomFilms()); customFilms.addAll(await widget.filmsStorageService.getCustomFilms());
_discardSelectedIfNotIncluded(); _discardSelectedIfNotIncluded();
if (mounted) setState(() {}); if (mounted) setState(() {});
widget.onInitialized?.call();
} }
/* Both type of films **/ /* Both type of films **/