diff --git a/GSCFieldApp/GSCFieldApp.csproj b/GSCFieldApp/GSCFieldApp.csproj index 87f30226..3aee7271 100644 --- a/GSCFieldApp/GSCFieldApp.csproj +++ b/GSCFieldApp/GSCFieldApp.csproj @@ -147,7 +147,6 @@ Always - Always diff --git a/GSCFieldApp/Package.appxmanifest b/GSCFieldApp/Package.appxmanifest index 7a39a1c1..213e497c 100644 --- a/GSCFieldApp/Package.appxmanifest +++ b/GSCFieldApp/Package.appxmanifest @@ -1,6 +1,6 @@  - + Geological Survey Canada Field Application diff --git a/GSCFieldApp/ViewModels/FieldBookDialogViewModel.cs b/GSCFieldApp/ViewModels/FieldBookDialogViewModel.cs index 79b5839c..0325ef82 100644 --- a/GSCFieldApp/ViewModels/FieldBookDialogViewModel.cs +++ b/GSCFieldApp/ViewModels/FieldBookDialogViewModel.cs @@ -4,6 +4,7 @@ using SQLite; using System; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Threading.Tasks; using Template10.Common; using Template10.Mvvm; @@ -22,7 +23,7 @@ public class FieldBookDialogViewModel : ViewModelBase public FieldBooks existingUserDetail; //UI - private List _projectType = new List(); + private ObservableCollection _projectType = new ObservableCollection(); private string _selectedProjectType = string.Empty; private string _startStationNumber = "1"; private string _projectName = string.Empty; @@ -119,7 +120,7 @@ public string StartStationNumber public string ActivityName { get { return _activityName; } set { _activityName = value; } } public string Notes { get { return _notes; } set { _notes = value; } } - public List ProjectType { get { return _projectType; } set { _projectType = value; } } + public ObservableCollection ProjectType { get { return _projectType; } set { _projectType = value; } } public string SelectedProjectType { get { return _selectedProjectType; } set { _selectedProjectType = value; } } public bool Enability { get { return _enability; } set { _enability = value; } } @@ -141,7 +142,10 @@ private void FillProjectType() //Init. string fieldName = Dictionaries.DatabaseLiterals.FieldUserInfoFWorkType; - _projectType = accessData.GetComboboxListWithVocab(userTable, fieldName, out _selectedProjectType); + foreach (var pType in accessData.GetComboboxListWithVocab(userTable, fieldName, out _selectedProjectType)) + { + _projectType.Add(pType); + } //Update UI RaisePropertyChanged("ProjectType"); @@ -259,6 +263,7 @@ public void AutoFillDialog(FieldBooks incomingData) RaisePropertyChanged("GeologistLN"); RaisePropertyChanged("Notes"); RaisePropertyChanged("ActivityName"); + RaisePropertyChanged("SelectedProjectType"); } diff --git a/GSCFieldApp/ViewModels/FieldBooksPageViewModel.cs b/GSCFieldApp/ViewModels/FieldBooksPageViewModel.cs index e44b115c..623b140b 100644 --- a/GSCFieldApp/ViewModels/FieldBooksPageViewModel.cs +++ b/GSCFieldApp/ViewModels/FieldBooksPageViewModel.cs @@ -19,7 +19,6 @@ using Windows.Storage.Pickers; using Windows.UI.Core; using Template10.Utils; -using System.Diagnostics; using Esri.ArcGISRuntime.Geometry; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; @@ -38,7 +37,7 @@ public class FieldBooksPageViewModel : ViewModelBase #region INITIALIZATION //UI - public ObservableCollection _projectCollection = new ObservableCollection(); + private ObservableCollection _projectCollection = new ObservableCollection(); public int _selectedProjectIndex = -1; private bool _noFieldBookWatermark = false; @@ -71,6 +70,7 @@ public class FieldBooksPageViewModel : ViewModelBase public ObservableCollection ProjectCollection { get { return _projectCollection; } + set { _projectCollection = value; } } public bool ProgressRingActive @@ -90,9 +90,6 @@ public bool ProgressRingVisibility public FieldBooksPageViewModel() { - _projectCollection = new ObservableCollection(); - RaisePropertyChanged("ProjectCollection"); - //Fill list view of projects FillProjectCollectionAsync(); @@ -119,116 +116,135 @@ private void FieldBookDialog_newFieldBookSaved(object sender, EventArgs e) /// /// Will fill the project collection with information related to it /// - private async void FillProjectCollectionAsync() + private async Task FillProjectCollectionAsync() { - _projectCollection.Clear(); + _projectCollection = new ObservableCollection(); + ProjectCollection = _projectCollection; RaisePropertyChanged("ProjectCollection"); List invalidFieldBookToDelete = new List(); - //Iterate through local state folder - IReadOnlyList localStateFolders = await ApplicationData.Current.LocalFolder.GetFoldersAsync(); - IEnumerable reverseStateList = localStateFolders.Reverse(); - - foreach (StorageFolder sf in reverseStateList) + if (_projectCollection.Count() == 0) { - //Get files - IReadOnlyList localFiles = await sf.GetFilesAsync(); - foreach (StorageFile sfi in localFiles) + //Iterate through local state folder + IReadOnlyList localStateFolders = await ApplicationData.Current.LocalFolder.GetFoldersAsync(); + IEnumerable reverseStateList = localStateFolders.Reverse(); + + foreach (StorageFolder sf in reverseStateList) { - //Get the database - if ((sfi.FileType.Contains(DatabaseLiterals.DBTypeSqlite) || sfi.FileType.Contains(DatabaseLiterals.DBTypeSqliteDeprecated)) - && sfi.DisplayName == DatabaseLiterals.DBName) + //Get files + IReadOnlyList localFiles = await sf.GetFilesAsync(); + foreach (StorageFile sfi in localFiles) { - FieldBooks currentDB = new FieldBooks(); - - using (SQLiteConnection currentConnection = accessData.GetConnectionFromPath(sfi.Path)) + //Get the database + if ((sfi.FileType.Contains(DatabaseLiterals.DBTypeSqlite) || sfi.FileType.Contains(DatabaseLiterals.DBTypeSqliteDeprecated)) + && sfi.DisplayName == DatabaseLiterals.DBName) { - List metadataTableRows = accessData.ReadTableFromDBConnectionWithoutClosingConnection(metadataModel.GetType(), string.Empty, currentConnection); - foreach (object m in metadataTableRows) + FieldBooks currentDB = new FieldBooks(); + + using (SQLiteConnection currentConnection = accessData.GetConnectionFromPath(sfi.Path)) { - //For metadata - Metadata met = m as Metadata; - currentDB.CreateDate = met.StartDate; - currentDB.GeologistGeolcode = met.Geologist + "[" + met.UserCode + "]"; - //currentDB.metadataForProject.FieldworkType = met.FieldworkType; - //currentDB.metadataForProject.MetaID = met.MetaID; - currentDB.ProjectPath = Path.GetDirectoryName(sfi.Path); - currentDB.ProjectDBPath = sfi.Path; - currentDB.metadataForProject = m as Metadata; - } + List metadataTableRows = accessData.ReadTableFromDBConnectionWithoutClosingConnection(metadataModel.GetType(), string.Empty, currentConnection); + foreach (object m in metadataTableRows) + { + //For metadata + Metadata met = m as Metadata; + currentDB.CreateDate = met.StartDate; + currentDB.GeologistGeolcode = met.Geologist + "[" + met.UserCode + "]"; + //currentDB.metadataForProject.FieldworkType = met.FieldworkType; + //currentDB.metadataForProject.MetaID = met.MetaID; + currentDB.ProjectPath = Path.GetDirectoryName(sfi.Path); + currentDB.ProjectDBPath = sfi.Path; + currentDB.metadataForProject = m as Metadata; + } - #region For stations - string stationQuerySelect = "SELECT *"; - string stationQueryFrom = " FROM " + DatabaseLiterals.TableStation; - string stationQueryWhere = " WHERE " + DatabaseLiterals.TableStation + "." + DatabaseLiterals.FieldStationAlias + " NOT LIKE '%" + DatabaseLiterals.KeywordStationWaypoint + "%'"; - string stationQueryFinal = stationQuerySelect + stationQueryFrom + stationQueryWhere; - List stationCountResult = accessData.ReadTableFromDBConnectionWithoutClosingConnection(stationModel.GetType(), stationQueryFinal, currentConnection); + #region For stations + string stationQuerySelect = "SELECT *"; + string stationQueryFrom = " FROM " + DatabaseLiterals.TableStation; + string stationQueryWhere = " WHERE " + DatabaseLiterals.TableStation + "." + DatabaseLiterals.FieldStationAlias + " NOT LIKE '%" + DatabaseLiterals.KeywordStationWaypoint + "%'"; + string stationQueryFinal = stationQuerySelect + stationQueryFrom + stationQueryWhere; + List stationCountResult = accessData.ReadTableFromDBConnectionWithoutClosingConnection(stationModel.GetType(), stationQueryFinal, currentConnection); - if (stationCountResult.Count != 0) - { - Station lastStation = (Station)stationCountResult[stationCountResult.Count - 1]; - currentDB.StationLastEntered = lastStation.StationAlias; - } + if (stationCountResult.Count != 0) + { + Station lastStation = (Station)stationCountResult[stationCountResult.Count - 1]; + currentDB.StationLastEntered = lastStation.StationAlias; + } - //If field book is invalid keep parent folder path at least user will be able to delete it. - if (metadataTableRows.Count == 0 && stationCountResult.Count == 0) - { - StorageFolder parentFolder = await sfi.GetParentAsync(); - currentDB.ProjectPath = parentFolder.Path; - } - #endregion + //If field book is invalid keep parent folder path at least user will be able to delete it. + if (metadataTableRows.Count == 0 && stationCountResult.Count == 0) + { + StorageFolder parentFolder = await sfi.GetParentAsync(); + currentDB.ProjectPath = parentFolder.Path; + } + #endregion - #region For locations - string queryLocation = "select count(*) from " + DatabaseLiterals.TableLocation; - List locationCountResult = accessData.ReadScalarFromDBConnectionWithoutClosingConnection(queryLocation, currentConnection); - if (locationCountResult != null && locationCountResult.Count() > 0) - { - currentDB.StationNumber = locationCountResult[0].ToString(); - } - else - { - currentDB.StationNumber = 0.ToString(); - } - + #region For locations + string queryLocation = "select count(*) from " + DatabaseLiterals.TableLocation; + List locationCountResult = accessData.ReadScalarFromDBConnectionWithoutClosingConnection(queryLocation, currentConnection); + if (locationCountResult != null && locationCountResult.Count() > 0) + { + currentDB.StationNumber = locationCountResult[0].ToString(); + } + else + { + currentDB.StationNumber = 0.ToString(); + } - #endregion - _projectCollection.Add(currentDB); - currentConnection.Close(); - GC.Collect(); - GC.WaitForPendingFinalizers(); + #endregion - System.Runtime.InteropServices.Marshal.ReleaseComObject(sfi); + currentConnection.Close(); - break; //Forget about other files + if (!_projectCollection.Contains(currentDB)) + { + //Detect duplicates + bool foundDup = false; + foreach (FieldBooks fbs in _projectCollection) + { + if (fbs.ProjectDBPath == currentDB.ProjectDBPath) + { + foundDup = true; break; + } + } + + if (!foundDup) + { + _projectCollection.Add(currentDB); + } + + } + + break; //Forget about other files + + } } } } - } - //Refresh UI - RaisePropertyChanged("ProjectCollection"); + //Refresh UI + RaisePropertyChanged("ProjectCollection"); - //Select the current active project, so it's highlighted in the list view - ResourceLoader loadLocal = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView(); - if (_projectCollection.Count == 0) - { + //Select the current active project, so it's highlighted in the list view + ResourceLoader loadLocal = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView(); + if (_projectCollection.Count == 0) + { - _noFieldBookWatermark = true; - RaisePropertyChanged("NoFieldBookWatermark"); + _noFieldBookWatermark = true; + RaisePropertyChanged("NoFieldBookWatermark"); - //Send event about missing field books. - fieldBooksUpdate?.Invoke(this, false); + //Send event about missing field books. + fieldBooksUpdate?.Invoke(this, false); - } - else - { - SelectActiveProject(); - _noFieldBookWatermark = false; - RaisePropertyChanged("NoFieldBookWatermark"); + } + else + { + SelectActiveProject(); + _noFieldBookWatermark = false; + RaisePropertyChanged("NoFieldBookWatermark"); - //Send event about missing field books. - fieldBooksUpdate?.Invoke(this, true); + //Send event about missing field books. + fieldBooksUpdate?.Invoke(this, true); + } } } @@ -666,6 +682,7 @@ public void ProjectEditButton_Tapped(object sender, Windows.UI.Xaml.Input.Tapped var modal = Window.Current.Content as ModalDialog; var view = modal.ModalContent as Views.FieldBookDialog; modal.ModalContent = view = new Views.FieldBookDialog(selectedProject); + view.ViewModel.projectEdit -= ViewModel_projectEdit; view.ViewModel.projectEdit += ViewModel_projectEdit; modal.IsModal = true; }); @@ -715,6 +732,7 @@ private void ViewModel_projectEdit(object sender) //Refresh page FillProjectCollectionAsync(); + RaisePropertyChanged("ProjectCollection"); } ///