浏览代码

Store state of selected radius instead of selected radius index for a bug fix

ashilkn 2 年之前
父节点
当前提交
7b5b39997f

+ 1 - 1
lib/core/constants.dart

@@ -62,6 +62,6 @@ const kilometersPerDegree = 111.16;
 
 
 const defaultRadiusValues = <double>[1, 2, 10, 20, 40, 80, 200, 400, 1200];
 const defaultRadiusValues = <double>[1, 2, 10, 20, 40, 80, 200, 400, 1200];
 
 
-const defaultRadiusValueIndex = 4;
+const defaultRadiusValue = 40.0;
 
 
 const galleryGridSpacing = 2.0;
 const galleryGridSpacing = 2.0;

+ 7 - 4
lib/models/typedefs.dart

@@ -2,11 +2,14 @@ import 'dart:async';
 
 
 import "package:photos/models/location/location.dart";
 import "package:photos/models/location/location.dart";
 
 
-typedef FutureVoidCallback = Future<void> Function();
 typedef BoolCallBack = bool Function();
 typedef BoolCallBack = bool Function();
-typedef FutureVoidCallbackParamStr = Future<void> Function(String);
+
 typedef VoidCallbackParamStr = void Function(String);
 typedef VoidCallbackParamStr = void Function(String);
-typedef FutureOrVoidCallback = FutureOr<void> Function();
 typedef VoidCallbackParamInt = void Function(int);
 typedef VoidCallbackParamInt = void Function(int);
-typedef VoidCallbackParamLocation = void Function(Location);
+typedef VoidCallbackParamDouble = Function(double);
 typedef VoidCallbackParamListDouble = void Function(List<double>);
 typedef VoidCallbackParamListDouble = void Function(List<double>);
+typedef VoidCallbackParamLocation = void Function(Location);
+
+typedef FutureVoidCallback = Future<void> Function();
+typedef FutureOrVoidCallback = FutureOr<void> Function();
+typedef FutureVoidCallbackParamStr = Future<void> Function(String);

+ 19 - 16
lib/states/location_state.dart

