Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using BWapp.Config;
- using BWapp.Model;
- using BWapp.Services;
- using GalaSoft.MvvmLight;
- using GalaSoft.MvvmLight.Command;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Xamarin.Forms;
- namespace BWapp.ViewModels
- {
- public class AddAuctionViewModel : ViewModelBase
- {
- INavigationService _navigationService;
- IDbService _dbService;
- IAuctionService _auctionService;
- bool _isEditing;
- public AddAuctionViewModel(INavigationService navigationService, IDbService dbService, IAuctionService auctionService)
- {
- _navigationService = navigationService;
- _dbService = dbService;
- _auctionService = auctionService;
- Init(_auctionService.SelectedAuction);
- _auctionService.OnSelectedAuctionChanged += _auctionService_OnSelectedAuctionChanged;
- }
- private void _auctionService_OnSelectedAuctionChanged(object sender, AuctionItem e)
- {
- Device.BeginInvokeOnMainThread(() => Init(e));
- }
- void Init(AuctionItem auction)
- {
- if (auction is AuctionItem auctionItem)
- {
- _isEditing = true;
- AuctionItem = auctionItem;
- if (DateTime.Now < AuctionItem.Date)
- {
- Today = DateTime.Now;
- Time = AuctionItem.Date.TimeOfDay;
- }
- else
- {
- Today = AuctionItem.Date;
- Time = Today.TimeOfDay;
- }
- }
- else
- {
- _isEditing = false;
- AuctionItem = new AuctionItem();
- }
- }
- public void OnViewAppearing()
- {
- if (!_isEditing)
- {
- AuctionItem = new AuctionItem();
- Today = DateTime.Now;
- Time = DateTime.Now.TimeOfDay;
- }
- }
- public void OnViewDisappearing()
- {
- AuctionItem = null;
- }
- public RelayCommand SaveAuction => new RelayCommand(async () =>
- {
- //Combination Date with Time
- //AuctionItem.Date=Date
- AuctionItem.Date = AuctionItem.Date.Add(Time);
- //Check input values
- if (AuctionItem.Name != null && AuctionItem.Date >= Today)
- {
- AuctionItem.DateString = AuctionItem.Date.ToString("yyyy-MM-dd HH:mm:ss");
- AuctionItem.BackgroundColor = ColorGenerator.GenerateRandomPleasingColor();
- _dbService.SaveAuction(AuctionItem);
- await _navigationService.GoBack();
- }
- //Show alfert when values are empty or datetime is wrong
- else
- {
- if (AuctionItem.Name == null)
- {
- await App.Current.MainPage.DisplayAlert("Puste pole", "Nie podałeś nazwy", "Ok");
- }
- else
- {
- await App.Current.MainPage.DisplayAlert("Zła godzina", "Ustawiłeś czas przeszły.\nPodaj aktualną godzinę aukcji.", "Ok");
- }
- }
- });
- //Property for AuctionItem to save to Database
- private AuctionItem _auctionItem;
- public AuctionItem AuctionItem
- {
- get
- {
- return _auctionItem;
- }
- set
- {
- _auctionItem = value;
- RaisePropertyChanged(nameof(AuctionItem));
- }
- }
- //Return current DATE in format from BW_Auctions for Placeholder
- private DateTime _today { get; set; }
- public DateTime Today
- {
- get { return _today; }
- set
- {
- _today = value;
- RaisePropertyChanged(nameof(Today));
- }
- }
- //Return current TIME in format from BW_Auctions for Placeholder
- private TimeSpan _time { get; set; }
- public TimeSpan Time
- {
- get { return _time; }
- set
- {
- _time = value;
- RaisePropertyChanged(nameof(Time));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement