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.

254 lines
9.8 KiB

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