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.

237 lines
7.7 KiB

1 year ago
1 year ago
1 year ago
  1. using GMCabsDriverAssistant.Models;
  2. using GMCabsDriverAssistant.Services;
  3. using GMCabsDriverAssistant.Utils;
  4. using QRCoder;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. namespace GMCabsDriverAssistantSolution.ViewModels
  12. {
  13. public class CouponsV2ViewModel : BaseViewModel
  14. {
  15. #region Fields
  16. Page page;
  17. private string emptyViewMessage;
  18. public ImageSource image;
  19. public float amount;
  20. public DateTime expiryDate;
  21. private CouponDto _selectedCoupon;
  22. private bool _hasCoupons = false;
  23. private bool _hasEmptyMessage = false;
  24. private bool _isNextButton = false;
  25. private bool _isBackButton = false;
  26. int couponPosition = 0;
  27. int couponCount = 0;
  28. #endregion
  29. #region Properties
  30. public string EmptyViewMessage
  31. {
  32. get => emptyViewMessage;
  33. set => SetProperty(ref emptyViewMessage, value);
  34. }
  35. public ObservableCollection<CouponDto> Coupons { get; }
  36. public Command SyncCouponCommand { get; }
  37. public ImageSource Image
  38. {
  39. get => image;
  40. set => SetProperty(ref image, value);
  41. }
  42. public float Amount
  43. {
  44. get => amount;
  45. set => SetProperty(ref amount, value);
  46. }
  47. public DateTime ExpiryDate
  48. {
  49. get => expiryDate;
  50. set => SetProperty(ref expiryDate, value);
  51. }
  52. public CouponDto SelectedCoupon
  53. {
  54. get => _selectedCoupon;
  55. set => SetProperty(ref _selectedCoupon, value);
  56. }
  57. public bool HasCoupons
  58. {
  59. get => _hasCoupons;
  60. set => SetProperty(ref _hasCoupons, value);
  61. }
  62. public bool HasEmptyMessage
  63. {
  64. get => _hasEmptyMessage;
  65. set => SetProperty(ref _hasEmptyMessage, value);
  66. }
  67. public bool IsNextButton
  68. {
  69. get => _isNextButton;
  70. set => SetProperty(ref _isNextButton, value);
  71. }
  72. public bool IsBackButton
  73. {
  74. get => _isBackButton;
  75. set => SetProperty(ref _isBackButton, value);
  76. }
  77. public Command NextCouponCommand { get; }
  78. public Command BackCouponCommand { get; }
  79. public Command RedemptionDetails { get; }
  80. #endregion
  81. #region Constructor
  82. public CouponsV2ViewModel(Page page)
  83. {
  84. this.page = page;
  85. Title = "Available Coupons";
  86. SyncCouponCommand = new Command(async () => await GetCoupons());
  87. BackCouponCommand = new Command(() => SelectCouponOption(true));
  88. NextCouponCommand = new Command(() => SelectCouponOption(false));
  89. RedemptionDetails = new Command(() => RedemptionDetailsDialog());
  90. Coupons = new ObservableCollection<CouponDto>();
  91. HandleReceivedMessages();
  92. }
  93. #endregion
  94. #region Methods
  95. public void OnAppearing()
  96. {
  97. Task.Run(async () => await GetCoupons());
  98. }
  99. private async Task GetCoupons()
  100. {
  101. HasCoupons = false;
  102. IsBackButton = false;
  103. IsNextButton = false;
  104. HasEmptyMessage = true;
  105. EmptyViewMessage = "Loading Coupons...";
  106. string appToken = Preferences.Get(SecureStorageData.Token, "");
  107. GMCabsDriverService gMCabsDriverService = new GMCabsDriverService();
  108. Coupons.Clear();
  109. List<CouponDto> coupons = await gMCabsDriverService.GetCoupons(appToken);
  110. if (coupons != null && coupons.Count > 0)
  111. {
  112. couponPosition = 0;
  113. couponCount = coupons.Count;
  114. HasCoupons = true;
  115. HasEmptyMessage = false;
  116. if (couponCount == 1)
  117. {
  118. IsBackButton = false;
  119. IsNextButton = false;
  120. }
  121. else
  122. {
  123. IsNextButton = true;
  124. }
  125. foreach (CouponDto coupon in coupons)
  126. {
  127. QRCodeGenerator qrGenerator = new QRCodeGenerator();
  128. QRCodeData qrCodeData = qrGenerator.CreateQrCode(coupon.Id.ToString(), QRCodeGenerator.ECCLevel.Q);
  129. BitmapByteQRCode qrCode = new BitmapByteQRCode(qrCodeData);
  130. byte[] qrCodeAsBitmapByteArr = qrCode.GetGraphic(20);
  131. coupon.Image = ImageSource.FromStream(() => new MemoryStream(qrCodeAsBitmapByteArr));
  132. Coupons.Add(coupon);
  133. }
  134. Console.WriteLine("Before Position :" + couponPosition);
  135. Image = Coupons[couponPosition].Image;
  136. }
  137. else
  138. {
  139. HasCoupons = false;
  140. HasEmptyMessage = true;
  141. EmptyViewMessage = "No Coupons available";
  142. }
  143. }
  144. private void SelectCouponOption(bool isback)
  145. {
  146. if (isback)
  147. {
  148. couponPosition = couponPosition - 1;
  149. if (couponPosition + 1 == 1 && couponCount > 1)
  150. {
  151. IsBackButton = false;
  152. IsNextButton = true;
  153. }
  154. else
  155. {
  156. IsNextButton = true;
  157. }
  158. }
  159. else
  160. {
  161. couponPosition = couponPosition + 1;
  162. IsBackButton = true;
  163. if (couponCount == couponPosition + 1 && couponCount > 1)
  164. {
  165. IsNextButton = false;
  166. }
  167. }
  168. SelectedCoupon = Coupons[couponPosition];
  169. Image = Coupons[couponPosition].Image;
  170. }
  171. [Obsolete]
  172. void HandleReceivedMessages()
  173. {
  174. MessagingCenter.Unsubscribe<App, CouponDto>(this, nameof(App));
  175. MessagingCenter.Subscribe<App, CouponDto>(this, nameof(App), (sender, couponDto) => {
  176. Device.BeginInvokeOnMainThread(() => {
  177. CouponDto redeemedcoupon = new CouponDto
  178. {
  179. Id = couponDto.Id,
  180. Status = couponDto.Status
  181. };
  182. List<CouponDto> copyCoupons = new List<CouponDto>(Coupons);
  183. Coupons.Clear();
  184. foreach (var item in copyCoupons)
  185. {
  186. if (item.Id == redeemedcoupon.Id)
  187. {
  188. item.Image = ImageSource.FromFile("scanned.png");
  189. }
  190. Coupons.Add(item);
  191. }
  192. Console.WriteLine("After Position :" + couponPosition);
  193. SelectedCoupon = Coupons[couponPosition];
  194. Image = Coupons[couponPosition].Image;
  195. });
  196. });
  197. }
  198. private async void RedemptionDetailsDialog()
  199. {
  200. int issuedFromLocationID = Coupons[couponPosition].IssuedLocationID;
  201. string appToken = Preferences.Get(SecureStorageData.Token, "");
  202. GMCabsDriverService gMCabsDriverService = new GMCabsDriverService();
  203. LocationVoucherDto locationVoucher = await gMCabsDriverService.GetLocationVoucherByLocationID(appToken, issuedFromLocationID);
  204. if (locationVoucher != null)
  205. {
  206. string voucherOfferText = locationVoucher.VoucherOfferText.Replace("<h1>", "").Replace("</h1>", "");
  207. await page.DisplayAlert("Redemption Details", voucherOfferText, "Ok");
  208. }
  209. }
  210. #endregion
  211. }
  212. }