Example #1
0
        private async void Current_PositionChanged(object sender, PositionEventArgs e)
        {
            CurrentLocation = e.Position;

            var nextStep = _currentWaypoints.OrderBy(w => w.Step).Last().Step + 1;

            _currentWaypoints.Add(new Waypoint
            {
                Step      = nextStep,
                Latitude  = CurrentLocation.Latitude,
                Longitude = CurrentLocation.Longitude,
                Timestamp = DateTimeOffset.UtcNow
            });

            if (CurrentWaypoints.Any())
            {
                var j = new Journey();
                foreach (var w in CurrentWaypoints.OrderBy(w => w.Step).ToList())
                {
                    j.Waypoints.Add(w);
                }

                CurrentDistance = await j.CalculateDistance();
            }
            else
            {
                CurrentDistance = 0;
            }

            HasMoved?.Invoke(this, EventArgs.Empty);
        }
        public async void Save()
        {
            if (Busy)
            {
                return;
            }

            Busy = true;

            if (string.IsNullOrEmpty(Vehicle.Id))
            {
                SaveFailed?.Invoke(this, "Vehicle is required");
                Busy = false;
                return;
            }

            if (Reason == "Required")
            {
                SaveFailed?.Invoke(this, "Reason is required.");
                Busy = false;
                return;
            }

            if (Authenticated)
            {
                if (string.IsNullOrEmpty(Company.Id))
                {
                    SaveFailed?.Invoke(this, "Company is required.");
                    Busy = false;
                    return;
                }
            }

            var journey = new Journey()
            {
                Company    = Company,
                Date       = Date,
                Invoiced   = Invoiced,
                Passengers = Passengers,
                Reason     = Reason,
                Vehicle    = Vehicle
            };

            foreach (var w in Services.Host.TrackerService.CurrentWaypoints.OrderBy(w => w.Step))
            {
                journey.Waypoints.Add(w);
            }

            journey.Distance = await journey.CalculateDistance();

            var result = await Services.Host.JourneyService.SaveJourney(journey);

            Saved?.Invoke(this, EventArgs.Empty);
            Busy = false;
        }
        public async void Save()
        {
            if (Busy)
            {
                return;
            }

            Busy = true;

            if (string.IsNullOrEmpty(OriginAddress.PlaceId))
            {
                SaveFailed?.Invoke(this, "Origin is required.");
                Device.StartTimer(TimeSpan.FromSeconds(1), Wait);
                return;
            }

            if (string.IsNullOrEmpty(DestinationAddress.PlaceId))
            {
                SaveFailed?.Invoke(this, "Destination is required.");
                Device.StartTimer(TimeSpan.FromSeconds(1), Wait);
                return;
            }

            if (string.IsNullOrEmpty(Vehicle.Id))
            {
                SaveFailed?.Invoke(this, "Vehicle is required");
                Device.StartTimer(TimeSpan.FromSeconds(1), Wait);
                return;
            }

            if (Reason == "Required")
            {
                SaveFailed?.Invoke(this, "Reason is required.");
                Device.StartTimer(TimeSpan.FromSeconds(1), Wait);
                return;
            }

            if (Authenticated)
            {
                if (string.IsNullOrEmpty(Company.Id))
                {
                    SaveFailed?.Invoke(this, "Company is required.");
                    Device.StartTimer(TimeSpan.FromSeconds(1), Wait);
                    return;
                }
            }

            var originWaypoint = new Waypoint()
            {
                PlaceId   = OriginAddress.PlaceId,
                Latitude  = OriginAddress.Latitude,
                Longitude = OriginAddress.Longitude,
                Label     = OriginAddress.Label,
                Step      = 0
            };
            var destinationWaypoint = new Waypoint()
            {
                PlaceId   = DestinationAddress.PlaceId,
                Latitude  = DestinationAddress.Latitude,
                Longitude = DestinationAddress.Longitude,
                Label     = DestinationAddress.Label,
                Step      = 1
            };

            var journey = new Journey()
            {
                Company    = Company,
                Date       = Date,
                Invoiced   = Invoiced,
                Passengers = Passengers,
                Reason     = Reason,
                Vehicle    = Vehicle
            };

            journey.Waypoints.Add(originWaypoint);
            journey.Waypoints.Add(destinationWaypoint);

            journey.Distance = await journey.CalculateDistance();

            var result = await Services.Host.JourneyService.SaveJourney(journey);

            Saved?.Invoke(this, EventArgs.Empty);
            Device.StartTimer(TimeSpan.FromSeconds(1), Wait);
        }