Skip to content

Xamarin IDataStorage interface

wvdvegt edited this page Apr 5, 2018 · 4 revisions

Portable part:

public class Bridge : IDataStorage 
{
    #region IDataStorage Members

    IDataStorage dataStorage = DependencyService.Get<IDataStorage>();

    /// <summary>
    /// Exists the given file.
    /// </summary>
    ///
    /// <param name="fileId"> The file identifier to delete. </param>
    ///
    /// <returns>
    /// true if it succeeds, false if it fails.
    /// </returns>
    public bool Exists(string fileId)
    {
        return dataStorage.Exists(fileId);
    }

    /// <summary>
    /// Gets the files.
    /// </summary>
    ///
    /// <returns>
    /// An array of filenames.
    /// </returns>
    public String[] Files()
    {
        return dataStorage.Files();
    }

    /// <summary>
    /// Saves the given file.
    /// </summary>
    ///
    /// <param name="fileId">   The file identifier to delete. </param>
    /// <param name="fileData"> Information describing the file. </param>
    public void Save(string fileId, string fileData)
    {
        dataStorage.Save(fileId, fileData);
    }

    /// <summary>
    /// Loads the given file.
    /// </summary>
    ///
    /// <param name="fileId"> The file identifier to delete. </param>
    ///
    /// <returns>
    /// A String.
    /// </returns>
    public string Load(string fileId)
    {
        return dataStorage.Load(fileId);
    }

    /// <summary>
    /// Deletes the given fileId.
    /// </summary>
    ///
    /// <param name="fileId"> The file identifier to delete. </param>
    ///
    /// <returns>
    /// true if it succeeds, false if it fails.
    /// </returns>
    public bool Delete(string fileId)
    {
        return dataStorage.Delete(fileId);
    }

    #endregion
} 

Android part:

public class Bridge_Android : IDataStorage, IDataArchive
{
    /// <summary>
    /// Creates path to file.
    /// </summary>
    ///
    /// <param name="filename"> Filename of the file. </param>
    ///
    /// <returns>
    /// The new path to file.
    /// </returns>
    private string CreatePathToFile(string filename)
    {
        string docsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string path = Path.Combine(docsPath, "DataStorage");

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        return Path.Combine(path, filename);
    }

    /// <summary>
    /// Creates path to archive.
    /// </summary>
    ///
    /// <param name="filename"> Filename of the file. </param>
    ///
    /// <returns>
    /// The new path to archive.
    /// </returns>
    private string CreatePathToArchive(string filename)
    {
        string docsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string path = Path.Combine(docsPath, "DataArchive");

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        return Path.Combine(path, filename);
    }

    /// <summary>
    /// Deletes the given fileId.
    /// </summary>
    ///
    /// <param name="fileId"> The file identifier to delete. </param>
    ///
    /// <returns>
    /// true if it succeeds, false if it fails.
    /// </returns>
    public bool Delete(string fileId)
    {
        string path = CreatePathToFile(fileId);

        if (File.Exists(path))
        {
            File.Delete(path);
            return true;
        }
        return false;
    }

    /// <summary>
    /// Determine if 'fileId' exists.
    /// </summary>
    ///
    /// <param name="fileId"> Identifier for the file. </param>
    ///
    /// <returns>
    /// true if it succeeds, false if it fails.
    /// </returns>
    public bool Exists(string fileId)
    {
        string path = CreatePathToFile(fileId);

        return File.Exists(path);
    }

    /// <summary>
    /// Gets the files.
    /// </summary>
    ///
    /// <returns>
    /// An array of filenames.
    /// </returns>
    public String[] Files()
    {
        string path = CreatePathToFile(String.Empty);

        return Directory.GetFiles(path).ToList().ConvertAll(new Converter<String, String>(
                        p => p.Replace(path + Path.DirectorySeparatorChar, "")
                    )).ToArray();
    }

    /// <summary>
    /// Loads the given file.
    /// </summary>
    ///
    /// <param name="fileId"> The file identifier to load. </param>
    ///
    /// <returns>
    /// A string.
    /// </returns>
    public string Load(string fileId)
    {
        string path = CreatePathToFile(fileId);

        using (StreamReader sr = File.OpenText(path))
        {
            return sr.ReadToEnd();
        }
    }

    /// <summary>
    /// Saves the given file.
    /// </summary>
    ///
    /// <param name="fileId">   Identifier for the file. </param>
    /// <param name="fileData"> Information describing the file. </param>
    public void Save(string fileId, string fileData)
    {
        string path = CreatePathToFile(fileId);
        using (StreamWriter sw = File.CreateText(path))
        {
            sw.Write(fileData);
        }
    }
}

iOS Part:

public class Bridge_iOS : IDataStorage, IDataArchive
{
    /// <summary>
    /// Creates path to file.
    /// </summary>
    ///
    /// <param name="filename"> Filename of the file. </param>
    ///
    /// <returns>
    /// The new path to file.
    /// </returns>
    private string CreatePathToFile(string filename)
    {
        string docsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string path = Path.Combine(docsPath, "DataStorage");

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        return Path.Combine(path, filename);
    }

    /// <summary>
    /// Creates path to archive.
    /// </summary>
    ///
    /// <param name="filename"> Filename of the file. </param>
    ///
    /// <returns>
    /// The new path to archive.
    /// </returns>
    private string CreatePathToArchive(string filename)
    {
        string docsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        string path = Path.Combine(docsPath, "DataArchive");

        if (!Directory.Exists(path))
        {
            Directory.CreateDirectory(path);
        }

        return Path.Combine(path, filename);
    }

    /// <summary>
    /// Deletes the given fileId.
    /// </summary>
    ///
    /// <param name="fileId"> The file identifier to delete. </param>
    ///
    /// <returns>
    /// true if it succeeds, false if it fails.
    /// </returns>
    public bool Delete(string fileId)
    {
        string path = CreatePathToFile(fileId);

        if (File.Exists(path))
        {
            File.Delete(path);
            return true;
        }
        return false;
    }

    /// <summary>
    /// Determine if 'fileId' exists.
    /// </summary>
    ///
    /// <param name="fileId"> Identifier for the file. </param>
    ///
    /// <returns>
    /// true if it succeeds, false if it fails.
    /// </returns>
    public bool Exists(string fileId)
    {
        string path = CreatePathToFile(fileId);

        return File.Exists(path);
    }

    /// <summary>
    /// Gets the files.
    /// </summary>
    ///
    /// <returns>
    /// An array of filenames.
    /// </returns>
    public String[] Files()
    {
        string path = CreatePathToFile(String.Empty);

        return Directory.GetFiles(path).ToList().ConvertAll(new Converter<String, String>(
                        p => p.Replace(path + Path.DirectorySeparatorChar, "")
                    )).ToArray();
    }

    /// <summary>
    /// Loads the given file.
    /// </summary>
    ///
    /// <param name="fileId"> The file identifier to load. </param>
    ///
    /// <returns>
    /// A string.
    /// </returns>
    public string Load(string fileId)
    {
        string path = CreatePathToFile(fileId);

        using (StreamReader sr = File.OpenText(path))
        {
            return sr.ReadToEnd();
        }
    }

    /// <summary>
    /// Saves the given file.
    /// </summary>
    ///
    /// <param name="fileId">   Identifier for the file. </param>
    /// <param name="fileData"> Information describing the file. </param>
    public void Save(string fileId, string fileData)
    {
        string path = CreatePathToFile(fileId);
        using (StreamWriter sw = File.CreateText(path))
        {
            sw.Write(fileData);
        }
    }
}

Clone this wiki locally