using System;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace ShareeBike.View.Map
{
using Serilog;
using ShareeBike.Model.Bikes.BikeInfoNS.DriveNS.BatteryNS;
using ShareeBike.ViewModel.Map;
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MapPage : ContentPage, IViewService
{
/// View model to notify about whether page appears or hides.
private MapPageViewModel MapPageViewModel { get; set; }
/// Initialization status to ensure initialization logic is not called multiple times.
private bool isInitializationStarted = false;
///
/// Constructs map page instance.
///
public MapPage()
{
InitializeComponent();
}
///
/// Displays alert message.
///
/// Title of message.
/// Message to display.
/// Type of buttons.
public new async Task DisplayAlert(string title, string message, string cancel)
=> await App.Current.MainPage.DisplayAlert(title, message, cancel);
/// Displays alert message.
/// Title of message.
/// Message to display.
/// Detailed error description.
/// Type of buttons.
public async Task DisplayAdvancedAlert(
string title,
string message,
string details,
string cancel)
=> await App.Current.MainPage.DisplayAlert(title, $"{message}\r\nDetails:\r\n{details}", cancel);
/// Displays detailed alert message.
/// Title of message.
/// Message to display.
/// Detailed error description.
/// Text of accept button.
/// Text of cancel button.
/// True if user pressed accept.
public async Task DisplayAdvancedAlert(string title, string message, string details, string accept, string cancel)
=> await App.Current.MainPage.DisplayAlert(title, !string.IsNullOrEmpty(details) ? $"{message}\r\nDetails:\r\n{details}" : $"{message}", accept, cancel);
///
/// Displays alert message.
///
/// Title of message.
/// Message to display.
/// Text of accept button.
/// Text of button.
/// True if user pressed accept.
public new async Task DisplayAlert(string title, string message, string accept, string cancel)
=> await App.Current.MainPage.DisplayAlert(title, message, accept, cancel);
/// Shows a page.
/// Route of the page to show.
public async Task ShowPage(string route) => await Shell.Current.GoToAsync(route);
/// Pushes a page onto the modal stack.
/// Type of page to display.
public async Task PushModalAsync(ViewTypes typeOfPage)
=> await Navigation.PushModalAsync((Page)Activator.CreateInstance(typeOfPage.GetViewType()));
/// Pops a page from the modal stack.
public async Task PopModalAsync()
=> await Navigation.PopModalAsync();
/// Pushes a page onto the stack.
/// Page to display.
public async Task PushAsync(ViewTypes typeOfPage)
{
var page = Activator.CreateInstance(typeOfPage.GetViewType());
if (page == null)
{
return;
}
await Navigation.PushAsync((Page)page);
}
#if USCSHARP9
public Task DisplayUserFeedbackPopup() => throw new NotSupportedException();
#else
public async Task DisplayUserFeedbackPopup(IBatteryMutable battery = null) => throw new NotSupportedException();
#endif
///
/// Invoked when page is shown.
/// Starts update process.
///
protected async override void OnAppearing()
{
// Don't repeat the initialization if it has been completed already.
if (isInitializationStarted) return;
isInitializationStarted = true;
// Pass reference to member Navigation to show bikes at station x dialog.
try
{
Log.ForContext().Verbose("Constructing map page view model.");
MapPageViewModel = CreateMapPageViewModel();
}
catch (Exception exception)
{
Log.ForContext().Error("Constructing map page view model failed. {Exception}", exception);
isInitializationStarted = false;
return;
}
try
{
BindingContext = MapPageViewModel;
}
catch (Exception exception)
{
Log.ForContext().Error("Setting binding/ navigation on map page failed. {Exception}", exception);
isInitializationStarted = false;
return;
}
try
{
base.OnAppearing();
}
catch (Exception exception)
{
// Continue because styling is not essential.
Log.ForContext().Error("Invoking OnAppearing of base failed. {Exception}", exception);
}
try
{
// Premove and scale maps to avoid initial display of map in Rome.
PremoveAndScaleMap();
}
catch (Exception exception)
{
// Continue because a map not beeing moved/ scaled is no reason for aborting startup.
Log.ForContext().Error("Moving and scaling map failed. {Exception}", exception);
}
try
{
Log.ForContext().Verbose("Invoking OnAppearing on map page view model.");
await MapPageViewModel.OnAppearing();
}
catch (Exception exception)
{
Log.ForContext().Error("Invoking OnAppearing on map page view model failed. {Exception}", exception);
isInitializationStarted = false;
return;
}
try
{
Log.ForContext().Verbose("Setting map settings.");
//show current user position (blue dot) and enable button for centering map manually.
MyMap.UiSettings.MyLocationButtonEnabled = MapPageViewModel.IsLocationPermissionGranted;
MyMap.IsShowingUser = MapPageViewModel.IsLocationPermissionGranted;
//disable +/- buttons
MyMap.UiSettings.ZoomControlsEnabled = false;
}
catch (Exception exception)
{
Log.ForContext().Verbose("Setting map settings failed. {Exception}", exception);
}
isInitializationStarted = false;
}
///
/// Premoves the Map to a certain location.
///
private void PremoveAndScaleMap()
{
Log.ForContext().Verbose("Moving and scaling map.");
MapPageViewModel.MoveAndScale(
(mapSpan) => MyMap.MoveToRegion(mapSpan),
App.ModelRoot.ActiveMapSpan);
}
///
/// Creates the Map Page's view model.
///
private MapPageViewModel CreateMapPageViewModel()
{
Log.ForContext().Verbose("Constructing map page view model.");
return new MapPageViewModel(
App.ModelRoot,
App.PermissionsService,
App.BluetoothService,
App.LocationServicesContainer.Active,
(mapspan) => MyMap.MoveToRegion(mapspan),
this,
Navigation);
}
///
/// Invoked when pages is closed/ hidden.
/// Stops update process.
///
protected async override void OnDisappearing()
{
if (MapPageViewModel != null)
{
// View model might be null.
await MapPageViewModel?.OnDisappearing();
}
base.OnDisappearing();
}
}
}