예제 #1
0
        public ContactsViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, IEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            Items = new AlphaKeyGroup <TLUserBase>("@");

            _contacts = new ObservableCollection <AlphaKeyGroup <TLUserBase> >();
            _contacts.Add((AlphaKeyGroup <TLUserBase>)Items);

            DisplayName = LowercaseConverter.Convert(AppResources.Contacts);
            Status      = AppResources.Loading;

            EventAggregator.Subscribe(this);
        }
예제 #2
0
        public ContactsViewModel(IFileManager fileManager, ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            _fileManager = fileManager;

            Items = new AlphaKeyGroup <TLUserBase>("@");

            _contacts = new ObservableCollection <AlphaKeyGroup <TLUserBase> > {
                (AlphaKeyGroup <TLUserBase>)Items
            };

            DisplayName = LowercaseConverter.Convert(AppResources.Contacts);
            Status      = AppResources.Loading;
        }
예제 #3
0
        private void OnSearchCompleted(object sender, ContactsSearchEventArgs args, bool fullReplace)
        {
            TLUtils.WritePerformance("::Search contacts time: " + _stopwatch.Elapsed);
            _stopwatch = Stopwatch.StartNew();
            var contacts = args.Results;

            //var usersCache = Items.ToDictionary(x => x.Index);

            var contactsCache         = new Dictionary <string, Contact>();
            var notRegisteredContacts = new List <TLUserNotRegistered>();

            foreach (var contact in contacts)
            {
                foreach (var phoneNumber in contact.PhoneNumbers)
                {
                    if (!contactsCache.ContainsKey(phoneNumber.PhoneNumber))
                    {
                        contactsCache.Add(phoneNumber.PhoneNumber, contact);
                    }
                }
                var completeName = contact.CompleteName;
                var firstName    = completeName != null ? completeName.FirstName ?? "" : "";
                var lastName     = completeName != null ? completeName.LastName ?? "" : "";


                if (string.IsNullOrEmpty(firstName) &&
                    string.IsNullOrEmpty(lastName))
                {
                    if (!string.IsNullOrEmpty(contact.DisplayName))
                    {
                        firstName = contact.DisplayName;
                    }
                    else
                    {
                        continue;
                    }
                }

                var clientId = contact.GetHashCode();
                var phone    = contact.PhoneNumbers.FirstOrDefault();
                if (phone != null)
                //&& !usersCache.ContainsKey(clientId))
                {
                    var notRegisteredUser = new TLUserNotRegistered
                    {
                        Id           = new TLInt(-1),
                        Phone        = new TLString(phone.PhoneNumber),
                        _firstName   = new TLString(firstName),
                        _lastName    = new TLString(lastName),
                        ClientId     = new TLLong(clientId),
                        _photo       = new TLPhotoEmpty(),
                        PhoneNumbers = contact.PhoneNumbers
                    };

                    if (lastName.Length > 0)
                    {
                        notRegisteredContacts.Add(notRegisteredUser);
                    }
                }
            }


            TLUtils.WritePerformance("::Get not registered phones time: " + _stopwatch.Elapsed);

            _stopwatch = Stopwatch.StartNew();

            var groups = AlphaKeyGroup <TLUserBase> .CreateGroups(
                notRegisteredContacts,
                Thread.CurrentThread.CurrentUICulture,
                x => x.FullName,
                false);


            TLUtils.WritePerformance("::Get groups time: " + _stopwatch.Elapsed);

            foreach (var @group in groups)
            {
                var gr = new AlphaKeyGroup <TLUserBase>(@group.Key);
                foreach (var u in @group.OrderBy(x => x.FullName))
                {
                    gr.Add(u);
                }

                BeginOnUIThread(() =>
                {
                    Contacts.Add(gr);
                });
            }

            var phones            = contactsCache.Keys.Take(Constants.MaxImportingContactsCount).ToList();
            var importingContacts = new TLVector <TLInputContactBase>();

            foreach (var phone in phones)
            {
                var completeName = contactsCache[phone].CompleteName;

                var firstName = completeName != null ? completeName.FirstName ?? "" : "";
                var lastName  = completeName != null ? completeName.LastName ?? "" : "";

                if (string.IsNullOrEmpty(firstName) &&
                    string.IsNullOrEmpty(lastName))
                {
                    if (!string.IsNullOrEmpty(contactsCache[phone].DisplayName))
                    {
                        firstName = contactsCache[phone].DisplayName;
                    }
                    else
                    {
                        continue;
                    }
                }

                if (firstName == "" && lastName == "")
                {
                    continue;
                }


                var contact = new TLInputContact
                {
                    Phone     = new TLString(phone),
                    FirstName = new TLString(firstName),
                    LastName  = new TLString(lastName),
                    ClientId  = new TLLong(contactsCache[phone].GetHashCode())
                };

                importingContacts.Add(contact);
            }

            _isLoading = true;
            var getResponse = false;

            BeginOnThreadPool(() =>
            {
                Thread.Sleep(1500);
                if (!getResponse)
                {
                    IsWorking = true;
                }
            });
            //IsWorking = true;
            MTProtoService.ImportContactsAsync(importingContacts, new TLBool(false),
                                               importedContacts =>
            {
                getResponse = true;
                _isLoading  = true;
                IsWorking   = false;
                Status      = Items.Count == 0 && LazyItems.Count == 0 && importedContacts.Users.Count == 0
                        ? string.Format("{0}", AppResources.NoContactsHere)
                        : string.Empty;
                InsertContacts(importedContacts.Users, fullReplace);
            },
                                               error =>
            {
                getResponse = true;
                _isLoading  = true;
                Status      = string.Empty;
                IsWorking   = false;
            });
        }
        public ShareContactViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            Source    = new List <TLUserBase>();
            Items     = new AlphaKeyGroup <TLUserBase>("@");
            _contacts = new ObservableCollection <AlphaKeyGroup <TLUserBase> > {
                (AlphaKeyGroup <TLUserBase>)Items
            };

            PhoneContact = StateService.PhoneContact;
            StateService.PhoneContact = null;

            if (PhoneContact != null)
            {
            }
            else
            {
                Status = AppResources.Loading;
                BeginOnThreadPool(() =>
                                  CacheService.GetContactsAsync(
                                      contacts =>
                {
                    var currentUser = contacts.FirstOrDefault(x => x.Index == StateService.CurrentUserId);
                    if (currentUser == null)
                    {
                        currentUser = CacheService.GetUser(new TLInt(StateService.CurrentUserId));
                        if (currentUser != null)
                        {
                            contacts.Add(currentUser);
                        }
                    }

                    foreach (var contact in contacts)
                    {
                        Source.Add(contact);
                    }

                    foreach (var contact in contacts)
                    {
                        LazyItems.Add(contact);
                    }

                    BeginOnUIThread(() =>
                    {
                        var importantCount = 0;
                        var count          = 0;
                        for (var i = 0; i < LazyItems.Count && importantCount < FirstSliceLength; i++, count++)
                        {
                            Items.Add(LazyItems[i]);
                            importantCount++;
                        }
                        Status = Items.Count == 0 ? string.Format("{0}", AppResources.NoUsersHere) : string.Empty;

                        BeginOnUIThread(TimeSpan.FromSeconds(0.5), () =>
                        {
                            for (var i = count; i < LazyItems.Count; i++)
                            {
                                Items.Add(LazyItems[i]);
                            }
                            LazyItems.Clear();

                            GetContactsAsync();
                        });
                    });
                }));
            }
        }
        public async void GetContactsAsync()
        {
            var contactStore = await ContactManager.RequestStoreAsync();

            if (contactStore == null)
            {
                IsWorking = false;
                Status    = Items.Count == 0 && LazyItems.Count == 0
                    ? AppResources.NoContactsHere
                    : string.Empty;

                return;
            }

            var contacts = await contactStore.FindContactsAsync();

            var notRegisteredContacts = new List <TLUserBase>();

            foreach (var contact in contacts)
            {
                var notRegisteredUser = ContactsViewModel.GetNotRegisteredUser(contact);
                if (notRegisteredUser != null)
                {
                    notRegisteredContacts.Add(notRegisteredUser);
                }
            }

            foreach (var notRegisteredContact in notRegisteredContacts)
            {
                Source.Add(notRegisteredContact);
            }

            var groups = AlphaKeyGroup <TLUserBase> .CreateGroups(
                notRegisteredContacts,
                Thread.CurrentThread.CurrentUICulture,
                x => x.FullName,
                false);

            var contactKeys = new Dictionary <string, string>();

            foreach (var contact in Contacts)
            {
                contactKeys[contact.Key] = contact.Key;
            }
            BeginOnThreadPool(() =>
            {
                foreach (var group in groups)
                {
                    var gr = new AlphaKeyGroup <TLUserBase>(group.Key);
                    foreach (var u in group.OrderBy(x => x.FullName))
                    {
                        gr.Add(u);
                    }

                    if (!contactKeys.ContainsKey(gr.Key))
                    {
                        BeginOnUIThread(() => Contacts.Add(gr));
                    }
                }

                BeginOnUIThread(() =>
                {
                    Status = Items.Count == 0 && LazyItems.Count == 0
                        ? AppResources.NoContactsHere
                        : string.Empty;
                });
            });
        }
