Exemplo n.º 1
0
        public bool UpdateIfNeeded(Day day, Action <Day, IEnumerable <Event> > onUpdate, Action <Exception> onError)
        {
            Week week = day.Week;

            if (UpdateNeeded(week))
            {
                Update(week, events => onUpdate(day, events), onError);
                return(true);
            }
            return(false);
        }
Exemplo n.º 2
0
        public IOrderedEnumerable <Event> GetEvents(Day day, Action <Day> onUpdateStart, Action <Day, IEnumerable <Event> > onUpdateFinish, Action <Exception> onError)
        {
            var currentDayEvents = new List <Event>();

            foreach (var ev in _events)
            {
                if (ev.StartsDuring(day))
                {
                    currentDayEvents.Add(ev);
                }
            }
            Week week = day.Week;

            if (UpdateNeeded(week))
            {
                Utils.RunOnUiThread(onUpdateStart, day);
                Update(week, events => onUpdateFinish(day, events), onError);
            }
            return(currentDayEvents.OrderBy(ev => ev));
        }
Exemplo n.º 3
0
        protected override IEnumerable <Event> DownloadEvents(Week week)
        {
            var events = new List <Event>();

            var svc          = new TimetableService();
            var academicYear = week.Number >= 36 ? week.Year : week.Year - 1;
            var firstWeek    = new Week(academicYear, 36);
            var academicWeek = (int)Math.Round((week.StartTime - firstWeek.StartTime).TotalDays / 7);
            var activityUri  = new Uri("GetActivitiesByStudent?id=" + Student.Id +
                                       "&week=" + academicWeek + "&acyear=" + academicYear,
                                       UriKind.Relative);

            var activityResponse      = svc.Execute <TTActivity>(activityUri);
            var locationStaffRequests = new List <DataServiceRequest>();

            if (activityResponse != null)
            {
                foreach (TTActivity tta in activityResponse)
                {
                    var startTime = week.StartTime.AddDays(Math.Log(tta.Day) / Math.Log(2)).AddHours(tta.StartTime);
                    var duration  = TimeSpan.FromHours(tta.Duration);
                    var course    = Student.Courses.FirstOrDefault(c => tta.Name.Contains(c.CatalogNumber));
                    // If we don't know about this course, the student information needs to be updated
                    if (course == null)
                    {
                        Student.Update();
                        course = Student.Courses.FirstOrDefault(c => tta.Name.Contains(c.CatalogNumber));
                    }
                    var groups = new List <Group>();
                    if (course != null)
                    {
                        Group group;
                        if (Student.Groups.TryGetValue(course, out group) && tta.Groups.Contains(group.Identifier))
                        {
                            groups.Add(group);
                        }
                    }

                    if (course == null)
                    {
                        course = new Course(0, tta.Name, tta.Description, academicYear);
                    }

                    events.Add(new Event(tta.ID, startTime, duration, course, groups, tta.ActivityType,
                                         tta.Description, new HashSet <Location>(), new List <string>()));

                    locationStaffRequests.Add(new DataServiceRequest <TTLocation>(
                                                  new Uri("GetLocationsByActivity?id=" + tta.ID, UriKind.Relative)));

                    locationStaffRequests.Add(new DataServiceRequest <TTStaff>(
                                                  new Uri("GetStaffByActivity?id=" + tta.ID, UriKind.Relative)));
                }

                if (locationStaffRequests.Count > 0)
                {
                    var locationResponses = svc.ExecuteBatch(locationStaffRequests.ToArray());

                    foreach (QueryOperationResponse response in locationResponses)
                    {
                        int activityId = int.Parse(response.Query.RequestUri.ToString().Split('=').Last());
                        var ev         = events.Find(e => e.Id == activityId);
                        if (response.Query.ElementType == typeof(TTLocation))
                        {
                            foreach (TTLocation ttl in response)
                            {
                                ev.Locations.Add(new Location(ttl.ID, ttl.Name, ttl.InfoURL));
                            }
                        }
                        else if (response.Query.ElementType == typeof(TTStaff))
                        {
                            foreach (TTStaff tts in response)
                            {
                                ev.Staff.Add(tts.Name);
                            }
                        }
                    }
                }
            }

            return(events);
        }
Exemplo n.º 4
0
 protected abstract IEnumerable <Event> DownloadEvents(Week week);
Exemplo n.º 5
0
 public bool UpdateNeeded(Week week)
 {
     return(!UpdateLog.ContainsKey(week) || DateTime.UtcNow - UpdateLog[week] > UpdateInterval);
 }