Esempio n. 1
0
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            string synced = string.Empty;

            try
            {
                if (NavigationContext.QueryString.TryGetValue("synced", out synced))
                {
                    int isSynced = Convert.ToInt32(synced);

                    if (isSynced == 1)
                    {
                        TitlePanel1.Visibility = System.Windows.Visibility.Visible;
                        TitlePanel2.Visibility = System.Windows.Visibility.Collapsed;
                    }
                    else
                    {
                        TitlePanel2.Visibility = System.Windows.Visibility.Visible;
                        TitlePanel1.Visibility = System.Windows.Visibility.Collapsed;
                    }
                }

                _model = App.Current.EntityService.CurrentPhoneContact;

                if (_model != null)
                    this.DataContext = (object)_model;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("OnNavigatedTo in ProfilePage failed: " + ex.Message);
            }
        }
Esempio n. 2
0
        // Updates names of contacts which is already in application cache (synced earlier) with info from current contacts book.
        // If some contacts was not found in cache, they need to be synced - so we do it in a background.
        private void _UpdateContacts(object sender, ContactsSearchEventArgs e)
        {
            try
            {
                // Get contacts from contacts book.
                var contacts = e.Results;

                if (contacts == null)
                    return;

                string allNumbers = string.Empty;

            // TODO. Probably, we should also REMOVE contacts from application, which was removed from phone...
            // but this is weird case and I don't actually know what exactly to do.

                // Add contacts to the contacts cache if it is not in there yet,
                // and create string of phone numbers to request (if need).
                int watchDog = 0;

                var cache = _contactsCache.GetItems();

                foreach (var contact in contacts)
                {
                    // Get only mobile.
                    var phone = contact.PhoneNumbers.FirstOrDefault(x => x.Kind == PhoneNumberKind.Mobile);

                    if (phone == null || string.IsNullOrEmpty(phone.PhoneNumber))
                        continue;

                    // Try to find mobile phone in cache.
                    var cachedContact = cache.FirstOrDefault(x => _IsEquals(x.VerifiedPhone, _GetOnlyDigits(phone.PhoneNumber)));
                    var viewContact = App.Current.EntityService.Contacts.FirstOrDefault(x => _IsEquals(x.VerifiedPhone, _GetOnlyDigits(phone.PhoneNumber)));

                    if (cachedContact == null)
                    {
                        // Create application contact for it and save... and do request.
                        var phoneContact = new PhoneContact(contact.DisplayName, _GetOnlyDigits(phone.PhoneNumber), App.Current.EntityService.DefaultAvatar);
                        _contactsCache.AddItem(phoneContact);

                        if (viewContact == null)
                            App.Current.EntityService.Contacts.Add(phoneContact);

                        // Do not send more than 1000 per one request.

            //TODO. Need functionality to re-request. But, does it really need? Who have more than 1000 contacts?

                        // Get string with phone numbers splitted by comma
                        string digits = _GetOnlyDigits(phone.PhoneNumber);
                        if (watchDog < 1000 && !string.IsNullOrEmpty(digits))
                        {
                            allNumbers += digits + ",";
                            watchDog += 1;
                        }
                    }
                    else
                    {
                        // Just update name info.
                        cachedContact.ContactName = contact.DisplayName;

                        if (viewContact != null)
                            viewContact.ContactName = contact.DisplayName;
                    }
                }

                _contactsCache.Save();

                // If need to do request info...do it.
                if (!string.IsNullOrEmpty(allNumbers))
                {
                    // Remove last comma.
                    allNumbers = allNumbers.Remove(allNumbers.Length - 1, 1);

                    GlobalIndicator.Instance.Text = AppResources.ContactsSyncing;
                    GlobalIndicator.Instance.IsLoading = true;

                    // Get info.
                    var op = new FriendsGetByPhones(allNumbers, _GetUserInfo);
                    op.Execute();
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ContactsSearch failed: " + ex.Message);
            }
        }