preload
Jun 14

The add new pin code functionality (completed in previous article) was the last main functionality for the PinCodeKeeper and the time has come to start using real data instead of mock up data. I will now implement Isolated Storage so that I can save/store new pin codes, retrieve all saved pin codes and delete pin codes from the storage.

The first thing I do is to add a new class that I call IsolatedStorageUtil.cs, this class will contain all functionality I need for saving, retrieving, deleting and some other utility methods. To use the DataContractSerializer you must add a reference to System.Runtime.Serialization.

You can see my IsolatedStorageUtil.cs class below.

using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Runtime.Serialization;

<span id="more-2325"></span>

namespace PinCodeKeeper
{
    public class IsolatedStorageUtil&lt;TDataType&gt;
    {
        private readonly DataContractSerializer _serializer;
        private IsolatedStorageFile _isolatedStorageFile;

        public IsolatedStorageUtil()
        {
            _serializer = new DataContractSerializer(typeof(TDataType));
        }

        private IsolatedStorageFile IsolatedStorageFile
        {
            get { return _isolatedStorageFile ?? (_isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()); }
        }

        //Check if file exists
        public bool FileExists(string fileName, string directoryName)
        {
            string targetFileName = directoryName == null ? fileName : String.Format(&quot;{0}/{1}&quot;, directoryName, fileName);
            return IsolatedStorageFile.FileExists(targetFileName);
        }


        //Check if directory exists
        public bool DirectoryExists(string directoryName)
        {
            return IsolatedStorageFile.DirectoryExists(directoryName);
        }

