m3_lightmeter/lib/providers/films_provider.dart
Vadim c66381f813
ML-191 Add an ability to add a generic film, that will accept a formula (#195)
* sync with resources

* separated `ExpandableSectionList` as widget

* fixed generic type

* implemented `FilmsScreen` (wip)

* made `SliverScreen` title a widget

* [`FilmEditScreen`] wip

* [`FilmEditScreen`] added validation

* fixed title overflow for `SliverScreen`

* [`FilmEditScreen`] separated add and edit blocs

* [`FilmEditScreen`] split into separate components

* added bottom widget to `SliverScreen`

* implemented films list tabs fo `FilmsScreen`

* added films screen to navigation

* replaced explicit routes names with enum values

* implemented CRUD for custom films

* added placeholder for empty custom films list

* added `FilmsStorageService`

* fixed unit tests

* fixed integration tests

* lint

* fixed golden tests

* added iap stub methods

* added custom films to features list

* use 2.0.0 resouces

* fixed film picket tests

* migrated to iap 1.0.1

* autofocus film name field

* wait for the film to edited

* migrated to iap 1.1.0

* typo

* wait for storage initialization

* migrated to iap 1.1.1

* fixed films initialization

* added conditions to films model `updateShouldNotifyDependent`

* typo

* fixed select film discard notify

* covered films model `updateShouldNotifyDependent`
2024-11-03 20:16:01 +01:00

180 lines
5.7 KiB
Dart

import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:lightmeter/utils/context_utils.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
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,
});
static FilmsProviderState of(BuildContext context) {
return context.findAncestorStateOfType<FilmsProviderState>()!;
}
@override
State<FilmsProvider> createState() => FilmsProviderState();
}
class FilmsProviderState extends State<FilmsProvider> {
final Map<String, SelectableFilm<Film>> predefinedFilms = {};
final Map<String, SelectableFilm<FilmExponential>> customFilms = {};
String _selectedId = '';
Film get _selectedFilm => customFilms[_selectedId]?.film ?? predefinedFilms[_selectedId]?.film ?? const FilmStub();
@override
void initState() {
super.initState();
_init();
}
@override
Widget build(BuildContext context) {
return Films(
predefinedFilms: context.isPro ? predefinedFilms : {},
customFilms: context.isPro ? customFilms : {},
selected: context.isPro ? _selectedFilm : const FilmStub(),
child: widget.child,
);
}
Future<void> _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 **/
Future<void> toggleFilm(Film film, bool enabled) async {
if (predefinedFilms.containsKey(film.id)) {
predefinedFilms[film.id] = (film: film, isUsed: enabled);
} else if (customFilms.containsKey(film.id)) {
customFilms[film.id] = (film: film as FilmExponential, isUsed: enabled);
} else {
return;
}
await widget.filmsStorageService.toggleFilm(film, enabled);
_discardSelectedIfNotIncluded();
setState(() {});
}
void selectFilm(Film film) {
if (_selectedFilm != film) {
_selectedId = film.id;
widget.filmsStorageService.selectedFilmId = _selectedId;
setState(() {});
}
}
/* Custom films **/
Future<void> addCustomFilm(FilmExponential film) async {
// ignore: avoid_redundant_argument_values
await widget.filmsStorageService.addFilm(film, isUsed: true);
customFilms[film.id] = (film: film, isUsed: true);
setState(() {});
}
Future<void> updateCustomFilm(FilmExponential film) async {
await widget.filmsStorageService.updateFilm(film);
customFilms[film.id] = (film: film, isUsed: customFilms[film.id]!.isUsed);
setState(() {});
}
Future<void> deleteCustomFilm(FilmExponential film) async {
await widget.filmsStorageService.deleteFilm(film);
customFilms.remove(film.id);
_discardSelectedIfNotIncluded();
setState(() {});
}
void _discardSelectedIfNotIncluded() {
if (_selectedId == const FilmStub().id) {
return;
}
final isSelectedUsed = predefinedFilms[_selectedId]?.isUsed ?? customFilms[_selectedId]?.isUsed ?? false;
if (!isSelectedUsed) {
_selectedId = const FilmStub().id;
widget.filmsStorageService.selectedFilmId = _selectedId;
}
}
}
enum _FilmsModelAspect {
customFilms,
predefinedFilms,
filmsInUse,
selected,
}
class Films extends InheritedModel<_FilmsModelAspect> {
final Map<String, SelectableFilm<Film>> predefinedFilms;
@protected
final Map<String, SelectableFilm<FilmExponential>> customFilms;
final Film selected;
const Films({
required this.predefinedFilms,
required this.customFilms,
required this.selected,
required super.child,
});
static List<Film> predefinedFilmsOf<T>(BuildContext context) {
return InheritedModel.inheritFrom<Films>(context, aspect: _FilmsModelAspect.predefinedFilms)!
.predefinedFilms
.values
.map((value) => value.film)
.toList();
}
static List<FilmExponential> customFilmsOf<T>(BuildContext context) {
return InheritedModel.inheritFrom<Films>(context, aspect: _FilmsModelAspect.customFilms)!
.customFilms
.values
.map((value) => value.film)
.toList();
}
/// [FilmStub()] + films in use selected by user
static List<Film> inUseOf<T>(BuildContext context) {
final model = InheritedModel.inheritFrom<Films>(context, aspect: _FilmsModelAspect.filmsInUse)!;
return [
const FilmStub(),
...model.customFilms.values.where((e) => e.isUsed).map((e) => e.film),
...model.predefinedFilms.values.where((e) => e.isUsed).map((e) => e.film),
];
}
static Film selectedOf(BuildContext context) {
return InheritedModel.inheritFrom<Films>(context, aspect: _FilmsModelAspect.selected)!.selected;
}
@override
bool updateShouldNotify(Films _) => true;
@override
bool updateShouldNotifyDependent(Films oldWidget, Set<_FilmsModelAspect> dependencies) {
return (dependencies.contains(_FilmsModelAspect.selected) && oldWidget.selected != selected) ||
((dependencies.contains(_FilmsModelAspect.predefinedFilms) ||
dependencies.contains(_FilmsModelAspect.filmsInUse)) &&
const DeepCollectionEquality().equals(oldWidget.predefinedFilms, predefinedFilms)) ||
((dependencies.contains(_FilmsModelAspect.customFilms) ||
dependencies.contains(_FilmsModelAspect.filmsInUse)) &&
const DeepCollectionEquality().equals(oldWidget.customFilms, customFilms));
}
}