예제 #6
0
        public async void ImportContactsAsync()
        {
            Telegram.Logs.Log.Write("Contacts start search 2");

            _stopwatch = Stopwatch.StartNew();
            var contactStore = await ContactManager.RequestStoreAsync();

            if (contactStore == null)
            {
                Telegram.Logs.Log.Write("ContactsStore is null");
                IsWorking = false;
                Status    = Items.Count == 0 && LazyItems.Count == 0
                    ? AppResources.NoContactsHere
                    : string.Empty;

                return;
            }

            var contacts = await contactStore.FindContactsAsync();

            Telegram.Logs.Log.Write("Contacts search completed count=" + contacts.Count);

            TLUtils.WritePerformance("::Search contacts time: " + _stopwatch.Elapsed);
            _stopwatch = Stopwatch.StartNew();

            var phonesCache           = new Dictionary <string, Contact>();
            var notRegisteredContacts = new List <TLUserBase>();

            foreach (var contact in contacts)
            {
                foreach (var phoneNumber in contact.Phones)
                {
                    phonesCache[phoneNumber.Number] = contact;
                }

                var notRegisteredUser = GetNotRegisteredUser(contact);
                if (notRegisteredUser != null)
                {
                    notRegisteredContacts.Add(notRegisteredUser);
                }
            }

            Telegram.Logs.Log.Write("Contacts skip empty count=" + notRegisteredContacts.Count);

            TLUtils.WritePerformance("::Get not registered phones time: " + _stopwatch.Elapsed);

            _stopwatch = Stopwatch.StartNew();

            var groups = AlphaKeyGroup <TLUserBase> .CreateGroups(
                notRegisteredContacts,
                Thread.CurrentThread.CurrentUICulture,
                x => x.FullName,
                false);

            TLUtils.WritePerformance("::Get groups time: " + _stopwatch.Elapsed);

            var contactKeys = new Dictionary <string, string>();

            foreach (var contact in Contacts)
            {
                contactKeys[contact.Key] = contact.Key;
            }
            BeginOnThreadPool(() =>
            {
                foreach (var @group in groups)
                {
                    var gr = new AlphaKeyGroup <TLUserBase>(@group.Key);
                    foreach (var u in @group.OrderBy(x => x.FullName))
                    {
                        gr.Add(u);
                    }

                    if (!contactKeys.ContainsKey(gr.Key))
                    {
                        BeginOnUIThread(() => Contacts.Add(gr));
                    }
                }
            });

            var importedPhonesCache = GetImportedPhones();

            Telegram.Logs.Log.Write("Contacts load imported count=" + importedPhonesCache.Count);

            var phones            = phonesCache.Keys.Take(Constants.MaxImportingContactsCount).ToList();
            var importingContacts = new TLVector <TLInputContactBase>();
            var importingPhones   = new List <string>();

            foreach (var phone in phones)
            {
                if (importedPhonesCache.ContainsKey(phone))
                {
                    continue;
                }

                var firstLastName = GetFirstLastName(phonesCache[phone]);

                var contact = new TLInputContact
                {
                    Phone     = new TLString(phone),
                    FirstName = new TLString(firstLastName.Item1),
                    LastName  = new TLString(firstLastName.Item2),
                    ClientId  = new TLLong(phonesCache[phone].GetHashCode())
                };

                importingContacts.Add(contact);
                importingPhones.Add(phone);
            }

            Telegram.Logs.Log.Write("Contacts skip imported count=" + importingContacts.Count);

            if (importingContacts.Count > 0)
            {
                System.Diagnostics.Debug.WriteLine("contacts.importContacts id={0}", string.Join(",", importingContacts.Select(x => ((TLInputContact)x).Phone)));

                IsWorking = true;
                MTProtoService.ImportContactsAsync(importingContacts,
                                                   result => Execute.BeginOnUIThread(() =>
                {
                    Telegram.Logs.Log.Write("Contacts contacts.importContacts result=" + result);

                    IsWorking = false;
                    Status    = Items.Count == 0 && LazyItems.Count == 0 && result.Users.Count == 0
                            ? AppResources.NoContactsHere
                            : string.Empty;

                    var retryContactsCount = result.RetryContacts.Count;
                    if (retryContactsCount > 0)
                    {
                        Execute.ShowDebugMessage("contacts.importContacts retry_contacts_count=" + retryContactsCount);
                    }

                    InsertContacts(result.Users);

                    SaveImportedPhones(importedPhonesCache, importingPhones);
                }),
                                                   error => Execute.BeginOnUIThread(() =>
                {
                    Telegram.Logs.Log.Write("Contacts contacts.importContacts error=" + error);

                    IsWorking = false;
                    Status    = string.Empty;

                    Execute.ShowDebugMessage("contacts.importContacts error=" + error);
                }));
            }
            else
            {
                Status = Items.Count == 0 && LazyItems.Count == 0
                    ? AppResources.NoContactsHere
                    : string.Empty;
            }
        }