@@ -28,7 +28,8 @@ class LocationTagStateProvider extends StatefulWidget {
 }
 }
 
 
 class _LocationTagStateProviderState extends State<LocationTagStateProvider> {
 class _LocationTagStateProviderState extends State<LocationTagStateProvider> {
-  int _selectedRaduisIndex = defaultRadiusValueIndex;
+  late double _selectedRadius;
+
   late Location? _centerPoint;
   late Location? _centerPoint;
   late LocalEntity<LocationTag>? _locationTagEntity;
   late LocalEntity<LocationTag>? _locationTagEntity;
   final Debouncer _selectedRadiusDebouncer =
   final Debouncer _selectedRadiusDebouncer =
@@ -47,9 +48,9 @@ class _LocationTagStateProviderState extends State<LocationTagStateProvider> {
     ///value to the list of default radius values only for this location tag and
     ///value to the list of default radius values only for this location tag and
     ///keep it in the state of this widget.
     ///keep it in the state of this widget.
     _radiusValues = _getRadiusValuesOfLocTag(_locationTagEntity?.item.radius);
     _radiusValues = _getRadiusValuesOfLocTag(_locationTagEntity?.item.radius);
-    _selectedRaduisIndex =
-        _locationTagEntity?.item.radiusIndex(_radiusValues) ??
-            defaultRadiusValueIndex;
+
+    _selectedRadius = _locationTagEntity?.item.radius ?? defaultRadiusValue;
+
     _locTagEntityListener =
     _locTagEntityListener =
         Bus.instance.on<LocationTagUpdatedEvent>().listen((event) {
         Bus.instance.on<LocationTagUpdatedEvent>().listen((event) {
       _locationTagUpdateListener(event);
       _locationTagUpdateListener(event);
@@ -66,11 +67,11 @@ class _LocationTagStateProviderState extends State<LocationTagStateProvider> {
   void _locationTagUpdateListener(LocationTagUpdatedEvent event) {
   void _locationTagUpdateListener(LocationTagUpdatedEvent event) {
     if (event.type == LocTagEventType.update) {
     if (event.type == LocTagEventType.update) {
       if (event.updatedLocTagEntities!.first.id == _locationTagEntity!.id) {
       if (event.updatedLocTagEntities!.first.id == _locationTagEntity!.id) {
-        //Update state when locationTag is updated.
         setState(() {
         setState(() {
           final updatedLocTagEntity = event.updatedLocTagEntities!.first;
           final updatedLocTagEntity = event.updatedLocTagEntities!.first;
-          _selectedRaduisIndex =
-              updatedLocTagEntity.item.radiusIndex(_radiusValues);
+
+          _selectedRadius = updatedLocTagEntity.item.radius;
+
           _centerPoint = updatedLocTagEntity.item.centerPoint;
           _centerPoint = updatedLocTagEntity.item.centerPoint;
           _locationTagEntity = updatedLocTagEntity;
           _locationTagEntity = updatedLocTagEntity;
         });
         });
@@ -78,12 +79,12 @@ class _LocationTagStateProviderState extends State<LocationTagStateProvider> {
     }
     }
   }
   }
 
 
-  void _updateSelectedIndex(int index) {
+  void _updateSelectedRadius(double radius) {
     _selectedRadiusDebouncer.cancelDebounce();
     _selectedRadiusDebouncer.cancelDebounce();
     _selectedRadiusDebouncer.run(() async {
     _selectedRadiusDebouncer.run(() async {
       if (mounted) {
       if (mounted) {
         setState(() {
         setState(() {
-          _selectedRaduisIndex = index;
+          _selectedRadius = radius;
         });
         });
       }
       }
     });
     });
@@ -126,9 +127,9 @@ class _LocationTagStateProviderState extends State<LocationTagStateProvider> {
   @override
   @override
   Widget build(BuildContext context) {
   Widget build(BuildContext context) {
     return InheritedLocationTagData(
     return InheritedLocationTagData(
-      _selectedRaduisIndex,
+      _selectedRadius,
       _centerPoint!,
       _centerPoint!,
-      _updateSelectedIndex,
+      _updateSelectedRadius,
       _locationTagEntity,
       _locationTagEntity,
       _updateCenterPoint,
       _updateCenterPoint,
       _updateRadiusValues,
       _updateRadiusValues,
@@ -140,18 +141,18 @@ class _LocationTagStateProviderState extends State<LocationTagStateProvider> {
 
 
 ///This InheritedWidget's state is used in add & edit location sheets
 ///This InheritedWidget's state is used in add & edit location sheets
 class InheritedLocationTagData extends InheritedWidget {
 class InheritedLocationTagData extends InheritedWidget {
-  final int selectedRadiusIndex;
+  final double selectedRadius;
   final Location centerPoint;
   final Location centerPoint;
   //locationTag is null when we are creating a new location tag in add location sheet
   //locationTag is null when we are creating a new location tag in add location sheet
   final LocalEntity<LocationTag>? locationTagEntity;
   final LocalEntity<LocationTag>? locationTagEntity;
-  final VoidCallbackParamInt updateSelectedIndex;
+  final VoidCallbackParamDouble updateSelectedRadius;
   final VoidCallbackParamLocation updateCenterPoint;
   final VoidCallbackParamLocation updateCenterPoint;
   final VoidCallbackParamListDouble updateRadiusValues;
   final VoidCallbackParamListDouble updateRadiusValues;
   final List<double> radiusValues;
   final List<double> radiusValues;
   const InheritedLocationTagData(
   const InheritedLocationTagData(
-    this.selectedRadiusIndex,
+    this.selectedRadius,
     this.centerPoint,
     this.centerPoint,
-    this.updateSelectedIndex,
+    this.updateSelectedRadius,
     this.locationTagEntity,
     this.locationTagEntity,
     this.updateCenterPoint,
     this.updateCenterPoint,
     this.updateRadiusValues,
     this.updateRadiusValues,
@@ -167,7 +168,9 @@ class InheritedLocationTagData extends InheritedWidget {
 
 
   @override
   @override
   bool updateShouldNotify(InheritedLocationTagData oldWidget) {
   bool updateShouldNotify(InheritedLocationTagData oldWidget) {
-    return oldWidget.selectedRadiusIndex != selectedRadiusIndex ||
+    print(selectedRadius);
+    print(oldWidget.selectedRadius != selectedRadius);
+    return oldWidget.selectedRadius != selectedRadius ||
         !oldWidget.radiusValues.equals(radiusValues) ||
         !oldWidget.radiusValues.equals(radiusValues) ||
         oldWidget.centerPoint != centerPoint ||
         oldWidget.centerPoint != centerPoint ||
         oldWidget.locationTagEntity != locationTagEntity;
         oldWidget.locationTagEntity != locationTagEntity;

+ 14 - 11
lib/ui/viewer/location/add_location_sheet.dart

@@ -51,14 +51,17 @@ class AddLocationSheet extends StatefulWidget {
 }
 }
 
 
 class _AddLocationSheetState extends State<AddLocationSheet> {
 class _AddLocationSheetState extends State<AddLocationSheet> {
-  //The value of these notifiers has no significance.
+  //The value of this notifier has no significance.
   //When memoriesCountNotifier is null, we show the loading widget in the
   //When memoriesCountNotifier is null, we show the loading widget in the
   //memories count section which also means the gallery is loading.
   //memories count section which also means the gallery is loading.
   final ValueNotifier<int?> _memoriesCountNotifier = ValueNotifier(null);
   final ValueNotifier<int?> _memoriesCountNotifier = ValueNotifier(null);
+
+  //The value of this notifier has no significance.
   final ValueNotifier<bool> _submitNotifer = ValueNotifier(false);
   final ValueNotifier<bool> _submitNotifer = ValueNotifier(false);
+
   final ValueNotifier<bool> _cancelNotifier = ValueNotifier(false);
   final ValueNotifier<bool> _cancelNotifier = ValueNotifier(false);
-  final ValueNotifier<int> _selectedRadiusIndexNotifier =
-      ValueNotifier(defaultRadiusValueIndex);
+  final ValueNotifier<double> _selectedRadiusNotifier =
+      ValueNotifier(defaultRadiusValue);
   final _focusNode = FocusNode();
   final _focusNode = FocusNode();
   final _textEditingController = TextEditingController();
   final _textEditingController = TextEditingController();
   final _isEmptyNotifier = ValueNotifier(true);
   final _isEmptyNotifier = ValueNotifier(true);
@@ -67,7 +70,7 @@ class _AddLocationSheetState extends State<AddLocationSheet> {
   @override
   @override
   void initState() {
   void initState() {
     _focusNode.addListener(_focusNodeListener);
     _focusNode.addListener(_focusNodeListener);
-    _selectedRadiusIndexNotifier.addListener(_selectedRadiusIndexListener);
+    _selectedRadiusNotifier.addListener(_selectedRadiusListener);
     super.initState();
     super.initState();
   }
   }
 
 
@@ -76,7 +79,7 @@ class _AddLocationSheetState extends State<AddLocationSheet> {
     _focusNode.removeListener(_focusNodeListener);
     _focusNode.removeListener(_focusNodeListener);
     _submitNotifer.dispose();
     _submitNotifer.dispose();
     _cancelNotifier.dispose();
     _cancelNotifier.dispose();
-    _selectedRadiusIndexNotifier.dispose();
+    _selectedRadiusNotifier.dispose();
     super.dispose();
     super.dispose();
   }
   }
 
 
@@ -149,7 +152,7 @@ class _AddLocationSheetState extends State<AddLocationSheet> {
                         ),
                         ),
                         const SizedBox(height: 24),
                         const SizedBox(height: 24),
                         RadiusPickerWidget(
                         RadiusPickerWidget(
-                          _selectedRadiusIndexNotifier,
+                          _selectedRadiusNotifier,
                         ),
                         ),
                         const SizedBox(height: 16),
                         const SizedBox(height: 16),
                         Text(
                         Text(
@@ -230,8 +233,8 @@ class _AddLocationSheetState extends State<AddLocationSheet> {
   Future<void> _addLocationTag() async {
   Future<void> _addLocationTag() async {
     final locationData = InheritedLocationTagData.of(context);
     final locationData = InheritedLocationTagData.of(context);
     final coordinates = locationData.centerPoint;
     final coordinates = locationData.centerPoint;
-    final radiusValues = locationData.radiusValues;
-    final radius = radiusValues[locationData.selectedRadiusIndex];
+    final radius = locationData.selectedRadius;
+
     await LocationService.instance.addLocation(
     await LocationService.instance.addLocation(
       _textEditingController.text.trim(),
       _textEditingController.text.trim(),
       coordinates,
       coordinates,
@@ -257,11 +260,11 @@ class _AddLocationSheetState extends State<AddLocationSheet> {
     }
     }
   }
   }
 
 
-  void _selectedRadiusIndexListener() {
+  void _selectedRadiusListener() {
     InheritedLocationTagData.of(
     InheritedLocationTagData.of(
       context,
       context,
-    ).updateSelectedIndex(
-      _selectedRadiusIndexNotifier.value,
+    ).updateSelectedRadius(
+      _selectedRadiusNotifier.value,
     );
     );
     _memoriesCountNotifier.value = null;
     _memoriesCountNotifier.value = null;
   }
   }

+ 1 - 6
lib/ui/viewer/location/dynamic_location_gallery_widget.dart

@@ -55,7 +55,7 @@ class _DynamicLocationGalleryWidgetState
   @override
   @override
   Widget build(BuildContext context) {
   Widget build(BuildContext context) {
     const galleryFilesLimit = 1000;
     const galleryFilesLimit = 1000;
-    final selectedRadius = _selectedRadius();
+    final selectedRadius = InheritedLocationTagData.of(context).selectedRadius;
     Future<FileLoadResult> filterFiles() async {
     Future<FileLoadResult> filterFiles() async {
       final FileLoadResult result = await fileLoadResult;
       final FileLoadResult result = await fileLoadResult;
       //wait for ignored files to be removed after init
       //wait for ignored files to be removed after init
@@ -121,11 +121,6 @@ class _DynamicLocationGalleryWidgetState
     );
     );
   }
   }
 
 
-  double _selectedRadius() {
-    final locationTagState = InheritedLocationTagData.of(context);
-    return locationTagState.radiusValues[locationTagState.selectedRadiusIndex];
-  }
-
   double _galleryHeight(int fileCount) {
   double _galleryHeight(int fileCount) {
     final photoGridSize = LocalSettings.instance.getPhotoGridSize();
     final photoGridSize = LocalSettings.instance.getPhotoGridSize();
     final totalWhiteSpaceBetweenPhotos =
     final totalWhiteSpaceBetweenPhotos =

+ 9 - 10
lib/ui/viewer/location/edit_location_sheet.dart

@@ -61,8 +61,8 @@ class _EditLocationSheetState extends State<EditLocationSheet> {
   final ValueNotifier<int?> _memoriesCountNotifier = ValueNotifier(null);
   final ValueNotifier<int?> _memoriesCountNotifier = ValueNotifier(null);
   final ValueNotifier<bool> _submitNotifer = ValueNotifier(false);
   final ValueNotifier<bool> _submitNotifer = ValueNotifier(false);
   final ValueNotifier<bool> _cancelNotifier = ValueNotifier(false);
   final ValueNotifier<bool> _cancelNotifier = ValueNotifier(false);
-  final ValueNotifier<int> _selectedRadiusIndexNotifier =
-      ValueNotifier(defaultRadiusValueIndex);
+  final ValueNotifier<double> _selectedRadiusNotifier =
+      ValueNotifier(defaultRadiusValue);
   final _focusNode = FocusNode();
   final _focusNode = FocusNode();
   final _textEditingController = TextEditingController();
   final _textEditingController = TextEditingController();
   final _isEmptyNotifier = ValueNotifier(false);
   final _isEmptyNotifier = ValueNotifier(false);
@@ -71,7 +71,7 @@ class _EditLocationSheetState extends State<EditLocationSheet> {
   @override
   @override
   void initState() {
   void initState() {
     _focusNode.addListener(_focusNodeListener);
     _focusNode.addListener(_focusNodeListener);
-    _selectedRadiusIndexNotifier.addListener(_selectedRadiusIndexListener);
+    _selectedRadiusNotifier.addListener(_selectedRadiusListener);
     super.initState();
     super.initState();
   }
   }
 
 
@@ -80,7 +80,7 @@ class _EditLocationSheetState extends State<EditLocationSheet> {
     _focusNode.removeListener(_focusNodeListener);
     _focusNode.removeListener(_focusNodeListener);
     _submitNotifer.dispose();
     _submitNotifer.dispose();
     _cancelNotifier.dispose();
     _cancelNotifier.dispose();
-    _selectedRadiusIndexNotifier.dispose();
+    _selectedRadiusNotifier.dispose();
     super.dispose();
     super.dispose();
   }
   }
 
 
@@ -162,7 +162,7 @@ class _EditLocationSheetState extends State<EditLocationSheet> {
                         const EditCenterPointTileWidget(),
                         const EditCenterPointTileWidget(),
                         const SizedBox(height: 20),
                         const SizedBox(height: 20),
                         RadiusPickerWidget(
                         RadiusPickerWidget(
-                          _selectedRadiusIndexNotifier,
+                          _selectedRadiusNotifier,
                         ),
                         ),
                         const SizedBox(height: 16),
                         const SizedBox(height: 16),
                       ],
                       ],
@@ -240,8 +240,7 @@ class _EditLocationSheetState extends State<EditLocationSheet> {
     final locationTagState = InheritedLocationTagData.of(context);
     final locationTagState = InheritedLocationTagData.of(context);
     await LocationService.instance.updateLocationTag(
     await LocationService.instance.updateLocationTag(
       locationTagEntity: locationTagState.locationTagEntity!,
       locationTagEntity: locationTagState.locationTagEntity!,
-      newRadius:
-          locationTagState.radiusValues[locationTagState.selectedRadiusIndex],
+      newRadius: locationTagState.selectedRadius,
       newName: _textEditingController.text.trim(),
       newName: _textEditingController.text.trim(),
       newCenterPoint: InheritedLocationTagData.of(context).centerPoint,
       newCenterPoint: InheritedLocationTagData.of(context).centerPoint,
     );
     );
@@ -265,11 +264,11 @@ class _EditLocationSheetState extends State<EditLocationSheet> {
     }
     }
   }
   }
 
 
-  void _selectedRadiusIndexListener() {
+  void _selectedRadiusListener() {
     InheritedLocationTagData.of(
     InheritedLocationTagData.of(
       context,
       context,
-    ).updateSelectedIndex(
-      _selectedRadiusIndexNotifier.value,
+    ).updateSelectedRadius(
+      _selectedRadiusNotifier.value,
     );
     );
     _memoriesCountNotifier.value = null;
     _memoriesCountNotifier.value = null;
   }
   }

+ 12 - 16
lib/ui/viewer/location/radius_picker_widget.dart

@@ -23,11 +23,11 @@ class CustomTrackShape extends RoundedRectSliderTrackShape {
 }
 }
 
 
 class RadiusPickerWidget extends StatefulWidget {
 class RadiusPickerWidget extends StatefulWidget {
-  ///This notifier can be listened to get the selected radius index from
-  ///a parent widget.
-  final ValueNotifier<int> selectedRadiusIndexNotifier;
+  ///This notifier can be listened from a parent widget to get the selected radius
+  final ValueNotifier<double> selectedRadiusNotifier;
+
   const RadiusPickerWidget(
   const RadiusPickerWidget(
-    this.selectedRadiusIndexNotifier, {
+    this.selectedRadiusNotifier, {
     super.key,
     super.key,
   });
   });
 
 
@@ -44,19 +44,18 @@ class _RadiusPickerWidgetState extends State<RadiusPickerWidget> {
 
 
   @override
   @override
   void didChangeDependencies() {
   void didChangeDependencies() {
-    widget.selectedRadiusIndexNotifier.value =
-        InheritedLocationTagData.of(context).selectedRadiusIndex;
+    widget.selectedRadiusNotifier.value =
+        InheritedLocationTagData.of(context).selectedRadius;
     super.didChangeDependencies();
     super.didChangeDependencies();
   }
   }
 
 
   @override
   @override
   Widget build(BuildContext context) {
   Widget build(BuildContext context) {
     final radiusValues = InheritedLocationTagData.of(context).radiusValues;
     final radiusValues = InheritedLocationTagData.of(context).radiusValues;
-    final selectedRadiusIndex = widget.selectedRadiusIndexNotifier.value;
-    final radiusValue = radiusValues[selectedRadiusIndex];
+    final selectedRadius = widget.selectedRadiusNotifier.value;
     final textTheme = getEnteTextTheme(context);
     final textTheme = getEnteTextTheme(context);
     final colorScheme = getEnteColorScheme(context);
     final colorScheme = getEnteColorScheme(context);
-    final roundedRadius = roundRadius(radiusValue);
+    final roundedRadius = roundRadius(selectedRadius);
     return Row(
     return Row(
       crossAxisAlignment: CrossAxisAlignment.start,
       crossAxisAlignment: CrossAxisAlignment.start,
       children: [
       children: [
@@ -136,11 +135,11 @@ class _RadiusPickerWidgetState extends State<RadiusPickerWidget> {
                     ),
                     ),
                     child: RepaintBoundary(
                     child: RepaintBoundary(
                       child: Slider(
                       child: Slider(
-                        value: selectedRadiusIndex.toDouble(),
+                        value: radiusValues.indexOf(selectedRadius).toDouble(),
                         onChanged: (value) {
                         onChanged: (value) {
                           setState(() {
                           setState(() {
-                            widget.selectedRadiusIndexNotifier.value =
-                                value.toInt();
+                            widget.selectedRadiusNotifier.value =
+                                radiusValues[value.toInt()];
                           });
                           });
                         },
                         },
                         min: 0,
                         min: 0,
@@ -187,10 +186,7 @@ class _RadiusPickerWidgetState extends State<RadiusPickerWidget> {
           locationTagState.updateRadiusValues([radius]);
           locationTagState.updateRadiusValues([radius]);
           if (mounted) {
           if (mounted) {
             setState(() {
             setState(() {
-              widget.selectedRadiusIndexNotifier.value =
-                  InheritedLocationTagData.of(context)
-                      .radiusValues
-                      .indexOf(radius);
+              widget.selectedRadiusNotifier.value = radius;
             });
             });
           }
           }
         } else {
         } else {