Пример #1
0
        public async void OnRefresh()
        {
            //Start querying the
            Instructions = "Downloading your Data";
            Animate      = true;
            Refreshing   = true;

            //Diagnostics
            string Message    = string.Empty;;
            string StackTrace = string.Empty;
            bool   _AnyError  = false;

            await Task.Run(() =>
            {
                //Query the user's data from the back end SSMS
                try
                {
                    DataVaultWebServiceClient dataService = new DataVaultWebServiceClient(ConfigurationManager.InSecurePublicBinding(), new System.ServiceModel.EndpointAddress(Constants.Data_InSecureUrl));
                    if (dataService.HasServiceAvailable())
                    {
                        #region Download Notes
                        var obj = new List <Notes>();
                        notesManager.Delete_AllNotesByContactID(Constants.InMemory_ContactID); //Clear all notes then download them

                        var notes = dataService._GetNotes_ByUserID(Constants.InMemory_ContactID);
                        if (notes._Notes.Count != 0)
                        {
                            notes._Notes.ForEach(w => obj.Add(LocalMapper.MapNote_FromServer(w)));

                            if (notesManager != null)
                            {
                                notesManager.AddNotes(obj);
                            }
                        }
                        #endregion

                        #region Passwords
                        var pObj = new List <Passwords>();
                        passwordManager.Delete_AllPasswordsByContactID(Constants.InMemory_ContactID); //Clear all passwords then download them

                        var passwords = dataService._GetPasswords_ByUserID(Constants.InMemory_ContactID)._Passwords;
                        if (passwords.Count != 0)
                        {
                            passwords.ForEach(w => pObj.Add(LocalMapper.MapPassword_FromServer(w)));

                            if (passwordManager != null)
                            {
                                passwordManager.AddPasswords(pObj);
                            }
                        }
                        #endregion

                        #region Photos

                        var photos = new List <PhotoVideo>();
                        photoVideoManager.Delete_PhotosByUserId(Constants.InMemory_ContactID); //Clear all photos then download them

                        var photos_server = dataService._GetPhotos_ByUserID(Constants.InMemory_ContactID);
                        if (photos_server._Photos.Count != 0)
                        {
                            photos_server._Photos.ForEach(w => photos.Add(LocalMapper.MapPhoto_FromServer(w)));

                            if (photoVideoManager != null)
                            {
                                photoVideoManager.AddPhoto_ByCollections(photos);
                            }
                        }
                        #endregion

                        #region Contacts
                        var curr = new List <Contact>();
                        contactManager.ClearContacts_ForUserID(Constants.InMemory_ContactID);

                        var server_contacts = dataService._GetContacts_ByUserID(Constants.InMemory_ContactID);
                        var contacts        = contactStore.Get_ContactsFromStore <Contact>();
                        if (contacts != null)
                        {
                            contacts.ForEach(w =>
                            {
                                //Add Contact to the contact store for the particular account
                                w.Sys_Creation    = DateTime.Now;
                                w.Sys_Transaction = DateTime.Now;

                                w.Contact_ID = contactManager.Get_NewContactID();
                                w.User_ID    = Constants.InMemory_ContactID;

                                if (!curr.Contains(w))
                                {
                                    curr.Add(w);
                                }
                            });
                        }

                        if (server_contacts._Contacts != null)
                        {
                            server_contacts._Contacts.ForEach(w =>
                            {
                                if (!curr.Contains(LocalMapper.MapContact_FromServer(w)))
                                {
                                    curr.Add(LocalMapper.MapContact_FromServer(w));
                                }
                            });
                        }

                        if (contactManager != null && curr.Count != 0)
                        {
                            contactManager.AddContacts_ByDetails(curr);
                        }

                        #endregion
                    }
                    else
                    {
                        throw new InvalidOperationException("Web Service is not available. Leaving local data intact");
                    }
                }
                catch (Exception ex)
                {
                    _AnyError = true;
                    if (ex.InnerException != null)
                    {
                        Message    = ex.InnerException.Message;
                        StackTrace = ex.InnerException.StackTrace;
                    }
                    else
                    {
                        Message    = ex.Message;
                        StackTrace = ex.StackTrace;
                    }

                    var mEx = new Exceptions(logging, Message, StackTrace);
                    if (mEx != null)
                    {
                        mEx.HandleException(mEx, logging);
                    }
                }
                finally
                {
                    //dispose of any memory here
                }
            }).ContinueWith((e) =>
            {
                //Hide the animator when done
                //If any errors occur render them on the dialogue service
                Device.BeginInvokeOnMainThread(() =>
                {
                    Animate    = false;
                    Refreshing = false;
                });
            });
        }