Beispiel #1
0
        // 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!");
            }
        }