Last active
March 2, 2016 20:43
-
-
Save BrianJVarley/1bd6db780370b7d6d36d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Parking_Tag_Picker_WRT.Models; | |
using SQLite; | |
using System; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
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 | |
{ | |
private SQLiteConnection dbConn; | |
/// <summary> | |
/// Load SQL_LiteTable from Solution | |
/// </summary> | |
/// <param name="DB_PATH"></param> | |
/// <returns></returns> | |
public async Task<bool> Init(string dbPath) | |
{ | |
bool isDatabaseExisting = false; | |
try | |
{ | |
StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync(dbPath); | |
isDatabaseExisting = true; | |
} | |
catch | |
{ | |
isDatabaseExisting = false; | |
} | |
if (!isDatabaseExisting) | |
{ | |
StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync(dbPath); | |
await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder); | |
} | |
return true; | |
} | |
/// <summary> | |
/// Retrieve the specific zone info from the database. | |
/// </summary> | |
/// <param name="zoneid"></param> | |
/// <returns></returns> | |
public ZoneInfo ReadZone(int zoneid, string dbPath) | |
{ | |
using (var dbConn = new SQLiteConnection(dbPath)) | |
{ | |
var existingZone = dbConn.Query<ZoneInfo>("select * from ZoneInfo where ObjectId =" + zoneid).FirstOrDefault(); | |
return existingZone; | |
} | |
} | |
/// <summary> | |
/// Retrieve zone info list from the database. | |
/// </summary> | |
/// <returns></returns> | |
public ObservableCollection<ZoneInfo> ReadZones(string dbPath) | |
{ | |
using (var dbConn = new SQLiteConnection(dbPath)) | |
{ | |
List<ZoneInfo> zoneInfo = dbConn.Table<ZoneInfo>().ToList<ZoneInfo>(); | |
ObservableCollection<ZoneInfo> zoneInfoCollection = new ObservableCollection<ZoneInfo>(zoneInfo); | |
return zoneInfoCollection; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Parking_Tag_Picker_WRT.Helpers; | |
using Parking_Tag_Picker_WRT.ViewModel; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices.WindowsRuntime; | |
using Windows.Foundation; | |
using Windows.Foundation.Collections; | |
using Windows.UI.Xaml; | |
using Windows.UI.Xaml.Controls; | |
using Windows.UI.Xaml.Controls.Primitives; | |
using Windows.UI.Xaml.Data; | |
using Windows.UI.Xaml.Input; | |
using Windows.UI.Xaml.Media; | |
using Windows.UI.Xaml.Navigation; | |
// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkID=390556 | |
namespace Parking_Tag_Picker_WRT | |
{ | |
/// <summary> | |
/// An empty page that can be used on its own or navigated to within a Frame. | |
/// </summary> | |
public sealed partial class TagRequestPage : Page | |
{ | |
TagRequestViewModel vm; | |
DatabaseHelper dbHelper; | |
public TagRequestPage() | |
{ | |
//init data context | |
this.NavigationCacheMode = NavigationCacheMode.Required; | |
this.InitializeComponent(); | |
vm = new TagRequestViewModel(dbHelper); | |
this.DataContext = vm; | |
} | |
/// <summary> | |
/// Invoked when this page is about to be displayed in a Frame. | |
/// </summary> | |
/// <param name="e">Event data that describes how this page was reached. | |
/// This parameter is typically used to configure the page.</param> | |
protected override void OnNavigatedTo(NavigationEventArgs e) | |
{ | |
((TagRequestViewModel)this.DataContext).SelectedCouncilId = (string)e.Parameter; | |
vm.InitZoneInfoAsync(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Parking_Tag_Picker_WRT.Helpers; | |
using Parking_Tag_Picker_WRT.Models; | |
using System; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Parking_Tag_Picker_WRT.ViewModel | |
{ | |
public class TagRequestViewModel : INotifyPropertyChanged | |
{ | |
private DatabaseHelper _dbHelper; | |
Dictionary<int, string> DBPathDictonary = new Dictionary<int, string>(); | |
public TagRequestViewModel(DatabaseHelper dbHelper) | |
{ | |
this._dbHelper = dbHelper; | |
InitDBPathDictionary(); | |
} | |
private string _selectedCouncilId; | |
public string SelectedCouncilId | |
{ | |
get | |
{ | |
return _selectedCouncilId; | |
} | |
set | |
{ | |
if (_selectedCouncilId != value) | |
{ | |
_selectedCouncilId = value; | |
RaisePropertyChanged("SelectedCouncilId"); | |
} | |
} | |
} | |
private ObservableCollection<ZoneInfo> _zoneInfoCollection; | |
public ObservableCollection<ZoneInfo> ZoneInfoCollection | |
{ | |
get | |
{ | |
return this._zoneInfoCollection; | |
} | |
set | |
{ | |
if (_zoneInfoCollection != value) | |
{ | |
_zoneInfoCollection = value; | |
RaisePropertyChanged("ZoneInfoCollection"); | |
} | |
} | |
} | |
private string _regNumber; | |
public string RegNumber | |
{ | |
get | |
{ | |
return this._regNumber; | |
} | |
set | |
{ | |
if (_regNumber != value) | |
{ | |
_regNumber = value; | |
RaisePropertyChanged("RegNumber"); | |
} | |
} | |
} | |
private ZoneInfo _selectedZone; | |
public ZoneInfo SelectedZone | |
{ | |
get | |
{ | |
return this._selectedZone; | |
} | |
set | |
{ | |
if (_selectedZone != value) | |
{ | |
_selectedZone = value; | |
RaisePropertyChanged("SelectedZone"); | |
} | |
} | |
} | |
private string _selectedParkDuration; | |
public string SelectedParkDuration | |
{ | |
get | |
{ | |
return this._selectedParkDuration; | |
} | |
set | |
{ | |
if (_selectedParkDuration != value) | |
{ | |
_selectedParkDuration = value; | |
RaisePropertyChanged("SelectedParkDuration"); | |
} | |
} | |
} | |
private void InitDBPathDictionary() | |
{ | |
DBPathDictonary.Add(0,"DublinCityCouncilTable"); | |
DBPathDictonary.Add(1,"DunLaoghaireCityCouncilTable"); | |
DBPathDictonary.Add(2,"FingalCouncilTable"); | |
DBPathDictonary.Add(3,"SouthDublinCouncilTable"); | |
DBPathDictonary.Add(4,"ArklowCouncilTable"); | |
DBPathDictonary.Add(5,"DLRCouncilTable"); | |
DBPathDictonary.Add(6,"WicklowCouncilTable"); | |
DBPathDictonary.Add(7,"TallaghtCouncilTable"); | |
DBPathDictonary.Add(8,"GreystonesCouncilTable"); | |
} | |
public void InitZoneInfoAsync() | |
{ | |
string dbPathValue; | |
int CouncilId = Int32.Parse(SelectedCouncilId); | |
dbPathValue = DBPathDictonary[CouncilId]; | |
var result = _dbHelper.Init(dbPathValue); | |
//Return zone database records | |
var zoneResult = _dbHelper.ReadZones(dbPathValue); | |
ZoneInfoCollection = zoneResult; | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
public void RaisePropertyChanged(string prop) | |
{ | |
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So a brief overview of what's going on with the code.
1.String parameter is passed to the navTo event of TagRequestPage.xaml.cs.
Then that parameter is assigned to a property in it's VM called SelectedCouncilId (TagRequestViewModel.cs)
2.After this property has been assigned in the page's code behind TagRequestPage.xaml.cs. I call a method in the VM that
grabs data from the DB in order to initilize data in the VM.
3.The GetAllZoneInfoAsync() is called, code runs fine up until this call - IsConnected = await _dbHelper.Init(dbPathValue);
4.At the Init call I get an AccessViolationException, the method itself isn't stepped into in the DBHelper class. So this points out to me that the exception is raised due to some code prior to that Init call.