Example #1
0
        private void UpdateSessionsAsync()
        {
            BeginOnThreadPool(() =>
            {
                IsWorking = true;
                MTProtoService.GetAuthorizationsAsync(
                    accountAuthorizations => Execute.BeginOnUIThread(() =>
                {
                    Status    = string.Empty;
                    IsWorking = false;

                    var firstRun = Current == null;
                    if (firstRun)
                    {
                        Items.Clear();
                        var firstChunkSize = 4;
                        var count          = 0;
                        var delayedItems   = new List <TLAccountAuthorization>();
                        for (var i = 0; i < accountAuthorizations.Authorizations.Count; i++)
                        {
                            var authorization = accountAuthorizations.Authorizations[i];
                            _authorizationsCache[authorization.Hash.Value] = authorization;
                            if (authorization.IsCurrent)
                            {
                                Current = authorization;
                                NotifyOfPropertyChange(() => Current);
                            }
                            else
                            {
                                if (count < firstChunkSize)
                                {
                                    Items.Add(authorization);
                                    count++;
                                }
                                else
                                {
                                    delayedItems.Add(authorization);
                                }
                            }
                        }

                        BeginOnUIThread(TimeSpan.FromSeconds(0.5), () =>
                        {
                            foreach (var authorization in delayedItems)
                            {
                                Items.Add(authorization);
                            }
                        });
                    }
                    else
                    {
                        var newAuthorizationsCache = new Dictionary <long, TLAccountAuthorization>();
                        var itemsToAdd             = new List <TLAccountAuthorization>();
                        for (var i = 0; i < accountAuthorizations.Authorizations.Count; i++)
                        {
                            var authorization = accountAuthorizations.Authorizations[i];
                            TLAccountAuthorization cachedAuthorization;
                            if (!_authorizationsCache.TryGetValue(authorization.Hash.Value, out cachedAuthorization))
                            {
                                itemsToAdd.Add(authorization);
                                newAuthorizationsCache[authorization.Hash.Value] = authorization;
                            }
                            else
                            {
                                cachedAuthorization.Update(authorization);
                                cachedAuthorization.NotifyOfPropertyChange(() => cachedAuthorization.AppFullName);
                                cachedAuthorization.NotifyOfPropertyChange(() => cachedAuthorization.DateActive);
                                cachedAuthorization.NotifyOfPropertyChange(() => cachedAuthorization.DeviceFullName);
                                cachedAuthorization.NotifyOfPropertyChange(() => cachedAuthorization.ApiId);
                                cachedAuthorization.NotifyOfPropertyChange(() => cachedAuthorization.IsOfficialApp);

                                newAuthorizationsCache[authorization.Hash.Value] = cachedAuthorization;
                            }
                        }

                        for (var i = 0; i < Items.Count; i++)
                        {
                            if (!newAuthorizationsCache.ContainsKey(Items[i].Hash.Value))
                            {
                                Items.RemoveAt(i--);
                            }
                        }

                        for (var i = 0; i < itemsToAdd.Count; i++)
                        {
                            Items.Insert(0, itemsToAdd[i]);
                        }

                        _authorizationsCache = newAuthorizationsCache;
                    }
                }),
                    error =>
                {
                    Status    = string.Empty;
                    IsWorking = false;
                    Execute.ShowDebugMessage("account.getAuthorizations error " + error);
                });
            });
        }