Skip to content

Commit

Permalink
fix: Force center update on location change for proximity based alarms
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Sep 13, 2023
1 parent 92a3f2f commit 9091e7b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class _LocationRadiusSelectorMapState extends State<LocationRadiusSelectorMap> {
else
getFlutterMapCircleLayer(),
CurrentLocationLayer(
followOnLocationUpdate: FollowOnLocationUpdate.always,
followOnLocationUpdate: FollowOnLocationUpdate.once,
)
],
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class _ViewAlarmSelectGeoBasedScreenState
super.initState();

final settings = context.read<SettingsService>();
final currentLocation = context.read<CurrentLocationService>();

if (settings.mapProvider == MapProvider.openStreetMap) {
flutterMapController = MapController();
Expand All @@ -66,16 +67,44 @@ class _ViewAlarmSelectGeoBasedScreenState
WidgetsBinding.instance.addPostFrameCallback((_) {
goToCurrentPosition();

_setPositionFromCurrentLocationService();
_setPositionFromCurrentLocationService(updateAlarmCenter: true);
_showHelperSheetIfRequired();
});

currentLocation.addListener(_setPositionFromCurrentLocationService);
}

@override
void dispose() {
final currentLocation = context.read<CurrentLocationService>();

flutterMapController?.dispose();
_positionStream?.drain();

currentLocation.removeListener(_setPositionFromCurrentLocationService);

super.dispose();
}

void _setPositionFromCurrentLocationService() {
void _setPositionFromCurrentLocationService({
final updateAlarmCenter = false,
}) {
final currentLocation = context.read<CurrentLocationService>();

if (currentLocation.currentPosition != null) {
_animateToPosition(currentLocation.currentPosition!);
if (currentLocation.currentPosition == null) {
return;
}

_animateToPosition(currentLocation.currentPosition!);

if (updateAlarmCenter ||
widget.type == LocationAlarmType.proximityLocation) {
setState(() {
alarmCenter = LatLng(
currentLocation.currentPosition!.latitude,
currentLocation.currentPosition!.longitude,
);
});
}
}

Expand All @@ -97,8 +126,8 @@ class _ViewAlarmSelectGeoBasedScreenState
final zoom = _hasSetInitialPosition
? (16 - log(position.accuracy / 200) / log(2)).toDouble()
: flutterMapController?.zoom ??
(await appleMapController?.getZoomLevel()) ??
16.0;
(await appleMapController?.getZoomLevel()) ??
16.0;

flutterMapController?.move(
LatLng(position.latitude, position.longitude),
Expand All @@ -123,39 +152,38 @@ class _ViewAlarmSelectGeoBasedScreenState

showHelperSheet(
context: context,
builder: (context) =>
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(l10n.location_addAlarm_radiusBased_help_description),
const SizedBox(height: MEDIUM_SPACE),
if (widget.type == LocationAlarmType.radiusBasedRegion) ...[
Row(
children: <Widget>[
const Icon(Icons.touch_app_rounded),
const SizedBox(width: MEDIUM_SPACE),
Flexible(
child: Text(
l10n.location_addAlarm_geo_help_tapDescription,
),
),
],
builder: (context) => Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(l10n.location_addAlarm_radiusBased_help_description),
const SizedBox(height: MEDIUM_SPACE),
if (widget.type == LocationAlarmType.radiusBasedRegion) ...[
Row(
children: <Widget>[
const Icon(Icons.touch_app_rounded),
const SizedBox(width: MEDIUM_SPACE),
Flexible(
child: Text(
l10n.location_addAlarm_geo_help_tapDescription,
),
),
],
const SizedBox(height: MEDIUM_SPACE),
Row(
children: <Widget>[
const Icon(Icons.pinch_rounded),
const SizedBox(width: MEDIUM_SPACE),
Flexible(
child: Text(
l10n.location_addAlarm_radiusBased_help_pinchDescription,
),
),
],
),
],
const SizedBox(height: MEDIUM_SPACE),
Row(
children: <Widget>[
const Icon(Icons.pinch_rounded),
const SizedBox(width: MEDIUM_SPACE),
Flexible(
child: Text(
l10n.location_addAlarm_radiusBased_help_pinchDescription,
),
),
],
),
],
),
title: l10n.location_addAlarm_radiusBased_help_title,
sheetName: HelperSheet.radiusBasedAlarms,
);
Expand Down Expand Up @@ -184,14 +212,6 @@ class _ViewAlarmSelectGeoBasedScreenState
});
}

@override
void dispose() {
flutterMapController?.dispose();
_positionStream?.drain();

super.dispose();
}

Future<void> _selectRegion() async {
final RadiusBasedRegionLocationAlarm? alarm = await showPlatformModalSheet(
context: context,
Expand All @@ -200,15 +220,14 @@ class _ViewAlarmSelectGeoBasedScreenState
isDismissible: true,
isScrollControlled: true,
),
builder: (_) =>
RadiusRegionMetaDataSheet(
center: alarmCenter!,
radius: radius.toDouble(),
),
builder: (_) => RadiusRegionMetaDataSheet(
center: alarmCenter!,
radius: radius.toDouble(),
),
);

final hasGrantedNotificationAccess =
await showNotificationPermissionDialog();
await showNotificationPermissionDialog();

if (!hasGrantedNotificationAccess) {
return;
Expand Down Expand Up @@ -239,28 +258,25 @@ class _ViewAlarmSelectGeoBasedScreenState
final l10n = AppLocalizations.of(context);

return PlatformScaffold(
material: (_, __) =>
MaterialScaffoldData(
resizeToAvoidBottomInset: false,
),
material: (_, __) => MaterialScaffoldData(
resizeToAvoidBottomInset: false,
),
appBar: PlatformAppBar(
title: Text(l10n.location_addAlarm_geo_title),
trailingActions: [
PlatformIconButton(
cupertino: (_, __) =>
CupertinoIconButtonData(
padding: EdgeInsets.zero,
),
cupertino: (_, __) => CupertinoIconButtonData(
padding: EdgeInsets.zero,
),
icon: Icon(context.platformIcons.help),
onPressed: showHelp,
),
],
cupertino: (_, __) =>
CupertinoNavigationBarData(
backgroundColor: isInScaleMode
? null
: getCupertinoAppBarColorForMapScreen(context),
),
cupertino: (_, __) => CupertinoNavigationBarData(
backgroundColor: isInScaleMode
? null
: getCupertinoAppBarColorForMapScreen(context),
),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
Expand Down

0 comments on commit 9091e7b

Please sign in to comment.