Пример #1
0
        public ManageViewModel(IDataContext data, INavigationService navigationService)
        {
            _data = data;
            _navigationService = navigationService;

            var places = data.Places;

            Routes = new AdaptedObservableCollection<Route, RouteSummaryViewModel>(route => new RouteSummaryViewModel(route), source: _data.Routes);
            PersonalPlaces = new AdaptedObservableCollection<Place, CheckableViewModel<Place>>(place => new CheckableViewModel<Place> {Data = place}, place => !place.IsSearchResult, (p1, p2) => String.CompareOrdinal(p1.Name, p2.Name), places);
            SearchResults = new AdaptedObservableCollection<Place, CheckableViewModel<Place>>(place => new CheckableViewModel<Place> {Data = place}, place => place.IsSearchResult, (p1, p2) => String.CompareOrdinal(p1.Name, p2.Name), places);

            CommandDeleteSelectedItems = new DelegateCommand(OnDeleteSelectedItems);
            CommandSelectAllItems = new DelegateCommand(OnSelectAllItems);
            CommandClearSelection = new DelegateCommand(OnClearSelection);
            CommandPinSelectedSearchResult = new DelegateCommand(OnPinSelectedSearchResult);
            CommandGoToPlace = new DelegateCommand<Place>(OnGoToPlace);
            CommandShowPlaceDetails = new DelegateCommand<Place>(OnShowPlaceDetails);
            CommandShowRouteDetails = new DelegateCommand<Route>(OnShowRouteDetails);

            _navigationService.Navigating += OnNavigating;
        }
Пример #2
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="MapViewModel" /> class.
        /// </summary>
        public MapViewModel()
        {
            _data = ApplicationContext.Data;
            _configuration = ApplicationContext.Configuration;
            _navigation = ApplicationContext.NavigationService;
            if (IsCompassSupported)
            {
                _compass = new Compass {TimeBetweenUpdates = TimeSpan.FromMilliseconds(200)};
                _compass.CurrentValueChanged += OnCompassReadingChanged;
            }

            // Initialise local properties.
            VisualState = new ObservableValue<VisualStates>();
            Suggestions = new ReadOnlyObservableCollection<SearchSuggestion>(_suggestions);
            Pushpins = new AdaptedObservableCollection<Place, PlaceViewModel>(p => new PlaceViewModel(p, this), source: _data.Places);
            Routes = new AdaptedObservableCollection<Route, RouteViewModel>(r => new RouteViewModel(r, this), source: _data.Routes);
            DepartureLocation = new RouteLocation();
            ArrivalLocation = new RouteLocation();

            // Initialise commands.
            CommandGetSuggestions = new DelegateCommand(OnGetSuggestions);
            CommandSearch = new DelegateCommand(OnSearch);
            CommandAddPlace = new DelegateCommand<Location>(OnAddPlace);
            CommandSelectPushpin = new DelegateCommand<PlaceViewModel>(vm => SelectedPushpin = vm);
            CommandSelectRoute = new DelegateCommand<RouteViewModel>(vm => SelectedRoute = vm);
            CommandDeletePlace = new DelegateCommand<PlaceViewModel>(OnDeletePlace);
            CommandUpdatePlace = new DelegateCommand<PlaceViewModel>(OnUpdatePlace);
            CommandStopTrackingCurrentLocation = new DelegateCommand(OnStopTrackingCurrentLocation);
            CommandGoToDefaultState = new DelegateCommand(OnGoToDefaultState);
            CommandAddCurrentPlace = new DelegateCommand(() => OnAddPlace(CurrentLocation.ToLocalLocation()), () => CurrentLocation != null && !CurrentLocation.IsUnknown);
            CommandZoomIn = new DelegateCommand(() =>
            {
                if (ZoomLevel.Value < MaxZoomLevel)
                    ZoomLevel.Value = Math.Min(ZoomLevel.Value += ZoomStep, MaxZoomLevel);
            });
            CommandZoomOut = new DelegateCommand(() =>
            {
                if (ZoomLevel.Value > MinZoomLevel)
                    ZoomLevel.Value = Math.Max(ZoomLevel.Value -= ZoomStep, MinZoomLevel);
            });
            CommandShowStreetLayer = new DelegateCommand(() => _data.MapBaseLayer.Value = Layer.Street);
            CommandShowSatelliteHybridLayer = new DelegateCommand(() => _data.MapBaseLayer.Value = Layer.SatelliteHybrid);
            CommandToggleMapOverlay = new DelegateCommand<Layer>(_data.ToggleMapOverlay);
            CommandToggleToolbar = new DelegateCommand(_configuration.ToggleToolbarState);
            CommandSetDepartLocationToCurrentLocation = new DelegateCommand(() => DepartureLocation.Address = CurrentLocationString);
            CommandSetArriveLocationToCurrentLocation = new DelegateCommand(() => ArrivalLocation.Address = CurrentLocationString);
            CommandRoute = new DelegateCommand(OnRoute);
            CommandActivate = new DelegateCommand(OnActivate);
            CommandDeactivate = new DelegateCommand(OnDeactivate);
            CommandSwapRouteLocations = new DelegateCommand(OnSwapRouteLocations);
            CommandSelectContactAddress = new DelegateCommand(OnSelectContactAddress);
            CommandSelectDepartLocationFromContacts = new DelegateCommand(OnSelectDepartLocationFromContacts);
            CommandSelectArriveLocationFromContacts = new DelegateCommand(OnSelectArriveLocationFromContacts);

            // Initialise application bar button and menu items sources.
            InitializeAppBarButtonSources();
            InitializeAppBarMenuItemsSource();

            // Handle events.
            Pushpins.CollectionChanged += OnPushpinsCollectionChanged;
            _data.MapBaseLayer.ValueChanged += (old, @new) => RaisePropertyChanged(IsStreetLayerVisibleProperty, IsSatelliteHybridLayerVisibleProperty);
            _data.SelectedPlace.ValueChanged += (old, @new) => SelectedPushpin = Pushpins.FirstOrDefault(p => p.Data == @new);
            _data.MapOverlays.CollectionChanged += (s, e) => RaisePropertyChanged(IsTrafficLayerVisibleProperty, IsTransitLayerVisibleProperty);
            _data.ClearRoutesBeforeAddingNewRoute.ValueChanged += (old, @new) => RaisePropertyChanged(GetRouteButtonTextProperty);
            VisualState.ValueChanged += OnVisualStateChanged;
            IsTrackingCurrentLocation.ValueChanged += OnIsTrackingCurrentLocationValueChanged;

            // Automatically track current position at first run.
            if (_data.LastRanVersion == null)
                IsTrackingCurrentLocation.Value = true;

            #if DEBUG
            if (DesignerProperties.IsInDesignTool) return;
            #endif

            // Initialise geo-coordinate watcher.
            _geoWatcher = ApplicationContext.GeoCoordinateWatcher;
            _geoWatcher.PositionChanged += OnGeoWatcherPositionChanged;

            // Try centering on current location if available.
            if (IsTrackingCurrentLocation.Value)
            {
                if (!_geoWatcher.Position.Location.IsUnknown)
                {
                    Center.Value = _geoWatcher.Position.Location;
                    ZoomLevel.Value = 15;
                }
            }
        }