示例#1
0
        private void SetupView(View view)
        {
            mProgressBar = mActivity.FindViewById <ProgressBar>(Resource.Id.search_progressBar);
            mClearButton = mActivity.FindViewById(Resource.Id.search_close_button);
            mSearchView  = mActivity.FindViewById <EditText>(Resource.Id.search_view);

            if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
            {
                mProgressBar.IndeterminateDrawable =
                    ContextCompat.GetDrawable(mActivity, Resource.Drawable.progressring);
            }

            mRecyclerView = view.FindViewById <RecyclerView>(Resource.Id.recycler_view);

            // use this setting to improve performance if you know that changes
            // in content do not change the layout size of the RecyclerView
            mRecyclerView.HasFixedSize = true;

            // use a linear layout manager
            mLayoutManager = new LinearLayoutManager(mActivity);
            mRecyclerView.SetLayoutManager(mLayoutManager);

            // specify an adapter (see also next example)
            mAdapter            = new LocationQueryAdapter(new List <LocationQueryViewModel>());
            mAdapter.ItemClick += ClickListener;
            mRecyclerView.SetAdapter(mAdapter);
        }
示例#2
0
        private void AddSearchFragment()
        {
            if (mSearchFragment != null)
            {
                return;
            }

            FragmentTransaction    ft             = ChildFragmentManager.BeginTransaction();
            LocationSearchFragment searchFragment = new LocationSearchFragment();

            searchFragment.SetClickListener(async(object sender, RecyclerClickEventArgs e) =>
            {
                if (mSearchFragment == null)
                {
                    return;
                }

                LocationQueryAdapter adapter = sender as LocationQueryAdapter;
                LocationQuery v = (LocationQuery)e.View;
                LocationQueryViewModel query_vm = null;

                try
                {
                    if (!String.IsNullOrEmpty(adapter.Dataset[e.Position].LocationQuery))
                    {
                        query_vm = adapter.Dataset[e.Position];
                    }
                }
                catch (Exception)
                {
                    query_vm = null;
                }
                finally
                {
                    if (query_vm == null)
                    {
                        query_vm = new LocationQueryViewModel();
                    }
                }

                if (String.IsNullOrWhiteSpace(query_vm.LocationQuery))
                {
                    // Stop since there is no valid query
                    return;
                }

                // Cancel other tasks
                mSearchFragment.CtsCancel();
                var ctsToken = mSearchFragment.GetCancellationTokenSource().Token;

                ShowLoading(true);

                if (ctsToken.IsCancellationRequested)
                {
                    ShowLoading(false);
                    return;
                }

                // Check if location already exists
                var locData = await Settings.GetLocationData();
                if (locData.Exists(l => l.query == query_vm.LocationQuery))
                {
                    ShowLoading(false);
                    ExitSearchUi();
                    return;
                }

                if (ctsToken.IsCancellationRequested)
                {
                    ShowLoading(false);
                    return;
                }

                var location = new LocationData(query_vm);
                if (!location.IsValid())
                {
                    Toast.MakeText(App.Context, App.Context.GetString(Resource.String.werror_noweather), ToastLength.Short).Show();
                    ShowLoading(false);
                    return;
                }
                Weather weather = await Settings.GetWeatherData(location.query);
                if (weather == null)
                {
                    try
                    {
                        weather = await wm.GetWeather(location);
                    }
                    catch (WeatherException wEx)
                    {
                        weather = null;
                        Toast.MakeText(App.Context, wEx.Message, ToastLength.Short).Show();
                    }
                }

                if (weather == null)
                {
                    ShowLoading(false);
                    return;
                }

                // We got our data so disable controls just in case
                mAdapter.Dataset.Clear();
                mAdapter.NotifyDataSetChanged();

                if (mSearchFragment?.View != null &&
                    mSearchFragment?.View?.FindViewById(Resource.Id.recycler_view) is RecyclerView recyclerView)
                {
                    recyclerView.Enabled = false;
                }

                // Save data
                await Settings.AddLocation(location);
                if (wm.SupportsAlerts && weather.weather_alerts != null)
                {
                    await Settings.SaveWeatherAlerts(location, weather.weather_alerts);
                }
                await Settings.SaveWeatherData(weather);

                var panel = new LocationPanelViewModel(weather)
                {
                    LocationData = location
                };

                // Set properties if necessary
                if (EditMode)
                {
                    panel.EditMode = true;
                }

                int index = mAdapter.Dataset.Count;
                mAdapter.Add(panel);

                // Update shortcuts
                Task.Run(Shortcuts.ShortcutCreator.UpdateShortcuts);

                // Hide dialog
                ShowLoading(false);
                ExitSearchUi();
            });
            searchFragment.UserVisibleHint = false;
            ft.Add(Resource.Id.search_fragment_container, searchFragment);
            ft.CommitAllowingStateLoss();
        }
