Mobile Framework for Mobile 5.0 - Managing Control State with ControlContainer

Preceived performance is more significant then actual performance.  If a user clicks a Login button and nothing happens within the first few second they'll click it again thinking perhaps it didn't register.  If a few more seconds pass they may wonder if it locked up.  

If however the button text changes immediately showing that it is checking their credentials (against a backend database) and then a second later changes again letting the user know that it is loading data, which may be from multiple data sources (in the case of the example below Facility, Level of Care and Patient data) the perceived performance will be high.  
See video clip (no audio) 

The following ObjectContainer class allows us to easily manage a Button's state (Text and Enabled properties) by taking advantage of the IDisposable interface:

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
 
namespace Library.Mobile.Interface.Containers
{
    /// <summary>
    /// Manages object state, i.e., if T is Button then the button
    /// will be disabled and the button text will be stored.  When
    /// the control disposes the text will be restored and the button
    /// reenabled.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ObjectContainer<T> : IDisposable
    {
        public T Data { get; set; }
        public string Text { get; set; }
 
        /// <summary>
        /// Constructor - sets up T so that it can be restored on dispose
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="text">The text.</param>
        public ObjectContainer(T control, string text)
        {
            Data = control;
 
            // Cast T to object so we can work withit
            object data = Data;
 
            if (control is Button)
            {
                Text = ((Button)data).Text;     // Store button text
                ((Button)data).Enabled = false; // Disable button
                ((Button)data).Text = text;     // Set new button text
            }
            Application.DoEvents();
        }
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing,
        /// or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            // Cast T to object so we can work withit
            object data = Data;
 
            if (Data is Button)
            {
                ((Button)data).Text = Text;     // Restore button text
                ((Button)data).Enabled = true// Enable button
            }
            Application.DoEvents();
        }
    }
}

Below we'll modify the button text, as business logic dictates, and when we're done the original state will be automagically restored:

Note that on the breakpoint for line 56 that we still have our standard "Login" text for our login button. 

On the very next instruction (line 61) we see that we've changed our button text to "Verifying Login.."; reference arrows below:

With a successful login we change our button text again, on Line 73, to show that we are logged in and loading Tom's patient data:

At this point we raise the OnLoginSuccess event which is subscribed to in the main view (we are inside of a Login UserControl).   Below we show the new modal form that is loaded.  We haven't hit line 79 yet; as far is the Login control is concerned we are still on line 74.

Once OnLoginSuccess is exited (line 74 below) and we exit the scope of the using statement (Line 61 thru 78) our ObjectContainer.Dispose event will fire.  So when we hit line 79 the ObjectContainer's Dispose event will have effectively restored the button state. 

See video clip (no audio)  In this video clip you saw the button text being changed beyond the scope of what was being shown above.  Each user control (usrFacility, usrLevelOfCare and usrPatient) on the Main view is passed a PatientDataEventArgs during it's OnViewReady() event where it gets a reference to the usrLogin control and updates the login view's button text.   Below is the typical user control presenter:


Tags:
Categories:


Actions: E-mail | Permalink |  Grammar/Typo/Better way? Please let me know