- 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"> <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> <HintPath>..\packages\Coding4Fun.Phone.Controls.Complete.1.2\lib\Microsoft.Phone.Controls.Toolkit.dll</HintPath>
</Reference> </Reference>
<Reference Include="Microsoft.Phone.Reactive" />
<Reference Include="Microsoft.Silverlight.Testing"> <Reference Include="Microsoft.Silverlight.Testing">
<HintPath>..\Libs\SL3_UTF_May\Microsoft.Silverlight.Testing.dll</HintPath> <HintPath>..\Libs\SL3_UTF_May\Microsoft.Silverlight.Testing.dll</HintPath>
</Reference> </Reference>
@@ -125,6 +126,7 @@
<Reference Include="System.Json"> <Reference Include="System.Json">
<HintPath>..\packages\Hammock.1.2.1\lib\sl4\System.Json.dll</HintPath> <HintPath>..\packages\Hammock.1.2.1\lib\sl4\System.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Observable" />
<Reference Include="System.Runtime.Serialization" /> <Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Runtime.Serialization.Json"> <Reference Include="System.Runtime.Serialization.Json">
<HintPath>..\packages\Hammock.1.2.1\lib\sl4\System.Runtime.Serialization.Json.dll</HintPath> <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;
using Microsoft.Phone.Controls; using Microsoft.Phone.Controls;
using Microsoft.Phone.Controls.Maps; using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Reactive;
using Microsoft.Silverlight.Testing; using Microsoft.Silverlight.Testing;
using MyFriendsAround.Common.Entities; using MyFriendsAround.Common.Entities;
using MyFriendsAround.WP7.Service; using MyFriendsAround.WP7.Service;
@@ -118,6 +119,9 @@ namespace MyFriendsAround.WP7.ViewModel
#region Properties & Fields #region Properties & Fields
private IDisposable rxGpsLocation;
private IDisposable rxGpsStatus;
private LocationRect LastBoundRect = null; private LocationRect LastBoundRect = null;
private PhotoChooserTask photoChooserTask; private PhotoChooserTask photoChooserTask;
@@ -227,7 +231,7 @@ namespace MyFriendsAround.WP7.ViewModel
} }
_SelectedFriend = value; _SelectedFriend = value;
if (_SelectedFriend != null && _SelectedFriend.Location!=GeoCoordinate.Unknown) if (_SelectedFriend != null && _SelectedFriend.Location != GeoCoordinate.Unknown)
MapCenter = _SelectedFriend.Location; MapCenter = _SelectedFriend.Location;
// Update bindings, no broadcast // Update bindings, no broadcast
@@ -309,9 +313,8 @@ namespace MyFriendsAround.WP7.ViewModel
pushPin.PinDistance = pushPin.PinDistance =
(GpsLocation != Location.Unknown) (GpsLocation != Location.Unknown)
? Haversine.Distance( ? Haversine.Distance(
new Position() new Position() { Latitude = pushPin.Location.Latitude, Longitude = pushPin.Location.Longitude },
{Latitude = pushPin.Location.Latitude, Longitude = pushPin.Location.Longitude}, new Position() { Latitude = GpsLocation.Latitude, Longitude = GpsLocation.Longitude },
new Position() {Latitude = GpsLocation.Latitude, Longitude = GpsLocation.Longitude},
DistanceType.Kilometers) DistanceType.Kilometers)
: 0; : 0;
} }
@@ -662,26 +665,29 @@ namespace MyFriendsAround.WP7.ViewModel
private void InitGps() private void InitGps()
{ {
App.LocationService.LocationChanged += LocationService_LocationChanged; var geoLocationObs = Observable.FromEvent<LocationChangedEventArgs>(App.LocationService, "LocationChanged").Select(r => r.EventArgs.Location);
App.LocationService.StatusChanged += LocationService_StatusChanged; rxGpsLocation = geoLocationObs.Subscribe(res =>
{
if (res != Location.Unknown)
{
GpsLocation = res;
RefreshMyLocationPushPins();
}
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(); App.LocationService.Start();
} }
private void LocationService_StatusChanged(object sender, LocationStatusEventArgs e)
{
GpsStatus = e.Status;
}
private void LocationService_LocationChanged(object sender, LocationChangedEventArgs e)
{
if (e.Location != Location.Unknown)
{
GpsLocation = e.Location;
RefreshMyLocationPushPins();
}
System.Diagnostics.Debug.WriteLine("watcher_PositionChanged + " + DateTime.Now.Second);
}
public void RefreshMyLocationPushPins() public void RefreshMyLocationPushPins()
{ {
@@ -893,12 +899,11 @@ namespace MyFriendsAround.WP7.ViewModel
PinDistance = PinDistance =
(GpsLocation != Location.Unknown) (GpsLocation != Location.Unknown)
? Haversine.Distance( ? Haversine.Distance(
new Position() {Latitude = f.Latitude, Longitude = f.Longitude}, new Position() { Latitude = f.Latitude, Longitude = f.Longitude },
new Position() new Position() { Latitude = GpsLocation.Latitude, Longitude = GpsLocation.Longitude },
{Latitude = GpsLocation.Latitude, Longitude = GpsLocation.Longitude},
DistanceType.Kilometers) DistanceType.Kilometers)
: 0 : 0
}); });
}); });
PushPins = result; PushPins = result;
if (result.Count > 0) if (result.Count > 0)
@@ -964,12 +969,12 @@ namespace MyFriendsAround.WP7.ViewModel
IsBusy = false; IsBusy = false;
if (args.Error is WebException) if (args.Error is WebException)
{ {
MessageBox.Show(args.Error.Message); MessageBox.Show(args.Error.Message);
} }
else else
{ {
var exception = new ExceptionPrompt(); var exception = new ExceptionPrompt();
exception.Show(args.Error); exception.Show(args.Error);
} }
}); });
} }
@@ -1067,6 +1072,10 @@ namespace MyFriendsAround.WP7.ViewModel
public override void Cleanup() public override void Cleanup()
{ {
// Clean up if needed // Clean up if needed
if (rxGpsLocation != null)
rxGpsLocation.Dispose();
if (rxGpsStatus != null)
rxGpsStatus.Dispose();
base.Cleanup(); base.Cleanup();
App.LocationService.Stop(); App.LocationService.Stop();