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.

107 lines
3.4 KiB

1 year ago
  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. public class CouponsViewModel : BaseViewModel
  13. {
  14. #region Fields
  15. private string emptyViewMessage;
  16. #endregion
  17. #region Properties
  18. public string EmptyViewMessage
  19. {
  20. get => emptyViewMessage;
  21. set => SetProperty(ref emptyViewMessage, value);
  22. }
  23. public ObservableCollection<CouponDto> Coupons { get; }
  24. public Command SyncCouponCommand { get; }
  25. #endregion
  26. #region Constructor
  27. [Obsolete]
  28. public CouponsViewModel()
  29. {
  30. Title = "Coupons";
  31. SyncCouponCommand = new Command(async () => await GetCoupons());
  32. Coupons = new ObservableCollection<CouponDto>();
  33. HandleReceivedMessages();
  34. }
  35. #endregion
  36. #region Methods
  37. public void OnAppearing()
  38. {
  39. Task.Run(async () => await GetCoupons());
  40. }
  41. private async Task GetCoupons()
  42. {
  43. EmptyViewMessage = "Loading Coupons...";
  44. string appToken = Preferences.Get(SecureStorageData.Token, "");
  45. GMCabsDriverService gMCabsDriverService = new GMCabsDriverService();
  46. Coupons.Clear();
  47. List<CouponDto> coupons = await gMCabsDriverService.GetCoupons(appToken);
  48. if (coupons != null && coupons.Count > 0)
  49. {
  50. //foreach (CouponDto coupon in coupons)
  51. //{
  52. // QRCodeGenerator qrGenerator = new QRCodeGenerator();
  53. // QRCodeData qrCodeData = qrGenerator.CreateQrCode(coupon.Id.ToString(), QRCodeGenerator.ECCLevel.Q);
  54. // BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData);
  55. // byte[] qrCodeAsBitmapByteArr = qrCode.GetGraphic(20);
  56. // coupon.Image = ImageSource.FromStream(() => new MemoryStream(qrCodeAsBitmapByteArr));
  57. // Coupons.Add(coupon);
  58. //}
  59. }
  60. else
  61. {
  62. EmptyViewMessage = "No Coupons available";
  63. }
  64. }
  65. [Obsolete]
  66. void HandleReceivedMessages()
  67. {
  68. MessagingCenter.Unsubscribe<App, CouponDto>(this, nameof(App));
  69. MessagingCenter.Subscribe<App, CouponDto>(this, nameof(App), (sender, couponDto) => {
  70. Device.BeginInvokeOnMainThread(() => {
  71. CouponDto redeemedcoupon = new CouponDto
  72. {
  73. Id = couponDto.Id,
  74. Status = couponDto.Status
  75. };
  76. List<CouponDto> copyCoupons = new List<CouponDto>(Coupons);
  77. Coupons.Clear();
  78. foreach (var item in copyCoupons)
  79. {
  80. if (item.Id == redeemedcoupon.Id)
  81. {
  82. item.Image = ImageSource.FromFile("scanned.png");
  83. }
  84. Coupons.Add(item);
  85. }
  86. });
  87. });
  88. }
  89. #endregion
  90. }
  91. }