コード例 #1
0
        private async void SendAlarm(BidType type)
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            if (Device.RuntimePlatform == Device.Android)
            {
                if (!await CheckPermission(Permission.Location, "Для отправки тревоги, необходимо разрешение на использование геоданных."))
                {
                    IsBusy = false;
                    return;
                }
            }

            var          guid    = Guid.NewGuid();
            var          user    = App.User;
            bool         res     = false;
            IBidsService service = new BidsService
            {
                AccessToken = (string)user.UserToken.Token.Clone(),
                TokenType   = (string)user.UserToken.TokenType.Clone()
            };

            try
            {
                res = await service.CreateBid(new Bid
                {
                    Guid     = guid,
                    Client   = user,
                    Location = await Location.GetCurrentGeolocationAsync(GeolocationAccuracy.Best),
                    Status   = BidStatus.PendingAcceptance,
                    Type     = type
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            if (res)
            {
                if (type == BidType.Call)
                {
                    App.Call("+7 911 447-11-83");
                }
                else if (type == BidType.Alert)
                {
                    await Application.Current.MainPage.DisplayAlert("Внимание", "Тревога отправлена", "OK");
                }

                await Task.Run(() =>
                {
                    DependencyService.Get <ILocationTrackingService>()
                    .StartService(guid);
                });
            }
            else
            {
                if (string.IsNullOrEmpty(service.LastError))
                {
                    await Application.Current.MainPage.DisplayAlert("Внимание", "Ошибка сервера.", "Ок");
                }
                else
                {
                    await Application.Current.MainPage.DisplayAlert("Внимание", service.LastError, "Ок");
                }
            }

            IsBusy = false;
        }