ScheduleWidget Tutorial 3

Tags: ScheduleWidget

This is the third in a series of tutorials on ScheduleWidget, a scheduling engine that handles recurring events for calendars. As with the previous tutorials we're going to use the Critical Mass bicycle event. This event happens in many major cities on the last Friday of every month. We looked at the Occurrences() method and the dates the engine generates. Now let's take a quick look at two other related methods:

  • NextOccurrence
  • PreviousOccurrence

As before create an Empty MVC 3 web project. Add a HomeController and paste this code for the controller class:

public class HomeController : Controller
{
    // using ScheduleWidget.Enums;
    // using ScheduleWidget.ScheduledEvents;
    public ActionResult Index()
    {
        var aEvent = CreateEvent();
        var schedule = new Schedule(aEvent);
        var range = new DateRange()
        {
            StartDateTime = DateTime.Now,
            EndDateTime = DateTime.Now.AddMonths(4)
        };

        var sb = new StringBuilder();
        foreach(var aDate in schedule.Occurrences(range))
        {
            var previousDate = schedule
                .PreviousOccurrence(aDate)
                .GetValueOrDefault(DateTime.Now);

            var nextDate = schedule
                .NextOccurrence(aDate)
                .GetValueOrDefault(DateTime.Now);

            sb.Append(string.Format(
                "Previous Date: {0}<br />", 
                previousDate.ToShortDateString()));

            sb.Append(string.Format(
                "<strong>Current Date: {0}</strong><br />", 
                aDate.ToShortDateString()));

            sb.Append(string.Format(
                "Next Date: {0}<br />", 
                nextDate.ToShortDateString()));

            sb.Append("<p />");
        }

        ViewBag.Data = sb.ToString();
        return View();
    }

    private static Event CreateEvent()
    {
        return new Event()
        {
            ID = 1,
            Title = "Critical Mass",
            FrequencyTypeOptions = FrequencyTypeEnum.Monthly,
            MonthlyIntervalOptions = MonthlyIntervalEnum.Last,
            DaysOfWeekOptions = DayOfWeekEnum.Fri,
        };
    }
}

Add an Index.cshtml view and paste this code to display the dates:

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@Html.Raw(ViewBag.Data)

Run the app and you should see the date display. Here's mine as of the publication of this blog post:

So what's going on here? The event and schedule objects are created the same as in previous tutorials. Take a look at the foreach loop. Here's I've passed into the schedule a four-month date range in order to get some dates out of the engine. Then for each occurrence I call PreviousOccurrence() and NextOccurrence() to get those dates as well. Finally, all three are formatted for display on the UI. 

There's another method in a schedule called IsOccurring() which takes any date and returns true if that date is on the schedule or false if it is not. For example:

if (schedule.IsOccurring(DateTime.Today))
{
    Debug.WriteLine("Saddle up for critical mass!");
}
else
{
    Debug.WriteLine("No bike ride today...");
}

Pretty straightforward stuff. And that pretty much covers ScheduleWidget. When I have time I'll make another blog post to illustrate how ScheduleWidget works hand-in-glove with FullCalendar, an awesome JQuery plugin for displaying a full-size calendar.