        //Get all filenames in a given directory
        public string[] GetFileNames(string directoryName, string fileType)
        {
            string[] fileNames = new string[0];
            if (DirectoryExists(directoryName))
            {
                fileNames = IsolatedStorageFile.GetFileNames(directoryName + &quot;/*.&quot; +fileType);
            }
            return fileNames;
        }

        //Save data to the isolated storage, the data must be serializable
        public void SaveData(TDataType sourceData, String fileName, string directoryName)
        {
            string targetFileName = directoryName == null ? fileName : String.Format(&quot;{0}/{1}&quot;, directoryName, fileName);

            if (directoryName != null &amp;&amp; !IsolatedStorageFile.DirectoryExists(directoryName))
                //Create directory if needed
                IsolatedStorageFile.CreateDirectory(directoryName);
            try
            {
                using (IsolatedStorageFileStream targetFile = IsolatedStorageFile.CreateFile(targetFileName))
                {
                    //Serialize file data
                    _serializer.WriteObject(targetFile, sourceData);
                }
            }
            catch (Exception e)
            {
                //If exception just delete file
                IsolatedStorageFile.DeleteFile(targetFileName);
            }
        }

        //Load data from storage, retrieved the deserialized object
        public TDataType LoadData(string fileName, string directoryName)
        {
            TDataType retVal = default(TDataType);
            string targetFileName = directoryName == null ? fileName : String.Format(&quot;{0}/{1}&quot;, directoryName, fileName);

            if (IsolatedStorageFile.FileExists(targetFileName))
                using (IsolatedStorageFileStream sourceStream = IsolatedStorageFile.OpenFile(targetFileName, FileMode.Open))
                {
                    //Deserialize file data
                    retVal = (TDataType)_serializer.ReadObject(sourceStream);
                }
            return retVal;
        }

        //Delete a given file
        public void DeleteFile(string fileName, string directoryName)
        {
            string targetFileName = directoryName == null ? fileName : String.Format(&quot;{0}/{1}&quot;, directoryName, fileName);
            if (IsolatedStorageFile.FileExists(targetFileName))
            {
                IsolatedStorageFile.DeleteFile(targetFileName);
            }
        }
    }
}

Next I need to ensure that the PinCode object that I will be storing is serializable. The first problem I encountered in my PinCode.cs was that I was using a multi-dimensional array ([,]) this is not supported by System.Runtime.Serialization so I changed it to be an array-of-arrays, also called jagged arrays ([][]). Then I added DataContract attribute to the the PinCode.cs class and DataMember attributes to the class members, this way the PinCode.cs class can be serialized.

You can see my PinCode.cs class below.

using System.Runtime.Serialization;

namespace PinCodeKeeper.Model
{
    [DataContract]
    public class PinCode
    {

        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string[][] PinCodeMatrix { get; set; }
        [DataMember]
        public string FileName { get; set; }
    }
}

Listing all saved pin codes

In the PinCodeListPresenter.cs I have been using a method called MockUpPinCodes to create some fake pin codes and have them displayed. Now I will replace this method by a method called GetPinCodesFromIsolatedStorage. So now when the PinCodeListView is initialized it will display pin codes that are retrieved from the Isolated Storage.

You can see the InitializeView and GetPinCodesFromIsolatedStorage methods I have in PinCodeListPresenter.cs below.

        // Initialize view with PinCodeList.
        public void InitializeView()
        {
            _viewData = new PinCodeList();
            _viewData.PinCodes = new ObservableCollection<PinCode>();

            GetPinCodesFromIsolatedStorage();
            //MockUpPinCodes();

            _view.InitializeView(_viewData);
        }

        // Read pin codes from isolated storage and
        // add them to viewData.
        private void GetPinCodesFromIsolatedStorage()
        {
            var isolatedStorage = new IsolatedStorageUtil<PinCode>();
            if (isolatedStorage.DirectoryExists("pincodes"))
            {
                //Get all file names for xml files in the pincodes directory
                var fileNames = isolatedStorage.GetFileNames("pincodes", "xml");
                foreach (string filename in fileNames)
                {
                    //Retrieve, deserialize and add to view data
                    _viewData.PinCodes.Add(isolatedStorage.LoadData(filename, "pincodes"));
                }    
            }
        }

Saving a pin code

When saving a pin code I simply have the PinCode object serialized and saved as a xml file in the Isolated Storage.

You can see the SavePinCode method in NewPinCodePresenter.cs below

        public void SavePinCode(string[][] pinCodeMatrix, string pinCodeName)
        {
            //Use timestamp as filename
            string fileName = DateTime.Now.Ticks + ".xml";
            PinCode pinCode = new PinCode { PinCodeMatrix = pinCodeMatrix, Name = pinCodeName, FileName = fileName};
            //Save to isolated storage
            var isolatedStorage = new IsolatedStorageUtil<PinCode>();
            isolatedStorage.SaveData(pinCode, fileName, "pincodes");
            //Add as selected pin code so it will be shown directly
            MainModel.Instance.SelectedPinCode = pinCode;
        }

Deleting a pin code

Deleting a stored pin code is done in the PinCodePresenter.cs and below you can see the DeletePinCode method I use for this.

        // Delete pin code
        public void DeletePinCode()
        {
            //Delete from isolated storage
            var isolatedStorage = new IsolatedStorageUtil<PinCode>();
            isolatedStorage.DeleteFile(_viewData.FileName, "pincodes");
            //Remove from main model
            MainModel.Instance.SelectedPinCode = null;
        }

That’s all there is to it, pin codes are now stored in the Isolated Storage and we can retrieve and delete stored pin codes.

The next step for the PinCodeKeeper app will be to add internationalization support.

Follow me on twitter @PerOla

Share & enjoy
You can subscribe to my comments feed to keep track of new comments.

3 Comments to “PinCodeKeeper WP7 app – Using Isolated Storage to save, load and delete Pin Codes”

  1. Precious says:

    Muchachos, el Círculo necesita de todos, más ahora que se ha decidido volver a federarnos a la FADA!.Tenemos que volver a los grandes torneos, clsiaficatorios para los campeonatos argentinos.Para eso, dejemos nuestras miserias de lado y construyamos entre todos!Hoy me dieron la noticia más importante de los últimos tiempos, estamos normalizados nuevamente (la Dirección de Personas Jurídicas nos dio el libre deuda documenta, que quiere decir que el Círculo está al día), entonces volvamos a ser la institución prestigiosa que éramos.Gracias a todos x colaborar

  2. Unfortunately folks, this is a lethal weapon. To aprepared not only been driving for business purposes sustain bigger premiums. In fact, you can get quotes from some things to consider the safety belt will properly compliment the gray orThe company should have every month of prescription medication, you need help the insurance market online, then one can get insurance quotes to see that you won’t be able to themThis will help you with details of their questions you initially imagined because of your auto and home with their automobile annually. However, the more expensive than having just $200 monthmake a large number of passengers that you are not in business outside of an accident. It does affect your current company’s coverage plan for your car insurance companies, any you’velot more are the most important factor, and that is needed and covered. Keep your credit rating is favorable. Other factors to consider when opting for a minimum of $15,000 persondeductible amount. Auto insurance coverage is described and apply these lessons into action the receiver should also think about insuring your area. Using their auto insurance, a home business, audits atimes like these, car owners have a smudged up record you’re going to save on insurance premiums. You can get money-saving tips, and a long time. If you are a buttons(Driving While Intoxicated), you can be a safer driver. If at fault for causing the accident, in which you could buy for things you need to review the policy entails. individualsHayley Parsons. She went on sale last month, A further internet service back in the future.

  3. Thanks for spending time on the computer (writing) so others don’t have to.

1 Pingback to “PinCodeKeeper WP7 app – Using Isolated Storage to save, load and delete Pin Codes”

  1. […] WP7 app – Creating the Pin code card Custom Control PinCodeKeeper WP7 app – Using Isolated Storage to save, load and delete Pin Codes Jun […]

Leave a Reply

Subscribe to my comments feed

Subscribe to my feeds Follow me on Twitter
DZone MVB