You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
7.2 KiB

1 year ago
  1. using GMCabsDriverAssistant.Messages;
  2. using GMCabsDriverAssistant.Models;
  3. using GMCabsDriverAssistant.Services;
  4. using GMCabsDriverAssistant.Utils;
  5. using GMCabsDriverAssistantSolution.ViewModels;
  6. using System.Diagnostics;
  7. namespace GMCabsDriverAssistantSolution.Views;
  8. public partial class NewPage1 : ContentPage
  9. {
  10. #region Fields
  11. private readonly HomeViewModel _viewModel;
  12. bool isLocationPermitted, isBatteryOptimizationDisabled;
  13. #endregion
  14. #region Properties
  15. public double CurrentLat { get; set; }
  16. public double CurrentLng { get; set; }
  17. #endregion
  18. #region Constructor
  19. public NewPage1()
  20. {
  21. //SentrySdk.CaptureMessage(nameof(HomePage));
  22. InitializeComponent();
  23. BindingContext = _viewModel = new HomeViewModel();
  24. MessagingCenter.Unsubscribe<App, string>(this, nameof(App));
  25. MessagingCenter.Subscribe<App, string>(this, nameof(App), async (sender, args) =>
  26. {
  27. Debug.WriteLine($"args: {args}");
  28. if (args == "DriverAlertNotification")
  29. {
  30. await _viewModel.UpdateLocation();
  31. }
  32. else
  33. {
  34. await _viewModel.OnAppearing();
  35. }
  36. });
  37. //handle canceling of an accepted booking
  38. MessagingCenter.Unsubscribe<App, CancelledBookingResponseDto>(this, nameof(App));
  39. MessagingCenter.Subscribe<App, CancelledBookingResponseDto>(this, nameof(App), async (sender, args) =>
  40. {
  41. await Navigation.PopToRootAsync();
  42. //await Shell.Current.GoToAsync($"{nameof(CancelledBookingPage)}?{nameof(CancelledBookingViewModel.PickUpAddress)}={args.PickUPAddress}&{nameof(CancelledBookingViewModel.DropUpAddress)}={args.DropUpAddress}");
  43. _viewModel.IsVisibleAcceptBookingView = false;
  44. });
  45. //MessagingCenter.Unsubscribe<AcceptBookingPage, AcceptBookingTimerDto>(this, nameof(AcceptBookingPage));
  46. //MessagingCenter.Subscribe<AcceptBookingPage, AcceptBookingTimerDto>(this, nameof(AcceptBookingPage), (sender, args) =>
  47. //{
  48. // _viewModel.seconds = args.PendingSeconds;
  49. // _viewModel.PickUpAddress = args.PickUPAddress;
  50. // _viewModel.DropUpAddress = args.DropUpAddress;
  51. // if (_viewModel.seconds > 0)
  52. // {
  53. // _viewModel.IsVisibleAcceptBookingView = true;
  54. // }
  55. //});
  56. // handle clearing of an accepted booking
  57. MessagingCenter.Unsubscribe<App, string>(this, "ClearAcceptance");
  58. MessagingCenter.Subscribe<App, string>(this, "ClearAcceptance", async (sender, args) =>
  59. {
  60. await Navigation.PopToRootAsync();
  61. _viewModel.IsVisibleAcceptBookingView = false;
  62. _viewModel.seconds = 0;
  63. });
  64. //MessagingCenter.Unsubscribe<AcceptBookingPage, string>(this, "ClearAcceptance");
  65. //MessagingCenter.Subscribe<AcceptBookingPage, string>(this, "ClearAcceptance", async (sender, args) =>
  66. //{
  67. // await Navigation.PopToRootAsync();
  68. // await Shell.Current.GoToAsync($"{nameof(HomePage)}");
  69. // _viewModel.IsVisibleAcceptBookingView = false;
  70. // _viewModel.seconds = 0;
  71. //});
  72. // Used to handle notification by using MessagingCenter same is tablet based notification system
  73. MessagingCenter.Subscribe<App, string>(this, nameof(App), (sender, args) =>
  74. {
  75. _viewModel.GetUnreadNotificationCount();
  76. });
  77. MessagingCenter.Subscribe<App, NotificationDto>(this, nameof(App), async (sender, notification) =>
  78. {
  79. _viewModel.GetUnreadNotificationCount();
  80. });
  81. }
  82. #endregion
  83. #region Events
  84. private void SettingsRequired_Tapped(object sender, EventArgs e)
  85. {
  86. // Navigation.ShowPopup(new AppPermissiontSetDialogPage(this, Constant.FROM_HOME_PAGE));
  87. }
  88. #endregion
  89. #region Methods
  90. protected override void OnAppearing()
  91. {
  92. base.OnAppearing();
  93. //CheckAppPermissionStatus();
  94. isLocationPermitted = Preferences.Get(SecureStorageData.IsLocationPermitted, false);
  95. isBatteryOptimizationDisabled = Preferences.Get(SecureStorageData.IsBatteryOptimizationDisabled, false);
  96. IsSettingRequired();
  97. //GoToLoginPage();
  98. Task.Run(async () =>
  99. {
  100. GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
  101. await _viewModel.OnAppearing();
  102. //DisplayBookingOrNotification(_viewModel.HasUnreadNotifications, _viewModel.IsBookingAvailable, _viewModel.availableBookingIDs);
  103. });
  104. }
  105. private async void GoToLoginPage()
  106. {
  107. ValidateTokenResponseDto validateTokenResponseDto = new ValidateTokenResponseDto();
  108. GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
  109. var token = Preferences.Get(SecureStorageData.Token, "");
  110. var fcmToken = Preferences.Get("FcmToken", "");
  111. validateTokenResponseDto = await gmCabsDriverService.ValidateAuthToken(token, fcmToken);
  112. if (!validateTokenResponseDto.Result)
  113. {
  114. var message = new StopServiceMessage();
  115. MessagingCenter.Send(message, "ServiceStopped");
  116. var lastLatitude = Convert.ToDouble(Preferences.Get("lastLat", "0"));
  117. var lastLongitude = Convert.ToDouble(Preferences.Get("lastLng", "0"));
  118. await gmCabsDriverService.LogoutDriverApp(token, lastLatitude, lastLongitude);
  119. //Navigation.ShowPopup(new LoginErrorAlertDialogPage());
  120. }
  121. }
  122. private async void CheckAppPermissionStatus()
  123. {
  124. var status = await Permissions.CheckStatusAsync<Permissions.LocationAlways>();
  125. if (status == PermissionStatus.Granted)
  126. {
  127. Preferences.Set(SecureStorageData.IsLocationPermitted, true);
  128. }
  129. else
  130. {
  131. Preferences.Set(SecureStorageData.IsLocationPermitted, false);
  132. }
  133. if (Device.RuntimePlatform == Device.Android)
  134. {
  135. if (DependencyService.Get<IBatteryInfo>().CheckIsIgnoringBatteryOptimizations())
  136. {
  137. Preferences.Set(SecureStorageData.IsBatteryOptimizationDisabled, true);
  138. }
  139. else
  140. {
  141. Preferences.Set(SecureStorageData.IsBatteryOptimizationDisabled, false);
  142. }
  143. }
  144. }
  145. private void IsSettingRequired()
  146. {
  147. if (isLocationPermitted && isBatteryOptimizationDisabled)
  148. {
  149. SettingsRequired.IsVisible = false;
  150. }
  151. else
  152. {
  153. if (Device.RuntimePlatform == Device.iOS && isLocationPermitted)
  154. {
  155. SettingsRequired.IsVisible = false;
  156. }
  157. else
  158. {
  159. SettingsRequired.IsVisible = true;
  160. }
  161. }
  162. }
  163. private async void OnBookingViewClicked(object sender, EventArgs e)
  164. {
  165. // await Navigation.PushAsync(new BookingsPage(_viewModel.CurrentLocation.Latitude, _viewModel.CurrentLocation.Longitude));
  166. await DisplayAlert("Success", "Booking view clicked","OK");
  167. }
  168. private async void OnNotificationViewClicked(object sender, EventArgs e)
  169. {
  170. // await Navigation.PushAsync(new NotificationsPage());
  171. await DisplayAlert("Success", "Notification view clicked", "OK");
  172. }
  173. #endregion
  174. }