Beispiel #1
0
        private async Task GetCertificates()
        {
            try
            {
                using (this.Dialogs.Loading("Progress (No Cancel)"))
                {
                    Certificates.Clear();

                    var buildingUsers = await _buildingService.GetBuildingUsers(BuildingId, false);

                    var cnUsers = buildingUsers.Where(x => x.UserPrivileges.Contains("CN")).ToList();

                    var certsResult = await _certificatesService.GetAllDownloadedCertificates(BuildingId);

                    if (!certsResult.Success)
                    {
                        Dialogs.Toast(certsResult.Message);
                        return;
                    }

                    var certificates = certsResult.ResultObject as List <Certificate>;

                    if (certificates != null && certificates.Any())
                    {
                        certificates = new List <Certificate>(certificates.OrderBy(x => x.CertNumber));

                        foreach (var cert in certificates)
                        {
                            cert.Contractor = cnUsers?.FirstOrDefault(x => x.UserId == cert.ConUserId)?.UserFullname;
                        }

                        Certificates.AddRange(certificates);
                    }
                }
            }
            catch (ServiceAuthenticationException e)
            {
                var result = await TryToLogin();

                if (!result)
                {
                    await NavigationService.NavigateToAsync <LoginViewModel>();
                }
                else
                {
                    await GetCertificates();
                }
            }
            catch (Exception e)
            {
                await ShowErrorAlert(e.Message);
            }
        }
        private async Task GetCertificates(CertificatesFilter filters, bool forceCloud = false)
        {
            try
            {
                using (var dlg = this.Dialogs.Loading("Progress (No Cancel)"))
                {
                    Certificates.Clear();

                    var buildingUsers = await _buildingService.GetBuildingUsers(BuildingId, false);

                    var cnUsers = buildingUsers.Where(x => x.UserPrivileges.Contains("CN")).ToList();

                    var downloadedCertsResult = await _certificatesService.GetAllDownloadedCertificates(BuildingId);

                    if (!downloadedCertsResult.Success)
                    {
                        Dialogs.Toast(downloadedCertsResult.Message);
                        return;
                    }

                    var downloadedCertificates = downloadedCertsResult.ResultObject as List <Certificate>;

                    if (downloadedCertificates != null)
                    {
                        DownloadedCertificates = downloadedCertificates;

                        var certsResult = await _certificatesService.GetAllCertificates(filters, BuildingId, forceCloud);

                        if (!certsResult.Success)
                        {
                            Dialogs.Toast(certsResult.Message);
                            return;
                        }

                        var certsAndNumber = certsResult.ResultObject as Tuple <IEnumerable <CertificateRow>, int, int>;
                        if (certsAndNumber == null)
                        {
                            return;
                        }

                        Dialogs.Toast("Results; " + certsAndNumber.Item3 + " records returned out of " + certsAndNumber.Item2 + " found.");

                        var certificates = certsAndNumber.Item1;

                        if (certificates != null && certificates.Any())
                        {
                            foreach (var certificate in certificates)
                            {
                                var selectableCertificate = new CertificateRowSelect()
                                {
                                    Certificate = certificate, IsSelected = false
                                };
                                //if (downloadedCertificates.Any(x => x.CertId == certificate.CertId))
                                //    selectableCertificate.IsSelected = true;
                                Certificates.Add(selectableCertificate);
                            }
                        }

                        foreach (var cert in Certificates)
                        {
                            cert.Certificate.Contractor = cnUsers?.FirstOrDefault(x => x.UserId == cert.Certificate.ConUserId)?.UserFullname;
                        }
                    }
                }
            }
            catch (ServiceAuthenticationException e)
            {
                var result = await TryToLogin();

                if (!result)
                {
                    await NavigationService.NavigateToAsync <LoginViewModel>();
                }
                else
                {
                    await GetCertificates(filters, forceCloud);
                }
            }
            catch (Exception e)
            {
                await ShowErrorAlert(e.Message);
            }
        }