D41d8cd98f00b204e9800998ecf8427e

What I'm doing is updating three separate text controls (for hours, minutes, and seconds) for a countdown timer. I'd like to keep the separate text controls, but otherwise this code looks a tad ugly. Any ideas on how to reduce this? (Note: m_TimerStartTick holds the time for when the time was started, and GetTimerInterval() returns the countdown interval, i.e. if I want to countdown from an hour, it returns a wxTimeSpan of an hour.)

void MainDialog::OnTickTimer(wxTimerEvent& WXUNUSED(e))
{
    const wxLongLong ElapsedTime     = wxGetLocalTime() - m_TimerStartTick;
    const wxTimeSpan ConvElapsedTime = wxTimeSpan(0, 0, ElapsedTime.ToLong(), 0);
    const wxTimeSpan TimeLeft        = GetTimerInterval() - ConvElapsedTime;

    const int  TotalHours   = TimeLeft.GetHours()   % 24;
    const int  TotalMinutes = TimeLeft.GetMinutes() % 60;
    const long TotalSeconds = TimeLeft.GetSeconds().ToLong() % 60;
    HoursTextCtrl  ->SetValue(wxString::Format("%.2i",  TotalHours  ));
    MinutesTextCtrl->SetValue(wxString::Format("%.2i",  TotalMinutes));
    SecondsTextCtrl->SetValue(wxString::Format("%.2li", TotalSeconds));
    if (TimeLeft.IsNull())
    {
        ChangeDeskBg();
        m_TimerStartTick = wxGetLocalTime();
    }
}

Refactorings

No refactoring yet !

D41d8cd98f00b204e9800998ecf8427e

Jake

November 20, 2010, November 20, 2010 05:00, permalink

No rating. Login to rate!

For a quick perk

if !(TimeLeft.IsNull()) return;    

ChangeDeskBg();
m_TimerStartTick = wxGetLocalTime();
D41d8cd98f00b204e9800998ecf8427e

Jake

November 20, 2010, November 20, 2010 05:01, permalink

No rating. Login to rate!

For a quick perk

if !(TimeLeft.IsNull()) return;    

ChangeDeskBg();
m_TimerStartTick = wxGetLocalTime();

Your refactoring





Format Copy from initial code

or Cancel