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,10 +50,7 @@ class _ApplicationWrapperState extends State<ApplicationWrapper> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return FilmsProvider( return FutureBuilder(
key: filmsProviderKey,
filmsStorageService: filmsStorageService,
child: FutureBuilder(
future: _initFuture, future: _initFuture,
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.error != null) { if (snapshot.error != null) {
@ -73,6 +69,9 @@ class _ApplicationWrapperState extends State<ApplicationWrapper> {
remoteConfigService: remoteConfigService, remoteConfigService: remoteConfigService,
child: EquipmentProfileProvider( child: EquipmentProfileProvider(
storageService: iapStorageService, storageService: iapStorageService,
child: FilmsProvider(
filmsStorageService: filmsStorageService,
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 **/