Exemplo n.º 1
0
        public static string GenerateIcs(Model.Schedule schedule)
        {
            int      dayCount = schedule.Count * 7;
            DateTime firstDay = RuntimeData.StartDate;
            Calendar calendar = new Calendar();

            for (int i = 0; i < dayCount; i++)
            {
                DateTime date = firstDay.AddDays(i);
                foreach (Model.ScheduleEntry e in schedule.GetDaySchedule(i))
                {
                    (var start, var end) = SessionTimeConverter.ConvertShort(e.SessionSpan, CampusSelector.IsCampusD(e.Room));
                    CalendarEvent calEvent = new CalendarEvent()
                    {
                        DtStart     = new CalDateTime(new DateTime(date.Year, date.Month, date.Day, start.Hour, start.Minute, 0)),
                        DtEnd       = new CalDateTime(new DateTime(date.Year, date.Month, date.Day, end.Hour, end.Minute, 0)),
                        Location    = e.Room,
                        Summary     = e.Name,
                        Description = $"教师: {e.Lecturer}",
                        Alarms      =
                        {
                            new Alarm()
                            {
                                Trigger = new Trigger(new TimeSpan(0, -15, 0))
                            }
                        }
                    };
                    calendar.Events.Add(calEvent);
                }
            }
            var serializer = new Ical.Net.Serialization.CalendarSerializer();

            return(serializer.SerializeToString(calendar));
        }
Exemplo n.º 2
0
        public static void ScheduleToast(List <Model.ScheduleEntry> entries)
        {
            bool courseSwitch = true;
            bool dailySwitch  = true;

            if (RuntimeData.LoadSetting("courseToastSwitch", out string _cSwitch) == false)
            {
                RuntimeData.SaveSetting("courseToastSwitch", "on");
            }
            else
            {
                if (_cSwitch == "off")
                {
                    courseSwitch = false;
                }
            }
            if (RuntimeData.LoadSetting("dailyToastSwitch", out string _dSwitch) == false)
            {
                RuntimeData.SaveSetting("dailyToastSwitch", "on");
            }
            else
            {
                if (_dSwitch == "off")
                {
                    dailySwitch = false;
                }
            }


            if (entries.Count == 0)
            {
                return;
            }

            ToastContent dailyGlance = new ToastContent()
            {
                Visual = new ToastVisual()
                {
                    BindingGeneric = new ToastBindingGeneric()
                    {
                        Children =
                        {
                            new AdaptiveText()
                            {
                                Text = "今日课程", HintStyle = AdaptiveTextStyle.Subtitle
                            }
                        }
                    }
                }
            };

            for (int i = 0; i < entries.Count; i++)
            {
                (var startTimeSch, _) = SessionTimeConverter.ConvertShort(entries[i].SessionSpan, CampusSelector.IsCampusD(entries[i].Room));
                DateTime     startTime          = startTimeSch.GetDateTime();
                ToastContent courseNotification = new ToastContent()
                {
                    Visual = new ToastVisual()
                    {
                        BindingGeneric = new ToastBindingGeneric()
                        {
                            Children =
                            {
                                new AdaptiveText()
                                {
                                    Text = entries[i].Name
                                },
                                new AdaptiveText()
                                {
                                    Text = $"{new SessionTimeConverter().Convert(entries[i].SessionSpan, typeof(string), null, null)}  {entries[i].Room}"
                                },
                            }
                        }
                    },
                    Scenario = ToastScenario.Reminder
                };

                dailyGlance.Visual.BindingGeneric.Children.Add
                (
                    new AdaptiveGroup()
                {
                    Children =
                    {
                        new AdaptiveSubgroup()
                        {
                            Children =
                            {
                                new AdaptiveText()
                                {
                                    Text = entries[i].Name, HintStyle = AdaptiveTextStyle.Base
                                },
                                new AdaptiveText()
                                {
                                    Text = $"{new SessionTimeConverter().Convert(entries[i].SessionSpan, typeof(string), null, null)}  {entries[i].Room}", HintStyle = AdaptiveTextStyle.BodySubtle
                                },
                            }
                        }
                    }
                }
                );
                try
                {
                    if (courseSwitch)
                    {
                        ScheduledToastNotification toast = new ScheduledToastNotification(courseNotification.GetXml(), new DateTimeOffset(startTime.AddMinutes(-15), new TimeSpan(8, 0, 0)));
                        toast.ExpirationTime = new DateTimeOffset(startTime.AddMinutes(15), TimeZoneInfo.Local.GetUtcOffset(DateTime.Now));
                        ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);
                    }
                }
                catch (ArgumentException) { }
            }

            if (dailySwitch)
            {
                if (DateTime.Now.Hour > 7 || (DateTime.Now.Hour == 7 && DateTime.Now.Minute > 25))
                {
                    ToastNotification dailyGlanceToast = new ToastNotification(dailyGlance.GetXml());
                    dailyGlanceToast.ExpirationTime = DateTime.Now.AddDays(1);
                    ToastNotificationManager.CreateToastNotifier().Show(dailyGlanceToast);
                }
                else
                {
                    ScheduledToastNotification dailyGlanceToast = new ScheduledToastNotification(dailyGlance.GetXml(),
                                                                                                 new DateTimeOffset(new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 7, 30, 0), TimeZoneInfo.Local.GetUtcOffset(DateTime.Now)));
                    dailyGlanceToast.ExpirationTime = DateTime.Now.AddDays(1);
                    ToastNotificationManager.CreateToastNotifier().AddToSchedule(dailyGlanceToast);
                }
            }
        }
