Ejemplo n.º 1
0
        public AccountViewModel
        (
            TimeAccount currentAccounting,
            double workedTime,
            bool fromHistory,
            INavigationService navigationService,
            HistoryViewModel historyViewModel,
            MainPage mainPage,
            TimeAccountingContext timeAccountingContext
        )
        {
            ShouldInit             = true;
            FromHistory            = fromHistory;
            _timeAccountingContext = timeAccountingContext;
            _mainPage          = mainPage;
            _historyViewModel  = historyViewModel;
            _navigationService = navigationService;
            _workedTime        = workedTime;
            _currentAccounting = currentAccounting;
            _isTimerStarted    = !currentAccounting.IsClosed;
            IsStarted          = currentAccounting.IsStarted;
            StartWorkTime      = currentAccounting.StartWorkTime;
            if (!_currentAccounting.IsClosed)
            {
                Breaks = currentAccounting.Breaks;
            }
            else
            {
                Breaks = new ObservableCollection <Break>(
                    currentAccounting.Breaks.OrderBy(x => x.StartBreakTime)
                    .Select(@break => new Break
                {
                    EndBreakTime   = @break.EndBreakTime,
                    StartBreakTime = @break.StartBreakTime,
                    TimeAccount    = @break.TimeAccount
                }));
            }

            EndWorkTime = currentAccounting.EndWorkTime;
            WorkDate    = currentAccounting.WorkDate;

            if (!FromHistory)
            {
                InitializationCommand = ReactiveCommand.CreateFromTask(InitializeAsync);
            }

            StartWorkCommand   = ReactiveCommand.Create(() => StartWorkTime = DateTime.Now.TimeOfDay);
            EndWorkCommand     = ReactiveCommand.Create(() => EndWorkTime = DateTime.Now.TimeOfDay);
            AddBreakCommand    = ReactiveCommand.CreateFromTask(AddBreakExecuteAsync);
            SaveCommand        = ReactiveCommand.CreateFromTask(SaveExecuteAsync);
            AllCommand         = ReactiveCommand.CreateFromTask(AllExecuteAsync);
            CurrentCommand     = ReactiveCommand.CreateFromTask(CurrentExecuteAsync);
            DeleteBreakCommand = ReactiveCommand.CreateFromTask <Break, Unit>(async _ =>
            {
                await DeleteBreakExecuteAsync(_);
                return(Unit.Default);
            });

            this.WhenAnyValue(x => x.StartWorkTime)
            .Skip(1)
            .Throttle(TimeSpan.FromMilliseconds(100), RxApp.MainThreadScheduler)
            .Subscribe(async _ => await UpdateStartWorkExecuteAsync());

            this.WhenAnyValue(x => x.EndWorkTime)
            .Skip(1)
            .Throttle(TimeSpan.FromMilliseconds(100), RxApp.MainThreadScheduler)
            .Subscribe(async _ => await UpdateEndWorkExecuteAsync());

            this.WhenAnyValue(x => x.SelectedBreak)
            .Skip(1)
            .Where(x => x != null)
            .InvokeCommand(DeleteBreakCommand);
            Initialize = true;
        }
