PushPatternViewModel.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.ComponentModel.Composition;
  6. using System.Windows.Input;
  7. using WindowsPhone.Recipes.Push.Server.Services;
  8. using System.Windows;
  9. using WindowsPhone.Recipes.Push.Server.Models;
  10. using WindowsPhone.Recipes.Push.Messasges;
  11. namespace WindowsPhone.Recipes.Push.Server.ViewModels
  12. {
  13. /// <summary>
  14. /// Represents a base class for all push pattern view models.
  15. /// </summary>
  16. /// <remarks>
  17. /// A push pattern view model contains the logic and behavior of a server side push notification pattern.
  18. /// For demonstration purposes, and since we are using WPF as the 'face' of this server, each view model
  19. /// exposes properties and commands to be controlled and activated by this server UI.
  20. /// You can find more information about the MVVM pattern in <see cref="http://en.wikipedia.org/wiki/Model_View_ViewModel"/>.
  21. /// </remarks>
  22. internal abstract class PushPatternViewModel : ViewModelBase
  23. {
  24. #region Fields
  25. /// <value>Indicates if current pattern is active or not.</value>
  26. private bool _isActive;
  27. /// <value>Collection of tile image relative uri's available in the phone application.</value>
  28. private Uri[] _tileImages;
  29. /// <value>Selected tile background image uri.</value>
  30. private Uri _backgroundImageUri;
  31. /// <value>Tile message count.</value>
  32. private int _count;
  33. /// <value>Tile message title.</value>
  34. private string _title;
  35. /// <value>Toast message title.</value>
  36. private string _toastTitle;
  37. /// <value>Toast message sub title.</value>
  38. private string _toastSubTitle;
  39. /// <value>Raw text message.</value>
  40. private string _rawMessage;
  41. #endregion
  42. #region Properties
  43. [Import]
  44. protected PushService PushService { get; private set; }
  45. [Import]
  46. private IMessageSendResultLogger ResultLogger { get; set; }
  47. /// <summary>
  48. /// Gets current pattern display name.
  49. /// </summary>
  50. public string DisplayName { get; protected set; }
  51. /// <summary>
  52. /// Gets current pattern description text.
  53. /// </summary>
  54. public string Description { get; protected set; }
  55. /// <summary>
  56. /// Gets or sets value for indicating whether this pattern is active or not.
  57. /// </summary>
  58. public bool IsActive
  59. {
  60. get
  61. {
  62. return _isActive;
  63. }
  64. set
  65. {
  66. if (_isActive != value)
  67. {
  68. _isActive = value;
  69. if (_isActive)
  70. {
  71. OnActivated();
  72. }
  73. else
  74. {
  75. OnDeactivated();
  76. }
  77. NotifyPropertyChanged("IsActive");
  78. }
  79. }
  80. }
  81. /// <summary>
  82. /// Gets a collection of tile image uris available in the phone client application.
  83. /// </summary>
  84. public Uri[] TileImages
  85. {
  86. get
  87. {
  88. if (_tileImages == null)
  89. {
  90. _tileImages = new Uri[]
  91. {
  92. ToUri("TileBackground1.jpg"),
  93. ToUri("TileBackground2.jpg"),
  94. ToUri("TileBackground3.jpg"),
  95. };
  96. BackgroundImageUri = _tileImages[0];
  97. }
  98. return _tileImages;
  99. }
  100. }
  101. /// <summary>
  102. /// Gets or sets the tile background uri.
  103. /// </summary>
  104. public Uri BackgroundImageUri
  105. {
  106. get { return _backgroundImageUri; }
  107. set
  108. {
  109. if (_backgroundImageUri != value)
  110. {
  111. _backgroundImageUri = value;
  112. NotifyPropertyChanged("BackgroundImageUri");
  113. }
  114. }
  115. }
  116. /// <summary>
  117. /// Gets or sets the tile count.
  118. /// </summary>
  119. public int Count
  120. {
  121. get { return _count; }
  122. set
  123. {
  124. if (_count != value)
  125. {
  126. _count = value;
  127. NotifyPropertyChanged("Count");
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// Gets or sets the tile tiltle.
  133. /// </summary>
  134. public string Title
  135. {
  136. get { return _title; }
  137. set
  138. {
  139. if (_title != value)
  140. {
  141. _title = value;
  142. NotifyPropertyChanged("Title");
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// Gets or sets the toast title.
  148. /// </summary>
  149. public string ToastTitle
  150. {
  151. get { return _toastTitle; }
  152. set
  153. {
  154. if (_toastTitle != value)
  155. {
  156. _toastTitle = value;
  157. NotifyPropertyChanged("ToastTitle");
  158. }
  159. }
  160. }
  161. /// <summary>
  162. /// Gets or sets the toast sub-title.
  163. /// </summary>
  164. public string ToastSubTitle
  165. {
  166. get { return _toastSubTitle; }
  167. set
  168. {
  169. if (_toastSubTitle != value)
  170. {
  171. _toastSubTitle = value;
  172. NotifyPropertyChanged("ToastSubTitle");
  173. }
  174. }
  175. }
  176. /// <summary>
  177. /// Gets or sets the raw text message.
  178. /// </summary>
  179. public string RawMessage
  180. {
  181. get { return _rawMessage; }
  182. set
  183. {
  184. if (_rawMessage != value)
  185. {
  186. _rawMessage = value;
  187. NotifyPropertyChanged("RawMessage");
  188. }
  189. }
  190. }
  191. #endregion
  192. #region Commands
  193. /// <summary>
  194. /// Gets the command which executes the send operation.
  195. /// </summary>
  196. public ICommand SendCommand
  197. {
  198. get
  199. {
  200. return new RelayCommand(p =>
  201. {
  202. try
  203. {
  204. OnSend();
  205. }
  206. catch (Exception ex)
  207. {
  208. MessageBox.Show(ex.Message, "Send Error");
  209. }
  210. });
  211. }
  212. }
  213. #endregion
  214. #region Protected
  215. /// <summary>
  216. /// Override this to send push message according to the pattern behavior.
  217. /// </summary>
  218. protected abstract void OnSend();
  219. /// <summary>
  220. /// Override this to add additional pattern-activation logic.
  221. /// </summary>
  222. protected virtual void OnActivated()
  223. {
  224. UpdateClient();
  225. PushService.Subscribed += PushService_Subscribed;
  226. PushService.GetInfo += PushService_GetInfo;
  227. }
  228. /// <summary>
  229. /// Override this to add additional pattern-deactivation logic.
  230. /// </summary>
  231. protected virtual void OnDeactivated()
  232. {
  233. PushService.Subscribed -= PushService_Subscribed;
  234. PushService.GetInfo -= PushService_GetInfo;
  235. }
  236. /// <summary>
  237. /// Override this to add logic when clients login.
  238. /// </summary>
  239. protected virtual void OnSubscribed(SubscriptionEventArgs args)
  240. {
  241. }
  242. /// <summary>
  243. /// Override this to add logic when clients request server's info.
  244. /// </summary>
  245. protected virtual void OnGetInfo(ServerInfoEventArgs args)
  246. {
  247. args.ServerInfo = new ServerInfo
  248. {
  249. PushPattern = DisplayName,
  250. Counter = Count
  251. };
  252. }
  253. /// <summary>
  254. /// Logs push message result.
  255. /// </summary>
  256. protected void Log(MessageSendResult result)
  257. {
  258. ResultLogger.Log(DisplayName, result);
  259. }
  260. /// <summary>
  261. /// Logs push message error.
  262. /// </summary>
  263. protected void Log(MessageSendException exception)
  264. {
  265. ResultLogger.Log(DisplayName, exception);
  266. }
  267. #endregion
  268. #region Event Handlers
  269. private void UpdateClient()
  270. {
  271. // Notify subscribers to get new info from the server.
  272. foreach (var subscriber in PushService.Subscribers)
  273. {
  274. new RawPushNotificationMessage(MessageSendPriority.High)
  275. {
  276. RawData = Encoding.ASCII.GetBytes("Update Info")
  277. }.SendAsync(subscriber.ChannelUri);
  278. }
  279. }
  280. private void PushService_Subscribed(object sender, SubscriptionEventArgs args)
  281. {
  282. OnSubscribed(args);
  283. }
  284. private void PushService_GetInfo(object sender, ServerInfoEventArgs args)
  285. {
  286. OnGetInfo(args);
  287. }
  288. #endregion
  289. #region Helpers
  290. private static Uri ToUri(string imageName)
  291. {
  292. return new Uri(string.Format("/Resources/TileImages/{0}", imageName), UriKind.Relative);
  293. }
  294. #endregion
  295. }
  296. }