Inheritance: DocumentDbModelBase
 public void ConnectToAccount(AccountCredentialsModel credentials)
 {
     var model = new AccountModel(credentials);
     Config.Instance.AddAccount(credentials);
     var accountVM = new AccountExpItemVM(model, _main);
     this.RootItems.Add(accountVM);
 }
        public void ConnectToSavedAccount(SavedAccountModel savedAccount)
        {
            var model = new AccountModel(savedAccount.Credentials);
            var accountVM = new AccountExpItemVM(model, _main);
            this.RootItems.Add(accountVM);
            if(savedAccount.IsExpanded)
            {
                accountVM.Expand().ContinueWith<Task>(async (t) =>
                {
                    if (savedAccount.Databases != null)
                    {
                        foreach (var db in savedAccount.Databases)
                        {
                            var found = accountVM.Children.FirstOrDefault(item => item is DatabaseExpItemVM && (item as DatabaseExpItemVM).Name == db.Name);
                            if (found != null)
                            {
                                if (db.IsExpanded)
                                {
                                    await found.Expand();
                                    foreach(var savedColl in db.Collections)
                                    {
                                        var foundColl = found.Children.FirstOrDefault(item => item is CollectionExpItemVM && (item as CollectionExpItemVM).Caption == savedColl.Name);
                                        if(foundColl !=null)
                                        {
                                            if(savedColl.IsExpanded)
                                                await foundColl.Expand();
                                        }
                                        else
                                        {
                                            Config.Instance.RemoveCollection(db.Name, savedAccount.Credentials.Endpoint, savedColl.Name);
                                        }
                                    }
                                    //set selected

                                    if (Config.Instance.SelectedAccount == savedAccount.Credentials.Endpoint && Config.Instance.SelectedDatabase == db.Name && Config.Instance.SelectedCollection.IsNotEmpty())
                                    {
                                        var selectedCollection = found.Children.FirstOrDefault(item => item is CollectionExpItemVM && (item as CollectionExpItemVM).Caption == Config.Instance.SelectedCollection) as CollectionExpItemVM;
                                        if (selectedCollection != null)
                                        {
                                            if (Config.Instance.SelectedView.IsEmpty())
                                            {
                                                selectedCollection.View = Config.Instance.SelectedCollectionViewModel;
                                                //nope, we don't set selected, views are to save nice views, opening the app doesn't restore the last views.
                                                //await selectedCollection.Select();
                                            }
                                            else
                                            {
                                                var selectedView = selectedCollection.Children.FirstOrDefault(item => item is CollectionExpItemVM && (item as CollectionExpItemVM).Caption == Config.Instance.SelectedView) as CollectionExpItemVM;
                                                selectedView.View = Config.Instance.SelectedCollectionViewModel;
                                                //nope, we don't set selected, views are to save nice views, opening the app doesn't restore the last views.
                                                //await selectedView.Select();
                                            }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                Config.Instance.RemoveDatabase(db.Name, savedAccount.Credentials.Endpoint);
                            }
                        }
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext());
            }
        }
 public AccountExpItemVM(AccountModel model, MainVM main) : base(main)
 {
     IsBroken = false;
     _model = model;
     this.Children.Add(new DummyExpItemVM(main));
     this.ContextMenuItems.Add(new MenuItemVM()
     {
         Caption = "Refresh",
         Command = new LambdaCommand(async (o) =>
         {
             await this.LoadDatabases();
         })
     });
     this.ContextMenuItems.Add(new MenuItemVM()
     {
         Caption = "Create database",
         Command = new LambdaCommand(async (o) =>
         {
             if(IsBroken)
             {
                 MessageBox.Show("Can't create database on a broken connect, check error message, try to reenter credentials.");
                 return;
             }
             var dlg = new NameSelector("New database name");
             if (dlg.ShowDialog() == true && dlg.Value.IsNotEmpty())
             {
                 var created = await  _model.CreateDatabase(dlg.Value);
                 this.Children.Insert(0,new DatabaseExpItemVM(this, created, this.Main));
             }
         })
     });
     this.ContextMenuItems.Add(new MenuItemVM()
     {
         Caption = "Change access data",
         Command = new LambdaCommand((o) =>
         {
             var oldEndpoint = _model.Endpoint;
             DocDbAccountCredentialsDlg.ShowDialog(_model.Credentials, async (creds) =>
             {
                 await performWithIsBrokenTest(async () =>
                 {
                     this.Children.Clear();
                     await _model.RefreshCredentials();
                     foreach (var db in this._model.Databases)
                     {
                         this.Children.Add(new DatabaseExpItemVM(this, db, this.Main));
                     }
                     Config.Instance.ChangeAccountCredentials(oldEndpoint, _model.Credentials);
                 });
             });
         })
     });
     this.ContextMenuItems.Add(new MenuItemVM()
     {
         Caption = "Remove",
         Command = new LambdaCommand((o) =>
         {
             MessageBoxResult messageBoxResult = System.Windows.MessageBox.Show("Are you sure?", "Removal Confirmation", System.Windows.MessageBoxButton.YesNo);
             if (messageBoxResult == MessageBoxResult.Yes)
             {
                 main.Explorer.RootItems.Remove(this);
                 Config.Instance.RemoveAccount(_model.Endpoint);
             }
         })
     });
 }
 public DatabaseModel(DocumentClient client, AccountModel account, Database db) : base(client)
 {
     _db = db;
     Account = account;
 }