PageRenderTime 165ms CodeModel.GetById 33ms RepoModel.GetById 9ms app.codeStats 0ms

/BaconographyWP8Core/PlatformServices/NotificationService.cs

https://github.com/hippiehunter/Baconography
C# | 114 lines | 105 code | 8 blank | 1 comment | 12 complexity | af17812e0ade5b17bd4f7b740be6737c MD5 | raw file
  1. using BaconographyPortable.Messages;
  2. using BaconographyPortable.Services;
  3. using Coding4Fun.Toolkit.Controls;
  4. using GalaSoft.MvvmLight.Messaging;
  5. using Microsoft.Phone.Shell;
  6. using Microsoft.Practices.ServiceLocation;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Input;
  13. using System.Windows.Media.Imaging;
  14. namespace BaconographyWP8.PlatformServices
  15. {
  16. class NotificationService : INotificationService
  17. {
  18. TaskScheduler _scheduler;
  19. public NotificationService()
  20. {
  21. try
  22. {
  23. _scheduler = TaskScheduler.FromCurrentSynchronizationContext();
  24. }
  25. catch
  26. {
  27. _scheduler = null;
  28. //we're running in the background task disable the notifications
  29. }
  30. }
  31. public void CreateNotification(string text)
  32. {
  33. if (_scheduler == null)
  34. return;
  35. Task.Factory.StartNew(() =>
  36. {
  37. ToastPrompt toast = new ToastPrompt();
  38. toast.Title = "Baconography";
  39. toast.Message = text;
  40. toast.TextWrapping = System.Windows.TextWrapping.Wrap;
  41. toast.ImageSource = new BitmapImage(new Uri("Assets\\ApplicationIconSmall.png", UriKind.RelativeOrAbsolute));
  42. toast.Show();
  43. }, System.Threading.CancellationToken.None, TaskCreationOptions.None, _scheduler);
  44. }
  45. public void CreateNotificationWithNavigation(string text, Type navTarget, object arguments)
  46. {
  47. if (_scheduler == null)
  48. return;
  49. Task.Factory.StartNew(() =>
  50. {
  51. ToastPrompt toast = new ToastPrompt();
  52. toast.Title = "Baconography";
  53. toast.Message = text;
  54. toast.TextWrapping = System.Windows.TextWrapping.Wrap;
  55. toast.ImageSource = new BitmapImage(new Uri("Assets\\ApplicationIconSmall.png", UriKind.RelativeOrAbsolute));
  56. toast.Tap += (sender, obj) => ServiceLocator.Current.GetInstance<INavigationService>().Navigate(navTarget, arguments);
  57. toast.Show();
  58. }, System.Threading.CancellationToken.None, TaskCreationOptions.None, _scheduler);
  59. }
  60. public void CreateErrorNotification(Exception exception)
  61. {
  62. if (_scheduler == null)
  63. return;
  64. Task.Factory.StartNew(() =>
  65. {
  66. if (exception is System.Net.WebException)
  67. {
  68. ToastPrompt toast = new ToastPrompt();
  69. toast.Title = "Baconography";
  70. toast.Message = "We're having a hard time connecting to reddit";
  71. toast.ImageSource = new BitmapImage(new Uri("Assets\\ApplicationIconSmall.png", UriKind.RelativeOrAbsolute));
  72. toast.TextWrapping = System.Windows.TextWrapping.Wrap;
  73. toast.Show();
  74. Messenger.Default.Send<ConnectionStatusMessage>(new ConnectionStatusMessage { IsOnline = false, UserInitiated = false });
  75. }
  76. else if (exception.Message == "NotFound")
  77. {
  78. CreateNotification("There doesnt seem to be anything here");
  79. }
  80. else
  81. {
  82. ToastPrompt toast = new ToastPrompt();
  83. toast.Title = "Baconography";
  84. toast.Message = "We're having a hard time connecting to reddit, you might want to try again later";
  85. toast.ImageSource = new BitmapImage(new Uri("Assets\\ApplicationIconSmall.png", UriKind.RelativeOrAbsolute));
  86. toast.TextWrapping = System.Windows.TextWrapping.Wrap;
  87. toast.Show();
  88. }
  89. }, System.Threading.CancellationToken.None, TaskCreationOptions.None, _scheduler);
  90. }
  91. public void CreateKitaroDBNotification(string text)
  92. {
  93. if (_scheduler == null)
  94. return;
  95. Task.Factory.StartNew(() =>
  96. {
  97. ToastPrompt toast = new ToastPrompt();
  98. toast.Title = "Baconography";
  99. toast.Message = text;
  100. toast.ImageSource = new BitmapImage(new Uri("Assets\\BaconographyKitaroPlug.png", UriKind.RelativeOrAbsolute));
  101. toast.TextWrapping = System.Windows.TextWrapping.Wrap;
  102. toast.Show();
  103. }, System.Threading.CancellationToken.None, TaskCreationOptions.None, _scheduler);
  104. }
  105. }
  106. }