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.

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