import 'package:flutter/material.dart'; import 'package:lightmeter/utils/context_utils.dart'; import 'package:lightmeter/utils/selectable_provider.dart'; import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart'; import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart'; class FilmsProvider extends StatefulWidget { final IAPStorageService storageService; final List? availableFilms; final Widget child; const FilmsProvider({ required this.storageService, this.availableFilms, required this.child, super.key, }); static FilmsProviderState of(BuildContext context) { return context.findAncestorStateOfType()!; } @override State createState() => FilmsProviderState(); } class FilmsProviderState extends State { late List _filmsInUse; late Film _selected; @override void initState() { super.initState(); _filmsInUse = widget.storageService.filmsInUse; _selected = widget.storageService.selectedFilm; _discardSelectedIfNotIncluded(); } @override Widget build(BuildContext context) { return Films( values: [ const FilmStub(), ...widget.availableFilms ?? films, ], filmsInUse: [ const FilmStub(), if (context.isPro) ..._filmsInUse, ], selected: context.isPro ? _selected : const FilmStub(), child: widget.child, ); } void setFilm(Film film) { if (_selected != film) { _selected = film; widget.storageService.selectedFilm = film; setState(() {}); } } void saveFilms(List films) { _filmsInUse = films; widget.storageService.filmsInUse = films; _discardSelectedIfNotIncluded(); setState(() {}); } void _discardSelectedIfNotIncluded() { if (_selected != const FilmStub() && !_filmsInUse.contains(_selected)) { _selected = const FilmStub(); widget.storageService.selectedFilm = const FilmStub(); } } } class Films extends SelectableInheritedModel { final List filmsInUse; const Films({ super.key, required super.values, required this.filmsInUse, required super.selected, required super.child, }); /// [FilmStub()] + all the custom fields with actual reciprocity formulas static List of(BuildContext context) { return InheritedModel.inheritFrom(context)!.values; } /// [FilmStub()] + films in use selected by user static List inUseOf(BuildContext context) { return InheritedModel.inheritFrom( context, aspect: SelectableAspect.list, )! .filmsInUse; } static Film selectedOf(BuildContext context) { return InheritedModel.inheritFrom(context, aspect: SelectableAspect.selected)!.selected; } }