コード例 #1
0
 /// <summary>
 /// Show a notification to a user.
 /// </summary>
 /// <param name="viewModel"></param>
 /// <returns></returns>
 public Task ShowNotification(NotificationBoxDialogViewModel viewModel)
 {
     return(IoC.Task.Run(() =>
     {
         NotificationArea.AddNotification(viewModel);
     }));
 }
コード例 #2
0
        /// <summary>
        /// Show a notification containing patch notes to a user.
        /// </summary>
        /// <returns></returns>
        public Task ShowPatchNotes()
        {
            return(IoC.Task.Run(async() =>
            {
                // Get client.
                var client = IoC.Web.Http.GetClientForHost(new Uri(SettingsConfiguration.RemotePatchNotesFilePath));
                string message = string.Empty;
                bool isOk = false;

                // Get data.
                try
                {
                    // Read data.
                    message = await client.GetStringAsync(SettingsConfiguration.RemotePatchNotesFilePath);
                    IoC.Logger.Log($"Patch Notes has been read successfully!", LogLevel.Debug);
                    isOk = true;
                }
                catch (HttpRequestException e) // Internet connection issues.
                {
                    IoC.Logger.Log($"{e.GetType().ToString()}: {e.Message} (expected exception)", LogLevel.Verbose);
                }
                catch (TaskCanceledException e) // Timeout.
                {
                    IoC.Logger.Log($"{e.GetType().ToString()}: {e.Message} (expected exception)", LogLevel.Debug);
                }
                catch (Exception e) // Unexpected.
                {
                    IoC.Logger.Log($"{e.GetType().ToString()}: {e.Message}", LogLevel.Fatal);
                }

                // We have data OK
                if (isOk)
                {
                    // Remove first line of the message
                    message = message.RemoveFirstLines(1);

                    // Create notification view model
                    var vm = new NotificationBoxDialogViewModel()
                    {
                        Title = "PATCH NOTES",
                        MessageFormatting = true,
                        Message = message,
                        Result = NotificationBoxResult.Ok,
                    };

                    // Create notification
                    NotificationArea.AddNotification(vm);
                }
            }));
        }
コード例 #3
0
        /// <summary>
        /// Show a notification containing news to a user.
        /// </summary>
        /// <param name="onlyWhenNew">True: shows patch notes only when the first line of news file which represents latest news, is newer.</param>
        /// <returns></returns>
        public Task ShowNews(bool onlyWhenNew)
        {
            return(IoC.Task.Run(async() =>
            {
                // Get client.
                var client = IoC.Web.Http.GetClientForHost(new Uri(SettingsConfiguration.RemoteNewsFilePath));
                string message = string.Empty;
                bool isOk = false;

                // Get data.
                try
                {
                    // Read data.
                    message = await client.GetStringAsync(SettingsConfiguration.RemoteNewsFilePath);
                    IoC.Logger.Log($"News has been read successfully!", LogLevel.Debug);
                    isOk = true;
                }
                catch (HttpRequestException e) // Internet connection issues.
                {
                    IoC.Logger.Log($"{e.GetType().ToString()}: {e.Message} (expected exception)", LogLevel.Verbose);
                }
                catch (TaskCanceledException e) // Timeout.
                {
                    IoC.Logger.Log($"{e.GetType().ToString()}: {e.Message} (expected exception)", LogLevel.Debug);
                }
                catch (Exception e) // Unexpected.
                {
                    IoC.Logger.Log($"{e.GetType().ToString()}: {e.Message}", LogLevel.Fatal);
                }

                // We have data OK
                if (isOk)
                {
                    // Get first line of the message = date of latest news
                    DateTime date;
                    DateTime.TryParseExact(
                        message.GetFirstLines(1).Replace(@"<!--", "").Replace(@"-->", "").Replace(" ", ""),
                        "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);

                    // Compare user's date of last news show with the latest news date.
                    // To show the notification, the remote date has to be greater than the one saved in the cookies and the saved one cannot be equal to today's date.
                    if (!onlyWhenNew ||
                        (DateTime.Compare(IoC.Application.Cookies.NewsLatestReviewDate, date) < 0 &&
                         DateTime.Compare(IoC.Application.Cookies.NewsLatestReviewDate, DateTime.Today) != 0)
                        )
                    {
                        // If user download the app and the news are not up-to-date. Don!t show them and update default user's review time to today's date.
                        if (DateTime.Compare(date, DateTime.Today) < 0)
                        {
                            IoC.Application.Cookies.NewsLatestReviewDate = DateTime.Today;
                        }
                        // Otherwise, show notification.
                        else
                        {
                            // Create notification view model
                            var vm = new NotificationBoxDialogViewModel()
                            {
                                Title = "NEWS",
                                MessageFormatting = true,
                                Message = message,
                                Result = NotificationBoxResult.Ok,
                                OkAction = () =>
                                {
                                    IoC.Application.Cookies.NewsLatestReviewDate = DateTime.Today;
                                },
                            };

                            // Create notification
                            NotificationArea.AddNotification(vm);
                        }
                    }
                }
            }));
        }