Skip to content

Instantly share code, notes, and snippets.

@felipebaltazar
Created July 14, 2020 16:52
Show Gist options
  • Save felipebaltazar/7da51d44d9c69e4bc88323c170e2af37 to your computer and use it in GitHub Desktop.
Save felipebaltazar/7da51d44d9c69e4bc88323c170e2af37 to your computer and use it in GitHub Desktop.
public class MainPageViewModel : ViewModelBase
{
private readonly IDisposable _gpsDisposer;
...
public MainPageViewModel(INavigationService navigationService, IGpsManager gpsManager)
: base(navigationService)
{
...
// Vamos inscrever a viewmodel para ser
// notificada quando houver uma nova leitura
_gpsDisposer = gpsManager.WhenReading()
.Subscribe(OnNext, OnError);
}
public void OnError(Exception error)
{
CurrentLocation = error.Message;
}
public void OnNext(IGpsReading reading)
{
CurrentLocation = $"Latitude: {reading.Position.Latitude} | Longitude: {reading.Position.Longitude}";
}
...
public override void Destroy()
{
base.Destroy();
// Lembrando sempre de desinscrever o observador
_gpsDisposer?.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment