/DEV/mal.balades.Admin/ViewModels/VisitViewModels/Edit/VisitEditViewModel.cs

# · C# · 413 lines · 341 code · 63 blank · 9 comment · 43 complexity · 1df09ba405e5d13f91907fe50501b528 MD5 · raw file

  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.Windows.Input;
  4. using GalaSoft.MvvmLight.Command;
  5. using GalaSoft.MvvmLight.Messaging;
  6. using Mal.Balades.Admin.Services;
  7. using Mal.Balades.Admin.ViewModels.VisitViewModels.Edit.Data;
  8. namespace Mal.Balades.Admin.ViewModels.VisitViewModels.Edit
  9. {
  10. public class VisitEditViewModel : PageViewModelBase
  11. {
  12. #region Fields
  13. IAdminService service;
  14. VisitViewModel sourceVisit;
  15. private bool isLoading;
  16. private bool isVisitLoading;
  17. private bool isPicturesLoading;
  18. private bool isInformationsLoading;
  19. private bool isSaving;
  20. private List<ITabItemViewModel> tabs;
  21. private TabViewModelBase selectedTab;
  22. private int selectedTabIndex = -1;
  23. private RelayCommand saveCommand;
  24. private RelayCommand addCommand;
  25. #endregion
  26. #region Construction / Destruction
  27. public VisitEditViewModel()
  28. //: base(mainViewModel)
  29. {
  30. this.service = AdminServiceFactory.GetInstance();
  31. this.service.GetVisitCompleted += new ServiceResponse<IVisitAdmin>(OnGetVisitCompleted);
  32. this.service.GetVisitInformationsCompleted += new ServiceResponse<ReadOnlyCollection<IInformation>>(OnGetVisitInformationsCompleted);
  33. this.service.GetVisitPicturesCompleted += new ServiceResponse<ReadOnlyCollection<IPicture>>(OnGetVisitPicturesCompleted);
  34. this.service.SaveVisitCompleted += new ServiceResponse<ISaveVisitResult>(OnSaveVisitCompleted);
  35. }
  36. #endregion
  37. #region enum, struct, class
  38. #endregion
  39. #region Properties
  40. public bool IsLoading
  41. {
  42. get { return isLoading; }
  43. set
  44. {
  45. if (isLoading != value)
  46. {
  47. isLoading = value;
  48. RaisePropertyChanged("IsLoading");
  49. }
  50. if (isLoading)
  51. {
  52. Messenger.Default.Send<MainViewModel.MessageBarMessage>(
  53. new MainViewModel.MessageBarMessage(this,"Chargement de la visite en cours ...."));
  54. }
  55. else
  56. {
  57. Messenger.Default.Send<MainViewModel.MessageBarMessage>(
  58. new MainViewModel.MessageBarMessage(this));
  59. }
  60. }
  61. }
  62. public bool IsSaving
  63. {
  64. get { return isSaving; }
  65. set
  66. {
  67. if (isSaving != value)
  68. {
  69. isSaving = value;
  70. RaisePropertyChanged("IsSaving");
  71. SetIsLoading();
  72. if (isSaving)
  73. {
  74. Messenger.Default.Send<MainViewModel.MessageBarMessage>(
  75. new MainViewModel.MessageBarMessage(this, "Sauvegarde de la visite en cours ...."));
  76. }
  77. else
  78. {
  79. Messenger.Default.Send<MainViewModel.MessageBarMessage>(
  80. new MainViewModel.MessageBarMessage(this));
  81. }
  82. }
  83. }
  84. }
  85. public bool IsVisitLoading
  86. {
  87. get { return isVisitLoading; }
  88. set
  89. {
  90. if (isVisitLoading != value)
  91. {
  92. isVisitLoading = value;
  93. RaisePropertyChanged("IsVisitLoading");
  94. SetIsLoading();
  95. }
  96. }
  97. }
  98. public bool IsInformationsLoading
  99. {
  100. get { return isInformationsLoading; }
  101. set
  102. {
  103. if (isInformationsLoading != value)
  104. {
  105. isInformationsLoading = value;
  106. RaisePropertyChanged("IsInformationsLoading");
  107. SetIsLoading();
  108. }
  109. }
  110. }
  111. public bool IsPicturesLoading
  112. {
  113. get { return isPicturesLoading; }
  114. set
  115. {
  116. if (isPicturesLoading != value)
  117. {
  118. isPicturesLoading = value;
  119. RaisePropertyChanged("IsPicturesLoading");
  120. SetIsLoading();
  121. }
  122. }
  123. }
  124. public VisitViewModel SourceVisit
  125. {
  126. get { return sourceVisit; }
  127. set
  128. {
  129. sourceVisit = value;
  130. RaisePropertyChanged("SourceVisit");
  131. BuildTabs();
  132. }
  133. }
  134. public int SelectedTabIndex
  135. {
  136. get { return selectedTabIndex; }
  137. set
  138. {
  139. if (selectedTabIndex != value)
  140. {
  141. selectedTabIndex = value;
  142. RaisePropertyChanged("SelectedTabIndex");
  143. this.SelectedTab = (TabViewModelBase)this.Tabs[selectedTabIndex];
  144. }
  145. }
  146. }
  147. public TabViewModelBase SelectedTab
  148. {
  149. get { return selectedTab; }
  150. set
  151. {
  152. if (selectedTab != value)
  153. {
  154. selectedTab = value;
  155. RaisePropertyChanged("SelectedTab");
  156. }
  157. }
  158. }
  159. public List<ITabItemViewModel> Tabs
  160. {
  161. get { return tabs; }
  162. set
  163. {
  164. if (tabs != value)
  165. {
  166. tabs = value;
  167. RaisePropertyChanged("Tabs");
  168. }
  169. }
  170. }
  171. public ICommand SaveCommand
  172. {
  173. get
  174. {
  175. if (this.saveCommand == null)
  176. {
  177. this.saveCommand = new RelayCommand(OnSave);
  178. }
  179. return this.saveCommand;
  180. }
  181. }
  182. public ICommand AddCommand
  183. {
  184. get
  185. {
  186. if (this.addCommand == null)
  187. {
  188. this.addCommand = new RelayCommand(OnAdd/*,CanAdd*/);
  189. }
  190. return this.addCommand;
  191. }
  192. }
  193. #endregion
  194. #region Methods
  195. public override void Cleanup()
  196. {
  197. if (this.service != null)
  198. {
  199. this.service.GetVisitCompleted -= new ServiceResponse<IVisitAdmin>(OnGetVisitCompleted);
  200. this.service.GetVisitInformationsCompleted -= new ServiceResponse<ReadOnlyCollection<IInformation>>(OnGetVisitInformationsCompleted);
  201. this.service.GetVisitPicturesCompleted -= new ServiceResponse<ReadOnlyCollection<IPicture>>(OnGetVisitPicturesCompleted);
  202. this.service.SaveVisitCompleted -= new ServiceResponse<ISaveVisitResult>(OnSaveVisitCompleted);
  203. this.service.Cleanup();
  204. this.service = null;
  205. }
  206. base.Cleanup();
  207. }
  208. private void LoadVisit(int visitId)
  209. {
  210. this.IsVisitLoading = true;
  211. this.IsInformationsLoading = true;
  212. this.IsPicturesLoading = true;
  213. this.service.GetVisitAsync(visitId);
  214. }
  215. private void SetIsLoading()
  216. {
  217. this.IsLoading = this.isPicturesLoading || this.isVisitLoading || this.isInformationsLoading || this.isSaving;
  218. }
  219. private void BuildTabs()
  220. {
  221. this.Tabs.Add(new MainTabViewModel(this));
  222. this.Tabs.Add(new InformationsTabViewModel(this));
  223. this.Tabs.Add(new PicturesTabViewModel(this));
  224. this.Tabs.Add(new MapTabViewModel(this));
  225. this.SelectedTabIndex = 0;
  226. RaisePropertyChanged("Tabs");
  227. }
  228. protected void OnSave()
  229. {
  230. this.IsSaving = true;
  231. if (this.SourceVisit.VisitID != 0)
  232. {
  233. SaveVisitObject saveVisitObject = new SaveVisitObject();
  234. saveVisitObject.Visit.Latitude = this.SourceVisit.Latitude;
  235. saveVisitObject.Visit.Longitude = this.SourceVisit.Longitude;
  236. saveVisitObject.Visit.MarketingCategoryID = this.SourceVisit.MarketingCategoryID;
  237. saveVisitObject.Visit.PlaceCategoryID = this.SourceVisit.PlaceCategoryID;
  238. saveVisitObject.Visit.QualityCategories = this.SourceVisit.QualityCategories;
  239. saveVisitObject.Visit.QualityGps = this.SourceVisit.QualityGps;
  240. saveVisitObject.Visit.QualityInformations = this.SourceVisit.QualityInformations;
  241. saveVisitObject.Visit.QualityPictures = this.SourceVisit.QualityPictures;
  242. saveVisitObject.Visit.QualitySumUp = this.SourceVisit.QualitySumUp;
  243. saveVisitObject.Visit.QualityText = this.SourceVisit.QualityText;
  244. saveVisitObject.Visit.SumUp = this.SourceVisit.SumUp;
  245. saveVisitObject.Visit.Text = this.SourceVisit.Text;
  246. saveVisitObject.Visit.Title = this.SourceVisit.Title;
  247. saveVisitObject.Visit.UrlName = this.SourceVisit.UrlName;
  248. saveVisitObject.Visit.Visible = this.SourceVisit.Visible;
  249. saveVisitObject.Visit.VisitID = this.SourceVisit.VisitID;
  250. foreach (PictureViewModel picture in this.SourceVisit.Pictures)
  251. {
  252. Picture pictureSave = new Picture();
  253. pictureSave.PictureID = picture.PictureID;
  254. pictureSave.PictureTypeID = picture.PictureTypeID;
  255. pictureSave.Rank = picture.Rank;
  256. pictureSave.Text = picture.Text;
  257. pictureSave.Title = picture.Title;
  258. pictureSave.Url = picture.Url;
  259. pictureSave.VisitID = picture.VisitID;
  260. saveVisitObject.AddPicture(pictureSave);
  261. }
  262. foreach (InformationViewModel information in this.SourceVisit.Informations)
  263. {
  264. Information informationSave = new Information();
  265. informationSave.InformationID = information.InformationID;
  266. informationSave.InformationTypeID = information.InformationTypeID;
  267. informationSave.Rank = information.Rank;
  268. informationSave.Text = information.Text;
  269. informationSave.VisitID = information.VisitID;
  270. saveVisitObject.AddInformation(informationSave);
  271. }
  272. this.service.SaveVisitAsync(saveVisitObject);
  273. }
  274. }
  275. protected void OnAdd()
  276. {
  277. if (this.SelectedTab != null)
  278. {
  279. this.SelectedTab.Add();
  280. }
  281. }
  282. //protected bool CanAdd()
  283. //{
  284. // if (this.selectedTab != null)
  285. // {
  286. // return ((TabViewModelBase)this.selectedTab).AddCommandEnabled;
  287. // }
  288. // return false;
  289. //}
  290. #endregion
  291. #region Events
  292. void OnGetVisitCompleted(ServiceResponseArgs<IVisitAdmin> e)
  293. {
  294. this.IsVisitLoading = false;
  295. if (e.Error != null)
  296. throw e.Error;
  297. this.SourceVisit = new VisitViewModel(e.Result);
  298. this.service.GetVisitPicturesAsync(this.SourceVisit.VisitID.Value);
  299. this.service.GetVisitInformationsAsync(this.SourceVisit.VisitID.Value);
  300. }
  301. void OnGetVisitPicturesCompleted(ServiceResponseArgs<ReadOnlyCollection<IPicture>> e)
  302. {
  303. this.IsPicturesLoading = false;
  304. if (e.Error != null)
  305. throw e.Error;
  306. foreach (IPicture picture in e.Result)
  307. {
  308. this.SourceVisit.Pictures.Add(new PictureViewModel(picture));
  309. }
  310. }
  311. void OnGetVisitInformationsCompleted(ServiceResponseArgs<System.Collections.ObjectModel.ReadOnlyCollection<IInformation>> e)
  312. {
  313. this.IsInformationsLoading = false;
  314. if (e.Error != null)
  315. throw e.Error;
  316. foreach (IInformation info in e.Result)
  317. {
  318. this.SourceVisit.Informations.Add(new InformationViewModel(info));
  319. }
  320. }
  321. void OnSaveVisitCompleted(ServiceResponseArgs<ISaveVisitResult> e)
  322. {
  323. this.IsSaving = false;
  324. if (e.Error != null)
  325. throw e.Error;
  326. }
  327. public override void OnNavigate()
  328. {
  329. this.Tabs = new List<ITabItemViewModel>();
  330. bool createNewVisit = true;
  331. if (this.NavigationContext.QueryString.ContainsKey("visitId"))
  332. {
  333. int visitId = 0;
  334. if (int.TryParse(this.NavigationContext.QueryString["visitId"], out visitId))
  335. {
  336. if (visitId > 0)
  337. {
  338. createNewVisit = false;
  339. LoadVisit(visitId);
  340. }
  341. }
  342. }
  343. if (createNewVisit)
  344. {
  345. this.SourceVisit = new VisitViewModel();
  346. }
  347. }
  348. #endregion
  349. }
  350. }