public async void Run(IBackgroundTaskInstance taskInstance) { Defferal = taskInstance?.GetDeferral(); //var details = taskInstance.TriggerDetails as ToastNotificationActionTriggerDetail; //if (details != null) //{ // await Launcher.LaunchUriAsync(new Uri(details.Argument)); // Defferal.Complete(); // return; //} try { if (taskInstance != null) //we are already running -> started on demand { ResourceLocator.RegisterBase(); ResourceLocator.RegisterAppDataServiceAdapter(new ApplicationDataServiceService()); ResourceLocator.RegisterPasswordVaultAdapter(new PasswordVaultProvider()); ResourceLocator.RegisterMessageDialogAdapter(new MessageDialogProvider()); ResourceLocator.RegisterHttpContextAdapter(new MalHttpContextProvider()); ResourceLocator.RegisterDataCacheAdapter(new Adapters.DataCache(null)); Credentials.Init(); } } catch (Exception) { //app is running so we don't check, checks are conducted in runtime manually //Defferal?.Complete(); //return; } List <MalNotification> notifications = new List <MalNotification>(); try { if ( Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.FriendRequestAcceptDeny) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.NewRelatedAnime) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.BlogComment) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.ClubMessages) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.ForumQuoute) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.FriendRequest) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.NowAiring) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.ProfileComment) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.Payment) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.UserMentions) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.WatchedTopics)) { notifications.AddRange(await MalNotificationsQuery.GetNotifications()); notifications = notifications.Where( notification => !notification.IsRead && (Settings.EnabledNotificationTypes & notification.Type) == notification.Type).ToList(); } } catch (Exception) { //http exec error } if ((Settings.EnabledNotificationTypes & MalNotificationsTypes.Messages) == MalNotificationsTypes.Messages) { try { var msgs = await AccountMessagesManager.GetMessagesAsync(1); foreach (var malMessageModel in msgs) { if (!malMessageModel.IsRead) { notifications.Add(new MalNotification(malMessageModel)); //I'm assuming that Ids are unique } } } catch (Exception) { //no messages } } bool watchedTopicsUpdated = false; foreach (var watchedTopic in ResourceLocator.HandyDataStorage.WatchedTopics.StoredItems) { if (!watchedTopic.OnCooldown) { var count = await new ForumTopicMessageCountQuery(watchedTopic.Id).GetMessageCount(true); if (count == null) { continue; } if (count > watchedTopic.LastCheckedReplyCount) { var notif = new MalNotification(watchedTopic); var diff = count - watchedTopic.LastCheckedReplyCount; if (diff == 1) { notif.Content += " There is one new reply."; } else { notif.Content += $" There are {diff} new replies."; } notifications.Add(notif); watchedTopic.OnCooldown = true; watchedTopic.LastCheckedReplyCount = count.Value; watchedTopicsUpdated = true; } } } if (watchedTopicsUpdated) { ResourceLocator.HandyDataStorage.WatchedTopics.SaveData(); } if (!notifications.Any()) { Defferal?.Complete(); return; } var allTriggeredNotifications = (string)(ApplicationData.Current.LocalSettings.Values[nameof(RoamingDataTypes.ReadNotifications)] ?? string.Empty); var triggeredNotifications = allTriggeredNotifications.Split(';').ToList(); var triggeredAny = false; //trigger new notifications foreach (var notification in notifications) { if (triggeredNotifications.Contains(notification.Id)) { continue; } triggeredNotifications.Add(notification.Id); triggeredAny = true; ScheduleToast(notification); } //remove old triggered entries var presentNotifications = new List <string>(); foreach (var triggeredNotification in triggeredNotifications) { if (notifications.Any(notif => notif.Id == triggeredNotification)) { presentNotifications.Add(triggeredNotification); } } ApplicationData.Current.LocalSettings.Values[nameof(RoamingDataTypes.ReadNotifications)] = string.Join(";", presentNotifications); if (!triggeredAny) { Defferal?.Complete(); } }
public override async void OnReceive(Context context, Intent sourceIntent) { try { ResourceLocator.RegisterBase(); ResourceLocator.RegisterAppDataServiceAdapter(new ApplicationDataServiceService()); ResourceLocator.RegisterPasswordVaultAdapter(new PasswordVaultProvider()); ResourceLocator.RegisterMessageDialogAdapter(new MessageDialogProvider()); ResourceLocator.RegisterHttpContextAdapter(new MalHttpContextProvider()); ResourceLocator.RegisterDataCacheAdapter(new Adapters.DataCache(null)); Credentials.Init(); } catch (Exception) { //may be already registered... voodoo I guess } List <MalNotification> notifications = new List <MalNotification>(); try { if ( Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.FriendRequestAcceptDeny) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.NewRelatedAnime) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.BlogComment) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.ClubMessages) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.ForumQuoute) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.FriendRequest) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.NowAiring) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.ProfileComment) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.Payment) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.UserMentions) || Settings.EnabledNotificationTypes.HasFlag(MalNotificationsTypes.WatchedTopics)) { List <MalNotification> notifs = null; await Task.Run(async() => { notifs = await MalNotificationsQuery.GetNotifications(); }); notifications.AddRange(notifs); notifications = notifications.Where( notification => !notification.IsRead && (Settings.EnabledNotificationTypes & notification.Type) == notification.Type).ToList(); } } catch (Exception e) { //http exec error } if ((Settings.EnabledNotificationTypes & MalNotificationsTypes.Messages) == MalNotificationsTypes.Messages) { try { List <MalMessageModel> msgs = null; await Task.Run(async() => { msgs = await AccountMessagesManager.GetMessagesAsync(1); }); foreach (var malMessageModel in msgs) { if (!malMessageModel.IsRead) { notifications.Add(new MalNotification(malMessageModel)); //I'm assuming that Ids are unique } } } catch (Exception) { //no messages } } bool watchedTopicsUpdated = false; foreach (var watchedTopic in ResourceLocator.HandyDataStorage.WatchedTopics.StoredItems) { if (!watchedTopic.OnCooldown) { var count = await new ForumTopicMessageCountQuery(watchedTopic.Id).GetMessageCount(true); if (count == null) { continue; } if (count > watchedTopic.LastCheckedReplyCount) { var notif = new MalNotification(watchedTopic); var diff = count - watchedTopic.LastCheckedReplyCount; if (diff == 1) { notif.Content += " There is one new reply."; } else { notif.Content += $" There are {diff} new replies."; } notifications.Add(notif); watchedTopic.OnCooldown = true; watchedTopic.LastCheckedReplyCount = count.Value; watchedTopicsUpdated = true; } } } if (watchedTopicsUpdated) { ResourceLocator.HandyDataStorage.WatchedTopics.SaveData(); } if (!notifications.Any()) { return; } var dataService = (ResourceLocator.ApplicationDataService as ApplicationDataServiceService); dataService.OverridePreferenceManager(context); var allTriggeredNotifications = (ResourceLocator.ApplicationDataService[nameof(RoamingDataTypes.ReadNotifications)] ?? string.Empty) as string; var triggeredNotifications = allTriggeredNotifications?.Split(';').ToList() ?? new List <string>(); Log.Debug("MALClient", $"Checking notifications: trig: {triggeredNotifications.Count} fetched: {notifications.Count}"); //trigger new notifications foreach (var notification in notifications) { if (triggeredNotifications.Contains(notification.Id)) { continue; } triggeredNotifications.Add(notification.Id); ScheduleToast(context, notification); } //remove old triggered entries var presentNotifications = new List <string>(); foreach (var triggeredNotification in triggeredNotifications) { if (notifications.Any(notif => notif.Id == triggeredNotification)) { presentNotifications.Add(triggeredNotification); } } ResourceLocator.ApplicationDataService[nameof(RoamingDataTypes.ReadNotifications)] = string.Join(";", presentNotifications); dataService.ResetPreferenceManagerOverride(); }