Exemplo n.º 3
0
        public static async Task UpdateTile(Model.Schedule schedule)
        {
            TileUpdater updater = TileUpdateManager.CreateTileUpdaterForApplication();

            if (schedule == null)
            {
                updater.Clear();
                return;
            }
            //Windows.Foundation.Diagnostics.LoggingChannel lc = new Windows.Foundation.Diagnostics.LoggingChannel("UCQU_BackgroundTaskPayload", null, new Guid("4bd2826e-54a1-4ba9-bf63-92b73ea1ac4a"));
            //lc.LogMessage("Entered Payload Method.");
            //lc.LogMessage("Tile Updated Created.");
            string id = "", pwdHash = "";

            if (Login.LoadCredentials(out id, out pwdHash) == false)
            {
                //lc.LogMessage("Credential Load Failed. Clearing tile and exiting...");
                updater.Clear();
                return;
            }

            TileContent content = null;
            //lc.LogMessage("Obtaining Schedule.");
            //lc.LogMessage("Getting Schedule of Today.");
            List <Model.ScheduleEntry> entries = schedule.GetDaySchedule((DateTime.Today /*CommonResources.TestDate*/ - RuntimeData.StartDate).Days).ToList();

            entries.RemoveAll(x =>
            {
                (_, var endTime) = SessionTimeConverter.ConvertShort(x.SessionSpan, CampusSelector.IsCampusD(x.Room));
                return(endTime.GetDateTime() < DateTime.Now);
            });

            entries.Sort((x, y) => x.StartSlot - y.StartSlot);

            //lc.LogMessage("Constructing Tile Content.");
            TileBindingContentAdaptive midTileContent   = new TileBindingContentAdaptive();
            TileBindingContentAdaptive wideTileContent  = new TileBindingContentAdaptive();
            TileBindingContentAdaptive largeTileContent = new TileBindingContentAdaptive();

            if (entries.Count > 0)
            {
                if (!IsToastScheduled)
                {
                    try
                    {
                        ScheduleToast(entries);
                    }
                    catch (Exception ex) { }
                    IsToastScheduled = true;
                }

                //lc.LogMessage("Course Detected. Creating daily schedule tile.");
                foreach (Model.ScheduleEntry e in entries)
                {
                    (var start, var end) = SessionTimeConverter.ConvertShort(e.SessionSpan, CampusSelector.IsCampusD(e.Room));

                    AdaptiveGroup midGroup = new AdaptiveGroup()
                    {
                        Children =
                        {
                            new AdaptiveSubgroup()
                            {
                                Children =
                                {
                                    new AdaptiveText()
                                    {
                                        Text      = e.Name,
                                        HintStyle = AdaptiveTextStyle.Caption,
                                        HintWrap  = true
                                    },
                                    new AdaptiveText()
                                    {
                                        Text      = $"{start}-{end}  {e.Room}",
                                        HintStyle = AdaptiveTextStyle.CaptionSubtle
                                    },
                                }
                            }
                        }
                    };
                    AdaptiveGroup wideGroup = new AdaptiveGroup()
                    {
                        Children =
                        {
                            new AdaptiveSubgroup()
                            {
                                Children =
                                {
                                    new AdaptiveText()
                                    {
                                        Text      = e.Name,
                                        HintStyle = AdaptiveTextStyle.Caption,
                                    },
                                    new AdaptiveText()
                                    {
                                        Text      = $"{start}-{end}  {e.Room}",
                                        HintStyle = AdaptiveTextStyle.CaptionSubtle
                                    },
                                }
                            }
                        }
                    };
                    AdaptiveGroup largeGroup = new AdaptiveGroup()
                    {
                        Children =
                        {
                            new AdaptiveSubgroup()
                            {
                                Children =
                                {
                                    new AdaptiveText()
                                    {
                                        Text      = e.Name,
                                        HintStyle = AdaptiveTextStyle.Base,
                                        HintWrap  = true
                                    },
                                    new AdaptiveText()
                                    {
                                        Text      = $"{start}-{end}  {e.Room}",
                                        HintStyle = AdaptiveTextStyle.CaptionSubtle
                                    },
                                    new AdaptiveText()
                                    {
                                        HintStyle = AdaptiveTextStyle.Caption
                                    }
                                }
                            }
                        }
                    };
                    midTileContent.Children.Add(midGroup);
                    wideTileContent.Children.Add(wideGroup);
                    largeTileContent.Children.Add(largeGroup);
                }
                //lc.LogMessage("Assigning Tile Content.");
                content = new TileContent()
                {
                    Visual = new TileVisual()
                    {
                        Branding  = TileBranding.Name,
                        TileLarge = new TileBinding()
                        {
                            Content = largeTileContent
                        },
                        TileMedium = new TileBinding()
                        {
                            Content = midTileContent
                        },
                        TileWide = new TileBinding()
                        {
                            Content = wideTileContent
                        },
                    }
                };
            }
            else
            {
                //lc.LogMessage("No Course Detected. Creating image tile.");
                //lc.LogMessage("Getting Image Entries Metadata.");
                List <HuxiImgEntry> huxiImgEntries = await HuxiImg.GetEntries();

                //lc.LogMessage("Creating Randomizer.");
                Random randomizer = new Random((int)DateTime.Now.Ticks);
                int    index      = randomizer.Next(0, huxiImgEntries.Count);

                if (!IsToastScheduled)
                {
                    try
                    {
                        ScheduleToast(huxiImgEntries[index]);
                    }
                    catch (Exception ex) { }
                    IsToastScheduled = true;
                }
                //lc.LogMessage("Randomly Selected Index: " + index);
                //lc.LogMessage("Constructing Image Tile.");
                largeTileContent.BackgroundImage = new TileBackgroundImage()
                {
                    Source      = huxiImgEntries[index].Uri,
                    HintOverlay = 40
                };
                largeTileContent.Children.Add
                (
                    new AdaptiveText()
                {
                    Text      = huxiImgEntries[index].Title,
                    HintStyle = AdaptiveTextStyle.Base
                }
                );
                largeTileContent.Children.Add
                (
                    new AdaptiveText()
                {
                    Text      = huxiImgEntries[index].Author,
                    HintStyle = AdaptiveTextStyle.CaptionSubtle
                }
                );
                //lc.LogMessage("Assigning Tile Content.");
                content = new TileContent()
                {
                    Visual = new TileVisual()
                    {
                        Branding  = TileBranding.Name,
                        TileLarge = new TileBinding()
                        {
                            Content = largeTileContent
                        },
                        TileMedium = new TileBinding()
                        {
                            Content = largeTileContent
                        },
                        TileWide = new TileBinding()
                        {
                            Content = largeTileContent
                        },
                    }
                };
            }
            //lc.LogMessage("Creating Tile Notification.");
            var notification = new TileNotification(content.GetXml());

            //lc.LogMessage("Setting Expiry Time.");
            notification.ExpirationTime = DateTimeOffset.UtcNow.AddMinutes(60);
            //lc.LogMessage("Clearing Existing Tiles");
            updater.Clear();
            //lc.LogMessage("Updating Tiles");
            TileUpdateManager.CreateTileUpdaterForApplication().Update(notification);
            //lc.LogMessage("Payload Method Complete.");
        }