Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. using BWapp.Config;
  2. using BWapp.Model;
  3. using BWapp.Services;
  4. using GalaSoft.MvvmLight;
  5. using GalaSoft.MvvmLight.Command;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using Xamarin.Forms;
  12.  
  13. namespace BWapp.ViewModels
  14. {
  15. public class AddAuctionViewModel : ViewModelBase
  16. {
  17. INavigationService _navigationService;
  18. IDbService _dbService;
  19. IAuctionService _auctionService;
  20. bool _isEditing;
  21.  
  22. public AddAuctionViewModel(INavigationService navigationService, IDbService dbService, IAuctionService auctionService)
  23. {
  24. _navigationService = navigationService;
  25. _dbService = dbService;
  26. _auctionService = auctionService;
  27. Init(_auctionService.SelectedAuction);
  28. _auctionService.OnSelectedAuctionChanged += _auctionService_OnSelectedAuctionChanged;
  29. }
  30.  
  31. private void _auctionService_OnSelectedAuctionChanged(object sender, AuctionItem e)
  32. {
  33. Device.BeginInvokeOnMainThread(() => Init(e));
  34. }
  35.  
  36. void Init(AuctionItem auction)
  37. {
  38. if (auction is AuctionItem auctionItem)
  39. {
  40. _isEditing = true;
  41. AuctionItem = auctionItem;
  42.  
  43. if (DateTime.Now < AuctionItem.Date)
  44. {
  45. Today = DateTime.Now;
  46. Time = AuctionItem.Date.TimeOfDay;
  47. }
  48. else
  49. {
  50. Today = AuctionItem.Date;
  51. Time = Today.TimeOfDay;
  52. }
  53. }
  54. else
  55. {
  56. _isEditing = false;
  57. AuctionItem = new AuctionItem();
  58. }
  59. }
  60.  
  61. public void OnViewAppearing()
  62. {
  63. if (!_isEditing)
  64. {
  65. AuctionItem = new AuctionItem();
  66. Today = DateTime.Now;
  67. Time = DateTime.Now.TimeOfDay;
  68. }
  69. }
  70.  
  71. public void OnViewDisappearing()
  72. {
  73. AuctionItem = null;
  74. }
  75.  
  76. public RelayCommand SaveAuction => new RelayCommand(async () =>
  77. {
  78. //Combination Date with Time
  79. //AuctionItem.Date=Date
  80. AuctionItem.Date = AuctionItem.Date.Add(Time);
  81.  
  82. //Check input values
  83. if (AuctionItem.Name != null && AuctionItem.Date >= Today)
  84. {
  85. AuctionItem.DateString = AuctionItem.Date.ToString("yyyy-MM-dd HH:mm:ss");
  86. AuctionItem.BackgroundColor = ColorGenerator.GenerateRandomPleasingColor();
  87. _dbService.SaveAuction(AuctionItem);
  88. await _navigationService.GoBack();
  89. }
  90. //Show alfert when values are empty or datetime is wrong
  91. else
  92. {
  93. if (AuctionItem.Name == null)
  94. {
  95. await App.Current.MainPage.DisplayAlert("Puste pole", "Nie podałeś nazwy", "Ok");
  96. }
  97. else
  98. {
  99. await App.Current.MainPage.DisplayAlert("Zła godzina", "Ustawiłeś czas przeszły.\nPodaj aktualną godzinę aukcji.", "Ok");
  100. }
  101. }
  102. });
  103.  
  104. //Property for AuctionItem to save to Database
  105. private AuctionItem _auctionItem;
  106. public AuctionItem AuctionItem
  107. {
  108. get
  109. {
  110. return _auctionItem;
  111. }
  112. set
  113. {
  114. _auctionItem = value;
  115. RaisePropertyChanged(nameof(AuctionItem));
  116. }
  117. }
  118.  
  119. //Return current DATE in format from BW_Auctions for Placeholder
  120. private DateTime _today { get; set; }
  121. public DateTime Today
  122. {
  123. get { return _today; }
  124. set
  125. {
  126. _today = value;
  127. RaisePropertyChanged(nameof(Today));
  128. }
  129. }
  130. //Return current TIME in format from BW_Auctions for Placeholder
  131. private TimeSpan _time { get; set; }
  132. public TimeSpan Time
  133. {
  134. get { return _time; }
  135. set
  136. {
  137. _time = value;
  138. RaisePropertyChanged(nameof(Time));
  139. }
  140. }
  141.  
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement