2023-04-03 16:02:18 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-04-05 18:46:07 +00:00
|
|
|
import 'package:lightmeter/data/models/metering_screen_layout_config.dart';
|
|
|
|
import 'package:lightmeter/data/shared_prefs_service.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
2023-04-03 16:02:18 +00:00
|
|
|
|
|
|
|
class MeteringScreenLayoutProvider extends StatefulWidget {
|
|
|
|
final Widget child;
|
|
|
|
|
|
|
|
const MeteringScreenLayoutProvider({required this.child, super.key});
|
|
|
|
|
|
|
|
static MeteringScreenLayoutProviderState of(BuildContext context) {
|
|
|
|
return context.findAncestorStateOfType<MeteringScreenLayoutProviderState>()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<MeteringScreenLayoutProvider> createState() => MeteringScreenLayoutProviderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MeteringScreenLayoutProviderState extends State<MeteringScreenLayoutProvider> {
|
2023-04-05 18:46:07 +00:00
|
|
|
late final MeteringScreenLayoutConfig _config =
|
|
|
|
context.read<UserPreferencesService>().meteringScreenLayout;
|
2023-04-03 16:02:18 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MeteringScreenLayout(
|
2023-04-05 18:46:07 +00:00
|
|
|
config: MeteringScreenLayoutConfig.from(_config),
|
2023-04-03 16:02:18 +00:00
|
|
|
child: widget.child,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-05 18:46:07 +00:00
|
|
|
void updateFeatures(MeteringScreenLayoutConfig config) {
|
2023-04-03 16:02:18 +00:00
|
|
|
setState(() {
|
2023-04-05 18:46:07 +00:00
|
|
|
config.forEach((key, value) {
|
|
|
|
_config.update(
|
2023-04-03 17:05:48 +00:00
|
|
|
key,
|
|
|
|
(_) => value,
|
|
|
|
ifAbsent: () => value,
|
|
|
|
);
|
|
|
|
});
|
2023-04-03 16:02:18 +00:00
|
|
|
});
|
2023-04-05 18:46:07 +00:00
|
|
|
context.read<UserPreferencesService>().meteringScreenLayout = _config;
|
2023-04-03 16:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MeteringScreenLayout extends InheritedModel<MeteringScreenLayoutFeature> {
|
2023-04-05 18:46:07 +00:00
|
|
|
final MeteringScreenLayoutConfig config;
|
2023-04-03 16:02:18 +00:00
|
|
|
|
|
|
|
const MeteringScreenLayout({
|
2023-04-05 18:46:07 +00:00
|
|
|
required this.config,
|
2023-04-03 16:02:18 +00:00
|
|
|
required super.child,
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
2023-04-05 18:46:07 +00:00
|
|
|
static MeteringScreenLayoutConfig of(BuildContext context, {bool listen = true}) {
|
2023-04-03 16:02:18 +00:00
|
|
|
if (listen) {
|
2023-04-05 18:46:07 +00:00
|
|
|
return context.dependOnInheritedWidgetOfExactType<MeteringScreenLayout>()!.config;
|
2023-04-03 16:02:18 +00:00
|
|
|
} else {
|
2023-04-05 18:46:07 +00:00
|
|
|
return context.findAncestorWidgetOfExactType<MeteringScreenLayout>()!.config;
|
2023-04-03 16:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 17:05:48 +00:00
|
|
|
static bool featureStatusOf(BuildContext context, MeteringScreenLayoutFeature feature) {
|
|
|
|
return InheritedModel.inheritFrom<MeteringScreenLayout>(context, aspect: feature)!
|
2023-04-05 18:46:07 +00:00
|
|
|
.config[feature]!;
|
2023-04-03 17:05:48 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 16:02:18 +00:00
|
|
|
@override
|
|
|
|
bool updateShouldNotify(MeteringScreenLayout oldWidget) => true;
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool updateShouldNotifyDependent(
|
|
|
|
MeteringScreenLayout oldWidget,
|
|
|
|
Set<MeteringScreenLayoutFeature> dependencies,
|
|
|
|
) {
|
|
|
|
for (final dependecy in dependencies) {
|
2023-04-05 18:46:07 +00:00
|
|
|
if (oldWidget.config[dependecy] != config[dependecy]) {
|
2023-04-03 16:02:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|