HowsMyFuel Step 2: Time Display

Ok, so having created the basic app with a map view using TDD, the next step is to add a time display. Creating a time display is really easy in iOS. Doing it using TDD and a well planned architecture maybe not so easy.

Time events and information may be needed in several places in the app. In addition to simply displaying the current time, I may need time information to determine if/when I’ve stopped at a gas station. So I probably need a time service to provide time notifications than can be observed wherever needed. I could sprinkle NSTimer and NSDate code around the app wherever time info is needed, but that would be ugly, and violate the DRY principle (“Don’t Repeat Yourself”). In a TDD/unit tested environment, having a single copy of well tested code becomes even more of a benefit.

So I’m going to create a model object (eg MVC) to generate time notifications every second.

Next question: I’m going to need time formatted into a text string for display. Where should we put the text string formatting code? We’re trying to keep the ViewController thin, so that means moving it up into the Presenter (since it is UI related), or over into the Time model. So I ask myself the question: “In the future, when I’ve long forgotten how I implemented this code, where will I look for it?” In this case, there isn’t a clear answer. The Time model will generate notifications which the Presenter will observe, and the Presenter will then send a message to the ViewController to display/update it’s time display. The Time model needs to understand the NSTimer and NSDate classes, so maybe if I do the formatting in the Time model, I can relieve other objects from having to. So I’m going to include a formatted time string in the notifications that the Time model broadcasts. If more control, or multiple formats are need in the future, then I can easily extend the Time model to do so. For now, we’ll stick with the TDD concept of doing the minimum thing.

So to follow along in the code, refer to the Commit log on Github starting April 27, 2013.

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.