public async Task <bool> InsertFishInSession(Fish fish) { if (fish.Weight > 0 && TypePickerItemSelected != null) { if (HomeViewModel.RunningSession.FishingLoc != null) { if (HomeViewModel.RunningSession.FishingLoc.Name != "GPS") { //known location CurrentFish.FishingLoc_IdFishingLoc = HomeViewModel.RunningSession.FishingLoc_IdFishingLoc; } else { //get current Loc var locator = CrossGeolocator.Current; var position = await locator.GetPositionAsync(); if (position != null) { //add the waypoint to the fishing Loc in the session & in the Fish Waypoint wp = new Waypoint { IdWaypoint = Guid.NewGuid().ToString(), Coordinate = new Coordinate { IdCoordinate = Guid.NewGuid().ToString(), Lattitude = position.Latitude, Longitude = position.Longitude } }; HomeViewModel.RunningSession.FishingLoc.Waypoints.Add(wp); CurrentFish.Waypoint_IdWaypoint = wp.IdWaypoint; } } } //link the fish with the fish type fish.FishType = ((FishType)TypePickerItemSelected); fish.CatchedDateTime = DateTime.Now; fish.User_IdUser = App.user.IdUser; fish.Bait = CurrentFish.Bait; fish.Weight = CurrentFish.Weight; fish.Length = CurrentFish.Length; fish.IdFish = Guid.NewGuid().ToString(); fish.Session_IdSession = HomeViewModel.RunningSession.IdSession; HomeViewModel.RunningSession.Fishes.Add(fish); Session.InsertFullSession(HomeViewModel.RunningSession); //ResetingAddFields(); HomeViewModel.UpdateFishes(); HomeViewModel.UpdateSessions(); } return(true); }
public async Task <bool> InsertFish(Fish fish) { if (fish == null) { fish = new Fish(); } if (fish.Weight > 0 && TypePickerItemSelected != null) { if (!AutoLocationSwitchState) { var locator = CrossGeolocator.Current; var position = await locator.GetPositionAsync(); if (position != null) { //we need to add this new auto generated location to DB fish.FishingLoc = new FishingLoc() { IdFishingLoc = Guid.NewGuid().ToString(), Type = "GPS", Name = null, PlaceCount = 0 }; fish.FishingLoc.Waypoints.Add( new Waypoint { IdWaypoint = Guid.NewGuid().ToString(), Coordinate = new Coordinate { IdCoordinate = Guid.NewGuid().ToString(), Lattitude = position.Latitude, Longitude = position.Longitude } }); FishingLoc.Insert(fish.FishingLoc); } } else { if (LocPickerItemSelected != null && ManuLocationSwitchState) { fish.FishingLoc = (FishingLoc)LocPickerItemSelected; } else if (!ManuLocationSwitchState) { fish.FishingLoc = null; } } if (fish.FishingLoc != null) { //When inserted from this method, Locations should have only 1 waypoint so we take this one in reference fish.Waypoint_IdWaypoint = ((from w in fish.FishingLoc.Waypoints select w).ToList().FirstOrDefault()).IdWaypoint; } //link the fish with the fish type fish.FishType = ((FishType)TypePickerItemSelected); fish.CatchedDateTime = DateTime.Now; fish.User_IdUser = App.user.IdUser; fish.Bait = CurrentFish.Bait; fish.Weight = CurrentFish.Weight; fish.Length = CurrentFish.Length; fish.IdFish = Guid.NewGuid().ToString(); Fish.Insert(fish); if (CurrentFish != null) { //ResetingAddFields(); HomeViewModel.UpdateFishes(); return(true); } return(false); } else { await App.Current.MainPage.DisplayAlert("Empty field", "Please fill the type and the weight", "OK"); return(false); } }