Example #1
0
        private void ReadContactList_Loaded(object sender, RoutedEventArgs e)
        {
            DatabaseHelperClass Db_Helper = new DatabaseHelperClass();
            ProgressIndicator   progress  = new ProgressIndicator
            {
                IsVisible       = true,
                IsIndeterminate = true,
                Text            = "Đang tải dữ liệu về từ Server..."
            };

            ReadAllContactsList dbcontacts = new ReadAllContactsList();
            var template = dbcontacts.GetAllContacts();

            //neu ko co du lieu ta lay tu webservice ve va insert vao database
            if (template.Count == 0)
            {
                //Chi load tu Service khi tu man hinh login vao
                if (App.isLoaded == false)
                {
                    App.isLoaded = true;
                }
                else
                {
                    return;
                }
                if (NetworkInterface.GetIsNetworkAvailable() == true)
                {
                    ProgressHub.ShowProgress(this.LayoutRoot);
                    SystemTray.SetProgressIndicator(this, progress);
                    ApiHelper.Instance.ExecuteGetAsyn("http://api.androidhive.info/contacts/",
                                                      (response) =>
                    {
                        //parse string response thanh doi tuong cua minh
                        RootObject dataJson = JsonConvert.DeserializeObject <RootObject>(response);
                        //ta co duoc du lieu roi gio insert xuong database nhe
                        foreach (ServiceContacts item in dataJson.ListRoot)
                        {
                            Contacts contact = new Contacts(item.Name, item.Phone.Mobile, item.Email, item.Address, item.Gender);
                            Db_Helper.Insert(contact);
                            DB_ContactList.Add(contact);
                        }

                        //Doc len tu database lai

                        //this.listBoxobj.ItemsSource = DB_ContactList;
                        progress.IsVisible = false;
                        ProgressHub.CloseProgress();
                    }, (statuscode, message) =>
                    {
                        progress.IsVisible = false;
                    });
                }
                else
                {
                    MessageBox.Show("không có kết nối mạng, vui lòng kiểm tra kết nối mạng", "THÔNG BÁO", MessageBoxButton.OK);
                }
            }
            else // con khong thi do du lieu len
            {
                if (App.isLoaded == false)
                {
                    App.isLoaded = true;
                }
                //listBoxobj.ItemsSource = DB_ContactList.OrderByDescending(i => i.Id).ToList();//Latest contact ID can Display first
                foreach (var item in template)
                {
                    DB_ContactList.Add(item);
                }
            }
        }