using Plugin.Connectivity; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using TINK.Model.Bike.BluetoothLock; using TINK.View.MasterDetail; using TINK.ViewModel; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace TINK.View.MyBikes { using Plugin.Permissions.Abstractions; using Plugin.Permissions; using TINK.Model; using TINK.Services.BluetoothLock.Tdo; using System.Collections.Generic; using Serilog; using Plugin.BLE; using TINK.Services.BluetoothLock; using TINK.ViewModel.MyBikes; using Xamarin.CommunityToolkit.Extensions; [XamlCompilation(XamlCompilationOptions.Compile)] public partial class MyBikesPage : ContentPage, IViewService { /// Refernce to view model. MyBikesPageViewModel m_oViewModel = null; /// /// Constructs a my bikes page. /// public MyBikesPage() { } /// /// Invoked when page is shown. /// Starts update process. /// protected async override void OnAppearing() { if (m_oViewModel != null) { // No need to create view model, set binding context an items source if already done. // If done twice tap events are fired multiple times (when hiding page using home button). await m_oViewModel.OnAppearing(); return; } try { var model = App.ModelRoot; // Backup synchronization context when called from GUI-thread. var synchronizationContext = SynchronizationContext.Current; m_oViewModel = new MyBikesPageViewModel( model.ActiveUser, model.Permissions, CrossBluetoothLE.Current, Device.RuntimePlatform, () => model.GetIsConnected(), (isConnected) => model.GetConnector(isConnected), model.Geolocation, model.LocksServices.Active, model.Polling, (d, obj) => synchronizationContext.Post(d, obj), this); } catch (Exception exception) { Log.ForContext().Error("Displaying bikes at station page failed. {Exception}", exception); this.DisplayAlert("Fehler", $"Seite Räder an Station kann nicht angezeigt werden. ${exception.Message}", "OK"); return; } InitializeComponent(); BindingContext = m_oViewModel; MyBikesListView.ItemsSource = m_oViewModel; await m_oViewModel.OnAppearing(); } /// /// Invoked when pages is closed/ hidden. /// Stops update process. /// protected async override void OnDisappearing() { if (m_oViewModel == null) { // View model might be null (Example: Occured when page to querry for location permissions was opened) return; } await m_oViewModel.OnDisappearing(); } /// /// Displays altert message. /// /// Title of message. /// Message to display. /// Type of buttons. 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); } /// Displays altert 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 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 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); } /// /// Creates and a page an shows it. /// /// Type of page to show. public void ShowPage(ViewTypes p_oType, string p_strTitle = null) { throw new NotSupportedException(); } /// Pushes a page onto the modal stack. /// Page to display. public Task PushModalAsync(ViewTypes p_oTypeOfPage) { throw new NotSupportedException(); } /// Pops a page from the modal stack. public Task PopModalAsync() { throw new NotSupportedException(); } /// Pushes a page onto the stack. /// Page to display. public Task PushAsync(ViewTypes p_oTypeOfPage) { throw new NotSupportedException(); } public async Task DisplayUserFeedbackPopup() => await Navigation.ShowPopupAsync(new FeedbackPopup()); } }