2023-04-05 10:54:39 +00:00
|
|
|
import "dart:async";
|
|
|
|
|
2023-03-29 02:51:02 +00:00
|
|
|
import "package:flutter/material.dart";
|
|
|
|
import "package:photos/core/constants.dart";
|
2023-04-05 10:54:39 +00:00
|
|
|
import "package:photos/core/event_bus.dart";
|
|
|
|
import "package:photos/events/location_tag_updated_event.dart";
|
2023-04-05 04:00:18 +00:00
|
|
|
import "package:photos/models/local_entity_data.dart";
|
2023-04-03 01:50:14 +00:00
|
|
|
import "package:photos/models/location/location.dart";
|
2023-04-03 11:52:07 +00:00
|
|
|
import "package:photos/models/location_tag/location_tag.dart";
|
2023-03-29 02:51:02 +00:00
|
|
|
import "package:photos/models/typedefs.dart";
|
|
|
|
import "package:photos/utils/debouncer.dart";
|
|
|
|
|
|
|
|
class LocationTagStateProvider extends StatefulWidget {
|
2023-04-03 11:52:07 +00:00
|
|
|
//This is used when we want to edit a locaiton tag
|
2023-04-05 04:00:18 +00:00
|
|
|
final LocalEntity<LocationTag>? locationTagEntity;
|
2023-04-03 11:52:07 +00:00
|
|
|
//This is used when we want to create a new location tag. We can't use
|
|
|
|
//LocationTag becuase aSquare and bSquare will be null.
|
|
|
|
final Location? centerPoint;
|
2023-03-29 02:51:02 +00:00
|
|
|
final Widget child;
|
2023-04-04 05:55:21 +00:00
|
|
|
const LocationTagStateProvider(
|
|
|
|
this.child, {
|
|
|
|
this.centerPoint,
|
2023-04-05 04:00:18 +00:00
|
|
|
this.locationTagEntity,
|
2023-04-04 05:55:21 +00:00
|
|
|
super.key,
|
|
|
|
});
|
2023-03-29 02:51:02 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<LocationTagStateProvider> createState() =>
|
|
|
|
_LocationTagStateProviderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LocationTagStateProviderState extends State<LocationTagStateProvider> {
|
2023-04-05 10:54:39 +00:00
|
|
|
int _selectedRaduisIndex = defaultRadiusValueIndex;
|
|
|
|
late Location? _centerPoint;
|
|
|
|
late LocalEntity<LocationTag>? _locationTagEntity;
|
2023-03-29 02:51:02 +00:00
|
|
|
final Debouncer _selectedRadiusDebouncer =
|
|
|
|
Debouncer(const Duration(milliseconds: 300));
|
2023-04-05 10:54:39 +00:00
|
|
|
late final StreamSubscription _locTagEntityListener;
|
2023-03-31 05:04:38 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2023-04-05 10:54:39 +00:00
|
|
|
_locationTagEntity = widget.locationTagEntity;
|
|
|
|
_centerPoint = widget.centerPoint;
|
|
|
|
assert(_centerPoint != null || _locationTagEntity != null);
|
|
|
|
_centerPoint = _locationTagEntity?.item.centerPoint ?? _centerPoint!;
|
|
|
|
_selectedRaduisIndex =
|
|
|
|
_locationTagEntity?.item.radiusIndex ?? defaultRadiusValueIndex;
|
|
|
|
_locTagEntityListener =
|
|
|
|
Bus.instance.on<LocationTagUpdatedEvent>().listen((event) {
|
|
|
|
_locationTagUpdateListener(event);
|
|
|
|
});
|
2023-03-31 05:04:38 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
2023-03-29 02:51:02 +00:00
|
|
|
|
2023-04-05 10:54:39 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_locTagEntityListener.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
void _locationTagUpdateListener(LocationTagUpdatedEvent event) {
|
|
|
|
if (event.type == LocTagEventType.update) {
|
|
|
|
if (event.updatedLocTagEntities!.first.id == _locationTagEntity!.id) {
|
|
|
|
setState(() {
|
|
|
|
final updatedLocTagEntity = event.updatedLocTagEntities!.first;
|
|
|
|
_selectedRaduisIndex = updatedLocTagEntity.item.radiusIndex;
|
|
|
|
_centerPoint = updatedLocTagEntity.item.centerPoint;
|
|
|
|
_locationTagEntity = updatedLocTagEntity;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 02:51:02 +00:00
|
|
|
void _updateSelectedIndex(int index) {
|
|
|
|
_selectedRadiusDebouncer.cancelDebounce();
|
|
|
|
_selectedRadiusDebouncer.run(() async {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() {
|
2023-04-05 10:54:39 +00:00
|
|
|
_selectedRaduisIndex = index;
|
2023-03-29 02:51:02 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return InheritedLocationTagData(
|
2023-04-05 10:54:39 +00:00
|
|
|
_selectedRaduisIndex,
|
|
|
|
_centerPoint!,
|
2023-03-29 02:51:02 +00:00
|
|
|
_updateSelectedIndex,
|
2023-04-05 10:54:39 +00:00
|
|
|
_locationTagEntity,
|
2023-03-29 02:51:02 +00:00
|
|
|
child: widget.child,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class InheritedLocationTagData extends InheritedWidget {
|
|
|
|
final int selectedRadiusIndex;
|
2023-04-03 01:50:14 +00:00
|
|
|
final Location centerPoint;
|
2023-04-04 05:55:21 +00:00
|
|
|
//locationTag is null when we are creating a new location tag in a add location sheet
|
2023-04-05 04:00:18 +00:00
|
|
|
final LocalEntity<LocationTag>? locationTagEntity;
|
2023-03-29 02:51:02 +00:00
|
|
|
final VoidCallbackParamInt updateSelectedIndex;
|
|
|
|
const InheritedLocationTagData(
|
|
|
|
this.selectedRadiusIndex,
|
2023-04-03 01:50:14 +00:00
|
|
|
this.centerPoint,
|
2023-04-04 05:55:21 +00:00
|
|
|
this.updateSelectedIndex,
|
2023-04-05 04:00:18 +00:00
|
|
|
this.locationTagEntity, {
|
2023-03-29 02:51:02 +00:00
|
|
|
required super.child,
|
|
|
|
super.key,
|
|
|
|
});
|
|
|
|
|
|
|
|
static InheritedLocationTagData of(BuildContext context) {
|
|
|
|
return context
|
|
|
|
.dependOnInheritedWidgetOfExactType<InheritedLocationTagData>()!;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
bool updateShouldNotify(InheritedLocationTagData oldWidget) {
|
2023-04-04 05:55:21 +00:00
|
|
|
return oldWidget.selectedRadiusIndex != selectedRadiusIndex ||
|
|
|
|
oldWidget.centerPoint != centerPoint ||
|
2023-04-05 04:00:18 +00:00
|
|
|
oldWidget.locationTagEntity != locationTagEntity;
|
2023-03-29 02:51:02 +00:00
|
|
|
}
|
|
|
|
}
|