Exemplo n.º 1
0
        protected override void OnInitialize()
        {
            Status = AppResources.Loading;
            MTProtoService.GetBlockedAsync(new TLInt(0), new TLInt(int.MaxValue),
                                           result =>
            {
                var contacts = result as TLContactsBlocked;
                if (contacts != null)
                {
                    foreach (var user in contacts.Users)
                    {
                        var cachedUser = CacheService.GetUser(new TLInt(user.Index));
                        if (cachedUser != null)
                        {
                            LazyItems.Add(cachedUser);
                        }
                        else
                        {
                            LazyItems.Add(user);
                        }
                    }

                    Status = Items.Count > 0 || LazyItems.Count > 0? string.Empty : string.Format("{0}", AppResources.NoUsersHere);
                    BeginOnUIThread(PopulateItems);
                }
            },
                                           error => BeginOnUIThread(() => Status = string.Empty));

            base.OnInitialize();
        }
Exemplo n.º 2
0
        public ChooseParticipantsViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            Items  = new ObservableCollection <TLUserBase>();
            Status = AppResources.Loading;
            BeginOnThreadPool(() =>
            {
                _source = _source ??
                          CacheService.GetContacts()
                          .Where(x => !(x is TLUserEmpty) && x.Index != StateService.CurrentUserId)
                          .OrderBy(x => x.FullName)
                          .ToList();

                Status = string.Empty;
                foreach (var contact in _source)
                {
                    LazyItems.Add(contact);
                }

                if (Items.Count == 0 && LazyItems.Count == 0)
                {
                    Status = AppResources.NoUsersHere;
                }

                BeginOnUIThread(() => PopulateItems(() => { _isFullResults = true; }));
            });
        }
        public ChooseCountryViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator) 
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            const string Groups = "abcdefghijklmnopqrstuvwxyz";
            Items = new ObservableCollection<CountriesInGroup>();

            var countries = new List<CountriesInGroup>(Groups.Length);
            var groups = new Dictionary<string, CountriesInGroup>();

            foreach (var c in Groups)
            {
                var group = new CountriesInGroup(c.ToString(CultureInfo.InvariantCulture));
                countries.Add(group);
                groups[c.ToString(CultureInfo.InvariantCulture)] = group;
            }

            foreach (var country in CountryUtils.CountriesSource)
            {
                groups[country.GetKey()].Add(country);
            }

            var count = 0;

            for (var i = 0; i < count; i++)
            {
                Items.Add(countries[i]);
            }

            for (var i = count; i < countries.Count; i++)
            {
                LazyItems.Add(countries[i]);
            }

            BeginOnUIThread(PopulateItems);
        }
Exemplo n.º 4
0
        public void ForwardInAnimationComplete()
        {
            Items.Clear();
            LazyItems.Clear();

            var chat = _currentChat;

            if (chat != null)
            {
                var participants = chat.Participants as TLChatParticipants40;
                if (participants != null)
                {
                    var users = new List <TLUserBase>(participants.Participants.Count);
                    foreach (var participant in participants.Participants)
                    {
                        var user = CacheService.GetUser(participant.UserId);
                        if (user != null)
                        {
                            var canDeleteUserFromChat = false;

                            var inviter = participant as IInviter;
                            if (inviter != null &&
                                inviter.InviterId.Value == StateService.CurrentUserId)
                            {
                                canDeleteUserFromChat = true;
                            }

                            var creator = participant as TLChatParticipantCreator;
                            if (creator != null &&
                                creator.UserId.Value == StateService.CurrentUserId)
                            {
                                canDeleteUserFromChat = true;
                            }

                            if (participant.UserId.Value == StateService.CurrentUserId)
                            {
                                canDeleteUserFromChat = true;
                            }

                            user.DeleteActionVisibility = canDeleteUserFromChat
                                ? Visibility.Visible
                                : Visibility.Collapsed;
                            user.IsSelected = IsEnabled;
                            users.Add(user);
                        }
                    }
                    users = users.OrderByDescending(x => x.StatusValue).ToList();

                    UpdateUsers(users, UpdateItems);

                    return;
                }
                else
                {
                    UpdateItems();
                }
            }
        }
        public CreateChannelViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            GroupedUsers = new ObservableCollection <TLUserBase>();
            //GroupedUsers = new ObservableCollection<AlphaKeyGroup<TLUserBase>>();

            BeginOnThreadPool(() =>
            {
                //Thread.Sleep(300);
                _source = _source ??
                          CacheService.GetContacts()
                          .Where(x => !(x is TLUserEmpty) && x.Index != StateService.CurrentUserId)
                          .OrderBy(x => x.FullName)
                          .ToList();

                Status = string.Empty;
                foreach (var contact in _source)
                {
                    contact._isSelected = false;
                    LazyItems.Add(contact);
                }

                if (_source.Count == 0)
                {
                    Status = AppResources.NoUsersHere;
                }

                BeginOnUIThread(PopulateItems);
                Thread.Sleep(500);
                BeginOnUIThread(() =>
                {
                    foreach (var item in _source)
                    {
                        GroupedUsers.Add(item);
                    }
                });
                //var groups = AlphaKeyGroup<TLUserBase>.CreateGroups(
                //    _source,
                //    Thread.CurrentThread.CurrentUICulture,
                //    x => x.FullName,
                //    false);

                //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(() =>
                //    {
                //        GroupedUsers.Add(gr);
                //    });
                //}
            });
        }
        public ChooseCountryViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            const string Groups = "abcdefghijklmnopqrstuvwxyz";

            Items = new ObservableCollection <CountriesInGroup>();

            SearchItems     = new ObservableCollection <Country>();
            LazySearchItems = new List <Country>();

            var countries = new List <CountriesInGroup>(Groups.Length);
            var groups    = new Dictionary <string, CountriesInGroup>();

            foreach (var c in Groups)
            {
                var group = new CountriesInGroup(c.ToString(CultureInfo.InvariantCulture));
                countries.Add(group);
                groups[c.ToString(CultureInfo.InvariantCulture)] = group;
            }

            foreach (var country in CountryUtils.CountriesSource)
            {
                groups[country.GetKey()].Add(country);
            }

            var count = 1;

            for (var i = 0; i < count; i++)
            {
                Items.Add(countries[i]);
            }

            for (var i = count; i < countries.Count; i++)
            {
                LazyItems.Add(countries[i]);
            }

            if (StateService.HideCountryCode)
            {
                StateService.HideCountryCode = false;
                HideCountryCode = true;
            }

            _residenceCountry             = StateService.ResidenceCountry;
            StateService.ResidenceCountry = false;

            PropertyChanged += (sender, args) =>
            {
                if (Property.NameEquals(args.PropertyName, () => IsSearching))
                {
                    if (!IsSearching)
                    {
                        Text = string.Empty;
                    }
                }
            };
        }
        public void ForwardInAnimationComplete()
        {
            foreach (var lazyItem in LazyItems)
            {
                Items.Add(lazyItem);
            }

            LazyItems.Clear();
        }
