public static void ImportContactsAsync(IFileManager fileManager, ContactsOperationToken token, IList <TLUserBase> contacts, Action <Telegram.Api.WindowsPhone.Tuple <int, int> > progressCallback, System.Action cancelCallback)
        {
#if WP8
            Execute.BeginOnThreadPool(async() =>
            {
                //var contacts = _cacheService.GetContacts();
                var totalCount = contacts.Count;
                if (totalCount == 0)
                {
                    return;
                }


                var store           = await ContactStore.CreateOrOpenAsync();
                var importedCount   = 0;
                var delayedContacts = new TLVector <TLInt>();
                foreach (var contact in contacts)
                {
                    if (token.IsCanceled)
                    {
                        cancelCallback.SafeInvoke();
                        return;
                    }

                    try
                    {
                        var delayedContact = await UpdateContactInternalAsync(contact, fileManager, store, true);
                        if (delayedContact != null)
                        {
                            delayedContacts.Add(delayedContact.Id);
                        }
                    }
                    catch (Exception ex)
                    {
                        // continue import after failed contact
                    }
                    //Thread.Sleep(100);
                    importedCount++;
                    progressCallback.SafeInvoke(new Telegram.Api.WindowsPhone.Tuple <int, int>(importedCount, totalCount));
                    //var duration = importedCount == totalCount ? 0.5 : 2.0;
                    //_mtProtoService.SetMessageOnTime(duration, string.Format("Sync contacts ({0} of {1})...", importedCount, totalCount));
                }

                var result = new TLVector <TLInt>();
                foreach (var delayedContact in delayedContacts)
                {
                    result.Add(delayedContact);
                }
                SaveDelayedContactsAsync(result);
            });
#endif
        }
        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (Property.NameEquals(e.PropertyName, () => PeopleHub))
            {
#if WP8
                if (PreviousToken != null)
                {
                    PreviousToken.IsCanceled = true;
                }

                if (PeopleHub)
                {
                    var contacts = CacheService.GetContacts().Where(x => x != null && x.IsContact).ToList();
                    var token    = new ContactsOperationToken();

                    PreviousToken = token;
                    ContactsHelper.ImportContactsAsync(DownloadManager, token, contacts,
                                                       tuple =>
                    {
                        var importedCount = tuple.Item1;
                        var totalCount    = tuple.Item2;

                        var isComplete = importedCount == totalCount;
                        if (isComplete)
                        {
                            PreviousToken = null;
                        }

                        var duration = isComplete ? 0.5 : 2.0;
                        MTProtoService.SetMessageOnTime(duration,
                                                        string.Format(AppResources.SyncContactsProgress, importedCount, totalCount));
                    },
                                                       () =>
                    {
                        MTProtoService.SetMessageOnTime(0.0, string.Empty);
                    });
                }
                else
                {
                    IsPeopleHubEnabled = false;
                    MTProtoService.SetMessageOnTime(25.0, AppResources.DeletingContacts);
                    ContactsHelper.DeleteContactsAsync(() =>
                    {
                        IsPeopleHubEnabled = true;
                        MTProtoService.SetMessageOnTime(0.0, string.Empty);
                    });
                }
#endif
            }
        }
        public static void UpdateDelayedContactsAsync(ICacheService cacheService, IMTProtoService mtProtoService)
        {
#if WP8
            GetDelayedContactsAsync(contactIds =>
            {
                var contacts = new List <TLUserBase>();
                foreach (var contactId in contactIds)
                {
                    var userBase = cacheService.GetUser(contactId);
                    if (userBase != null && userBase.IsContact)
                    {
                        contacts.Add(userBase);
                    }
                }

                if (contacts.Count > 0)
                {
                    var token       = new ContactsOperationToken();
                    var fileManager = IoC.Get <IFileManager>();
                    ChatSettingsViewModel.PreviousToken = token;
                    ImportContactsAsync(fileManager, token, contacts,
                                        tuple =>
                    {
                        var importedCount = tuple.Item1;
                        var totalCount    = tuple.Item2;

                        var isComplete = importedCount == totalCount;
                        if (isComplete)
                        {
                            ChatSettingsViewModel.PreviousToken = null;
                        }

                        var duration = isComplete ? 0.5 : 2.0;
                        mtProtoService.SetMessageOnTime(duration,
                                                        string.Format(AppResources.SyncContactsProgress, importedCount, totalCount));
                    },
                                        () =>
                    {
                        mtProtoService.SetMessageOnTime(0.0, string.Empty);
                    });
                }
            });
#endif
        }