mirror of
https://github.com/vodemn/m3_lightmeter.git
synced 2025-08-05 04:36:41 +00:00
added LogbookPhotosProvider
This commit is contained in:
parent
435a551235
commit
73c1c0d66e
2 changed files with 111 additions and 4 deletions
|
@ -15,6 +15,7 @@ import 'package:lightmeter/data/volume_events_service.dart';
|
||||||
import 'package:lightmeter/environment.dart';
|
import 'package:lightmeter/environment.dart';
|
||||||
import 'package:lightmeter/providers/equipment_profile_provider.dart';
|
import 'package:lightmeter/providers/equipment_profile_provider.dart';
|
||||||
import 'package:lightmeter/providers/films_provider.dart';
|
import 'package:lightmeter/providers/films_provider.dart';
|
||||||
|
import 'package:lightmeter/providers/logbook_photos_provider.dart';
|
||||||
import 'package:lightmeter/providers/remote_config_provider.dart';
|
import 'package:lightmeter/providers/remote_config_provider.dart';
|
||||||
import 'package:lightmeter/providers/services_provider.dart';
|
import 'package:lightmeter/providers/services_provider.dart';
|
||||||
import 'package:lightmeter/providers/user_preferences_provider.dart';
|
import 'package:lightmeter/providers/user_preferences_provider.dart';
|
||||||
|
@ -46,6 +47,9 @@ class _ApplicationWrapperState extends State<ApplicationWrapper> {
|
||||||
final filmsStorageService = FilmsStorageService();
|
final filmsStorageService = FilmsStorageService();
|
||||||
final filmsStorageServiceCompleter = Completer<void>();
|
final filmsStorageServiceCompleter = Completer<void>();
|
||||||
|
|
||||||
|
final logbookPhotosStorageService = LogbookPhotosStorageService();
|
||||||
|
final logbookPhotosStorageServiceCompleter = Completer<void>();
|
||||||
|
|
||||||
late final Future<void> _initFuture;
|
late final Future<void> _initFuture;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -80,10 +84,14 @@ class _ApplicationWrapperState extends State<ApplicationWrapper> {
|
||||||
child: FilmsProvider(
|
child: FilmsProvider(
|
||||||
storageService: filmsStorageService,
|
storageService: filmsStorageService,
|
||||||
onInitialized: filmsStorageServiceCompleter.complete,
|
onInitialized: filmsStorageServiceCompleter.complete,
|
||||||
child: UserPreferencesProvider(
|
child: LogbookPhotosProvider(
|
||||||
hasLightSensor: hasLightSensor,
|
storageService: logbookPhotosStorageService,
|
||||||
userPreferencesService: userPreferencesService,
|
onInitialized: logbookPhotosStorageServiceCompleter.complete,
|
||||||
child: widget.child,
|
child: UserPreferencesProvider(
|
||||||
|
hasLightSensor: hasLightSensor,
|
||||||
|
userPreferencesService: userPreferencesService,
|
||||||
|
child: widget.child,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
@ -104,6 +112,7 @@ class _ApplicationWrapperState extends State<ApplicationWrapper> {
|
||||||
remoteConfigService.activeAndFetchFeatures(),
|
remoteConfigService.activeAndFetchFeatures(),
|
||||||
equipmentProfilesStorageService.init(),
|
equipmentProfilesStorageService.init(),
|
||||||
filmsStorageService.init(),
|
filmsStorageService.init(),
|
||||||
|
logbookPhotosStorageService.init(),
|
||||||
]).then((value) {
|
]).then((value) {
|
||||||
userPreferencesService = UserPreferencesService((value[0] as SharedPreferences?)!)
|
userPreferencesService = UserPreferencesService((value[0] as SharedPreferences?)!)
|
||||||
..cameraFocalLength = value[2] as int?;
|
..cameraFocalLength = value[2] as int?;
|
||||||
|
@ -115,6 +124,7 @@ class _ApplicationWrapperState extends State<ApplicationWrapper> {
|
||||||
Future.wait([
|
Future.wait([
|
||||||
equipmentProfilesStorageServiceCompleter.future,
|
equipmentProfilesStorageServiceCompleter.future,
|
||||||
filmsStorageServiceCompleter.future,
|
filmsStorageServiceCompleter.future,
|
||||||
|
logbookPhotosStorageServiceCompleter.future,
|
||||||
]).then((_) {
|
]).then((_) {
|
||||||
FlutterNativeSplash.remove();
|
FlutterNativeSplash.remove();
|
||||||
});
|
});
|
||||||
|
|
97
lib/providers/logbook_photos_provider.dart
Normal file
97
lib/providers/logbook_photos_provider.dart
Normal file
|
@ -0,0 +1,97 @@
|
||||||
|
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 LogbookPhotosProvider extends StatefulWidget {
|
||||||
|
final LogbookPhotosStorageService storageService;
|
||||||
|
final VoidCallback? onInitialized;
|
||||||
|
final Widget child;
|
||||||
|
|
||||||
|
const LogbookPhotosProvider({
|
||||||
|
required this.storageService,
|
||||||
|
this.onInitialized,
|
||||||
|
required this.child,
|
||||||
|
super.key,
|
||||||
|
});
|
||||||
|
|
||||||
|
static LogbookPhotosProviderState of(BuildContext context) {
|
||||||
|
return context.findAncestorStateOfType<LogbookPhotosProviderState>()!;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<LogbookPhotosProvider> createState() => LogbookPhotosProviderState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class LogbookPhotosProviderState extends State<LogbookPhotosProvider> {
|
||||||
|
final Map<String, LogbookPhoto> _photos = {};
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
_init();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return LogbookPhotos(
|
||||||
|
photos: context.isPro ? _photos.values.toList(growable: false) : [],
|
||||||
|
child: widget.child,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _init() async {
|
||||||
|
_photos.addAll(
|
||||||
|
Map.fromIterable(
|
||||||
|
await widget.storageService.getPhotos(),
|
||||||
|
key: (photo) => (photo as LogbookPhoto).id,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (mounted) setState(() {});
|
||||||
|
widget.onInitialized?.call();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> addPhoto(LogbookPhoto photo) async {
|
||||||
|
await widget.storageService.addPhoto(photo);
|
||||||
|
_photos[photo.id] = photo;
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> updateProfile(LogbookPhoto photo) async {
|
||||||
|
final oldProfile = _photos[photo.id]!;
|
||||||
|
if (oldProfile.note != photo.note) {
|
||||||
|
await widget.storageService.updatePhoto(
|
||||||
|
id: photo.id,
|
||||||
|
note: photo.note,
|
||||||
|
);
|
||||||
|
_photos[photo.id] = photo;
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> deleteProfile(LogbookPhoto photo) async {
|
||||||
|
await widget.storageService.deletePhoto(photo.id);
|
||||||
|
_photos.remove(photo.id);
|
||||||
|
setState(() {});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class LogbookPhotos extends InheritedWidget {
|
||||||
|
final List<LogbookPhoto> photos;
|
||||||
|
|
||||||
|
const LogbookPhotos({
|
||||||
|
required this.photos,
|
||||||
|
required super.child,
|
||||||
|
});
|
||||||
|
|
||||||
|
static List<LogbookPhoto> of(BuildContext context, {bool listen = false}) {
|
||||||
|
return (listen
|
||||||
|
? context.dependOnInheritedWidgetOfExactType<LogbookPhotos>()
|
||||||
|
: context.getInheritedWidgetOfExactType<LogbookPhotos>())!
|
||||||
|
.photos;
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool updateShouldNotify(LogbookPhotos oldWidget) => const DeepCollectionEquality().equals(oldWidget.photos, photos);
|
||||||
|
}
|
Loading…
Reference in a new issue