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.

253 lines
9.8 KiB

1 year ago
  1. using GMCabsDriverAssistant.Messages;
  2. using GMCabsDriverAssistant.Models;
  3. using GMCabsDriverAssistant.Services;
  4. using GMCabsDriverAssistant.Utils;
  5. using GMCabsDriverAssistantSolution.Models.Rydo;
  6. using GMCabsDriverAssistantSolution.ViewModels;
  7. using Sentry;
  8. using System.Diagnostics;
  9. using System.Text.Json;
  10. namespace GMCabsDriverAssistantSolution.Views;
  11. [XamlCompilation(XamlCompilationOptions.Compile)]
  12. public partial class HomePage : ContentPage
  13. {
  14. #region Fields
  15. private readonly HomeViewModel _viewModel;
  16. bool isLocationPermitted, isBatteryOptimizationDisabled;
  17. #endregion
  18. #region Properties
  19. public double CurrentLat { get; set; }
  20. public double CurrentLng { get; set; }
  21. #endregion
  22. #region Constructor
  23. [Obsolete]
  24. public HomePage()
  25. {
  26. //SentrySdk.CaptureMessage(nameof(HomePage));
  27. InitializeComponent();
  28. BindingContext = _viewModel = new HomeViewModel();
  29. MessagingCenter.Unsubscribe<App, string>(this, nameof(App));
  30. MessagingCenter.Subscribe<App, string>(this, nameof(App), async (sender, args) =>
  31. {
  32. Debug.WriteLine($"args: {args}");
  33. if (args == "DriverAlertNotification")
  34. {
  35. await _viewModel.UpdateLocation();
  36. }
  37. else
  38. {
  39. await _viewModel.OnAppearing();
  40. }
  41. });
  42. //handle canceling of an accepted booking
  43. MessagingCenter.Unsubscribe<App, CancelledBookingResponseDto>(this, nameof(App));
  44. MessagingCenter.Subscribe<App, CancelledBookingResponseDto>(this, nameof(App), async (sender, args) =>
  45. {
  46. await Navigation.PopToRootAsync();
  47. //await Shell.Current.GoToAsync($"{nameof(CancelledBookingPage)}?{nameof(CancelledBookingViewModel.PickUpAddress)}={args.PickUPAddress}&{nameof(CancelledBookingViewModel.DropUpAddress)}={args.DropUpAddress}");
  48. _viewModel.IsVisibleAcceptBookingView = false;
  49. });
  50. //MessagingCenter.Unsubscribe<AcceptBookingPage, AcceptBookingTimerDto>(this, nameof(AcceptBookingPage));
  51. //MessagingCenter.Subscribe<AcceptBookingPage, AcceptBookingTimerDto>(this, nameof(AcceptBookingPage), (sender, args) =>
  52. //{
  53. // _viewModel.seconds = args.PendingSeconds;
  54. // _viewModel.PickUpAddress = args.PickUPAddress;
  55. // _viewModel.DropUpAddress = args.DropUpAddress;
  56. // if (_viewModel.seconds > 0)
  57. // {
  58. // _viewModel.IsVisibleAcceptBookingView = true;
  59. // }
  60. //});
  61. // handle clearing of an accepted booking
  62. MessagingCenter.Unsubscribe<App, string>(this, "ClearAcceptance");
  63. MessagingCenter.Subscribe<App, string>(this, "ClearAcceptance", async (sender, args) =>
  64. {
  65. await Navigation.PopToRootAsync();
  66. _viewModel.IsVisibleAcceptBookingView = false;
  67. _viewModel.seconds = 0;
  68. });
  69. //MessagingCenter.Unsubscribe<AcceptBookingPage, string>(this, "ClearAcceptance");
  70. //MessagingCenter.Subscribe<AcceptBookingPage, string>(this, "ClearAcceptance", async (sender, args) =>
  71. //{
  72. // await Navigation.PopToRootAsync();
  73. // await Shell.Current.GoToAsync($"{nameof(HomePage)}");
  74. // _viewModel.IsVisibleAcceptBookingView = false;
  75. // _viewModel.seconds = 0;
  76. //});
  77. // Used to handle notification by using MessagingCenter same is tablet based notification system
  78. MessagingCenter.Subscribe<App, string>(this, nameof(App), (sender, args) =>
  79. {
  80. _viewModel.GetUnreadNotificationCount();
  81. });
  82. MessagingCenter.Subscribe<App, NotificationDto>(this, nameof(App), async (sender, notification) =>
  83. {
  84. _viewModel.GetUnreadNotificationCount();
  85. });
  86. }
  87. #endregion
  88. #region Events
  89. private void SettingsRequired_Tapped(object sender, EventArgs e)
  90. {
  91. // Navigation.ShowPopup(new AppPermissiontSetDialogPage(this, Constant.FROM_HOME_PAGE));
  92. }
  93. #endregion
  94. #region Methods
  95. [Obsolete]
  96. protected override void OnAppearing()
  97. {
  98. base.OnAppearing();
  99. //CheckAppPermissionStatus();
  100. isLocationPermitted = Preferences.Get(SecureStorageData.IsLocationPermitted, false);
  101. isBatteryOptimizationDisabled = Preferences.Get(SecureStorageData.IsBatteryOptimizationDisabled, false);
  102. IsSettingRequired();
  103. GoToLoginPage();
  104. Task.Run(async () =>
  105. {
  106. GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
  107. await _viewModel.OnAppearing();
  108. DisplayBookingOrNotification(_viewModel.HasUnreadNotifications, _viewModel.IsBookingAvailable, _viewModel.availableBookingIDs);
  109. });
  110. }
  111. [Obsolete]
  112. private async void CheckAppPermissionStatus()
  113. {
  114. var status = await Permissions.CheckStatusAsync<Permissions.LocationAlways>();
  115. if (status == PermissionStatus.Granted)
  116. {
  117. Preferences.Set(SecureStorageData.IsLocationPermitted, true);
  118. }
  119. else
  120. {
  121. Preferences.Set(SecureStorageData.IsLocationPermitted, false);
  122. }
  123. if (Device.RuntimePlatform == Device.Android)
  124. {
  125. if (DependencyService.Get<IBatteryInfo>().CheckIsIgnoringBatteryOptimizations())
  126. {
  127. Preferences.Set(SecureStorageData.IsBatteryOptimizationDisabled, true);
  128. }
  129. else
  130. {
  131. Preferences.Set(SecureStorageData.IsBatteryOptimizationDisabled, false);
  132. }
  133. }
  134. }
  135. private async void GoToLoginPage()
  136. {
  137. ValidateTokenResponseDto validateTokenResponseDto = new ValidateTokenResponseDto();
  138. GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
  139. var token = Preferences.Get(SecureStorageData.Token, "");
  140. var fcmToken = Preferences.Get("FcmToken", "");
  141. validateTokenResponseDto = await gmCabsDriverService.ValidateAuthToken(token, fcmToken);
  142. if (!validateTokenResponseDto.Result)
  143. {
  144. var message = new StopServiceMessage();
  145. MessagingCenter.Send(message, "ServiceStopped");
  146. var lastLatitude = Convert.ToDouble(Preferences.Get("lastLat", "0"));
  147. var lastLongitude = Convert.ToDouble(Preferences.Get("lastLng", "0"));
  148. await gmCabsDriverService.LogoutDriverApp(token, lastLatitude, lastLongitude);
  149. //Navigation.ShowPopup(new LoginErrorAlertDialogPage());
  150. }
  151. }
  152. protected override void OnDisappearing()
  153. {
  154. base.OnDisappearing();
  155. _viewModel.OnDisappearing();
  156. }
  157. private async void OnNotificationViewClicked(object sender, EventArgs e)
  158. {
  159. // await Navigation.PushAsync(new NotificationsPage());
  160. }
  161. private async void OnBookingViewClicked(object sender, EventArgs e)
  162. {
  163. // await Navigation.PushAsync(new BookingsPage(_viewModel.CurrentLocation.Latitude, _viewModel.CurrentLocation.Longitude));
  164. }
  165. [Obsolete]
  166. private async void DisplayBookingOrNotification(bool HasUnreadNotifications, bool IsBookingAvailable, List<Guid> availableBookingIDs)
  167. {
  168. if (isLocationPermitted)
  169. {
  170. var currentLocation = Geolocation.GetLastKnownLocationAsync();
  171. CurrentLat = currentLocation.Result.Latitude;
  172. CurrentLng = currentLocation.Result.Longitude;
  173. var isInitLaunched = Preferences.Get(SecureStorageData.InitLaunched, "");
  174. if (String.IsNullOrEmpty(isInitLaunched))
  175. {
  176. Preferences.Set(SecureStorageData.InitLaunched, "1");
  177. if (HasUnreadNotifications && !IsBookingAvailable)
  178. {
  179. //open notification page
  180. //Device.BeginInvokeOnMainThread(() =>
  181. //{
  182. // Navigation.PushAsync(new NotificationsPage());
  183. //});
  184. }
  185. else if (!HasUnreadNotifications && IsBookingAvailable && availableBookingIDs != null && availableBookingIDs.Count == 1)
  186. {
  187. string bookingJson = "";
  188. string rydoAccessToken = Preferences.Get(EftposLoginResponse.RYDO_ACCESS_TOKEN, "");
  189. Guid driverId = Guid.Parse(Preferences.Get(LoginResponseDto.USER_CODE, ""));
  190. GMCabsDriverService gmCabsDriverService = new GMCabsDriverService();
  191. var bookings = await gmCabsDriverService.GetBookings(rydoAccessToken, driverId, CurrentLat, CurrentLng);
  192. if (bookings.Count > 0)
  193. {
  194. if (bookings.Exists(x => x.BookingId == availableBookingIDs.First()))
  195. {
  196. bookingJson = JsonSerializer.Serialize(bookings.Single(x => x.BookingId == availableBookingIDs.First()));
  197. }
  198. else
  199. {
  200. bookingJson = JsonSerializer.Serialize(bookings.First());
  201. }
  202. Device.BeginInvokeOnMainThread(async () =>
  203. {
  204. //await Shell.Current.GoToAsync($"{nameof(BookingDetailsPage)}?{nameof(BookingDetailViewModel.BookingJson)}={bookingJson}");
  205. });
  206. }
  207. }
  208. }
  209. }
  210. }
  211. [Obsolete]
  212. private void IsSettingRequired()
  213. {
  214. if (isLocationPermitted && isBatteryOptimizationDisabled)
  215. {
  216. SettingsRequired.IsVisible = false;
  217. }
  218. else
  219. {
  220. if (Device.RuntimePlatform == Device.iOS && isLocationPermitted)
  221. {
  222. SettingsRequired.IsVisible = false;
  223. }
  224. else
  225. {
  226. SettingsRequired.IsVisible = true;
  227. }
  228. }
  229. }
  230. #endregion
  231. }