Fixed issue where location permissions was called infinite times on decline.

This commit is contained in:
Tobias Reski 2021-11-09 14:22:53 +01:00
parent 8aa3089f32
commit 34590fbc7d
3 changed files with 89 additions and 45 deletions

View file

@ -282,7 +282,7 @@ namespace TINK
if (_PermissionsService != null)
return _PermissionsService;
_PermissionsService = new Services.Permissions.Plugin.Permissions();
_PermissionsService = new Services.Permissions.Essentials.Permissions();
return _PermissionsService;
}
}

View file

@ -21,6 +21,9 @@ namespace TINK.View.Map
/// <summary> View model to notify about whether page appears or hides. </summary>
private MapPageViewModel MapPageViewModel { get; set; }
/// <summary> Initialization status to ensure initialization logic is not called multiple times. </summary>
private bool viewInitialized = false;
/// <summary>
/// Constructs map page instance.
/// </summary>
@ -125,30 +128,22 @@ namespace TINK.View.Map
public INavigationMasterDetail NavigationMasterDetail { private get; set; }
#endif
/// <summary>
/// Invoked when page is shown.
/// Starts update process.
/// </summary>
protected async override void OnAppearing()
{
// Don't repeat the initialization if it has been completed already.
if (viewInitialized) return;
// Pass reference to member Navigation to show bikes at station x dialog.
try
{
Log.ForContext<MapPage>().Verbose("Constructing map page view model.");
#if TRYNOTBACKSTYLE
MapPageViewModel = new MapPageViewModel();
#else
MapPageViewModel = new MapPageViewModel(
App.ModelRoot,
App.PermissionsService,
App.BluetoothService,
App.GeolocationServicesContainer.Active,
(mapspan) => MyMap.MoveToRegion(mapspan),
this,
Navigation);
#endif
} catch (Exception exception)
MapPageViewModel = createMapPageViewModel();
}
catch (Exception exception)
{
Log.ForContext<MapPage>().Error("Constructing map page view model failed. {Exception}", exception);
@ -171,15 +166,7 @@ namespace TINK.View.Map
try
{
if (Device.RuntimePlatform == Device.iOS)
{
TINKButton.BackgroundColor = Color.LightGray;
TINKButton.BorderColor = Color.Black;
TINKButton.Margin = new Thickness(10, 10, 10, 10);
KonradButton.BackgroundColor = Color.LightGray;
KonradButton.BorderColor = Color.Black;
KonradButton.Margin = new Thickness(10, 10, 10, 10);
}
applyCustomiOSStyling();
}
catch (Exception exception)
{
@ -201,11 +188,7 @@ namespace TINK.View.Map
try
{
// Pre move and scanle maps to avoid initial display of map in Rome.
Log.ForContext<MapPage>().Verbose("Moving and scaling map.");
MapPageViewModel.MoveAndScale(
(mapSpan) => MyMap.MoveToRegion(mapSpan),
App.ModelRoot.Uris.ActiveUri,
App.ModelRoot.GroupFilterMapPage);
premoveAndScaleMap();
}
catch(Exception exception)
{
@ -223,6 +206,54 @@ namespace TINK.View.Map
Log.ForContext<MapPage>().Error("Invoking OnAppearing on map page view model failed. {Exception}", exception);
return;
}
viewInitialized = true;
}
/// <summary>
/// Premoves the Map to a certain location.
/// </summary>
private void premoveAndScaleMap()
{
Log.ForContext<MapPage>().Verbose("Moving and scaling map.");
MapPageViewModel.MoveAndScale(
(mapSpan) => MyMap.MoveToRegion(mapSpan),
App.ModelRoot.Uris.ActiveUri,
App.ModelRoot.GroupFilterMapPage);
}
/// <summary>
/// Creates the Map Page's view model.
/// </summary>
private MapPageViewModel createMapPageViewModel()
{
Log.ForContext<MapPage>().Verbose("Constructing map page view model.");
return new MapPageViewModel(
App.ModelRoot,
App.PermissionsService,
App.BluetoothService,
App.GeolocationServicesContainer.Active,
(mapspan) => MyMap.MoveToRegion(mapspan),
this,
Navigation);
}
/// <summary>
/// Applies iOS specific styling to branded Buttons.
/// </summary>
private void applyCustomiOSStyling()
{
if (Device.RuntimePlatform == Device.iOS)
{
TINKButton.BackgroundColor = Color.LightGray;
TINKButton.BorderColor = Color.Black;
TINKButton.Margin = new Thickness(10, 10, 10, 10);
KonradButton.BackgroundColor = Color.LightGray;
KonradButton.BorderColor = Color.Black;
KonradButton.Margin = new Thickness(10, 10, 10, 10);
}
}
/// <summary>
@ -236,7 +267,7 @@ namespace TINK.View.Map
// View model might be null.
await MapPageViewModel?.OnDisappearing();
}
viewInitialized = false;
base.OnDisappearing();
}
}

View file

@ -322,9 +322,9 @@ namespace TINK.ViewModel.Map
&& !GeolocationService.IsSimulation
&& status != Status.Granted)
{
var permissionResult = await PermissionsService.RequestAsync();
status = await PermissionsService.RequestAsync();
if (permissionResult != Status.Granted)
if (status != Status.Granted)
{
var dialogResult = await ViewService.DisplayAlert(
AppResources.MessageTitleHint,
@ -420,19 +420,8 @@ namespace TINK.ViewModel.Map
// Move and scale before getting stations and bikes which takes some time.
ActionText = AppResources.ActivityTextCenterMap;
Location currentLocation = null;
try
{
currentLocation = TinkApp.CenterMapToCurrentLocation
? await GeolocationService.GetAsync()
: null;
}
catch (Exception ex)
{
Log.ForContext<MapPageViewModel>().Error("Getting location failed. {Exception}", ex);
}
MoveAndScale(m_oMoveToRegionDelegate, TinkApp.Uris.ActiveUri, ActiveFilterMap, currentLocation);
moveMapToCurrentPositionOfUser();
m_oViewUpdateManager = CreateUpdateTask();
@ -468,6 +457,30 @@ namespace TINK.ViewModel.Map
}
}
/// <summary>
/// Moves the map to the current position of the user.
/// If location permission hasn't been granted, the position is not adjusted.
/// </summary>
private async void moveMapToCurrentPositionOfUser()
{
var status = await PermissionsService.CheckStatusAsync();
if (status != Status.Granted) return;
Location currentLocation = null;
try
{
currentLocation = TinkApp.CenterMapToCurrentLocation
? await GeolocationService.GetAsync()
: null;
}
catch (Exception ex)
{
Log.ForContext<MapPageViewModel>().Error("Getting location failed. {Exception}", ex);
}
MoveAndScale(m_oMoveToRegionDelegate, TinkApp.Uris.ActiveUri, ActiveFilterMap, currentLocation);
}
/// <summary> Moves map and scales visible region depending on active filter. </summary>
public static void MoveAndScale(
Action<MapSpan> moveToRegionDelegate,