Ejemplo n.º 1
0
        private async Task RefreshInstructionsAsync()
        {
            this.IsRefreshingInstructions = true;
            var mostRecentAction = "Started refreshing manifest screen";

            if (!_initialised)
            {
                return;
            }

            if (!_infoService.CurrentDriverID.HasValue)
            {
                // If there is no current driver id then log the user out.
                // This can happen if the Android lifecycle causes the activity to be destroyed and recreated.
                // A better solution would be to implement save and restore of the state on the lifecyle events, but the
                // simple attempt at this that has been tried introduced more bugs than it solved, so more work is needed.
                MvxTrace.Error("CurrentDriverID null when attempting to refresh the Manifest screen.");
                await _navigationService.DirectLogoutAsync();

                return;
            }

            try
            {
                var today = DateTime.Today;

                // get instruction data models from repository and order them
                var activeInstructionsDataModels = await _mobileDataRepository.GetInProgressInstructionsAsync(_infoService.CurrentDriverID.Value);

                var nonActiveInstructionsDataModels = await _mobileDataRepository.GetNotStartedInstructionsAsync(_infoService.CurrentDriverID.Value);

                mostRecentAction = "Got instructions from database";

                if (!_displayRetention.HasValue || !_displaySpan.HasValue)
                {
                    var applicationProfileData = await _applicationProfileRepository.GetAllAsync();

                    var applicationProfile = applicationProfileData.OrderByDescending(x => x.IntLink).First();
                    _displayRetention = applicationProfile.DisplayRetention;
                    _displaySpan      = applicationProfile.DisplaySpan;
                }

                mostRecentAction             = "Set display spans";
                activeInstructionsDataModels =
                    activeInstructionsDataModels
                    .Where(i => i.EffectiveDate <today.AddDays(_displaySpan.Value) && i.EffectiveDate> today.AddDays(-_displayRetention.Value))
                    .OrderBy(x => x.EffectiveDate);

                nonActiveInstructionsDataModels =
                    nonActiveInstructionsDataModels
                    .Where(i => i.EffectiveDate <today.AddDays(_displaySpan.Value) && i.EffectiveDate> today.AddDays(-_displayRetention.Value))
                    .OrderBy(x => x.EffectiveDate);

                mostRecentAction = "Getting non complete messages";
                var nonCompletedeMessages = await _mobileDataRepository.GetNonCompletedMessagesAsync(_infoService.CurrentDriverID.Value);

                var messageDataModels = nonCompletedeMessages.OrderBy(x => x.EffectiveDate);

                if (activeInstructionsDataModels.ToList().Count == 0)
                {
                    List <DummyMobileData> noneShowingList = new List <DummyMobileData>();
                    noneShowingList.Add(new DummyMobileData()
                    {
                        Order = new Order()
                        {
                            Description = "No Active Instructions"
                        }
                    });
                    IEnumerable <MobileData> noneShowingEnumerable = noneShowingList;
                    activeInstructionsDataModels = (IOrderedEnumerable <MobileData>)noneShowingEnumerable.OrderBy(x => 1);
                    mostRecentAction             = "Created dummy active instructions";
                }

                if (nonActiveInstructionsDataModels.ToList().Count == 0)
                {
                    List <MobileData> noneShowingList = new List <MobileData>();
                    noneShowingList.Add(new DummyMobileData()
                    {
                        Order = new Order()
                        {
                            Description = "No Instructions"
                        }
                    });
                    IEnumerable <MobileData> noneShowingEnumerable = noneShowingList;
                    nonActiveInstructionsDataModels = (IOrderedEnumerable <MobileData>)noneShowingEnumerable.OrderBy(x => 1);
                    mostRecentAction = "Created dummy non active instructions";
                }

                if (messageDataModels.ToList().Count == 0)
                {
                    List <MobileData> noneShowingList = new List <MobileData>();
                    noneShowingList.Add(new DummyMobileData()
                    {
                        Order = new Order()
                        {
                            Description = "No Messages"
                        }
                    });
                    IEnumerable <MobileData> noneShowingEnumerable = noneShowingList;
                    messageDataModels = (IOrderedEnumerable <MobileData>)noneShowingEnumerable.OrderBy(x => 1);
                    mostRecentAction  = "Created dummy messages ";
                }

                // Create the view models
                var activeInstructionsViewModels    = activeInstructionsDataModels.Select(md => new ManifestInstructionViewModel(this, md));
                var nonActiveInstructionsViewModels = nonActiveInstructionsDataModels.Select(md => new ManifestInstructionViewModel(this, md));
                var messageViewModels = messageDataModels.Select(md => new ManifestInstructionViewModel(this, md));
                mostRecentAction = "Created View Models";

                // Update the observable collections in each section
                _activeInstructionsSection.Instructions    = new ObservableCollection <ManifestInstructionViewModel>(activeInstructionsViewModels.OrderBy(ivm => ivm.ArrivalDate));
                _nonActiveInstructionsSection.Instructions = new ObservableCollection <ManifestInstructionViewModel>(nonActiveInstructionsViewModels.OrderBy(ivm => ivm.ArrivalDate));
                _messageSection.Instructions = new ObservableCollection <ManifestInstructionViewModel>(messageViewModels.OrderBy(ivm => ivm.ArrivalDate));
                mostRecentAction             = "Updated collections";
                // Let the UI know the number of instructions has changed
                RaisePropertyChanged(() => InstructionsCount);
                RaisePropertyChanged(() => Sections);
                mostRecentAction = "Raised Property Changes.";
            }
            catch (Exception ex)
            {
                MvxTrace.Error("Exception while refreshing manifest screen: {0}, most recent action: '{1}', at stack trace: {2}", ex.Message, mostRecentAction, ex.StackTrace);
            }
            finally
            {
                this.IsRefreshingInstructions = false;
            }
        }