Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Created June 6, 2016 12:26
Show Gist options
  • Select an option

  • Save BrianJVarley/cdbc2361712ca33e40c91b361cdb8f10 to your computer and use it in GitHub Desktop.

Select an option

Save BrianJVarley/cdbc2361712ca33e40c91b361cdb8f10 to your computer and use it in GitHub Desktop.
using CsvHelper;
using Parking_Tag_Picker_WRT.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Storage;
namespace Parking_Tag_Picker_WRT.Helpers
{
public class DatabaseHelper
{
public const string AppDBPath = @"ParkingZoneDatabase.db";
public const string PackageDBPath = @"Databases\ParkingZoneDatabase.db";
/// <summary>
/// Load SQL_LiteTable from Solution
/// </summary>
/// <param name="DBPATH"></param>
/// <returns></returns>
public async Task<bool> Init(string tableName)
{
bool isTableExisting = false;
try
{
StorageFile localFile = await ApplicationData.Current.LocalFolder.GetFileAsync(string.Format("{0}.csv", tableName));
isTableExisting = true;
}
catch
{
isTableExisting = false;
}
if (!isTableExisting)
{
StorageFile packagefile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(string.Format(@"ms-appx:///Databases/{0}.csv", tableName)));
await packagefile.CopyAsync(ApplicationData.Current.LocalFolder);
}
return true;
}
/// <summary>
/// Retrieve zone info list from the database.
/// </summary>
/// <returns></returns>
public async Task<ObservableCollection<ZoneInfo>> ReadZones(string tableName)
{
string fileName = string.Format("{0}.csv", tableName);
// access the local folder
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
// open the file 'filename' for reading
Stream fileStream = await local.OpenStreamForReadAsync(fileName);
using (var textReader = new StreamReader(fileStream))
{
var csv = new CsvReader(textReader);
List<ZoneInfo> zoneInfo = csv.GetRecords<ZoneInfo>().ToList();
ObservableCollection<ZoneInfo> zoneInfoCollection = new ObservableCollection<ZoneInfo>(zoneInfo);
return zoneInfoCollection;
}
}
}
}
async protected override void OnNavigatedTo(NavigationEventArgs e)
{
this.navigationHelper.OnNavigatedTo(e);
((TagRequestViewModel)this.DataContext).SelectedCouncilId = (string)e.Parameter;
await _vm.InitZoneInfoAsync();
}
public async Task InitZoneInfoAsync()
{
string tableName;
CouncilId = Int32.Parse(SelectedCouncilId);
tableName = TableNameDictionary[CouncilId];
CouncilHeaderDisplayName = CouncilDisplayNameDictionary[CouncilId];
//Init the table data
var result = await _dbHelper.Init(tableName);
//Return zone table
var zoneResult = await _dbHelper.ReadZones(tableName);
ZoneInfoCollection = zoneResult;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment