public async Task <bool> InsertLoc(FishingLoc loc, bool fromFish = false) { if (loc == null) { loc = new FishingLoc(); } Coordinate coor = new Coordinate() { IdCoordinate = Guid.NewGuid().ToString() }; if (LocActivationSwitchState) { //From geolocator try { var locator = CrossGeolocator.Current; var position = await locator.GetPositionAsync(); if (position != null) { coor.Lattitude = position.Latitude; coor.Longitude = position.Longitude; } } catch (Exception ex) { } } else { //from Selection loc.Name = CurrentLoc.Name; loc.PlaceCount = CurrentLoc.PlaceCount; loc.Type = CurrentLoc.Type; if (CreatedPins.Count == 1) { foreach (var pin in CreatedPins) { coor.Lattitude = pin.Pin.Position.Latitude; coor.Longitude = pin.Pin.Position.Longitude; } } } Waypoint wp = new Waypoint() { Coordinate = coor, IdWaypoint = Guid.NewGuid().ToString() }; loc.Waypoints.Add(wp); if (fromFish) { loc.Type = "GPS"; loc.Name = null; loc.PlaceCount = 0; } else { loc.Type = CurrentLoc.Type; loc.Name = CurrentLoc.Name; loc.PlaceCount = CurrentLoc.PlaceCount; } loc.IdFishingLoc = Guid.NewGuid().ToString(); loc.User_IdUser = App.user.IdUser; FishingLoc.Insert(loc); CurrentLoc = new FishingLoc(); CreatedPins.Clear(); HomeViewModel.UpdateLocs(); 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); } }