- handle GpsLocations events with Reactive Extensions

This commit is contained in:
2011-05-27 11:40:30 +03:00
parent 1f79fa6349
commit 62dd116449
2 changed files with 38 additions and 27 deletions
@@ -112,6 +112,7 @@
<Reference Include="Microsoft.Phone.Controls.Toolkit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b772ad94eb9ca604, processorArchitecture=MSIL">
<HintPath>..\packages\Coding4Fun.Phone.Controls.Complete.1.2\lib\Microsoft.Phone.Controls.Toolkit.dll</HintPath>
</Reference>
<Reference Include="Microsoft.Phone.Reactive" />
<Reference Include="Microsoft.Silverlight.Testing">
<HintPath>..\Libs\SL3_UTF_May\Microsoft.Silverlight.Testing.dll</HintPath>
</Reference>
@@ -125,6 +126,7 @@
<Reference Include="System.Json">
<HintPath>..\packages\Hammock.1.2.1\lib\sl4\System.Json.dll</HintPath>
</Reference>
<Reference Include="System.Observable" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Runtime.Serialization.Json">
<HintPath>..\packages\Hammock.1.2.1\lib\sl4\System.Runtime.Serialization.Json.dll</HintPath>
@@ -24,6 +24,7 @@ using Hammock.Serialization;
using Microsoft.Phone;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Reactive;
using Microsoft.Silverlight.Testing;
using MyFriendsAround.Common.Entities;
using MyFriendsAround.WP7.Service;
@@ -118,6 +119,9 @@ namespace MyFriendsAround.WP7.ViewModel
#region Properties & Fields
private IDisposable rxGpsLocation;
private IDisposable rxGpsStatus;
private LocationRect LastBoundRect = null;
private PhotoChooserTask photoChooserTask;
@@ -227,7 +231,7 @@ namespace MyFriendsAround.WP7.ViewModel
}
_SelectedFriend = value;
if (_SelectedFriend != null && _SelectedFriend.Location!=GeoCoordinate.Unknown)
if (_SelectedFriend != null && _SelectedFriend.Location != GeoCoordinate.Unknown)
MapCenter = _SelectedFriend.Location;
// Update bindings, no broadcast
@@ -309,9 +313,8 @@ namespace MyFriendsAround.WP7.ViewModel
pushPin.PinDistance =
(GpsLocation != Location.Unknown)
? Haversine.Distance(
new Position()
{Latitude = pushPin.Location.Latitude, Longitude = pushPin.Location.Longitude},
new Position() {Latitude = GpsLocation.Latitude, Longitude = GpsLocation.Longitude},
new Position() { Latitude = pushPin.Location.Latitude, Longitude = pushPin.Location.Longitude },
new Position() { Latitude = GpsLocation.Latitude, Longitude = GpsLocation.Longitude },
DistanceType.Kilometers)
: 0;
}
@@ -662,27 +665,30 @@ namespace MyFriendsAround.WP7.ViewModel
private void InitGps()
{
App.LocationService.LocationChanged += LocationService_LocationChanged;
App.LocationService.StatusChanged += LocationService_StatusChanged;
App.LocationService.Start();
}
private void LocationService_StatusChanged(object sender, LocationStatusEventArgs e)
var geoLocationObs = Observable.FromEvent<LocationChangedEventArgs>(App.LocationService, "LocationChanged").Select(r => r.EventArgs.Location);
rxGpsLocation = geoLocationObs.Subscribe(res =>
{
GpsStatus = e.Status;
}
private void LocationService_LocationChanged(object sender, LocationChangedEventArgs e)
if (res != Location.Unknown)
{
if (e.Location != Location.Unknown)
{
GpsLocation = e.Location;
GpsLocation = res;
RefreshMyLocationPushPins();
}
System.Diagnostics.Debug.WriteLine("watcher_PositionChanged + " + DateTime.Now.Second);
System.Diagnostics.Debug.WriteLine(
"watcher_PositionChanged + " + res.ToString());
});
var geoStatusObs = Observable.FromEvent<LocationStatusEventArgs>(App.LocationService, "StatusChanged").Select(r => r.EventArgs.Status);
rxGpsStatus = geoStatusObs.Subscribe(res =>
{
GpsStatus = res;
});
App.LocationService.Start();
}
public void RefreshMyLocationPushPins()
{
if (LastBoundRect != null && LastBoundRect.Intersects(new LocationRect(
@@ -893,9 +899,8 @@ namespace MyFriendsAround.WP7.ViewModel
PinDistance =
(GpsLocation != Location.Unknown)
? Haversine.Distance(
new Position() {Latitude = f.Latitude, Longitude = f.Longitude},
new Position()
{Latitude = GpsLocation.Latitude, Longitude = GpsLocation.Longitude},
new Position() { Latitude = f.Latitude, Longitude = f.Longitude },
new Position() { Latitude = GpsLocation.Latitude, Longitude = GpsLocation.Longitude },
DistanceType.Kilometers)
: 0
});
@@ -1067,6 +1072,10 @@ namespace MyFriendsAround.WP7.ViewModel
public override void Cleanup()
{
// Clean up if needed
if (rxGpsLocation != null)
rxGpsLocation.Dispose();
if (rxGpsStatus != null)
rxGpsStatus.Dispose();
base.Cleanup();
App.LocationService.Stop();