// constructor for collecting public TripPage(Project project) { this.project = project; projectName = project.ProjectName; trip = new Trip(); InitializeComponent(); List <Trip> trips = ORM.GetTrips(projectName); entryTripName.Text = projectName + " - Trip" + (trips.Count + 1).ToString(); }
protected override void OnStart() { // Handle when your app starts // load app variables from file bool result = AppVarsFile.ReadAppVars(); // create tables for ORM, if not already created ORM.GetConnection().CreateTable <Project>(); ORM.GetConnection().CreateTable <Trip>(); ORM.GetConnection().CreateTable <Site>(); ORM.GetConnection().CreateTable <Specimen>(); }
// default constructor // loads the Project List public ExportProject() { InitializeComponent(); projectList = ORM.GetProjects(); List <string> projectNameList = new List <string>(); foreach (Project p in projectList) { projectNameList.Add(p.ProjectName); } pickerExportProject.ItemsSource = projectNameList; }
public async void btnEditSpecimen_Clicked(object sender, EventArgs e) { try { List <Trip> tripList = ORM.GetTrips(project.ProjectName); List <Site> siteList = new List <Site>(); foreach (Trip trip in tripList) { siteList.AddRange(ORM.GetSites(trip.TripName)); } List <Specimen> specimenList = new List <Specimen>(); foreach (Site site in siteList) { specimenList.AddRange(ORM.GetSpecimens(site.SiteName)); } string[] specimens = new string[specimenList.Count]; for (int i = 0; i < specimens.Length; i++) { specimens[i] = specimenList[i].SpecimenName; } var action = await DisplayActionSheet("Choose a Specimen", "Cancel", null, specimens); foreach (Specimen s in specimenList) { if (s.SpecimenName.Equals(action)) { await Navigation.PushAsync(new SpecimenPage(s)); break; } } } catch (Exception ex) { Debug.WriteLine(ex.Message); DependencyService.Get <ICrossPlatformToast>().ShortAlert("Are there any Specimen for this Project?"); } }
// Export Project button event // - checks the local database for Projects before navigation // - loads the ExportProject Page public async void ExportProject_OnClick(object sender, EventArgs e) { try { List <Project> projectList = ORM.GetProjects(); if (projectList.Count > 0) { await Navigation.PushAsync(new ExportProject()); } else { DependencyService.Get <ICrossPlatformToast>().ShortAlert("No projects to export!"); } } catch (Exception ex) { DependencyService.Get <ICrossPlatformToast>().ShortAlert("No projects to export!"); Debug.WriteLine(ex.Message); } }
// AddSite button // - prompts user with list of Trips to add a Site to // - selected Trip is passed to the SitePage through SitePage's constructor public async void btnAddSite_Clicked(object sender, EventArgs e) { try { List <Trip> tripList = ORM.GetTrips(project.ProjectName); if (tripList.Count == 0) { DependencyService.Get <ICrossPlatformToast>().ShortAlert("Create Trip first!"); return; } string[] trips = new string[tripList.Count]; for (int i = 0; i < trips.Length; i++) { trips[i] = tripList[i].TripName; } var action = await DisplayActionSheet("Choose a Trip", "Cancel", null, trips); Debug.WriteLine("Trip chosen: " + action); foreach (Trip t in tripList) { if (t.TripName == action) { await Navigation.PushAsync(new SitePage(t)); break; } } } catch (Exception ex) { // no Trips created, so no Trip database to query DependencyService.Get <ICrossPlatformToast>().ShortAlert("Create Trip first!"); Debug.WriteLine(ex.Message); } }
public void btnSaveSite_Clicked(object sender, EventArgs e) { if (editing) // editing should only require all existing information to be changed { if (!entryHabitat.Text.Equals("") && !entryLocality.Text.Equals("") && !entryAssocTaxa.Text.Equals("") && entryLocationNotes.Text.Equals("") && !(entryHabitat.Text is null) && !(entryLocality.Text is null) && !(entryAssocTaxa.Text is null) && !(entryLocationNotes.Text is null)) { site.AssociatedTaxa = entryAssocTaxa.Text; site.GPSCoordinates = (siteGPS.Equals("")) ? site.GPSCoordinates : siteGPS; site.Habitat = entryHabitat.Text; site.Locality = entryLocality.Text; site.LocationNotes = entryLocationNotes.Text; int updateResult = ORM.GetConnection().Update(site, typeof(Site)); if (updateResult == 1) { DependencyService.Get <ICrossPlatformToast>().ShortAlert(site.SiteName + " save succeeded."); return; } else { DependencyService.Get <ICrossPlatformToast>().ShortAlert(site.SiteName + " save failed."); return; } } else { DependencyService.Get <ICrossPlatformToast>().ShortAlert("Need all info to save Site!"); return; } } site.GPSCoordinates = siteGPS; site.TripName = tripName; // only require name to save Site if (entrySiteName.Text is null || entrySiteName.Text.Equals("")) { DependencyService.Get <ICrossPlatformToast>().ShortAlert("Must enter a name for Site!"); return; } site.SiteName = entrySiteName.Text; site.Locality = entryLocality.Text is null ? "" : entryLocality.Text; site.Habitat = entryHabitat.Text is null ? "" : entryHabitat.Text; site.AssociatedTaxa = entryAssocTaxa.Text is null ? "" : entryAssocTaxa.Text; site.LocationNotes = entryLocationNotes.Text is null ? "" : entryLocationNotes.Text; // check for duplicate names before saving existingSites = ORM.GetSites(site.TripName); foreach (Site s in existingSites) { if (s.SiteName.Equals(site.SiteName)) { DependencyService.Get <ICrossPlatformToast>().ShortAlert("You already have a site with the same name!"); return; } } // save site to database int autoKeyResult = ORM.GetConnection().Insert(site); DependencyService.Get <ICrossPlatformToast>().ShortAlert("Site " + site.SiteName + " saved!"); Debug.WriteLine("inserted site, recordno is: " + autoKeyResult.ToString()); }
// Export CSV button event // - creates the CSV for export of the selected Project public async void btnExportProjectCSV_Clicked(object sender, EventArgs e) { var result = await CheckExternalFilePermissions(); if (!result) { DependencyService.Get <ICrossPlatformToast>().ShortAlert("Storage permission required for data export!"); } try { if (selectedProject == null) { DependencyService.Get <ICrossPlatformToast>().ShortAlert("Select a Project first"); return; } // data from project recordedBy = selectedProject.PrimaryCollector; samplingEffort = selectedProject.ProjectName; // get Trips for selected Project List <Trip> selectedProjectTrips = ORM.GetTrips(selectedProject.ProjectName); // get Sites for each Trip Dictionary <string, List <Site> > sitesForTrips = new Dictionary <string, List <Site> >(); foreach (Trip trip in selectedProjectTrips) { List <Site> sites = ORM.GetSites(trip.TripName); sitesForTrips.Add(trip.TripName, sites); } if (sitesForTrips.Count == 0) { DependencyService.Get <ICrossPlatformToast>().ShortAlert("Missing data"); return; } // get Specimen for each Site Dictionary <string, List <Specimen> > specimenForSites = new Dictionary <string, List <Specimen> >(); foreach (var trip in sitesForTrips) // trip, list<site> { foreach (var site in trip.Value) // go through list<site> { List <Specimen> specimenList = ORM.GetSpecimens(site.SiteName); specimenForSites.Add(site.SiteName, specimenList); } } if (specimenForSites.Count == 0) { DependencyService.Get <ICrossPlatformToast>().ShortAlert("Missing data"); return; } // csv content string to write to file string csvContent = ""; switch (AppVariables.DataExportFormat) { case "Darwin Core": csvContent = CreateCSVForExport(DataExportType.DarwinCore, selectedProjectTrips, specimenForSites, sitesForTrips); break; default: csvContent = CreateCSVForExport(DataExportType.DarwinCore, selectedProjectTrips, specimenForSites, sitesForTrips); break; } string filePath = DependencyService.Get <ICrossPlatform_GetShareFolder>().GetShareFolder(); // save to local app data // to share in email must use temporary file, can't use internal storage string fileName = selectedProject.ProjectName.Trim() + "_" + DateTime.Now.ToString("MM-dd-yyyy") + ".csv"; string localFileLocation = Path.Combine(filePath, fileName); File.WriteAllText(localFileLocation, csvContent, System.Text.Encoding.UTF8); // create csvfile with utf8 encoding, in permanent local storage CrossShareFile.Current.ShareLocalFile(localFileLocation, "Share Specimen Export"); // working on Android, not showing all sharing options on iOS... https://github.com/nielscup/ShareFile } catch (Exception ex) { DependencyService.Get <ICrossPlatformToast>().ShortAlert("Error: " + ex.Message); } }
// Add Specimen button // - prompts user with a Site to add Specimen to // - upon selecting a Site, the Site is passed to the SpecimenPage through SpecimenPage's constructor public async void btnAddSpecimen_Clicked(object sender, EventArgs e) { try { // get all sites for current Project List <Trip> tripList = ORM.GetTrips(project.ProjectName); List <Site> allSites = new List <Site>(); foreach (Trip trip in tripList) { List <Site> tripSiteList = ORM.GetSites(trip.TripName); foreach (Site site in tripSiteList) { allSites.Add(site); } } string[] sites = new string[allSites.Count + 1]; for (int i = 0; i < sites.Length - 1; i++) { sites[i] = allSites[i].SiteName; } sites[allSites.Count] = "Specimen" + (AppVariables.CollectionCount + 1).ToString(); AppVariables.CollectionCount += 1; var action = await DisplayActionSheet("Choose a Site, or add default Specimen", "Cancel", null, sites); Debug.WriteLine("Action chosen: " + action); if (action.Contains("Specimen")) { // if trip-today exists, add to it // else add trip-today, add to it Trip trip = new Trip { ProjectName = project.ProjectName, TripName = "Trip-" + DateTime.Now.ToString("MM-dd-yyyy"), CollectionDate = DateTime.Now }; if (!ORM.CheckExists(trip)) { ORM.InsertObject(trip); } // if site-today exists, add to it // else add site-today, add to it Site site = new Site { SiteName = "Site-" + DateTime.Now.ToString("MM-dd-yyyy"), TripName = trip.TripName, GPSCoordinates = await CurrentGPS.CurrentLocation() }; if (!ORM.CheckExists(site)) { ORM.InsertObject(site); } // add this specimen to the specimen database // message user that specimen was added Specimen specimen = new Specimen(); specimen.SiteName = site.SiteName; specimen.SpecimenName = action; specimen.SpecimenNumber = AppVariables.CollectionCount; specimen.GPSCoordinates = await CurrentGPS.CurrentLocation(); ORM.InsertObject(specimen); DependencyService.Get <ICrossPlatformToast>().ShortAlert(action + " saved!"); } else { await Navigation.PushAsync(new SpecimenPage(ORM.GetSiteByName(action))); } } catch (Exception ex) { // no Sites have been saved, no Site table to query against DependencyService.Get <ICrossPlatformToast>().ShortAlert("Create Site first!"); Debug.WriteLine(ex.Message); } }
// Collect button event // - loads a list of Projects for the user to choose to collect under // - upon user choosing, loads the Collecting Page public async void Collect_OnClick(object sender, EventArgs e) { try { List <Project> projectList = ORM.GetProjects(); string[] projects = new string[projectList.Count + 1]; for (int i = 0; i < projects.Length - 1; i++) { projects[i] = projectList[i].ProjectName; } // default 'today' project Project todayProject = new Project { ProjectName = string.Format("Project-{0}", DateTime.Now.ToString("MM-dd-yyyy")), PrimaryCollector = (AppVariables.CollectorName is null) ? "" : AppVariables.CollectorName, CreatedDate = DateTime.Now }; bool dayPExists = false; foreach (var p in projectList) { if (p.ProjectName.Equals(todayProject.ProjectName)) { dayPExists = true; break; } } if (!dayPExists) { projectList.Add(todayProject); projects[projects.Length - 1] = string.Format("Project-{0}", DateTime.Now.ToString("MM-dd-yyyy")); } else { projects = (from p in projectList select p.ProjectName).ToArray(); } var action = await DisplayActionSheet("Choose a project", "Cancel", null, projects); foreach (Project p in projectList) { if (p.ProjectName.Equals(action)) { if (action.Equals(string.Format("Project-{0}", DateTime.Now.ToString("MM-dd-yyyy")))) { // add today project to database if it is selected if (!ORM.CheckExists(p)) { int autoKeyResult = ORM.InsertObject(p); await Navigation.PushAsync(new CollectingPage(p)); break; } else { await Navigation.PushAsync(new CollectingPage(p)); break; } } else { await Navigation.PushAsync(new CollectingPage(p)); break; } } } } catch (Exception ex) { Debug.WriteLine(ex.Message); DependencyService.Get <ICrossPlatformToast>().ShortAlert("Can't start collecting until you've created a project!"); } }
public void btnSaveSpecimen_Clicked(object sender, EventArgs e) { if (editing) { if (!entrySubstrate.Text.Equals("") && !entryIndivCount.Text.Equals("") && !entryOccurrenceNotes.Text.Equals("") && !(pickerLifeStage.SelectedItem is null) && !(entrySubstrate.Text is null) && !(entryIndivCount.Text is null) && !(entryOccurrenceNotes.Text is null)) { specimen.Substrate = entrySubstrate.Text; specimen.IndividualCount = entryIndivCount.Text; specimen.Cultivated = switchCultivated.IsToggled; specimen.OccurrenceNotes = entryOccurrenceNotes.Text; specimen.LifeStage = pickerLifeStage.SelectedItem.ToString(); specimen.GPSCoordinates = (specimenGPS.Equals("")) ? specimen.GPSCoordinates : specimenGPS; int updateResult = ORM.GetConnection().Update(specimen, typeof(Specimen)); if (updateResult == 1) { DependencyService.Get <ICrossPlatformToast>().ShortAlert(specimen.SpecimenName + " save succeeded."); return; } else { DependencyService.Get <ICrossPlatformToast>().ShortAlert(specimen.SpecimenName + " save failed"); return; } } else { DependencyService.Get <ICrossPlatformToast>().ShortAlert("Need all info to save Specimen!"); return; } } specimen.SiteName = site.SiteName; specimen.GPSCoordinates = specimenGPS; specimen.OccurrenceNotes = entryOccurrenceNotes.Text is null ? "" : entryOccurrenceNotes.Text; specimen.Substrate = entrySubstrate.Text is null ? "" : entrySubstrate.Text; specimen.IndividualCount = entryIndivCount.Text is null ? "" : entryIndivCount.Text; AppVariables.CollectionCount = AppVariables.CollectionCount > 0 ? AppVariables.CollectionCount : 0; specimen.SpecimenNumber = AppVariables.CollectionCount + 1; specimen.FieldIdentification = entryFieldID.Text is null ? "Specimen" + specimen.SpecimenNumber.ToString() : entryFieldID.Text; specimen.SpecimenName = "Specimen" + specimen.SpecimenNumber; if (entryOtherLifeStage.IsVisible && !entryOtherLifeStage.Text.Equals("")) { specimen.LifeStage = entryOtherLifeStage.Text; } else if (entryOtherLifeStage.IsVisible) { DependencyService.Get <ICrossPlatformToast>().ShortAlert("Fill in \"Other\" Life Stage!"); return; } else { specimen.LifeStage = pickerLifeStage.SelectedItem is null ? "" : pickerLifeStage.SelectedItem.ToString(); } specimen.Cultivated = switchCultivated.IsToggled; // save Specimen to database int autoKeyResult = ORM.GetConnection().Insert(specimen); Debug.WriteLine("inserted specimen, recordno is: " + autoKeyResult.ToString()); // update CollectionCount AppVariables.CollectionCount = specimen.SpecimenNumber; DependencyService.Get <ICrossPlatformToast>().ShortAlert("Saved specimen " + specimen.FieldIdentification); }