示例#3
0
        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            var view = base.OnCreateView(inflater, container, savedInstanceState) as SwipeDismissFrameLayout;

            // Inflate the layout for this fragment
            inflater.Inflate(Resource.Layout.fragment_location_search, view, true);

            swipeViewLayout          = view.FindViewById <SwipeDismissFrameLayout>(Resource.Id.recycler_view_layout);
            swipeCallback            = new SwipeDismissCallback();
            swipeCallback.Dismissed += (layout) =>
            {
                layout.Visibility = ViewStates.Gone;
                //layout.Reset();
            };
            swipeViewLayout.AddCallback(swipeCallback);
            keyboardButton        = view.FindViewById <FloatingActionButton>(Resource.Id.keyboard_button);
            keyboardButton.Click += (sender, e) =>
            {
                mSearchView.Visibility = ViewStates.Visible;
                mSearchView.RequestFocus();
                ShowInputMethod(mSearchView);
            };
            voiceButton        = view.FindViewById <FloatingActionButton>(Resource.Id.voice_button);
            voiceButton.Click += (sender, e) =>
            {
                mSearchView.Visibility = ViewStates.Gone;
                mSearchView.Text       = String.Empty;
                view.RequestFocus();
                Intent intent = new Intent(RecognizerIntent.ActionRecognizeSpeech)
                                .PutExtra(RecognizerIntent.ExtraLanguageModel, RecognizerIntent.LanguageModelFreeForm)
                                .PutExtra(RecognizerIntent.ExtraPrompt, mActivity.GetString(Resource.String.location_search_hint));
                StartActivityForResult(intent, REQUEST_CODE_VOICE_INPUT);
            };

            mProgressBar             = view.FindViewById <ProgressBar>(Resource.Id.progressBar);
            mSearchView              = view.FindViewById <EditText>(Resource.Id.search_view);
            mSearchView.TextChanged += (object sender, TextChangedEventArgs e) =>
            {
                // If we're using searchfragment
                // make sure gps feature is off
                if (Settings.FollowGPS)
                {
                    Settings.FollowGPS = false;
                }
            };
            mSearchView.FocusChange += (object sender, View.FocusChangeEventArgs e) =>
            {
                View v = sender as View;

                if (e.HasFocus)
                {
                    ShowInputMethod(v.FindFocus());
                }
                else
                {
                    HideInputMethod(v);
                }
            };
            mSearchView.EditorAction += (object sender, TextView.EditorActionEventArgs e) =>
            {
                if (e.ActionId == ImeAction.Search)
                {
                    DoSearchAction();

                    // If we're using searchfragment
                    // make sure gps feature is off
                    if (Settings.FollowGPS)
                    {
                        Settings.FollowGPS = false;
                    }

                    e.Handled = true;
                }
                e.Handled = false;
            };

            mRecyclerView = view.FindViewById <WearableRecyclerView>(Resource.Id.recycler_view);

            // use this setting to improve performance if you know that changes
            // in content do not change the layout size of the RecyclerView
            mRecyclerView.HasFixedSize = true;

            // To align the edge children (first and last) with the center of the screen
            mRecyclerView.EdgeItemsCenteringEnabled = true;

            // use a linear layout manager
            mLayoutManager = new WearableLinearLayoutManager(mActivity);
            mRecyclerView.SetLayoutManager(mLayoutManager);

            // specify an adapter (see also next example)
            mAdapter            = new LocationQueryAdapter(new List <LocationQueryViewModel>());
            mAdapter.ItemClick += ClickListener;
            mRecyclerView.SetAdapter(mAdapter);

            return(view);
        }