|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using IssueVision.Data.Web;
using IssueVision.Common;
namespace IssueVision.ViewModel
{
[Export(ViewModelTypes.MyProfileViewModel, typeof(ViewModelBase))]
[PartCreationPolicy(CreationPolicy.NonShared)]
public class MyProfileViewModel : ViewModelBase
{
#region "Private Data Members"
private IIssueVisionModel _issueVisionModel;
#endregion "Private Data Members"
#region "Constructor"
[ImportingConstructor]
public MyProfileViewModel(IIssueVisionModel issueVisionModel)
{
_issueVisionModel = issueVisionModel;
// set up event handling
_issueVisionModel.GetCurrentUserComplete += new EventHandler<EntityResultsArgs<User>>(_issueVisionModel_GetCurrentUserComplete);
_issueVisionModel.GetSecurityQuestionsComplete += new EventHandler<EntityResultsArgs<SecurityQuestion>>(_issueVisionModel_GetSecurityQuestionsComplete);
_issueVisionModel.SaveChangesComplete += new EventHandler<SubmitOperationEventArgs>(_issueVisionModel_SaveChangesComplete);
_issueVisionModel.PropertyChanged += new PropertyChangedEventHandler(_issueVisionModel_PropertyChanged);
// load current user
_issueVisionModel.GetCurrentUserAsync();
// load security questions
_issueVisionModel.GetSecurityQuestionsAsync();
}
#endregion "Constructor"
#region "ICleanup interface implementation"
public override void Cleanup()
{
if (_issueVisionModel != null)
{
// unregister all event handling
_issueVisionModel.GetCurrentUserComplete -= new EventHandler<EntityResultsArgs<User>>(_issueVisionModel_GetCurrentUserComplete);
_issueVisionModel.GetSecurityQuestionsComplete -= new EventHandler<EntityResultsArgs<SecurityQuestion>>(_issueVisionModel_GetSecurityQuestionsComplete);
_issueVisionModel.SaveChangesComplete -= new EventHandler<SubmitOperationEventArgs>(_issueVisionModel_SaveChangesComplete);
_issueVisionModel.PropertyChanged -= new PropertyChangedEventHandler(_issueVisionModel_PropertyChanged);
_issueVisionModel = null;
}
// set properties back to null
CurrentUser = null;
SecurityQuestionEntries = null;
// unregister any messages for this ViewModel
base.Cleanup();
}
#endregion "ICleanup interface implementation"
#region "Public Properties"
private User _currentUser;
public User CurrentUser
{
get { return _currentUser; }
private set
{
if (!ReferenceEquals(_currentUser, value))
{
_currentUser = value;
this.RaisePropertyChanged("CurrentUser");
}
}
}
private IEnumerable<SecurityQuestion> _securityQuestionEntries;
public IEnumerable<SecurityQuestion> SecurityQuestionEntries
{
get { return _securityQuestionEntries; }
private set
{
if (!ReferenceEquals(_securityQuestionEntries, value))
{
_securityQuestionEntries = value;
this.RaisePropertyChanged("SecurityQuestionEntries");
}
}
}
#endregion "Public Properties"
#region "Public Commands"
private RelayCommand _submitChangeCommand = null;
public RelayCommand SubmitChangeCommand
{
get
{
if (_submitChangeCommand == null)
{
_submitChangeCommand = new RelayCommand(
() => this.OnSubmitChangeCommand(),
() => (this._issueVisionModel != null) && (this._issueVisionModel.HasChanges));
}
return _submitChangeCommand;
}
}
private void OnSubmitChangeCommand()
{
try
{
if (!_issueVisionModel.IsBusy)
{
if (this.CurrentUser != null)
{
// this should trigger validation even if the following field is not changed and is null
if (string.IsNullOrWhiteSpace(this.CurrentUser.Password))
this.CurrentUser.Password = string.Empty;
if (string.IsNullOrWhiteSpace(this.CurrentUser.NewPassword))
this.CurrentUser.NewPassword = string.Empty;
if (string.IsNullOrWhiteSpace(this.CurrentUser.NewPasswordConfirmation))
this.CurrentUser.NewPasswordConfirmation = string.Empty;
if (string.IsNullOrWhiteSpace(this.CurrentUser.PasswordAnswer))
this.CurrentUser.PasswordAnswer = string.Empty;
if (string.IsNullOrWhiteSpace(this.CurrentUser.PasswordAnswerConfirmation))
this.CurrentUser.PasswordAnswerConfirmation = string.Empty;
if (this.CurrentUser.TryValidateProperty("Name") && this.CurrentUser.TryValidateProperty("FirstName")
&& this.CurrentUser.TryValidateProperty("LastName") && this.CurrentUser.TryValidateProperty("Email")
&& this.CurrentUser.TryValidateProperty("Password") && this.CurrentUser.TryValidateProperty("NewPassword")
&& this.CurrentUser.TryValidateProperty("NewPasswordConfirmation") && this.CurrentUser.TryValidateProperty("PasswordQuestion")
&& this.CurrentUser.TryValidateProperty("PasswordAnswer") && this.CurrentUser.TryValidateProperty("PasswordAnswerConfirmation")
&& this.CurrentUser.TryValidateProperty("UserType"))
{
// change is not from User Maintenance screen
this.CurrentUser.IsUserMaintenance = false;
this._issueVisionModel.SaveChangesAsync();
}
}
}
}
catch (Exception ex)
{
// notify user if there is any error
AppMessages.RaiseErrorMessage.Send(ex);
}
}
private RelayCommand _cancelChangeCommand = null;
public RelayCommand CancelChangeCommand
{
get
{
if (_cancelChangeCommand == null)
{
_cancelChangeCommand = new RelayCommand(
() => this.OnCancelChangeCommand(),
() => (this._issueVisionModel != null) && (this._issueVisionModel.HasChanges));
}
return _cancelChangeCommand;
}
}
private void OnCancelChangeCommand()
{
try
{
if (!_issueVisionModel.IsBusy)
{
// ask to confirm canceling the current user in edit
DialogMessage dialogMessage = new DialogMessage(
this,
CommonResources.CancelAnyChangesMessageBoxText,
s =>
{
if (s == MessageBoxResult.OK)
{
// if confirmed, cancel any change to CurrentUser
this._issueVisionModel.RejectChanges();
}
})
{
Button = MessageBoxButton.OKCancel,
Caption = CommonResources.ConfirmMessageBoxCaption
};
AppMessages.PleaseConfirmMessage.Send(dialogMessage);
}
}
catch (Exception ex)
{
// notify user if there is any error
AppMessages.RaiseErrorMessage.Send(ex);
}
}
#endregion "Public Commands"
#region "Private Methods"
private void _issueVisionModel_GetCurrentUserComplete(object sender, EntityResultsArgs<User> e)
{
if (!e.HasError)
{
if (e.Results.Count() == 1)
{
var enumerator = e.Results.GetEnumerator();
enumerator.MoveNext();
this.CurrentUser = enumerator.Current;
}
}
else
{
// notify user if there is any error
AppMessages.RaiseErrorMessage.Send(e.Error);
}
}
private void _issueVisionModel_GetSecurityQuestionsComplete(object sender, EntityResultsArgs<SecurityQuestion> e)
{
if (!e.HasError)
{
SecurityQuestionEntries = e.Results.OrderBy(g => g.PasswordQuestion);
// raise property changed for CurrentUser to reflect changes with SecurityQuestionEntries
this.RaisePropertyChanged("CurrentUser");
}
else
{
// notify user if there is any error
AppMessages.RaiseErrorMessage.Send(e.Error);
}
}
private void _issueVisionModel_SaveChangesComplete(object sender, SubmitOperationEventArgs e)
{
if (!e.HasError)
{
// load the current user again
_issueVisionModel.GetCurrentUserAsync();
// notify user of my profile saved successfully
DialogMessage dialogMessage = new DialogMessage(
this,
CommonResources.MyProfileSavedText,
null)
{
Button = MessageBoxButton.OK,
Caption = CommonResources.MyProfileSavedCaption
};
AppMessages.StatusUpdateMessage.Send(dialogMessage);
}
else
{
// notify user if there is any error
AppMessages.RaiseErrorMessage.Send(e.Error);
}
}
private void _issueVisionModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("HasChanges"))
{
SubmitChangeCommand.RaiseCanExecuteChanged();
CancelChangeCommand.RaiseCanExecuteChanged();
}
}
#endregion "Private Methods"
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Weidong has been an information system professional since 1990. He has a Master's degree in Computer Science, and is currently a MCSD .NET