protected void HandleAccountLoading(DataLoader dataLoader)
        {
            if (dataLoader.ProfileManager.UserProfiles.Count > 0)
            {
                IsLoading = true;
            }

            dataLoader.WhenAccountLoading.Subscribe((account) =>
            {
                if (account == null)
                {
                    return;
                }
                _log.DebugFormat("Account {0} is loading", account.AccountId);

                AccountViewModel accountVm;
                AccountsById.TryGetValue(account.AccountId, out accountVm);

                if (accountVm == null)
                {
                    accountVm = new AccountViewModel(account, dataLoader);
                    AccountsById.Add(account.AccountId, accountVm);
                    Accounts.Add(accountVm);
                }
                else
                {
                    accountVm.Update(account);
                }
            });

            dataLoader.WhenAccountLoaded.Subscribe((account) =>
            {
                _log.DebugFormat("Account {0} is loaded", account.AccountId);

                var loadingAccounts = Accounts.Where((ac) => ac.IsLoading).ToArray();
                if (!loadingAccounts.Any() || (loadingAccounts.Count() == 1 && loadingAccounts.FirstOrDefault().Id == account.AccountId))
                {
                    IsLoading = false;
                }
            });

            dataLoader.WhenAccountLoadingError.Subscribe((error) =>
            {
                foreach (var account in error.ProviderDescriptor.Accounts)
                {
                    AccountViewModel accountVm;
                    AccountsById.TryGetValue(account.AccountId, out accountVm);
                    if (accountVm != null)
                    {
                        _log.ErrorFormat("Error on loading account {0}", accountVm.Id);
                        accountVm.HandleLoadingError(error.Message);
                    }
                }
            });

            dataLoader.WhenAccountsLoadingCompleted.Subscribe((loadedAccounts) =>
            {
                var first = loadedAccounts.FirstOrDefault();
                if (first == null)
                {
                    return;
                }

                // Elimenate accounts that exist but are not being loaded
                foreach (var accountKey in AccountsById.Keys)
                {
                    var storedAccount = AccountsById[accountKey];
                    if (storedAccount.UserId != first.UserId ||
                        ProvidersFactory.GetVendorIdByName(storedAccount.CompanyName) != first.VendorId)
                    {
                        continue;
                    }

                    if (loadedAccounts.All(a => a.AccountId != storedAccount.Id))
                    {
                        storedAccount.HandleLoadingError("Cannot access");
                    }
                }
            });
        }
Exemple #2
0
 internal AccounEventArgs(AccountViewModel account)
 {
     Account = account;
 }