public StopDetailViewModel(IMotiveConfiguration motiveConfig, IRepoManager repoManager, INavigation navigation, StopVisit stopVisit)
        {
            _repoManager  = repoManager ?? throw new ArgumentNullException(nameof(repoManager));
            _stopVisit    = stopVisit ?? throw new ArgumentNullException(nameof(stopVisit));
            _navigation   = navigation ?? throw new ArgumentNullException(nameof(navigation));
            _motiveConfig = motiveConfig ?? throw new ArgumentNullException(nameof(motiveConfig));

            StopVisit = new StopVisitViewModel(stopVisit);

            OpenStopNameCommand = new Command(async() =>
            {
                StopDetailNamePage namePage = new StopDetailNamePage(StopVisit);
                await OpenPage(namePage);
            });

            OpenStopMotiveCommand = new Command(async() =>
            {
                StopDetailMotivePage motivePage = new StopDetailMotivePage(Motive);
                await OpenPage(motivePage);
            });

            DataItems     = new ObservableRangeCollection <ListItem>();
            StopName      = StopNameFromString(StopVisit.Name);
            CompletedName = !string.IsNullOrEmpty(StopVisit.Name);

            StopVisit.PropertyChanged += StopVisit_PropertyChanged;

            if (_motiveConfig.Stops)
            {
                PrepareForMotive();
            }
        }
        private List <StopOption> ListNearbyStops(StopVisitViewModel stopVisitViewModel, double radius)
        {
            // Populate possible nearby stops if needed.
            double            lat          = stopVisitViewModel.StopVisit.Latitude;
            double            lon          = stopVisitViewModel.StopVisit.Longitude;
            List <Stop>       nearestStops = _repoManager.StopRepository.NearestStops(lat, lon, radius).ToList();
            List <StopOption> options      = new List <StopOption>();

            foreach (Stop ns in nearestStops)
            {
                // Name of stop should not be empty and it should be not the current selected stop.
                if (!string.IsNullOrWhiteSpace(ns.Name) && ns.Id != _stopVisitViewModel.StopVisit.StopId)
                {
                    StopOption stopOption = new StopOption(ns);
                    stopOption.Command = new Command(async() =>
                    {
                        bool result = await Acr.UserDialogs.UserDialogs.Instance.ConfirmAsync(
                            string.Format(AppResources.ReplaceWithExistingStopText, ns.Name),
                            AppResources.ReplaceWithExistingStopTitle,
                            AppResources.ReplaceWithExistingStopOkText,
                            AppResources.CancelText);

                        if (result)
                        {
                            ChangeAllVisible = false;
                            useExistingStop  = ns;

                            //Unsub to make sure this name change doesn't reset things
                            StopVisit.PropertyChanged -= NameChanged;

                            // Event handler to check if the user wants to manually change the name
                            stopVisitViewModel.Name    = ns.Name;
                            StopVisit.PropertyChanged += NameChanged;
                        }
                    });

                    stopOption.Distance = Util.DistanceBetween(lat, lon, ns.Latitude, ns.Longitude);
                    options.Add(stopOption);
                }
            }

            return(options.OrderBy(ps => ps.Distance).ToList());
        }
        public StopDetailNameViewModel(IRepoManager repoManager, INavigation navigation, IUserInterfaceConfiguration userInterfaceConfiguration, StopVisitViewModel stopVisitViewModel)

        {
            _repoManager                = repoManager ?? throw new ArgumentNullException(nameof(repoManager));
            _navigation                 = navigation ?? throw new ArgumentNullException(nameof(navigation));
            _stopVisitViewModel         = stopVisitViewModel ?? throw new ArgumentNullException(nameof(stopVisitViewModel));
            _userInterfaceConfiguration = userInterfaceConfiguration ?? throw new ArgumentNullException(nameof(userInterfaceConfiguration));

            SaveCommand = new Command(async() =>
            {
                saved        = true;
                Stop newStop = StopVisit.SaveViewModelToStop();
                if (useExistingStop != null)
                {
                    stopVisitViewModel.StopVisit.StopId = useExistingStop.Id;;
                }
                else if (ChangeAll && nameChanged)
                {
                    newStop.Id = stopVisitViewModel.StopVisit.StopId;
                    stopVisitViewModel.StopVisit.Stop = newStop;
                    repoManager.StopRepository.Update(newStop);
                }
                else if (nameChanged)
                {
                    // Add to repo and newStop will have an ID set
                    repoManager.StopRepository.Add(newStop);
                    stopVisitViewModel.StopVisit.StopId = newStop.Id;
                    stopVisitViewModel.StopVisit.Stop   = newStop;
                }

                repoManager.StopVisitRepository.Update(stopVisitViewModel.StopVisit);

                await PopPageAsync();
            });
            CancelCommand = new Command(async() =>
            {
                StopVisit.ResetViewModel();
                await PopPageAsync();
            });

            ChangeAllVisible = ShouldChangeAllBeVisible();
            if (ChangeAllVisible)
            {
                changeAll = true;
            }

            StopVisit.PropertyChanged += NameChanged;


            if (_userInterfaceConfiguration.StopNameReplaceAllEnabled)
            {
                // Only show the list of possible nearby stops if there are more than 1 stops nearby.
                int existingStopVisits = _repoManager.StopVisitRepository.MatchingStop(stopVisitViewModel.StopVisit.Stop).ToList().Count;
                if (existingStopVisits > 1)
                {
                    changeAllVisible = true;
                    changeAll        = true;
                }
            }

            if (_userInterfaceConfiguration.SuggestPossibleNearbyStopsEnabled)
            {
                PossibleStops        = ListNearbyStops(stopVisitViewModel, _userInterfaceConfiguration.SuggestPossibleNearbyStopsDistance);
                PossibleStopsVisible = PossibleStops.Count >= _userInterfaceConfiguration.SuggestPossibleNearbyStopsCount;
            }
        }