MainViewModel.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. namespace GDNXFD.Alert.Config.ViewModel
  2. {
  3. using GalaSoft.MvvmLight.Messaging;
  4. using GDNXFD.Alert.Config.Helpers;
  5. using GDNXFD.Alert.Config.Model;
  6. using GDNXFD.Alert.Config.Resources.Strings;
  7. using GDNXFD.Alert.Config.Services.Navigation;
  8. using GDNXFD.Alert.Config.ViewModel.Base;
  9. using Views;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Threading.Tasks;
  16. using System.Windows;
  17. using System.Xml;
  18. /// <summary>
  19. /// MainWindow ViewModel
  20. /// </summary>
  21. public class MainViewModel : VMBase
  22. {
  23. private const string APPIMAGE_PATH = "Resources/Images/icon.png";
  24. private readonly INavigationService navService;
  25. private bool isVisibleDialog;
  26. private CustomDialogMessage dialogMessage;
  27. /// <summary>
  28. /// Initializes a new instance of the MainViewModel class.
  29. /// </summary>
  30. public MainViewModel(
  31. INavigationService navService)
  32. {
  33. this.navService = navService;
  34. Messenger.Default.Register<LoadingMessage>(this, HandleLoadingMessage);
  35. Messenger.Default.Register<UserLoginMessage>(this, HandleUserLoginMessage);
  36. Messenger.Default.Register<CustomDialogMessage>(this, HandleDialogMessage);
  37. }
  38. /// <summary>
  39. /// Initialize data
  40. /// </summary>
  41. /// <returns></returns>
  42. public void InitializeData()
  43. {
  44. //Messenger.Default.Send(new LoadingMessage(true));
  45. navService.NavigateToLogin();//.NavigateToRuleList();
  46. }
  47. private static void ShowErrorMessage(string textMessage)
  48. {
  49. CustomDialogMessage message = new CustomDialogMessage(null, textMessage, Visibility.Collapsed);
  50. Messenger.Default.Send<CustomDialogMessage>(message);
  51. Messenger.Default.Send(new LoadingMessage(false));
  52. }
  53. public bool IsLogin
  54. {
  55. get
  56. {
  57. return string.IsNullOrWhiteSpace(GlobalVar.CurrentUser) ? false : true;
  58. }
  59. }
  60. public string Username
  61. {
  62. get
  63. {
  64. return StringProvider.GetString("Welcome") + GlobalVar.CurrentUser;
  65. }
  66. }
  67. /// <summary>
  68. /// Is visible dialog control.
  69. /// </summary>
  70. public bool IsVisibleDialog
  71. {
  72. get { return this.isVisibleDialog; }
  73. set
  74. {
  75. this.isVisibleDialog = value;
  76. RaisePropertyChanged();
  77. }
  78. }
  79. /// <summary>
  80. /// CustomDialogMessage instance used as datacontext for the confirmation dialog.
  81. /// </summary>
  82. public CustomDialogMessage DialogMessage
  83. {
  84. get { return this.dialogMessage; }
  85. set
  86. {
  87. this.dialogMessage = value;
  88. RaisePropertyChanged();
  89. }
  90. }
  91. private void HandleLoadingMessage(LoadingMessage msg)
  92. {
  93. IsBusy = msg.IsLoading;
  94. }
  95. private void HandleUserLoginMessage(UserLoginMessage msg)
  96. {
  97. foreach (Window window in App.Current.Windows)
  98. {
  99. if ((window as MainWindow) != null)
  100. {
  101. window.Top = window.Top - (750-window.Height) /2;
  102. window.Left = window.Left - (1024-window.Width)/2;
  103. window.Width = 1024;
  104. window.Height = 750;
  105. }
  106. }
  107. RaisePropertyChanged(() => IsLogin);
  108. RaisePropertyChanged(() => Username);
  109. }
  110. private void HandleDialogMessage(CustomDialogMessage msg)
  111. {
  112. IsVisibleDialog = true;
  113. msg.CancelAction = () =>
  114. {
  115. IsVisibleDialog = false;
  116. };
  117. DialogMessage = msg;
  118. }
  119. /// <summary>
  120. /// Dispose
  121. /// </summary>
  122. /// <param name="dispose"></param>
  123. protected override void Dispose(bool dispose)
  124. {
  125. base.Dispose(dispose);
  126. Messenger.Default.Unregister<LoadingMessage>(this, HandleLoadingMessage);
  127. Messenger.Default.Unregister<UserLoginMessage>(this, HandleUserLoginMessage);
  128. Messenger.Default.Unregister<CustomDialogMessage>(this, HandleDialogMessage);
  129. }
  130. }
  131. }