sharee.bike-App/TINK/TINK/View/Map/MapPage.xaml.cs

252 lines
9.4 KiB
C#
Raw Normal View History

2021-05-13 20:16:41 +02:00
using Plugin.Connectivity;
using System;
using System.Threading.Tasks;
2021-06-26 20:57:55 +02:00
#if USEMASTERDETAIL || USEFLYOUT
2021-05-13 20:16:41 +02:00
using TINK.View.MasterDetail;
2021-06-26 20:57:55 +02:00
#endif
2021-05-13 20:16:41 +02:00
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace TINK.View.Map
{
2021-06-26 20:57:55 +02:00
using Serilog;
2021-05-13 20:16:41 +02:00
using TINK.ViewModel.Map;
[XamlCompilation(XamlCompilationOptions.Compile)]
2021-06-26 20:57:55 +02:00
#if USEMASTERDETAIL || USEFLYOUT
2021-05-13 20:16:41 +02:00
public partial class MapPage : ContentPage, IViewService, IDetailPage
2021-06-26 20:57:55 +02:00
#else
public partial class MapPage : ContentPage, IViewService
#endif
2021-05-13 20:16:41 +02:00
{
/// <summary> View model to notify about whether page appears or hides. </summary>
2021-06-26 20:57:55 +02:00
private MapPageViewModel MapPageViewModel { get; set; }
2021-05-13 20:16:41 +02:00
/// <summary>
/// Constructs map page instance.
/// </summary>
public MapPage()
{
InitializeComponent();
}
/// <summary>
/// Displays alert message.
2021-05-13 20:16:41 +02:00
/// </summary>
/// <param name="p_strTitle">Title of message.</param>
/// <param name="p_strMessage">Message to display.</param>
/// <param name="p_strCancel">Type of buttons.</param>
public new async Task DisplayAlert(string p_strTitle, string p_strMessage, string p_strCancel)
=> await App.Current.MainPage.DisplayAlert(p_strTitle, p_strMessage, p_strCancel);
2021-05-13 20:16:41 +02:00
/// <summary> Displays alert message.</summary>
2021-05-13 20:16:41 +02:00
/// <param name="title">Title of message.</param>
/// <param name="message">Message to display.</param>
/// <param name="details">Detailed error description.</param>
/// <param name="cancel">Type of buttons.</param>
2021-06-26 20:57:55 +02:00
public async Task DisplayAdvancedAlert(
2021-05-13 20:16:41 +02:00
string title,
string message,
string details,
string cancel)
=> await App.Current.MainPage.DisplayAlert(title, $"{message}\r\nDetails:\r\n{details}", cancel);
2021-06-26 20:57:55 +02:00
/// <summary> Displays detailed alert message.</summary>
/// <param name="title">Title of message.</param>
/// <param name="message">Message to display.</param>
/// <param name="details">Detailed error description.</param>
/// <param name="accept">Text of accept button.</param>
/// <param name="cancel">Text of cancel button.</param>
/// <returns>True if user pressed accept.</returns>
public async Task<bool> DisplayAdvancedAlert(string title, string message, string details, string accept, string cancel)
=> await App.Current.MainPage.DisplayAlert(title, $"{message}\r\nDetails:\r\n{details}", accept, cancel);
2021-05-13 20:16:41 +02:00
/// <summary>
/// Displays alert message.
/// </summary>
/// <param name="p_strTitle">Title of message.</param>
/// <param name="p_strMessage">Message to display.</param>
/// <param name="p_strAccept">Text of accept button.</param>
/// <param name="p_strCancel">Text of button.</param>
/// <returns>True if user pressed accept.</returns>
public new async Task<bool> DisplayAlert(string p_strTitle, string p_strMessage, string p_strAccept, string p_strCancel)
{
return await App.Current.MainPage.DisplayAlert(p_strTitle, p_strMessage, p_strAccept, p_strCancel);
}
2021-06-26 20:57:55 +02:00
#if USEMASTERDETAIL || USEFLYOUT
2021-05-13 20:16:41 +02:00
/// <summary>
/// Creates and a page an shows it.
/// </summary>
/// <param name="p_oTypeOfPage">Type of page to show.</param>
public void ShowPage(ViewTypes p_oType, string p_strTitle = null)
2021-06-26 20:57:55 +02:00
=> NavigationMasterDetail.ShowPage(p_oType.GetViewType(), p_strTitle);
#else
/// <summary> Shows a page.</summary>
/// <param name="route">Route of the page to show.</param>
public async Task ShowPage(string route) => await Shell.Current.GoToAsync(route);
#endif
2021-05-13 20:16:41 +02:00
/// <summary> Pushes a page onto the modal stack. </summary>
/// <param name="p_oTypeOfPage">Type of page to display.</param>
public async Task PushModalAsync(ViewTypes p_oTypeOfPage)
{
await Navigation.PushModalAsync((Page)Activator.CreateInstance(p_oTypeOfPage.GetViewType()));
}
/// <summary> Pops a page from the modal stack. </summary>
public async Task PopModalAsync()
{
await Navigation.PopModalAsync();
}
/// <summary> Pushes a page onto the stack. </summary>
/// <param name="p_oTypeOfPage">Page to display.</param>
public async Task PushAsync(ViewTypes p_oTypeOfPage)
{
2021-06-26 20:57:55 +02:00
#if USEMASTERDETAIL || USEFLYOUT
2021-05-13 20:16:41 +02:00
var page = Activator.CreateInstance(p_oTypeOfPage.GetViewType()) as IDetailPage;
2021-06-26 20:57:55 +02:00
#else
var page = Activator.CreateInstance(p_oTypeOfPage.GetViewType());
#endif
2021-05-13 20:16:41 +02:00
if (page == null)
{
return;
}
2021-06-26 20:57:55 +02:00
#if USEMASTERDETAIL || USEFLYOUT
page.NavigationMasterDetail = NavigationMasterDetail;
#endif
2021-05-13 20:16:41 +02:00
await Navigation.PushAsync((Page)page);
}
2021-06-26 20:57:55 +02:00
#if USCSHARP9
2021-05-13 20:16:41 +02:00
public Task<IViewService.IUserFeedback> DisplayUserFeedbackPopup() => throw new NotSupportedException();
2021-06-26 20:57:55 +02:00
#else
public Task<IUserFeedback> DisplayUserFeedbackPopup() => throw new NotSupportedException();
#endif
2021-05-13 20:16:41 +02:00
2021-06-26 20:57:55 +02:00
#if USEMASTERDETAIL || USEFLYOUT
2021-05-13 20:16:41 +02:00
/// <summary> Delegate to perform navigation.</summary>
2021-06-26 20:57:55 +02:00
public INavigationMasterDetail NavigationMasterDetail { private get; set; }
2021-05-13 20:16:41 +02:00
2021-06-26 20:57:55 +02:00
#endif
2021-05-13 20:16:41 +02:00
/// <summary>
/// Invoked when page is shown.
/// Starts update process.
/// </summary>
protected async override void OnAppearing()
{
// Pass reference to member Navigation to show bikes at station x dialog.
2021-06-26 20:57:55 +02:00
try
{
Log.ForContext<MainPage>().Verbose("Constructing map page view model.");
2021-05-13 20:16:41 +02:00
#if TRYNOTBACKSTYLE
2021-06-26 20:57:55 +02:00
m_oMapPageViewModel = new MapPageViewModel();
2021-05-13 20:16:41 +02:00
#else
2021-06-26 20:57:55 +02:00
MapPageViewModel = new MapPageViewModel(
App.ModelRoot,
App.PermissionsService,
App.BluetoothService,
App.GeolocationServicesContainer.Active,
(mapspan) => MyMap.MoveToRegion(mapspan),
this,
Navigation);
#endif
} catch (Exception exception)
{
Log.ForContext<MainPage>().Error("Constructing map page view model failed. {Exception}", exception);
return;
}
try
{
BindingContext = MapPageViewModel;
#if USEMASTERDETAIL || USEFLYOUT
MapPageViewModel.NavigationMasterDetail = NavigationMasterDetail;
2021-05-13 20:16:41 +02:00
#endif
2021-06-26 20:57:55 +02:00
}
catch (Exception exception)
{
Log.ForContext<MainPage>().Error("Setting binding/ navigaton on map page failed. {Exception}", exception);
return;
}
2021-05-13 20:16:41 +02:00
2021-06-26 20:57:55 +02:00
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);
}
}
catch (Exception exception)
{
// Continue because styling is not essential.
Log.ForContext<MainPage>().Error("IOS specific styling of map page failed. {Exception}", exception);
}
2021-05-13 20:16:41 +02:00
2021-06-26 20:57:55 +02:00
try
{
base.OnAppearing();
}
catch (Exception exception)
2021-05-13 20:16:41 +02:00
{
2021-06-26 20:57:55 +02:00
// Continue because styling is not essential.
Log.ForContext<MainPage>().Error("Invoking OnAppearing of base failed. {Exception}", exception);
return;
2021-05-13 20:16:41 +02:00
}
2021-06-26 20:57:55 +02:00
try
{
// Pre move and scanle maps to avoid initial display of map in Rome.
Log.ForContext<MainPage>().Verbose("Moving and scaling map.");
MapPageViewModel.MoveAndScale(
(mapSpan) => MyMap.MoveToRegion(mapSpan),
App.ModelRoot.Uris.ActiveUri,
App.ModelRoot.GroupFilterMapPage);
2021-05-13 20:16:41 +02:00
2021-06-26 20:57:55 +02:00
}
catch(Exception exception)
{
// Continue because a map not beeing moved/ scaled is no reason for aborting startup.
Log.ForContext<MainPage>().Error("Moving and scaling map failed. {Exception}", exception);
}
2021-05-13 20:16:41 +02:00
2021-06-26 20:57:55 +02:00
try
{
Log.ForContext<MainPage>().Verbose("Invoking OnAppearing on map page view model.");
await MapPageViewModel.OnAppearing();
}
catch (Exception exception)
{
Log.ForContext<MainPage>().Error("Invoking OnAppearing on map page view model failed. {Exception}", exception);
return;
}
2021-05-13 20:16:41 +02:00
}
/// <summary>
/// Invoked when pages is closed/ hidden.
/// Stops update process.
/// </summary>
protected override async void OnDisappearing()
{
2021-06-26 20:57:55 +02:00
if (MapPageViewModel != null)
2021-05-13 20:16:41 +02:00
{
// View model might be null.
2021-06-26 20:57:55 +02:00
await MapPageViewModel?.OnDisappearing();
2021-05-13 20:16:41 +02:00
}
base.OnDisappearing();
}
}
}