Ejemplo n.º 1
0
    public bool updateData() {
      SQLiteConnection connection = DependencyService.Get<IDatabaseHandler>().getDBConnection();
      Info cacheInfo = connection.Table<Info>().Where(i => i.key.Equals("eventCacheTime")).FirstOrDefault();
      long lastCacheTime = cacheInfo.value;
      long currentTime = (long)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds;

      if (currentTime - lastCacheTime > 1000 * 60 * 60 * 6) {
        string html;

        Task<string> task = new HttpClient().GetStringAsync(new Uri("http://harrisburg.psu.edu/calendar"));
        task.Wait();
        html = task.Result;

        for (int i = 1; i < 9; i++) {
          Task<string> task2 = new HttpClient().GetStringAsync(new Uri("http://harrisburg.psu.edu/calendar?page=" + i));
          task2.Wait();
          html += task2.Result;
        }

        string regex = @"views-row-\d.*?<span class=""date-display-start""><span>(\w{3})\s(\d{4}).*?<\/time>\s*(<span class=""separator"">.*?<div class=""clr""><\/div>|<div class=""clr""><\/div>)<\/span>.*?<span class=""field-content""><a href=""([\/\w\d\-]*)"">(.*?)<\/a><\/span>";

        List<Event> events = new List<Event>();
        MatchCollection matches = Regex.Matches(html, regex, RegexOptions.Singleline);

        foreach (Match m in matches) {
          Event e = new Event(System.Net.WebUtility.HtmlDecode(m.Groups[5].Value), m.Groups[1].Value, m.Groups[2].Value, "http://harrisburg.psu.edu" + m.Groups[4].Value);

          if (!events.Contains(e))
            events.Add(e);
        }

        connection.BeginTransaction();
        connection.DeleteAll<Event>();

        foreach (Event e in events) {
          connection.Insert(e);
        }

        cacheInfo.value = currentTime; //Not entirely accurate, but will work alright
        connection.Update(cacheInfo);
        connection.Commit();
        connection.Close();
        updateData();

      } else if (event_picker.Items.Count == 0) {

        var eventEntries = connection.Table<Event>();

        event_picker.Items.Clear();

        foreach (var e in eventEntries) {
          string month = e.month + " " + e.year;

          if (!event_picker.Items.Contains(month))
            event_picker.Items.Add(month);
        }

        connection.Close();

        string now = numberToMonth(DateTime.Now.Month) + " " + DateTime.Now.Year;

        for (int i = 0; i < event_picker.Items.Count; i++) {
          if (event_picker.Items[i].Equals(now)) {
            event_picker.SelectedIndex = i;
            break;
          }
        }

        if (event_picker.SelectedIndex == -1 && event_picker.Items.Count > 0)
          event_picker.SelectedIndex = 0;
      }

      return false;
    }
Ejemplo n.º 2
0
 public EventCell(Event evnt){
   this.evnt = evnt;
   Title = evnt.name;
   Color = Theme.getTextColor();
 }