Ejemplo n.º 1
0
        private void RefreshMessages()
        {
            lock (ReceiverLock)
            {
                if (_lastMessageId == 0)
                {
                    if (AppController.Settings.LastMessageId == 0)
                    {
                        return;
                    }

                    _lastMessageId = AppController.Settings.LastMessageId;
                    AppController.Settings.LastMessageId = 0;
                }

                if (_cts2 != null && !_cts2.IsCancellationRequested)
                {
                    _cts2.Cancel();
                }

                _cts2 = new CancellationTokenSource();

                var            cts         = _cts2;
                Poco.Message[] newMessages = null;
                AppController.RefreshMessages(
                    _cts2,
                    _lastMessageId,
                    _email,
                    (data) =>
                {
                    if ((cts?.IsCancellationRequested).GetValueOrDefault(true))
                    {
                        return;
                    }

                    lock (ReceiverLock)
                    {
                        newMessages = data?.Messages?.ToArray();
                        if (newMessages?.Length > 0)
                        {
                            var lm         = newMessages.Last();
                            _lastMessageId = lm.MessageId;
                        }
                    }
                },
                    (error) =>
                {
                    // Do Nothing
                },
                    () =>
                {
                    if ((cts?.IsCancellationRequested).GetValueOrDefault(true))
                    {
                        return;
                    }

                    lock (ReceiverLock)
                    {
                        bool playSound = false;
                        if (newMessages != null)
                        {
                            foreach (var m in newMessages)
                            {
                                if (!_source.HasMessage(m.MessageId))
                                {
                                    playSound = true;

                                    // Add message to the message list
                                    Message message = new Message
                                    {
                                        MessageId = m.MessageId,
                                        Sender    = m.Sender,
                                        Content   = m.Content,
                                        SendDate  = m.SendDate.GetValueOrDefault()
                                    };

                                    _source.AddItem(message);
                                }
                            }
                        }

                        this.MessageList.ReloadData();
                        this.MessageList.ScrollToRow(
                            NSIndexPath.FromItemSection((nint)(_source.Count - 1), 0),
                            UITableViewScrollPosition.Bottom,
                            false);

                        if (playSound)
                        {
                            PlaySound();
                        }
                    }
                });
            }
        }
Ejemplo n.º 2
0
        private void RefreshMessages()
        {
            lock (ReceiverLock)
            {
                if (_lastMessageId == 0)
                {
                    if (AppController.Settings.LastMessageId == 0)
                    {
                        return;
                    }

                    _lastMessageId = AppController.Settings.LastMessageId;
                    AppController.Settings.LastMessageId = 0;
                }

                if (_cts2 != null && !_cts2.IsCancellationRequested)
                {
                    _cts2.Cancel();
                }

                _cts2 = new CancellationTokenSource();

                var            cts         = _cts2;
                Poco.Message[] newMessages = null;
                AppController.RefreshMessages(
                    _cts2,
                    _lastMessageId,
                    _email,
                    (data) =>
                {
                    if (cts.IsCancellationRequested)
                    {
                        return;
                    }

                    lock (ReceiverLock)
                    {
                        newMessages    = data.Messages?.ToArray();
                        _lastMessageId = (newMessages?.Last().MessageId).GetValueOrDefault(0);
                    }
                },
                    (error) =>
                {
                    // Do Nothing
                },
                    () =>
                {
                    if (cts.IsCancellationRequested)
                    {
                        return;
                    }

                    lock (ReceiverLock)
                    {
                        bool playSound = false;
                        if (newMessages != null)
                        {
                            foreach (var m in newMessages)
                            {
                                if (!_adapter.HasMessage(m.MessageId))
                                {
                                    playSound = true;

                                    // Add message to the message list
                                    Message message = new Message
                                    {
                                        MessageId = m.MessageId,
                                        Sender    = m.Sender,
                                        Content   = m.Content,
                                        SendDate  = m.SendDate.GetValueOrDefault()
                                    };

                                    _adapter.AddItem(message);
                                }
                            }
                        }

                        this.MessageList.ReloadData();
                        this.MessageList.SmoothScrollToPosition(_adapter.ItemCount);

                        if (playSound)
                        {
                            PlaySound();
                        }
                    }
                });
            }
        }