Ejemplo n.º 2
0
        private async Task CurrentExecuteAsync()
        {
            IsEnable = false;
            await Task.Delay(100);

            var currentDate       = DateTime.Today;
            var currentAccounting = await _timeAccountingContext.TimeAccounts
                                    .Include(x => x.Breaks)
                                    .SingleOrDefaultAsync(x => x.WorkDate == currentDate && !x.IsClosed)
                                    .ConfigureAwait(false);

            var todayWorks = await _timeAccountingContext.TimeAccounts.Where(x => x.WorkDate == currentDate && x.IsClosed).ToArrayAsync();

            var workedTime = todayWorks?.Sum(v => v.Breaks == null
                                    ? (v.EndWorkTime - v.StartWorkTime).TotalMinutes
                                    : (v.EndWorkTime - v.StartWorkTime).TotalMinutes
                                             - v.Breaks.Sum(d => d.EndBreakTime - d.StartBreakTime))
                             ?? 0;

            if (currentAccounting != null)
            {
                currentAccounting.Breaks = new ObservableCollection <Break>
                                           (
                    currentAccounting.Breaks.OrderBy(y => y.StartBreakTime)
                                           );
                await _navigationService.NavigateToAsync
                (
                    _accountFactory(currentAccounting, workedTime, false)
                );

                await Task.Delay(150);

                IsEnable = true;
                return;
            }
            var isWorkDay = DateService.IsWorking(currentDate.DayOfWeek);

            if (!isWorkDay)
            {
                var result = await Application.Current.MainPage.DisplayAlert(
                    TranslationCodeExtension.GetTranslation("WeekendAlertTitle"),
                    TranslationCodeExtension.GetTranslation("WeekendAlertText"),
                    TranslationCodeExtension.GetTranslation("YesWeekendAlertText"),
                    TranslationCodeExtension.GetTranslation("NoText"));

                if (!result)
                {
                    IsEnable = true;
                    return;
                }
                await Task.Delay(50);
            }
            var previousAccount = await _timeAccountingContext.TimeAccounts
                                  .Include(x => x.Breaks)
                                  .OrderByDescending(x => x.WorkDate)
                                  .FirstOrDefaultAsync(x => x.WorkDate < currentDate && !x.IsClosed)
                                  .ConfigureAwait(false);

            if (previousAccount != null && workedTime <= default(double))
            {
                if (previousAccount.IsStarted)
                {
                    previousAccount.IsClosed    = true;
                    previousAccount.EndWorkTime = new TimeSpan(23, 59, 59);

                    if (previousAccount.StartWorkTime > previousAccount.EndWorkTime)
                    {
                        previousAccount.StartWorkTime = previousAccount.EndWorkTime;
                    }

                    var workTime = previousAccount.Breaks == null
                        ? (previousAccount.EndWorkTime - previousAccount.StartWorkTime).TotalMinutes
                        : (previousAccount.EndWorkTime - previousAccount.StartWorkTime).TotalMinutes
                                   - previousAccount.Breaks.Sum(d => d.EndBreakTime - d.StartBreakTime);
                    previousAccount.OverWork = previousAccount.IsWorking
                        ? workTime - DateService.WorkingTime(previousAccount.WorkDate.DayOfWeek)
                        : workTime;
                    _timeAccountingContext.Entry(previousAccount).State = EntityState.Modified;
                }
                else
                {
                    _timeAccountingContext.TimeAccounts.Remove(previousAccount);
                }

                await _timeAccountingContext.SaveChangesAsync()
                .ConfigureAwait(false);
            }

            var notClosedAccounts = await _timeAccountingContext.TimeAccounts
                                    .Include(x => x.Breaks)
                                    .Where(x => !x.IsClosed)
                                    .ToArrayAsync()
                                    .ConfigureAwait(false); // Can be if user change date on the device

            if (notClosedAccounts.Length != 0)
            {
                foreach (var notClosedAccount in notClosedAccounts)
                {
                    if (notClosedAccount.Breaks.Count != 0)
                    {
                        _timeAccountingContext.Breaks.RemoveRange(notClosedAccount.Breaks);
                    }
                    _timeAccountingContext.TimeAccounts.Remove(notClosedAccount);
                }
            }

            currentAccounting = new TimeAccount {
                WorkDate = currentDate, IsWorking = isWorkDay
            };
            _timeAccountingContext.TimeAccounts.Add(currentAccounting);
            await _timeAccountingContext.SaveChangesAsync()
            .ConfigureAwait(false);

            await _navigationService.NavigateToAsync
            (
                _accountFactory(currentAccounting, workedTime, false)
            );

            await Task.Delay(150);

            IsEnable = true;
        }