Exemplo n.º 8
0
 public void Handle(string command)
 {
     if (string.Equals(command, Commands.LogOutCommand))
     {
         LazyItems.Clear();
         BeginOnUIThread(() => Items.Clear());
         Status    = string.Empty;
         IsWorking = false;
     }
 }
Exemplo n.º 9
0
 public void Handle(string command)
 {
     if (string.Equals(command, Commands.LogOutCommand))
     {
         _runOnce = true;
         LazyItems.Clear();
         Items.Clear();
         Status    = string.Empty;
         IsWorking = false;
     }
 }
 public void Handle(DialogRemovedEventArgs args)
 {
     if (With == args.Dialog.With)
     {
         BeginOnUIThread(() =>
         {
             LazyItems.Clear();
             Items.Clear();
             IsEmptyDialog = true;
         });
     }
 }
Exemplo n.º 11
0
 public void Handle(string command)
 {
     if (string.Equals(command, Commands.LogOutCommand))
     {
         _runOnce = true;
         LazyItems.Clear();
         Items.Clear();
         Status    = string.Empty;
         IsWorking = false;
         FileUtils.Delete(_importedPhonesRoot, Constants.ImportedPhonesFileName);
     }
 }
Exemplo n.º 12
0
        private void LoadCache()
        {
            var isAuthorized = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);

            if (!isAuthorized)
            {
                return;
            }

            Status = string.Empty;

            var contacts        = CacheService.GetContacts();
            var orderedContacts = contacts.OrderBy(x => x.FullName).ToList();
            var count           = 0;

            Items.Clear();
            LazyItems.Clear();
            for (var i = 0; i < orderedContacts.Count; i++)
            {
                if (!(orderedContacts[i] is TLUserEmpty) &&
                    orderedContacts[i].Index != StateService.CurrentUserId)
                {
                    if (count < 10)
                    {
                        Items.Add(orderedContacts[i]);
                    }
                    else
                    {
                        LazyItems.Add(orderedContacts[i]);
                    }
                    count++;
                }
            }

            Status = Items.Count == 0 && LazyItems.Count == 0 ? AppResources.Loading : string.Empty;

            if (LazyItems.Count > 0)
            {
                PopulateItems(() =>
                {
                    ImportContactsAsync();
                    GetContactsAsync();
                    EventAggregator.Subscribe(this);
                });
            }
            else
            {
                ImportContactsAsync();
                GetContactsAsync();
                EventAggregator.Subscribe(this);
            }
        }
Exemplo n.º 13
0
        protected override void OnInitialize()
        {
            BeginOnThreadPool(() =>
            {
                var isAuthorized = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);
                if (!FirstRun)
                {
                    return;
                }
                if (!isAuthorized)
                {
                    return;
                }

                FirstRun = false;

                Status = Items.Count == 0 && LazyItems.Count == 0? AppResources.Loading : string.Empty;
                MTProtoService.GetDialogsAsync(new TLInt(0), new TLInt(0), new TLInt(Constants.DialogsSlice),
                                               dialogs =>
                {
                    foreach (var dialog in dialogs.Dialogs)
                    {
                        LazyItems.Add(dialog);
                    }

                    Status = Items.Count == 0 && LazyItems.Count == 0? string.Format("{0}", AppResources.NoDialogsHere) : string.Empty;
                    if (LazyItems.Count > 0)
                    {
                        BeginOnUIThread(() => PopulateItems(() =>
                        {
                            EventAggregator.Publish(new InvokeImportContacts());
                        }));
                    }
                    else
                    {
                        EventAggregator.Publish(new InvokeImportContacts());
                    }
                },
                                               error =>
                {
                    EventAggregator.Publish(new InvokeImportContacts());
                    Telegram.Api.Helpers.Execute.ShowDebugMessage("messages.getHistory error " + error);
                    Status = string.Empty;
                });
            });



            base.OnInitialize();
        }
        public AddSecretChatParticipantViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            BeginOnThreadPool(() =>
            {
                var isAuthorized = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);
                if (isAuthorized)
                {
                    var contacts = CacheService.GetContacts()
                                   .Where(x => !(x is TLUserEmpty) && x.Index != StateService.CurrentUserId)
                                   .OrderBy(x => x.FullName)
                                   .ToList();

                    Status = string.Empty;

                    var count = 0;
                    const int firstSliceCount = 10;
                    var secondSlice           = new List <TLUserBase>();
                    foreach (var contact in contacts)
                    {
                        if (count < firstSliceCount)
                        {
                            LazyItems.Add(contact);
                        }
                        else
                        {
                            secondSlice.Add(contact);
                        }
                        count++;
                    }

                    BeginOnUIThread(() => PopulateItems(() =>
                    {
                        foreach (var item in secondSlice)
                        {
                            Items.Add(item);
                        }
                    }));
                }

                GetDHConfig();
            });
        }
