2024-11-03 19:16:01 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2023-10-20 14:12:43 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-01-15 19:47:10 +00:00
|
|
|
import 'package:lightmeter/utils/context_utils.dart';
|
2023-10-20 14:12:43 +00:00
|
|
|
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
|
|
|
|
import 'package:m3_lightmeter_resources/m3_lightmeter_resources.dart';
|
|
|
|
|
|
|
|
class FilmsProvider extends StatefulWidget {
|
2024-11-03 19:16:01 +00:00
|
|
|
final FilmsStorageService filmsStorageService;
|
|
|
|
final VoidCallback? onInitialized;
|
2023-10-20 14:12:43 +00:00
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
const FilmsProvider({
|
2024-11-03 19:16:01 +00:00
|
|
|
required this.filmsStorageService,
|
|
|
|
this.onInitialized,
|
2023-10-20 14:12:43 +00:00
|
|
|
required this.child,
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
|
|
|
static FilmsProviderState of(BuildContext context) {
|
|
|
|
return context.findAncestorStateOfType<FilmsProviderState>()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<FilmsProvider> createState() => FilmsProviderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class FilmsProviderState extends State<FilmsProvider> {
|
2024-11-03 19:16:01 +00:00
|
|
|
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();
|
2023-10-20 14:12:43 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-11-03 19:16:01 +00:00
|
|
|
_init();
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Films(
|
2024-11-03 19:16:01 +00:00
|
|
|
predefinedFilms: context.isPro ? predefinedFilms : {},
|
|
|
|
customFilms: context.isPro ? customFilms : {},
|
|
|
|
selected: context.isPro ? _selectedFilm : const FilmStub(),
|
2023-10-20 14:12:43 +00:00
|
|
|
child: widget.child,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-03 19:16:01 +00:00
|
|
|
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;
|
2023-10-20 14:12:43 +00:00
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-03 19:16:01 +00:00
|
|
|
/* 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);
|
2023-10-20 14:12:43 +00:00
|
|
|
_discardSelectedIfNotIncluded();
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
void _discardSelectedIfNotIncluded() {
|
2024-11-03 19:16:01 +00:00
|
|
|
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;
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-03 19:16:01 +00:00
|
|
|
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;
|
2023-10-20 14:12:43 +00:00
|
|
|
|
|
|
|
const Films({
|
2024-11-03 19:16:01 +00:00
|
|
|
required this.predefinedFilms,
|
|
|
|
required this.customFilms,
|
|
|
|
required this.selected,
|
2023-10-20 14:12:43 +00:00
|
|
|
required super.child,
|
|
|
|
});
|
|
|
|
|
2024-11-03 19:16:01 +00:00
|
|
|
static List<Film> predefinedFilmsOf<T>(BuildContext context) {
|
|
|
|
return InheritedModel.inheritFrom<Films>(context, aspect: _FilmsModelAspect.predefinedFilms)!
|
|
|
|
.predefinedFilms
|
|
|
|
.values
|
|
|
|
.map((value) => value.film)
|
|
|
|
.toList();
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
2024-11-03 19:16:01 +00:00
|
|
|
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
|
2023-10-20 14:12:43 +00:00
|
|
|
static List<Film> inUseOf<T>(BuildContext context) {
|
2024-11-03 19:16:01 +00:00
|
|
|
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),
|
|
|
|
];
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Film selectedOf(BuildContext context) {
|
2024-11-03 19:16:01 +00:00
|
|
|
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));
|
2023-10-20 14:12:43 +00:00
|
|
|
}
|
|
|
|
}
|