public void LoadNextSlice()
        {
            if (!string.IsNullOrEmpty(Text))
            {
                return;
            }
            if (!_secondSliceLoaded)
            {
                return;
            }

            var maxSliceCount = int.MaxValue;
            var index         = 0;

            var lastRow = Rows.LastOrDefault();

            if (lastRow != null)
            {
                var lastDialogItem = lastRow.GetLast();
                if (lastDialogItem != null)
                {
                    index = DialogsSource.IndexOf(lastDialogItem);
                }
            }

            var currentRow = lastRow;

            if (currentRow != null && currentRow.HasEmptyItem())
            {
                for (index = index + 1; index < DialogsSource.Count; index++)
                {
                    if (!currentRow.HasEmptyItem())
                    {
                        break;
                    }

                    var d = DialogsSource[index];

                    d.Row = currentRow;

                    currentRow.Add(d, true);
                }
            }
            else
            {
                index++;
            }

            _lastSlice = new List <DialogRow>();
            for (var count = 0; index < DialogsSource.Count && count < maxSliceCount; index++, count++)
            {
                if (currentRow == null || !currentRow.HasEmptyItem())
                {
                    currentRow = new DialogRow();
                    _lastSlice.Add(currentRow);
                }

                var d = DialogsSource[index];
                d.Row = currentRow;

                currentRow.Add(d);
            }

            if (_lastSlice.Count > 0 &&
                Rows.Count < 10)
            {
                for (var i = 0; i < 10 - Rows.Count; i++)
                {
                    var row = _lastSlice.FirstOrDefault();
                    if (row != null)
                    {
                        _lastSlice.RemoveAt(0);
                        Rows.Add(row);
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
        private void UpdateDialogsAsync(TLDialog lastDialog)
        {
            if (_lastSliceLoaded)
            {
                return;
            }

            var             offsetDate = 0;
            var             offsetId   = 0;
            TLInputPeerBase offsetPeer = new TLInputPeerEmpty();

            if (lastDialog != null)
            {
                var lastMessage = lastDialog.TopMessage as TLMessageCommon;
                if (lastMessage != null)
                {
                    offsetDate = lastMessage.DateIndex;
                    offsetId   = lastMessage.Index;
                    if (lastMessage.ToId is TLPeerUser)
                    {
                        offsetPeer = !lastMessage.Out.Value
                            ? DialogDetailsViewModel.PeerToInputPeer(new TLPeerUser {
                            Id = lastMessage.FromId
                        })
                            : DialogDetailsViewModel.PeerToInputPeer(lastMessage.ToId);
                    }
                    else
                    {
                        offsetPeer = DialogDetailsViewModel.PeerToInputPeer(lastMessage.ToId);
                    }
                }
            }

            var stopwatch = Stopwatch.StartNew();

            IoC.Get <IMTProtoService>().GetDialogsAsync(stopwatch,
                                                        new TLInt(offsetDate),
                                                        new TLInt(offsetId),
                                                        offsetPeer,
                                                        new TLInt(int.MaxValue),
                                                        new TLInt(0),
                                                        result => Execute.BeginOnUIThread(() =>
            {
                _lastSliceLoaded = true;

                var dialogs        = result.Dialogs;
                var clearedDialogs = new List <TLDialogBase>();
                foreach (var d in dialogs)
                {
                    if (Skip(d))
                    {
                        continue;
                    }

                    clearedDialogs.Add(d);
                }

                foreach (var clearedDialog in clearedDialogs)
                {
                    var d = new DialogItem {
                        Dialog = clearedDialog
                    };

                    DialogsSource.Add(d);
                }

                LoadNextSlice();
            }),
                                                        error => Execute.BeginOnUIThread(() =>
            {
            }));
        }
        private void LoadDialogs()
        {
            TLDialogBase currentUserDialog = null;

            _dialogs = IoC.Get <ICacheService>().GetDialogs();

            var clearedDialogs = new List <TLDialogBase>();

            foreach (var d in _dialogs)
            {
                if (Skip(d))
                {
                    var user = d.With as TLUser;
                    if (user != null && user.IsSelf)
                    {
                        currentUserDialog = d;
                    }

                    continue;
                }

                clearedDialogs.Add(d);
            }

            if (currentUserDialog != null)
            {
                clearedDialogs.Insert(0, currentUserDialog);
            }
            else
            {
                var currentUser = IoC.Get <ICacheService>().GetUser(new TLInt(IoC.Get <IStateService>().CurrentUserId));

                currentUserDialog = new TLDialog {
                    Peer = new TLPeerUser {
                        Id = currentUser.Id
                    }, With = currentUser
                };
                clearedDialogs.Insert(0, currentUserDialog);
            }

            _dialogsSource = clearedDialogs;

            DialogRow currentRow = null;
            var       rows       = new Group <DialogRow>("group");

            Rows = rows;
            var groups = new ObservableCollection <Group <DialogRow> > {
                rows
            };
            var secondSlice = new List <DialogRow>();

            if (clearedDialogs.Count > 0)
            {
                var maxFirstSliceCount  = 12;
                var maxSecondSliceCount = 24;
                for (var i = 0; i < clearedDialogs.Count; i++)
                {
                    if (i % 4 == 0)
                    {
                        currentRow = new DialogRow();
                        if (i < maxFirstSliceCount)
                        {
                            rows.Add(currentRow);
                        }
                        else if (i < maxSecondSliceCount)
                        {
                            secondSlice.Add(currentRow);
                        }
                    }

                    var d = new DialogItem {
                        Dialog = clearedDialogs[i], Row = currentRow
                    };

                    DialogsSource.Add(d);

                    currentRow.Add(d);
                }
            }

            var lastDialog = _dialogs.LastOrDefault(x => x is TLDialog) as TLDialog;

            UpdateDialogsAsync(lastDialog);

            Execute.BeginOnUIThread(() =>
            {
                SetListVerticalAlignment(Rows.Count);
                Dialogs.ItemsSource = groups;
                Dialogs.Visibility  = Visibility.Visible;
                Dialogs.Opacity     = 0.0;

                Execute.BeginOnUIThread(() =>
                {
                    Dialogs.Opacity        = 1.0;
                    var storyboard         = new Storyboard();
                    var translateAnimaiton = new DoubleAnimationUsingKeyFrames();
                    translateAnimaiton.KeyFrames.Add(new EasingDoubleKeyFrame {
                        KeyTime = TimeSpan.FromSeconds(0.0), Value = Dialogs.ActualHeight
                    });
                    translateAnimaiton.KeyFrames.Add(new EasingDoubleKeyFrame {
                        KeyTime = TimeSpan.FromSeconds(0.4), Value = 0.0, EasingFunction = new ExponentialEase {
                            Exponent = 5.0, EasingMode = EasingMode.EaseOut
                        }
                    });
                    Storyboard.SetTarget(translateAnimaiton, Dialogs);
                    Storyboard.SetTargetProperty(translateAnimaiton, new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
                    storyboard.Children.Add(translateAnimaiton);

                    storyboard.Begin();

                    if (secondSlice.Count > 0)
                    {
                        storyboard.Completed += (o, e) =>
                        {
                            foreach (var item in secondSlice)
                            {
                                Rows.Add(item);
                            }
                            _secondSliceLoaded = true;

                            LoadNextSlice();
                        };
                    }
                    else
                    {
                        _secondSliceLoaded = true;

                        LoadNextSlice();
                    }
                });
            });
        }