Exemplo n.º 15
0
        public CreateChannelStep3ViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            GroupedUsers = new ObservableCollection <TLUserBase>();

            _newChannel             = StateService.NewChannel;
            StateService.NewChannel = null;

            BeginOnThreadPool(() =>
            {
                //Thread.Sleep(300);
                _source = _source ??
                          CacheService.GetContacts()
                          .Where(x => !(x is TLUserEmpty) && x.Index != StateService.CurrentUserId)
                          .OrderBy(x => x.FullName)
                          .ToList();

                Status = string.Empty;
                foreach (var contact in _source)
                {
                    contact._isSelected = false;
                    LazyItems.Add(contact);
                }

                if (_source.Count == 0)
                {
                    Status = AppResources.NoUsersHere;
                }

                BeginOnUIThread(PopulateItems);
                Thread.Sleep(500);
                BeginOnUIThread(() =>
                {
                    foreach (var item in _source)
                    {
                        GroupedUsers.Add(item);
                    }
                });
            });
        }
Exemplo n.º 16
0
        public ChooseDialogViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            EventAggregator.Subscribe(this);

            LogFileName = StateService.LogFileName;
            StateService.LogFileName = null;

            ForwardedMessages            = StateService.ForwardMessages;
            StateService.ForwardMessages = null;

            SharedContact = StateService.SharedContact;
            StateService.SharedContact = null;

            _accessToken             = StateService.AccessToken;
            StateService.AccessToken = null;

            _bot             = StateService.Bot;
            StateService.Bot = null;

            _webLink             = StateService.WebLink;
            StateService.WebLink = null;

            _url             = StateService.Url;
            StateService.Url = null;

            _text = StateService.UrlText;
            StateService.UrlText = null;

            Status = AppResources.Loading;

            BeginOnThreadPool(() =>
            {
                var isAuthorized = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);
                if (isAuthorized)
                {
                    var dialogs = CacheService.GetDialogs();

                    var dialogsCache   = new Dictionary <int, TLDialogBase>();
                    var clearedDialogs = new List <TLDialogBase>();
                    foreach (var dialog in dialogs)
                    {
                        if (!dialogsCache.ContainsKey(dialog.Index))
                        {
                            if (dialog is TLDialog || dialog is TLBroadcastDialog)
                            {
                                if (!SkipDialog(_bot, dialog))
                                {
                                    clearedDialogs.Add(dialog);
                                }
                                dialogsCache[dialog.Index] = dialog;
                            }
                        }
                        else
                        {
                            var cachedDialog = dialogsCache[dialog.Index];
                            if (cachedDialog.Peer is TLPeerUser && dialog.Peer is TLPeerUser)
                            {
                                CacheService.DeleteDialog(dialog);
                                continue;
                            }
                            if (cachedDialog.Peer is TLPeerChat && dialog.Peer is TLPeerChat)
                            {
                                CacheService.DeleteDialog(dialog);
                                continue;
                            }
                        }
                    }

                    BeginOnUIThread(() =>
                    {
                        foreach (var clearedDialog in clearedDialogs)
                        {
                            LazyItems.Add(clearedDialog);
                        }

                        var lastDialog = clearedDialogs.LastOrDefault(x => x.TopMessageId != null);
                        _maxId         = lastDialog != null ? lastDialog.TopMessageId.Value : 0;

                        Status = LazyItems.Count == 0 ? AppResources.Loading : string.Empty;

                        for (var i = 0; i < LazyItems.Count && i < FirstSliceLength; i++)
                        {
                            Items.Add(LazyItems[i]);
                        }

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

                            LoadNextSlice();
                        });
                    });
                }
            });
        }
Exemplo n.º 17
0
        protected override void OnInitialize()
        {
            BeginOnThreadPool(() =>
            {
                var isAuthorized = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);
                if (!FirstRun)
                {
                    return;
                }
                if (!isAuthorized)
                {
                    return;
                }

                FirstRun = false;

                Status    = Items.Count == 0 && LazyItems.Count == 0 ? AppResources.Loading : string.Empty;
                var limit = Constants.DialogsSlice;
                MTProtoService.GetDialogsAsync(
#if LAYER_40
                    new TLInt(0), new TLInt(limit),
#else
                    new TLInt(0), new TLInt(0), new TLInt(limit),
#endif
                    dialogs => Execute.BeginOnUIThread(() =>
                {
                    IsLastSliceLoaded = dialogs.Dialogs.Count < limit;
                    _offset           = Constants.DialogsSlice;

                    _isUpdated = true;
                    const int firstSliceCount = 8;
                    var count = 0;
                    for (var i = 0; i < dialogs.Dialogs.Count; i++)
                    {
                        if (count < firstSliceCount)
                        {
                            Items.Add(dialogs.Dialogs[i]);
                            count++;
                        }
                        else
                        {
                            LazyItems.Add(dialogs.Dialogs[i]);
                        }
                    }

                    Status = Items.Count == 0 && LazyItems.Count == 0 ? string.Format("{0}", AppResources.NoDialogsHere) : string.Empty;

                    if (LazyItems.Count > 0)
                    {
                        BeginOnUIThread(() =>
                        {
                            for (var i = 0; i < LazyItems.Count; i++)
                            {
                                Items.Add(LazyItems[i]);
                            }
                            LazyItems.Clear();

                            InvokeImportContacts();
                            UpdateChannels();
                        });
                    }
                    else
                    {
                        InvokeImportContacts();
                        UpdateChannels();
                    }
                }),
                    error => BeginOnUIThread(() =>
                {
                    InvokeImportContacts();
                    UpdateChannels();

                    Execute.ShowDebugMessage("messages.getDialogs error " + error);
                    _isUpdated = true;
                    Status     = string.Empty;
                }));
            });

            base.OnInitialize();
        }
