Exemplo n.º 1
0
        private void InitializeViewModel()
        {
            // init model
            _viewModel = new LoginWindowViewModel()
            {
                ChangeViewCommand = new Command(() =>
                {
                    // show login/registration form
                    _viewModel.FlipViewIndex = _viewModel.FlipViewIndex == 0 ? 1 : 0;

                    // cleaup passwords
                    txtPassword1.Password       = null;
                    txtPassword2.Password       = null;
                    txtConfirmPassword.Password = null;
                }),
                LoginCommand = new Command(() =>
                {
                    var user = _viewModel.FlipViewIndex == 0 ? Login() : Register();
                    if (user == null)
                    {
                        return;
                    }

                    // save global variables
                    GlobalVariables.UserId        = user.Id;
                    GlobalVariables.LastUsageDate = user.LastUsageDate.Date;

                    // load general settings
                    var settings = _settingsService.GetForUser(user.Id);
                    // apply theme from settings
                    MCThemeManager.Instance.SetTheme(settings.AccentColor, settings.ThemeColor, true);

                    // save settings
                    AppSettings.Instance.LastLogin    = user.Name;
                    AppSettings.Instance.LastLanguage = user.Language;
                    AppSettings.Instance.Save();

                    // update user last usage
                    // TODO: update last usage date on application closing
                    user.LastUsageDate = DateTime.Today;
                    _userService.Update(user);

                    // show main window
                    var mainWindow = new MainWindow();
                    mainWindow.Show();
                    this.Close();
                },
                                           () => !string.IsNullOrEmpty(_viewModel.Name) && !string.IsNullOrEmpty(_viewModel.Password) &&
                                           (_viewModel.FlipViewIndex == 0 || _viewModel.Password == _viewModel.ConfirmPassword)),

                CancelCommand = new Command(() => this.Close())
            };

            // add command validation on property changed
            _viewModel.PropertyChanged += (sender, e) => _viewModel.LoginCommand.ValidateCanExecute();
            // set datacontext
            this.DataContext = _viewModel;
        }
Exemplo n.º 2
0
        public override void Reload()
        {
            base.Reload();

            if (_viewModel.Settings == null)
            {
                _viewModel.Settings = _service.GetForUser(GlobalVariables.UserId);

                _viewModel.Settings.PropertyChanged += (sender, e) =>
                {
                    // set new language
                    if (e.PropertyName == nameof(GeneralSettingModel.Language))
                    {
                        MultiLangResourceManager.Instance.SetLanguage(_viewModel.Settings.Language);
                        AppSettings.Instance.LastLanguage = _viewModel.Settings.Language;
                        AppSettings.Instance.Save();
                    }

                    // set new accent color
                    if (e.PropertyName == nameof(GeneralSettingModel.AccentColor))
                    {
                        MCThemeManager.Instance.SetAccentColor(_viewModel.Settings.AccentColor);
                        AppSettings.Instance.LastAccentColor = _viewModel.Settings.AccentColor;
                        AppSettings.Instance.Save();
                    }

                    // set new theme color
                    if (e.PropertyName == nameof(GeneralSettingModel.ThemeColor))
                    {
                        MCThemeManager.Instance.SetThemeColor(_viewModel.Settings.ThemeColor);
                        AppSettings.Instance.LastThemeColor = _viewModel.Settings.ThemeColor;
                        AppSettings.Instance.Save();
                    }

                    // save changes
                    _service.Update(_viewModel.Settings);
                };
            }
        }