Exemple #1
0
        public HomeLocationEditorViewModel(
            ISchedulerProvider schedulerProvider,
            INavigationService navigationService,
            IPlacesProvider placesProvider,
            IEmptyAutocompleteResultProvider autocompleteResultProvider,
            ISettingsProvider settingsProvider,
            IGeocodingProvider geocodingProvider) : base(schedulerProvider, placesProvider, geocodingProvider, navigationService)
        {
            _autocompleteResultProvider = autocompleteResultProvider;

            SelectedAutoCompleteResultModelObservable
            .Subscribe(async result =>
            {
                var address = await geocodingProvider.ReverseGeocodingFromPlaceId(result.PlaceId);
                settingsProvider.SetHomeAddress(address);
                navigationService.GoBack();
            });

            this.Changed(() => SelectedSearchResult)
            .Where(r => r is SetLocationOnMapResultModel)
            .Subscribe(result =>
            {
                navigationService.NavigateTo <ChooseHomeAddressOnMapViewModel>();
            });
        }
        protected override async Task SetAutoCompleteResultModel(string placeId)
        {
            var address = await _geocodingProvider.ReverseGeocodingFromPlaceId(placeId);

            _settingsProvider.SetWorkAddress(address);
            _navigationService.GoBack();
        }
        protected BaseLocationEditorViewModel(
            ISchedulerProvider schedulerProvider,
            IPlacesProvider placesProvider,
            IGeocodingProvider geocodingProvider,
            INavigationService navigationService)
        {
            _navigationService = navigationService;

            this.Changed(() => SearchText)
            .Where(text => !string.IsNullOrEmpty(text))
            .Throttle(TimeSpan.FromMilliseconds(300), schedulerProvider.DefaultScheduler)
            .ObserveOn(schedulerProvider.SynchronizationContextScheduler)
            .Subscribe(async searchText =>
            {
                IsSearching   = true;
                SearchResults = await placesProvider.GetAutoCompleteResponse(searchText);
                IsSearching   = false;
            });

            this.Changed(() => SearchText)
            .Where(string.IsNullOrEmpty)
            .Subscribe(_ => SearchResults = GetEmptyAutocompleteResult());

            var selectedEmptyAutocompleteResultModelObservable = this.Changed(() => SelectedSearchResult)
                                                                 .Where(result => result is EmptyAutocompleteResultModel)
                                                                 .Cast <EmptyAutocompleteResultModel>();

            selectedEmptyAutocompleteResultModelObservable.Where(r => r.Address == null)
            .ObserveOn(schedulerProvider.SynchronizationContextScheduler)
            .Subscribe(selectedResult =>
            {
                switch (selectedResult)
                {
                case HomeResultModel _:
                    _navigationService.NavigateToSearchView <HomeLocationEditorViewModel>(callBack: null);
                    break;

                case WorkResultModel _:
                    _navigationService.NavigateToSearchView <WorkLocationEditorViewModel>(callBack: null);
                    break;
                }
            });

            var selectedEmptyAddress = selectedEmptyAutocompleteResultModelObservable
                                       .Where(r => r.Address != null)
                                       .Select(r => r.Address);

            SelectedAddress = SelectedAutoCompleteResultModelObservable
                              .Select(x => Observable.FromAsync(() => geocodingProvider.ReverseGeocodingFromPlaceId(x.PlaceId)))
                              .Concat()
                              .Merge(selectedEmptyAddress);
        }
        protected virtual async Task SetAutoCompleteResultModel(string placeId)
        {
            SelectedAddress = await _geocodingProvider.ReverseGeocodingFromPlaceId(placeId);

            _navigationService.GoBack();
        }