Exemplo n.º 18
0
        public void Search()
        {
            if (_lastUsersRequest != null)
            {
                _lastUsersRequest.Cancel();
            }

            var text = Text.Trim();

            if (string.IsNullOrEmpty(text))
            {
                LazyItems.Clear();
                Items.Clear();
                Status = string.IsNullOrEmpty(Text)? string.Empty : AppResources.NoResults;
                return;
            }

            SearchUsersRequest nextUsersRequest;

            if (!_searchResultsCache.TryGetValue(text, out nextUsersRequest))
            {
                IList <TLUserBase> source;

                if (_lastUsersRequest != null &&
                    text.IndexOf(_lastUsersRequest.Text, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    source = _lastUsersRequest.Source;
                }
                else
                {
                    _source = _source ??
                              CacheService.GetContacts()
                              .Where(x => !(x is TLUserEmpty) && x.Index != StateService.CurrentUserId)
                              .OrderBy(x => x.FullName)
                              .ToList();

                    source = _source;
                }

                nextUsersRequest = new SearchUsersRequest(text, source);
            }

            IsWorking = true;
            nextUsersRequest.ProcessAsync(results =>
                                          Execute.BeginOnUIThread(() =>
            {
                if (nextUsersRequest.IsCanceled)
                {
                    return;
                }

                Status = string.Empty;
                Items.Clear();
                LazyItems.Clear();
                if (results.Count > 0)
                {
                    Items.Add(new TLServiceText {
                        Text = AppResources.Contacts
                    });
                }
                for (var i = 0; i < results.Count; i++)
                {
                    if (i < 6)
                    {
                        Items.Add(results[i]);
                    }
                    else
                    {
                        LazyItems.Add(results[i]);
                    }
                }

                IsWorking = false;
                //Status = Items.Count == 0 ? AppResources.NoResults : string.Empty;

                //return;

                if (LazyItems.Count > 0)
                {
                    PopulateItems(() => ProcessGlobalSearch(nextUsersRequest));
                }
                else
                {
                    ProcessGlobalSearch(nextUsersRequest);
                }
            }));
            _searchResultsCache[nextUsersRequest.Text] = nextUsersRequest;
            _lastUsersRequest = nextUsersRequest;
        }
Exemplo n.º 19
0
        public ChooseDialogViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            FilteredItems = new CollectionViewSource {
                Source = Items
            }.View;
            FilteredItems.Filter += item =>
            {
                var dialog = item as TLDialog;
                if (dialog != null)
                {
                    var chat = dialog.With as TLChat41;
                    if (chat != null)
                    {
                        return(!chat.IsMigrated);
                    }

                    var user = dialog.With as TLUser;
                    if (user != null)
                    {
                        return(!user.IsSelf);
                    }
                }

                var dialog71 = dialog as TLDialog71;
                if (dialog71 != null)
                {
                    return(!dialog71.IsPromo);
                }

                return(true);
            };

            EventAggregator.Subscribe(this);

            LogFileName = StateService.LogFileName;
            StateService.LogFileName = null;

            ForwardedMessages            = StateService.ForwardMessages;
            StateService.ForwardMessages = null;

            SharedContact = StateService.SharedContact;
            StateService.SharedContact = null;

            _gameString             = StateService.GameString;
            StateService.GameString = null;

            _accessToken             = StateService.AccessToken;
            StateService.AccessToken = null;

            _bot             = StateService.Bot;
            StateService.Bot = null;

            _webLink             = StateService.WebLink;
            StateService.WebLink = null;

            _storageItems             = StateService.StorageItems;
            StateService.StorageItems = null;

            _url             = StateService.Url;
            StateService.Url = null;

            _text = StateService.UrlText;
            StateService.UrlText = null;

            _switchInlineButton             = StateService.SwitchInlineButton;
            StateService.SwitchInlineButton = null;

            Status = AppResources.Loading;

            BeginOnThreadPool(() =>
            {
                var isAuthorized = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);
                if (isAuthorized)
                {
                    var dialogs = CacheService.GetDialogs();

                    var dialogsCache   = new Dictionary <int, TLDialogBase>();
                    var clearedDialogs = new List <TLDialogBase>();
                    foreach (var dialog in dialogs)
                    {
                        if (!dialogsCache.ContainsKey(dialog.Index))
                        {
                            var user = dialog.With as TLUser;
                            if (user != null && user.IsSelf)
                            {
                                CurrentUser = dialog;
                            }

                            if (dialog is TLDialog || dialog is TLBroadcastDialog)
                            {
                                if (!SkipDialogForBot(_bot, dialog))
                                {
                                    clearedDialogs.Add(dialog);
                                }
                                dialogsCache[dialog.Index] = dialog;
                            }
                        }
                        else
                        {
                            var cachedDialog = dialogsCache[dialog.Index];
                            if (cachedDialog.Peer is TLPeerUser && dialog.Peer is TLPeerUser)
                            {
                                CacheService.DeleteDialog(dialog);
                                continue;
                            }
                            if (cachedDialog.Peer is TLPeerChat && dialog.Peer is TLPeerChat)
                            {
                                CacheService.DeleteDialog(dialog);
                                continue;
                            }
                        }
                    }

                    if (CurrentUser == null)
                    {
                        var currentUser = CacheService.GetUser(new TLInt(StateService.CurrentUserId));
                        if (currentUser != null)
                        {
                            var dialog = new TLDialog71
                            {
                                With  = currentUser,
                                Flags = new TLInt(0),
                                Peer  = new TLPeerUser {
                                    Id = currentUser.Id
                                },
                                Messages            = new ObservableCollection <TLMessageBase>(),
                                TopMessageId        = new TLInt(0),
                                ReadInboxMaxId      = new TLInt(0),
                                ReadOutboxMaxId     = new TLInt(0),
                                UnreadCount         = new TLInt(0),
                                UnreadMentionsCount = new TLInt(0),
                                NotifySettings      = new TLPeerNotifySettings78 {
                                    Flags = new TLInt(0), MuteUntil = new TLInt(0), Sound = new TLString("Default")
                                }
                            };
                            CurrentUser = dialog;
                        }
                    }

                    BeginOnUIThread(() =>
                    {
                        NotifyOfPropertyChange(() => CurrentUser);

                        foreach (var clearedDialog in clearedDialogs)
                        {
                            LazyItems.Add(clearedDialog);
                        }

                        var lastDialog = clearedDialogs.LastOrDefault(x => x.TopMessageId != null);
                        _maxId         = lastDialog != null ? lastDialog.TopMessageId.Value : 0;

                        Status             = LazyItems.Count == 0 ? AppResources.Loading : string.Empty;
                        var importantCount = 0;
                        var count          = 0;
                        for (var i = 0; i < LazyItems.Count && importantCount < FirstSliceLength; i++, count++)
                        {
                            Items.Add(LazyItems[i]);
                            var chat41 = LazyItems[i].With as TLChat41;
                            if (chat41 == null || chat41.MigratedTo == null)
                            {
                                importantCount++;
                            }
                        }

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

                            LoadNextSlice();
                        });
                    });
                }
            });
        }
        protected override void OnInitialize()
        {
            if (Channel == null)
            {
                return;
            }

            //Status = AppResources.Loading;
            IsWorking = true;
            MTProtoService.GetParticipantsAsync(Channel.ToInputChannel(), new TLChannelParticipantsKicked68 {
                Q = TLString.Empty
            }, new TLInt(0), new TLInt(100), new TLInt(0),
                                                result =>
            {
                var channelParticipants = result as TLChannelParticipants;
                if (channelParticipants != null)
                {
                    var kickedParticipants = new Dictionary <int, int>();
                    foreach (var participant in channelParticipants.Participants)
                    {
                        var kickedParticipant = participant as TLChannelParticipantBanned;
                        if (kickedParticipant != null)
                        {
                            kickedParticipants[kickedParticipant.UserId.Value] = kickedParticipant.UserId.Value;
                        }
                    }

                    var contacts = channelParticipants;
                    foreach (var user in contacts.Users)
                    {
                        if (kickedParticipants.ContainsKey(user.Index))
                        {
                            var cachedUser = CacheService.GetUser(new TLInt(user.Index));
                            if (cachedUser != null)
                            {
                                LazyItems.Add(cachedUser);
                            }
                            else
                            {
                                LazyItems.Add(user);
                            }
                        }
                    }

                    BeginOnUIThread(() =>
                    {
                        IsWorking   = false;
                        IsEmptyList = Items.Count == 0 && LazyItems.Count == 0;
                        Status      = string.Empty;
                        PopulateItems();
                    });
                }
            },
                                                error => BeginOnUIThread(() =>
            {
                IsWorking = false;
                Status    = string.Empty;
            }));

            base.OnInitialize();
        }
