private async void MetroWindow_LoadedAsync(object sender, RoutedEventArgs e)
        {
            message.Text = "Подключение к БД...";
            DbService.Init();
            await DbService.ConnectToDbAsync();

            message.Text = "Загрузка настроек...";
            await GlobalService.InitAsync();

            var w = new MainWindow();
            await Task.Delay(1000);

            message.Text = "Запуск программы...";
            w.Show();
            Close();
        }
        public MainViewModel()
        {
            Init = ReactiveCommand.CreateFromTask(async _ =>
            {
                await Interactions.StartLongTimeOperation("Ждите...", "Подключение к БД...");
                DbService.Init();
                DbVersion = (await DbService.ConnectToDbAsync()).ToString();


                //throw new DbCriticalException("ASD");

                await Interactions.StartLongTimeOperation("Ждите...", "Загрузка настроек...");
                await GlobalService.InitAsync();

                DbVersion  = DbService.DbVersion;
                AppVersion = DbService.AppVersion.ToString();
                Updater    = GlobalService.Updater;

                await Interactions.FinishLongTimeOperation();
                await CheckAutoLogin.Execute();
            });
            Init.ThrownExceptions.Subscribe(async ex => await Interactions.RaiseCriticalError(ex.Message));
            CheckAutoLogin = ReactiveCommand.Create <Unit, bool>(_ =>
            {
                if (GlobalService.AppSettings.HasSavedUserPassword == false)
                {
                    return(false);
                }
                else
                {
                    string decryptedSavedPassword;
                    try
                    {
                        decryptedSavedPassword = GlobalService.AppSettings.DecryptLastUserPassword();
                        DbService.SetCurrentUser(GlobalService.AppSettings.LastUserID, decryptedSavedPassword);
                    }
                    catch
                    {
                        return(false);
                    }
                    CurrentUserName = DbService.CurrentUser.Caption;
                    return(true);
                }
            });
            CheckAutoLogin.Subscribe(b =>
            {
                if (b == false)
                {
                    resetAutoLogin();
                    HasSavedUserPassword = false;
                    loadUsers();
                }
            });
            Login = ReactiveCommand.Create <string, bool>(password =>
            {
                try
                {
                    DbService.SetCurrentUser(SelectedUser.ID, password);
                }
                catch
                {
                    WrongPassword = true;
                    return(false);
                }

                GlobalService.AppSettings.LastUserID           = DbService.CurrentUser.ID;
                GlobalService.AppSettings.HasSavedUserPassword = HasSavedUserPassword;
                if (HasSavedUserPassword)
                {
                    GlobalService.AppSettings.EncryptLastUserPassword(password);
                }
                GlobalService.SaveSettings();

                CurrentUserName = DbService.CurrentUser.Caption;
                WrongPassword   = false;
                return(true);
            }, this.WhenAnyValue(p => p.SelectedUser).Select(p => p != null));
            Login.ThrownExceptions.Subscribe(async ex => await Interactions.ShowError(ex.Message));
            FastLogout = ReactiveCommand.Create(() =>
            {
                resetAutoLogin();
                DbService.ResetCurrentUser();
                CurrentUserName = "******";
                Apps.AppRepository.Reset();
                loadUsers();
            });
            Logout = ReactiveCommand.CreateFromTask <Unit, bool>(async _ =>
            {
                var confirm = await Interactions.ShowConfirmationAsync("Выход", "Сменить пользователя?", "Сменить пользователя", "Нет");
                if (confirm == InteractionResult.Yes)
                {
                    //resetAutoLogin();
                    //DbService.ResetCurrentUser();
                    //CurrentUserName = "******";
                    //Apps.AppRepository.Reset();
                    //loadUsers();
                    return(true);
                }
                else
                {
                    return(false);
                }
            });
            Logout.Where(p => p == true).Select(p => Unit.Default).InvokeCommand(FastLogout);

            Interactions.UserLogout.RegisterHandler(interaction =>
            {
                FastLogout.Execute().Subscribe();
                interaction.SetOutput(InteractionResult.OK);
            });
        }