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?"); } }
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); } }