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.

85 lines
2.7 KiB

  1. using GMCabsDriverAssistant.Models;
  2. using GMCabsDriverAssistant.Services;
  3. using GMCabsDriverAssistant.Utils;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace GMCabsDriverAssistantSolution.ViewModels
  11. {
  12. class NotificationsViewModel : BaseViewModel
  13. {
  14. #region Fields
  15. private GMCabsDriverService gmCabsDriverService;
  16. private NotificationDto _selectedNotification;
  17. #endregion
  18. #region Properties
  19. public ObservableCollection<NotificationDto> Notifications { get; set; }
  20. public Command MarkNotificationAsViewedCommand { get; }
  21. public NotificationDto SelectedNotification
  22. {
  23. get => _selectedNotification;
  24. set => SetProperty(ref _selectedNotification, value);
  25. }
  26. #endregion
  27. #region Constructor
  28. public NotificationsViewModel()
  29. {
  30. Title = "Notifications";
  31. Notifications = new ObservableCollection<NotificationDto>();
  32. MarkNotificationAsViewedCommand = new Command(OnMarkNotificationAsViewedClicked);
  33. GetNotifications();
  34. }
  35. #endregion
  36. #region Methods
  37. private async void GetNotifications()
  38. {
  39. string appToken = Preferences.Get(SecureStorageData.Token, "");
  40. gmCabsDriverService = new GMCabsDriverService();
  41. Notifications.Clear();
  42. List<NotificationDto> notifications = await gmCabsDriverService.GetNotifications(appToken);
  43. if (notifications.Count > 0)
  44. {
  45. foreach (NotificationDto notification in notifications)
  46. {
  47. Notifications.Add(notification);
  48. }
  49. SelectedNotification = Notifications[0];
  50. }
  51. else
  52. {
  53. await Shell.Current.GoToAsync("..");
  54. }
  55. }
  56. private async void OnMarkNotificationAsViewedClicked(object obj)
  57. {
  58. string appToken = Preferences.Get(SecureStorageData.Token, "");
  59. gmCabsDriverService = new GMCabsDriverService();
  60. bool isSuccess = await gmCabsDriverService.MarkNotificationAsViewed(appToken, SelectedNotification.Id);
  61. if (isSuccess)
  62. {
  63. Notifications.Remove(SelectedNotification);
  64. if (Notifications.Count > 0)
  65. {
  66. SelectedNotification = Notifications[0];
  67. }
  68. else
  69. {
  70. await Shell.Current.GoToAsync("..");
  71. }
  72. }
  73. }
  74. #endregion
  75. }
  76. }