Ejemplo n.º 1
0
        /// <summary>
        /// Requests a single GPS reading by starting the listener and stopping once a reading is received
        /// Requests a single GPS reading - This will start & stop the gps listener if wasn't running already
        /// </summary>
        /// <param name="gpsManager"></param>
        /// <returns></returns>
        public static IObservable <IGpsReading> GetCurrentPosition(this IGpsManager gpsManager) => Observable.FromAsync(async ct =>
        {
            var iStarted = false;
            try
            {
                await currentLocSemaphore
                .WaitAsync(ct)
                .ConfigureAwait(false);

                var task = gpsManager
                           .WhenReading()
                           .Take(1)
                           .ToTask(ct);
                if (!gpsManager.IsListening())
                {
                    iStarted = true;
                    await gpsManager
                    .StartListener(GpsRequest.Foreground)
                    .ConfigureAwait(false);
                }
                var reading = await task.ConfigureAwait(false);

                return(reading);
            }
            finally
            {
                currentLocSemaphore.Release();
                if (iStarted)
                {
                    await gpsManager.StopListener().ConfigureAwait(false);
                }
            }
        });
Ejemplo n.º 2
0
        /// <summary>
        /// This method will start the GPS with realtime and close on disposable (or when the app backgrounds)
        /// </summary>
        /// <param name="gpsManager"></param>
        /// <returns></returns>
        public static IObservable <IGpsReading> StartAndReceive(this IGpsManager gpsManager) => Observable.Create <IGpsReading>(ob =>
        {
            var composite = new CompositeDisposable();
            var platform  = ShinyHost.Resolve <IPlatform>();
            gpsManager
            .WhenReading()
            .Subscribe(
                ob.OnNext,
                ob.OnError
                )
            .DisposedBy(composite);

            platform
            .WhenStateChanged()
            .Where(x => x == PlatformState.Background)
            .Subscribe(_ => ob.Respond(null))
            .DisposedBy(composite);

            gpsManager
            .StartListener(GpsRequest.Foreground)
            .ContinueWith(x =>
            {
                if (x.IsFaulted)
                {
                    ob.OnError(x.Exception);
                }
            });

            return(() =>
            {
                composite.Dispose();
                gpsManager.StopListener();
            });
        });
Ejemplo n.º 3
0
        public async Task Run(CancellationToken token)
        {
            if (_gpsManager.IsListening)
            {
                return;
            }

            #region Subscribe GPS reading event
            //購読重複を避けるために購読を解除する
            _gpsObserver?.Dispose();

            //位置情報取得イベントを購読
            _gpsObserver = _gpsManager
                           .WhenReading()
                           .Subscribe(async x =>
            {
                if (token.IsCancellationRequested)
                {
                    await _gpsManager.StopListener();
                    _gpsObserver?.Dispose();
                    return;
                }
                MainThread.BeginInvokeOnMainThread(() => {
                    var message = new LocationReadMessage
                    {
                        GpsInfo = x,
                    };
                    MessagingCenter.Send(message, nameof(LocationReadMessage));
                });
                await _notificationManager.ScheduleNotification($"{x.Position.Latitude}, {x.Position.Longitude}", "test");
            });
            #endregion

            #region GPS start listen.
            //位置情報取得許可を得る
            var checkResult = await Permissions.CheckStatusAsync <Permissions.LocationAlways>();

            if (checkResult != PermissionStatus.Granted)
            {
                var requestResult = await Permissions.RequestAsync <Permissions.LocationAlways>();

                if (requestResult != PermissionStatus.Granted)
                {
                    return;
                }
            }

            var request = new GpsRequest
            {
                Interval      = TimeSpan.FromSeconds(5),
                UseBackground = false,
            };
            await _gpsManager.StartListener(request);

            #endregion
        }
Ejemplo n.º 4
0
        public GpsViewModel(IGpsManager gps)
        {
            this.Start = ReactiveCommand.CreateFromTask(
                async() =>
            {
                this.foreground = gps
                                  .WhenReading()
                                  .Subscribe(reading =>
                {
                    this.Latitude  = reading.Position.Latitude;
                    this.Longitude = reading.Position.Longitude;
                });

                await gps.StartListener(GpsRequest.Realtime(true));
            }
                );

            this.Stop = ReactiveCommand.CreateFromTask(async() =>
            {
                await gps.StopListener();
                this.foreground?.Dispose();
            });
        }