public void Add(DialogItem dialog, bool notify = false)
 {
     if (Dialog1 == null)
     {
         Dialog1 = dialog;
         if (notify)
         {
             NotifyOfPropertyChange(() => Dialog1);
         }
     }
     else if (Dialog2 == null)
     {
         Dialog2 = dialog;
         if (notify)
         {
             NotifyOfPropertyChange(() => Dialog2);
         }
     }
     else if (Dialog3 == null)
     {
         Dialog3 = dialog;
         if (notify)
         {
             NotifyOfPropertyChange(() => Dialog3);
         }
     }
     else if (Dialog4 == null)
     {
         Dialog4 = dialog;
         if (notify)
         {
             NotifyOfPropertyChange(() => Dialog4);
         }
     }
 }
        public IEnumerable <DialogItem> Between(DialogItem item1, DialogItem item2)
        {
            if (item1 == Dialog1)
            {
                if (item2 == Dialog3)
                {
                    yield return(Dialog2);
                }
                else if (item2 == Dialog4)
                {
                    yield return(Dialog2);

                    yield return(Dialog3);
                }
            }
            else if (item1 == Dialog2)
            {
                if (item2 == Dialog4)
                {
                    yield return(Dialog3);
                }
            }
            else if (item1 == Dialog3)
            {
                if (item2 == Dialog1)
                {
                    yield return(Dialog2);
                }
            }
            else if (item1 == Dialog4)
            {
                if (item2 == Dialog2)
                {
                    yield return(Dialog3);
                }
                else if (item2 == Dialog1)
                {
                    yield return(Dialog2);

                    yield return(Dialog3);
                }
            }
        }
        private void ChangeDialogSelection(DialogItem dialogItem, bool isSelected)
        {
            if (isSelected)
            {
                _selectedPictures[dialogItem.Dialog.Index] = dialogItem;
            }
            else
            {
                _selectedPictures.Remove(dialogItem.Dialog.Index);
            }

            dialogItem.SuppressAnimation = false;
            dialogItem.IsSelected        = isSelected;
            dialogItem.RaisePropertyChanged("IsSelected");

            if (_hasManipulatingDelta &&
                _previousDialog != null &&
                _previousDialog.Row == dialogItem.Row)
            {
                var row     = dialogItem.Row;
                var between = row.Between(_previousDialog, dialogItem).ToList();
                if (between.Count > 0)
                {
                    if (isSelected)
                    {
                        foreach (var item in between)
                        {
                            _selectedPictures[item.Dialog.Index] = item;
                        }
                    }
                    else
                    {
                        foreach (var item in between)
                        {
                            _selectedPictures.Remove(item.Dialog.Index);
                        }
                    }

                    foreach (var item in between)
                    {
                        item.SuppressAnimation = false;
                        item.IsSelected        = isSelected;
                        item.RaisePropertyChanged("IsSelected");
                    }
                }
            }

            _previousDialog = dialogItem;

            _selectButton.IsEnabled = _selectedPictures.Any();

            if (_selectedPictures.Any())
            {
                BeginOpenCommentGridStoryboard();
            }
            else
            {
                BeginCloseCommentGridStoryboard();
            }
            //if (IsSingleItem)
            //{
            //    ChooseButton_OnClick(null, null);
            //}
        }
        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();
                    }
                });
            });
        }