Exemplo n.º 21
0
        public void Search()
        {
            if (_lastDocumentsRequest != null)
            {
                _lastDocumentsRequest.Cancel();
            }

            var text = Text.Trim();

            if (string.IsNullOrEmpty(text))
            {
                LazyItems.Clear();
                Items.Clear();
                Status = string.IsNullOrEmpty(Text) ? AppResources.SearchAmongYourFiles : AppResources.NoResults;
                return;
            }

            SearchDocumentsRequest nextDocumentsRequest;

            if (!_searchResultsCache.TryGetValue(text, out nextDocumentsRequest))
            {
                IList <TLMessageBase> source;

                if (_lastDocumentsRequest != null &&
                    text.IndexOf(_lastDocumentsRequest.Text, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    source = _lastDocumentsRequest.Source;
                }
                else
                {
                    source = _source;
                }

                nextDocumentsRequest = new SearchDocumentsRequest(CurrentItem.ToInputPeer(), text, source);
            }

            IsWorking = true;
            nextDocumentsRequest.ProcessAsync(results =>
                                              Telegram.Api.Helpers.Execute.BeginOnUIThread(() =>
            {
                if (nextDocumentsRequest.IsCanceled)
                {
                    return;
                }

                Status = results.Count > 0 ? string.Empty : Status;
                Items.Clear();
                LazyItems.Clear();
                for (var i = 0; i < results.Count; i++)
                {
                    if (i < 6)
                    {
                        Items.Add((TLMessage)results[i]);
                    }
                    else
                    {
                        LazyItems.Add((TLMessage)results[i]);
                    }
                }

                IsWorking = false;
                NotifyOfPropertyChange(() => IsEmptyList);

                if (LazyItems.Count > 0)
                {
                    PopulateItems(() => ProcessGlobalSearch(nextDocumentsRequest));
                }
                else
                {
                    ProcessGlobalSearch(nextDocumentsRequest);
                }
            }));

            _searchResultsCache[nextDocumentsRequest.Text] = nextDocumentsRequest;
            _lastDocumentsRequest = nextDocumentsRequest;
        }
Exemplo n.º 22
0
        public void Search()
        {
            if (_lastUsersRequest != null)
            {
                _lastUsersRequest.Cancel();
            }

            var text = Text.Trim();

            if (string.IsNullOrEmpty(text))
            {
                System.Diagnostics.Debug.WriteLine("EmptyText text={0} canceled={1} items_count={2} current_text={3}", text, _lastUsersRequest != null ? _lastUsersRequest.IsCanceled.ToString() : "null", LazyItems.Count, Text);

                LazyItems.Clear();
                Items.Clear();
                Status = string.Empty;
                return;
            }

            SearchUsersRequest nextUsersRequest;

            if (!_searchResultsCache.TryGetValue(text, out nextUsersRequest))
            {
                IList <TLUserBase> source;

                if (_lastUsersRequest != null &&
                    text.IndexOf(_lastUsersRequest.Text, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    source = _lastUsersRequest.Source;
                }
                else
                {
                    source = Source;
                }

                nextUsersRequest = new SearchUsersRequest(text, source);
            }

            IsWorking = true;
            nextUsersRequest.ProcessAsync(results =>
                                          Execute.BeginOnUIThread(() =>
            {
                if (nextUsersRequest.IsCanceled)
                {
                    return;
                }

                Items.Clear();
                LazyItems.Clear();
                for (var i = 0; i < results.Count; i++)
                {
                    if (i < 6)
                    {
                        Items.Add(results[i]);
                    }
                    else
                    {
                        LazyItems.Add(results[i]);
                    }
                }
                Status = Items.Count > 0 ? string.Empty : AppResources.NoResults;

                IsWorking = false;
                if (LazyItems.Count > 0)
                {
                    Execute.BeginOnUIThread(() =>
                    {
                        if (nextUsersRequest.IsCanceled)
                        {
                            return;
                        }

                        System.Diagnostics.Debug.WriteLine("ContinueResults text={0} canceled={1} items_count={2} current_text={3}", nextUsersRequest.Text, nextUsersRequest.IsCanceled, LazyItems.Count, Text);

                        foreach (var item in LazyItems)
                        {
                            Items.Add(item);
                        }
                        LazyItems.Clear();
                        Status = Items.Count > 0 ? string.Empty : AppResources.NoResults;
                    });
                }
            }));
            _searchResultsCache[nextUsersRequest.Text] = nextUsersRequest;
            _lastUsersRequest = nextUsersRequest;
        }
Exemplo n.º 23
0
        public void Search()
        {
            if (_lastUsersRequest != null)
            {
                _lastUsersRequest.Cancel();
            }

            var text = Text.Trim();

            if (string.IsNullOrEmpty(text))
            {
                LazyItems.Clear();
                Items.Clear();
                foreach (var item in _source)
                {
                    Items.Add(item);
                }
                //Status = string.IsNullOrEmpty(Text) ? AppResources.SearchAmongYourContacts : AppResources.NoResults;
                return;
            }

            SearchUsersRequest nextUsersRequest;

            if (!_searchResultsCache.TryGetValue(text, out nextUsersRequest))
            {
                IList <TLUserBase> source;

                if (_lastUsersRequest != null &&
                    text.IndexOf(_lastUsersRequest.Text, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    source = _lastUsersRequest.Source;
                }
                else
                {
                    _source = _source ??
                              Items
                              .OrderBy(x => x.FullName)
                              .ToList();

                    source = _source;
                }

                nextUsersRequest = new SearchUsersRequest(text, source);
            }

            IsWorking = true;
            nextUsersRequest.ProcessAsync(results =>
                                          Telegram.Api.Helpers.Execute.BeginOnUIThread(() =>
            {
                if (nextUsersRequest.IsCanceled)
                {
                    return;
                }

                //Status = string.Empty;
                Items.Clear();
                LazyItems.Clear();
                if (results.Count > 0)
                {
                    //Items.Add(new TLServiceText { Text = AppResources.Contacts });
                }
                for (var i = 0; i < results.Count; i++)
                {
                    if (i < 6)
                    {
                        Items.Add(results[i]);
                    }
                    else
                    {
                        LazyItems.Add(results[i]);
                    }
                }

                IsWorking = false;
                //Status = Items.Count == 0 ? AppResources.NoResults : string.Empty;

                //return;

                if (LazyItems.Count > 0)
                {
                    PopulateItems();
                }
            }));
            _searchResultsCache[nextUsersRequest.Text] = nextUsersRequest;
            _lastUsersRequest = nextUsersRequest;
        }
Exemplo n.º 24
0
        protected override void OnActivate()
        {
            base.OnActivate();

            //if (FirstRun)
            //{
            //    SignInImport();
            //}
            //else
            {
                if (!_runOnce)
                {
                    BeginOnThreadPool(() =>
                    {
                        Thread.Sleep(300);
                        try
                        {
                            foreach (var item in Items)
                            {
                                item.NotifyOfPropertyChange("Status");
                            }
                        }
                        catch (Exception e)
                        {
                            TLUtils.WriteLine(e.ToString(), LogSeverity.Error);
                        }
                    });

                    return;
                }
                _runOnce = false;
                //BeginOnThreadPool(() =>
                {
#if WP7
                    Thread.Sleep(400);
#endif
                    var isAuthorized = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);
                    if (isAuthorized)
                    {
                        Status = Items.Count == 0 && LazyItems.Count == 0 ? AppResources.Loading : string.Empty;
                        var contacts = CacheService.GetContacts();
                        Status = string.Empty;
                        var orderedContacts = contacts.OrderBy(x => x.FullName).ToList();
                        var count           = 0;

                        Items.Clear();
                        for (var i = 0; count < 10 && i < orderedContacts.Count; i++)
                        {
                            if (!(orderedContacts[i] is TLUserEmpty) &&
                                orderedContacts[i].Index != StateService.CurrentUserId)
                            {
                                Items.Add(orderedContacts[i]);
                            }
                            count++;
                        }

                        LazyItems.Clear();
                        for (var i = count; i < orderedContacts.Count; i++)
                        {
                            if (!(orderedContacts[i] is TLUserEmpty) && orderedContacts[i].Index != StateService.CurrentUserId)
                            {
                                LazyItems.Add(orderedContacts[i]);
                            }
                        }


                        Status = Items.Count == 0 && LazyItems.Count == 0 ? AppResources.Loading : string.Empty;
                        if (LazyItems.Count > 0)
                        {
                            PopulateItems(() =>
                            {
                                ImportContactsAsync();
                                UpdateItemsAsync();
                            });
                        }
                        else
                        {
                            ImportContactsAsync();
                            UpdateItemsAsync();
                        }
                    }
                }//);
            }
        }
Exemplo n.º 25
0
        public override void Search(string text)
        {
            _lastText = text;
            Status    = string.Empty;
            Items.Clear();

            if (string.IsNullOrEmpty(text))
            {
                Status = AppResources.NoResults;
                return;
            }

            var messages = CacheService.GetMessages().OfType <TLMessage>();

            var cache = new Dictionary <int, TLMessage>();

            LazyItems.Clear();
            foreach (var message in messages)
            {
                if (message.Message.Value.IndexOf(text, StringComparison.OrdinalIgnoreCase) != -1)
                {
                    var dialog = CacheService.GetDialog(message);
                    LazyItems.Add(new TLDialog24 {
                        With = dialog.With, TopMessage = message
                    });
                    cache[message.Index] = message;
                }
            }

            IsWorking          = true;
            _offset            = 0;
            _isLastSliceLoaded = false;
            MTProtoService.SearchAsync(
                new TLInputPeerEmpty(),
                new TLString(text),
                null,
                new TLInputMessagesFilterEmpty(),
                new TLInt(0),
                new TLInt(0),
                new TLInt(_offset),
                new TLInt(0),
                new TLInt(Limit),
                new TLInt(0),
                result =>
            {
                CacheService.AddChats(result.Chats, results => { });
                CacheService.AddUsers(result.Users, results => { });

                _offset  += Limit;
                IsWorking = false;

                var newMessages = result as TLMessages;
                if (newMessages != null)
                {
                    foreach (var message in newMessages.Messages.OfType <TLMessageCommon>())
                    {
                        if (cache.ContainsKey(message.Index))
                        {
                            continue;
                        }

                        var dialog = new TLDialog {
                            TopMessage = message
                        };
                        var peer = TLUtils.GetPeerFromMessage(message);
                        if (peer is TLPeerUser)
                        {
                            var user = newMessages.Users.FirstOrDefault(x => x.Index == peer.Id.Value);
                            if (user == null)
                            {
                                continue;
                            }
                            dialog.With = user;
                        }
                        else if (peer is TLPeerChat)
                        {
                            var chat = newMessages.Chats.FirstOrDefault(x => x.Index == peer.Id.Value);
                            if (chat == null)
                            {
                                continue;
                            }

                            dialog.With = chat;
                        }
                        else if (peer is TLPeerChannel)
                        {
                            var channel = newMessages.Chats.FirstOrDefault(x => x.Index == peer.Id.Value);
                            if (channel == null)
                            {
                                continue;
                            }

                            dialog.With = channel;
                        }
                        LazyItems.Add(dialog);
                    }
                }

                _lastCache = cache;
                if (Items.Count == 0 && LazyItems.Count == 0)
                {
                    Status = AppResources.NoResults;
                }

                Deployment.Current.Dispatcher.BeginInvoke(PopulateItems);
            },
                error =>
            {
                IsWorking = false;
            });

            Deployment.Current.Dispatcher.BeginInvoke(PopulateItems);
        }
Exemplo n.º 26
0
        private void LoadCalls()
        {
            var timespan = Items.Count > 0 ? 1.0 : 0.0;

            BeginOnThreadPool(TimeSpan.FromSeconds(timespan), () =>
            {
                var isAuthorized = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);
                //if (!FirstRun)
                //{
                //    return;
                //}
                if (!isAuthorized)
                {
                    return;
                }

                FirstRun = false;

                Status    = Items.Count == 0 && LazyItems.Count == 0 ? AppResources.Loading : string.Empty;
                var limit = 50;//Constants.DialogsSlice;
                MTProtoService.SearchAsync(
                    new TLInputPeerEmpty(),
                    TLString.Empty,
                    null,
                    new TLInputMessagesFilterPhoneCalls {
                    Flags = new TLInt(0)
                },
                    new TLInt(0),
                    new TLInt(0),
                    new TLInt(0),
                    new TLInt(0),
                    new TLInt(limit),
                    new TLInt(0),
                    result =>
                {
                    CacheService.AddChats(result.Chats, results => { });
                    CacheService.AddUsers(result.Users, results => { });

                    var items       = new List <TLDialogBase>();
                    var newMessages = result as TLMessages;
                    if (newMessages != null)
                    {
                        var usersCache = new Dictionary <int, TLUserBase>();
                        foreach (var user in newMessages.Users)
                        {
                            usersCache[user.Index] = user;
                        }

                        var chatsCache = new Dictionary <int, TLChatBase>();
                        foreach (var chat in newMessages.Chats)
                        {
                            chatsCache[chat.Index] = chat;
                        }

                        foreach (var message in newMessages.Messages.OfType <TLMessageCommon>())
                        {
                            var dialog = new TLDialog {
                                TopMessage = message
                            };
                            var peer = TLUtils.GetPeerFromMessage(message);
                            if (peer is TLPeerUser)
                            {
                                TLUserBase user;
                                if (!usersCache.TryGetValue(peer.Id.Value, out user))
                                {
                                    continue;
                                }
                                dialog.With = user;
                                items.Add(dialog);
                            }
                        }
                    }

                    Telegram.Api.Helpers.Execute.BeginOnUIThread(() =>
                    {
                        IsLastSliceLoaded = result.Messages.Count < limit;
                        _offset           = Constants.DialogsSlice;

                        _isUpdated = true;

                        const int maxMessagesSlice = 8;
                        Items.Clear();
                        for (var i = 0; i < items.Count; i++)
                        {
                            if (i < maxMessagesSlice)
                            {
                                Items.Add(items[i]);
                            }
                            else
                            {
                                LazyItems.Add(items[i]);
                            }
                        }

                        Status = Items.Count == 0 && LazyItems.Count == 0 ? AppResources.NoCallsHere : string.Empty;

                        if (LazyItems.Count > 0)
                        {
                            BeginOnUIThread(() =>
                            {
                                for (var i = 0; i < LazyItems.Count; i++)
                                {
                                    Items.Add(LazyItems[i]);
                                }
                                LazyItems.Clear();
                            });
                        }
                    });
                },
                    error => Execute.BeginOnUIThread(() =>
                {
                    Telegram.Api.Helpers.Execute.ShowDebugMessage("messages.getDialogs error " + error);
                    //_isUpdated = true;
                    Status = string.Empty;
                }));
            });

            base.OnInitialize();
        }
Exemplo n.º 27
0
        public void Search(string text)
        {
            if (_lastSearchRequest != null)
            {
                _lastSearchRequest.Cancel();
            }

            var trimmedText = text.Trim();

            if (string.IsNullOrEmpty(trimmedText))
            {
                if (_isFullResults)
                {
                    return;
                }

                LazyItems.Clear();
                Items.Clear();

                //foreach (var contact in _source)
                //{
                //    Items.Add(contact);
                //}

                _isFullResults = true;

                return;
            }

            var nextSearchRequest = CreateSearchRequest(text);

            _isFullResults = false;
            IsWorking      = true;
            nextSearchRequest.ProcessAsync(results =>
                                           Execute.BeginOnUIThread(() =>
            {
                if (nextSearchRequest.IsCanceled)
                {
                    return;
                }

                Items.Clear();
                LazyItems.Clear();

                for (var i = 0; i < results.Count; i++)
                {
                    if (i < 6)
                    {
                        Items.Add(results[i]);
                    }
                    else
                    {
                        LazyItems.Add(results[i]);
                    }
                }

                IsWorking = false;
                Status    = Items.Count == 0 ? AppResources.NoResults : string.Empty;

                PopulateItems();
            }));

            _searchResultsCache[nextSearchRequest.Text] = nextSearchRequest;
            _lastSearchRequest = nextSearchRequest;
        }
Exemplo n.º 28
0
        public DialogsViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, IEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            Items = new ObservableCollection <TLDialogBase>();
            EventAggregator.Subscribe(this);

            DisplayName = (string)new LowercaseConverter().Convert(AppResources.Dialogs, null, null, null);


            Status = Items.Count == 0 && LazyItems.Count == 0 ? AppResources.Loading : string.Empty;
            //CacheService.DialogAdded += OnDialogAdded;
            //CacheService.TopMessageUpdated += OnTopMessageUpdated;

            BeginOnThreadPool(() =>
            {
                var isAuthorized = SettingsHelper.GetValue <bool>(Constants.IsAuthorizedKey);
                if (isAuthorized)
                {
                    var dialogs = CacheService.GetDialogs();

                    var dialogsCache   = new Dictionary <int, TLDialogBase>();
                    var clearedDialogs = new List <TLDialogBase>();
                    foreach (var dialog in dialogs)
                    {
                        if (!dialogsCache.ContainsKey(dialog.Index))
                        {
                            clearedDialogs.Add(dialog);
                            dialogsCache[dialog.Index] = dialog;
                        }
                        else
                        {
                            var cachedDialog = dialogsCache[dialog.Index];
                            if (cachedDialog.Peer is TLPeerUser && dialog.Peer is TLPeerUser)
                            {
                                CacheService.DeleteDialog(dialog);
                                continue;
                            }
                            if (cachedDialog.Peer is TLPeerChat && dialog.Peer is TLPeerChat)
                            {
                                CacheService.DeleteDialog(dialog);
                                continue;
                            }
                        }
                    }


                    // load cache
                    Status = dialogs.Count == 0? AppResources.Loading : string.Empty;

                    //BeginOnUIThread(() =>
                    //{
                    //    Items.Clear();
                    //    foreach (var d in clearedDialogs)
                    //    {
                    //        Items.Add(d);
                    //    }
                    //});
                    LazyItems.Clear();
                    //((BindableCollection<TLDialog>)Items).AddRange(dialogs);
                    //BeginOnUIThread(() =>
                    {
                        foreach (var dialog in clearedDialogs)
                        {
                            //Items.Add(dialog);
                            LazyItems.Add(dialog);
                        }
                    }
                    //);

                    if (LazyItems.Count == 0)
                    {
                        UpdateItemsAsync(0, 0, Telegram.Api.Constants.CachedDialogsCount);
                    }
                    else
                    {
                        BeginOnUIThread(() => PopulateItems(() => UpdateItemsAsync(0, 0, Telegram.Api.Constants.CachedDialogsCount)));
                    }
                    // update history
                }
            });
        }
        public ChooseParticipantsViewModel(ICacheService cacheService, ICommonErrorHandler errorHandler, IStateService stateService, INavigationService navigationService, IMTProtoService mtProtoService, ITelegramEventAggregator eventAggregator)
            : base(cacheService, errorHandler, stateService, navigationService, mtProtoService, eventAggregator)
        {
            Items  = new ObservableCollection <TLUserBase>();
            Status = AppResources.Loading;
            BeginOnThreadPool(() =>
            {
                if (_source == null)
                {
                    var source   = new List <TLUserBase>();
                    var contacts = CacheService.GetContacts().OrderBy(x => x.FullName);
                    foreach (var contact in contacts)
                    {
                        if (contact is TLUserEmpty)
                        {
                            continue;
                        }
                        var user = contact as TLUser;
                        if (user != null && user.IsSelf)
                        {
                            CurrentUser = user;
                            //continue;
                        }

                        source.Add(contact);
                    }

                    _source = source;
                }

                Status = string.Empty;
                NotifyOfPropertyChange(() => CurrentUser);

                var count = 0;
                const int firstSliceCount = 10;
                var secondSlice           = new List <TLUserBase>();
                foreach (var contact in _source)
                {
                    if (count < firstSliceCount)
                    {
                        LazyItems.Add(contact);
                    }
                    else
                    {
                        secondSlice.Add(contact);
                    }
                    count++;
                }

                if (Items.Count == 0 && LazyItems.Count == 0)
                {
                    Status = AppResources.NoUsersHere;
                }

                BeginOnUIThread(() => PopulateItems(() =>
                {
                    _isFullResults = true;
                    foreach (var item in secondSlice)
                    {
                        Items.Add(item);
                    }
                }));
            });
        }
Exemplo n.º 30
0
        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();
                        });
                    });
                }));
            }
        }