Exemplo n.º 1
0
        private async Task <RemoteViews> BuildRemoteViews(Context context, int[] appWidgetIds)
        {
            var widgetView = new RemoteViews(context.PackageName, Resource.Layout.Widget);

            RegisterClicks(context, appWidgetIds, widgetView);

            try
            {
                var location = await Geolocation.GetLocationAsync();

                if (location != null)
                {
                    var placeName = "";
                    var address   = await Nominatim.GetAddress(location.Latitude, location.Longitude);

                    if (!string.IsNullOrEmpty(address.Address.Administrative))
                    {
                        placeName = address.Address.Administrative;
                    }
                    else if (!string.IsNullOrEmpty(address.Address.City))
                    {
                        placeName = address.Address.City;
                    }
                    else if (!string.IsNullOrEmpty(address.Address.Village))
                    {
                        placeName = address.Address.Village;
                    }

                    widgetView.SetTextViewText(Resource.Id.PlaceName, placeName);
                    await SetLocation(placeName, location.Latitude, location.Longitude, widgetView);

                    ShowError(widgetView, ErrorType.None);
                }
                else
                {
                    ShowError(widgetView, ErrorType.LocalizationError);
                }
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                ShowError(widgetView, ErrorType.LocalizationError);
            }
            catch (FeatureNotEnabledException fneEx)
            {
                ShowError(widgetView, ErrorType.LocalizationError);
            }
            catch (PermissionException pEx)
            {
                ShowError(widgetView, ErrorType.LocalizationError);
            }
            catch (Exception)
            {
                ShowError(widgetView, ErrorType.InternetConnectionError);
            }

            return(widgetView);
        }
Exemplo n.º 2
0
        public SearchViewModel(string searchText)
        {
            SearchCommand = new Command(
                execute: async(object address) =>
            {
                IsLoading  = true;
                var search = await Nominatim.GetAddress(address as string);
                Update(search);
            },
                canExecute: (object address) => true);

            BackCommand = new Command(
                execute: async() =>
            {
                await Application.Current.MainPage.Navigation.PopModalAsync();
            },
                canExecute: () => true);

            SearchText = searchText;
        }
Exemplo n.º 3
0
        private WeatherViewModel()
        {
            SearchCommand = new Command(
                execute: async(object address) =>
            {
                var page            = new SearchPage();
                var model           = new SearchViewModel(address as string);
                page.BindingContext = model;
                await Application.Current.MainPage.Navigation.PushModalAsync(page);
                SearchText = "";
                var search = await Nominatim.GetAddress(address as string);
                model.Update(search);
            },
                canExecute: (object address) => true);

            CurrentLocationCommand = new Command(
                execute: async() =>
            {
                try
                {
                    DateText = DateTime.Now.ToString("d");
                    RefreshFavoritePlaces();
                    var location = await Geolocation.GetLocationAsync();

                    if (location != null)
                    {
                        var placeName = "";
                        var address   = await Nominatim.GetAddress(location.Latitude, location.Longitude);
                        if (!string.IsNullOrEmpty(address.Address.Administrative))
                        {
                            placeName = address.Address.Administrative;
                        }
                        else if (!string.IsNullOrEmpty(address.Address.City))
                        {
                            placeName = address.Address.City;
                        }
                        else if (!string.IsNullOrEmpty(address.Address.Village))
                        {
                            placeName = address.Address.Village;
                        }

                        SetLocation(placeName, location.Latitude, location.Longitude);
                    }
                    else
                    {
                        if (Settings.Settings.Instance.Places.Count == 0)
                        {
                            await Application.Current.MainPage.DisplayAlert(Resources.AppTranslations.MissingLocalizationTitle,
                                                                            Resources.AppTranslations.MissingLocalizationText,
                                                                            Resources.AppTranslations.MissingLocalizationClose);
                        }
                        else
                        {
                            SetLocation(Settings.Settings.Instance.Places.ElementAt(0).Add.PlaceName,
                                        Settings.Settings.Instance.Places.ElementAt(0).Add.Latitude,
                                        Settings.Settings.Instance.Places.ElementAt(0).Add.Longitude);
                        }
                    }
                }
                catch (FeatureNotSupportedException fnsEx)
                {
                    await Application.Current.MainPage.DisplayAlert(Resources.AppTranslations.MissingLocalizationTitle,
                                                                    Resources.AppTranslations.MissingLocalizationText,
                                                                    Resources.AppTranslations.MissingLocalizationClose);
                }
                catch (FeatureNotEnabledException fneEx)
                {
                    await Application.Current.MainPage.DisplayAlert(Resources.AppTranslations.MissingLocalizationTitle,
                                                                    Resources.AppTranslations.MissingLocalizationText,
                                                                    Resources.AppTranslations.MissingLocalizationClose);
                }
                catch (PermissionException pEx)
                {
                    await Application.Current.MainPage.DisplayAlert(Resources.AppTranslations.MissingLocalizationTitle,
                                                                    Resources.AppTranslations.MissingLocalizationText,
                                                                    Resources.AppTranslations.MissingLocalizationClose);
                }
                catch (Exception ex)
                {
                    if (Settings.Settings.Instance.Places.Count == 0)
                    {
                        await Application.Current.MainPage.DisplayAlert(Resources.AppTranslations.NoInternetTitle,
                                                                        Resources.AppTranslations.NoInternetText,
                                                                        Resources.AppTranslations.NoInternetClose);
                    }
                    else
                    {
                        SetLocation(Settings.Settings.Instance.Places.ElementAt(0).Add.PlaceName,
                                    Settings.Settings.Instance.Places.ElementAt(0).Add.Latitude,
                                    Settings.Settings.Instance.Places.ElementAt(0).Add.Longitude);
                    }
                }
                IsLoading = false;
            },
                canExecute: () => true);

            Connectivity.ConnectivityChanged += (sender, args) =>
            {
                Refresh();
            };
        }