Exemplo n.º 1
0
        private void OnPanelClick(object sender, EventArgs e)
        {
            View view = sender as View;

            if (view == null && e != null && e is RecyclerClickEventArgs)
            {
                view = (e as RecyclerClickEventArgs).View;
            }

            if (view != null && view.Enabled && view.Tag is LocationData locData)
            {
                if (locData.Equals(Settings.HomeData))
                {
                    // Pop all since we're going home
                    AppCompatActivity.SupportFragmentManager
                    .PopBackStack(null, (int)Android.App.PopBackStackFlags.Inclusive);
                }
                else
                {
                    // Navigate to WeatherNowFragment
                    Fragment fragment = WeatherNowFragment.NewInstance(locData);
                    AppCompatActivity.SupportFragmentManager.BeginTransaction()
                    .Add(Resource.Id.fragment_container, fragment, null)
                    .Hide(this)
                    .AddToBackStack(null)
                    .Commit();
                }
            }
        }
        /**
         * Use this factory method to create a new instance of
         * this fragment using the provided parameters.
         *
         * @param query Weather query.
         * @param index Location index.
         * @return A new instance of fragment WeatherNowFragment.
         */
        public static WeatherNowFragment NewInstance(LocationData data)
        {
            var fragment = new WeatherNowFragment();

            if (data != null)
            {
                var args = new Bundle();
                args.PutString("data", data.ToJson());
                fragment.Arguments = args;
            }
            return(fragment);
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.activity_main);

            // Create your application here
            Toolbar toolbar = FindViewById <Toolbar>(Resource.Id.toolbar);

            SetSupportActionBar(toolbar);

            DrawerLayout drawer = FindViewById <DrawerLayout>(Resource.Id.drawer_layout);

            Android.Support.V7.App.ActionBarDrawerToggle toggle = new Android.Support.V7.App.ActionBarDrawerToggle(
                this, drawer, toolbar, Resource.String.navigation_drawer_open, Resource.String.navigation_drawer_close);
            drawer.AddDrawerListener(toggle);
            toggle.SyncState();

            NavigationView navigationView = FindViewById <NavigationView>(Resource.Id.nav_view);

            navigationView.SetNavigationItemSelectedListener(this);

            // Back stack listener
            SupportFragmentManager.BackStackChanged += delegate { RefreshNavViewCheckedItem(); };

            Fragment fragment = SupportFragmentManager.FindFragmentById(Resource.Id.fragment_container);

            // Alerts
            if (WeatherWidgetService.ACTION_SHOWALERTS.Equals(Intent?.Action))
            {
                Fragment newFragment = WeatherNowFragment.NewInstance(Intent.Extras);

                if (fragment == null)
                {
                    fragment = newFragment;
                    // Navigate to WeatherNowFragment
                    // Make sure we exit if location is not home
                    SupportFragmentManager.BeginTransaction()
                    .Replace(Resource.Id.fragment_container, fragment, "notification")
                    .Commit();
                }
                else
                {
                    // Navigate to WeatherNowFragment
                    // Make sure we exit if location is not home
                    SupportFragmentManager.BeginTransaction()
                    .Add(Resource.Id.fragment_container, newFragment)
                    .AddToBackStack(null)
                    .Commit();
                }
            }

            // Check if fragment exists
            if (fragment == null)
            {
                if (Intent.HasExtra("data"))
                {
                    fragment = WeatherNowFragment.NewInstance(Intent.Extras);
                }
                else
                {
                    fragment = new WeatherNowFragment();
                }

                // Navigate to WeatherNowFragment
                SupportFragmentManager.BeginTransaction()
                .Replace(Resource.Id.fragment_container, fragment, "home")
                .Commit();
            }

            if ((bool)Intent?.HasExtra("shortcut-data"))
            {
                using (var jsonTextReader = new Newtonsoft.Json.JsonTextReader(
                           new System.IO.StringReader(Intent.GetStringExtra("shortcut-data"))))
                {
                    var locData = LocationData.FromJson(jsonTextReader);

                    // Navigate to WeatherNowFragment
                    Fragment newFragment = WeatherNowFragment.NewInstance(locData);

                    SupportFragmentManager.BeginTransaction()
                    .Replace(Resource.Id.fragment_container, newFragment, "shortcut")
                    .Commit();

                    // Disable navigation
                    toggle.DrawerIndicatorEnabled = false;
                    drawer.SetDrawerLockMode(DrawerLayout.LockModeLockedClosed);
                }
            }

            navigationView.SetCheckedItem(Resource.Id.nav_weathernow);

            Task.Run(Shortcuts.ShortcutCreator.UpdateShortcuts);
        }
        public bool OnNavigationItemSelected(IMenuItem item)
        {
            // Handle navigation view item clicks here.
            int id = item.ItemId;

            DrawerLayout        drawer      = FindViewById <DrawerLayout>(Resource.Id.drawer_layout);
            FragmentTransaction transaction = SupportFragmentManager.BeginTransaction();
            Fragment            fragment    = null;

            if (id == Resource.Id.nav_weathernow)
            {
                fragment = new WeatherNowFragment();
            }
            else if (id == Resource.Id.nav_locations)
            {
                fragment = new LocationsFragment();
            }
            else if (id == Resource.Id.nav_settings)
            {
                StartActivity(new Intent(this, typeof(SettingsActivity)));
                drawer.CloseDrawer(GravityCompat.Start);
                return(false);
            }

            if (fragment != null)
            {
                if (fragment.Class != SupportFragmentManager.FindFragmentById(Resource.Id.fragment_container).Class)
                {
                    if (fragment is WeatherNowFragment)
                    {
                        // Pop all since we're going home
                        fragment = null;
                        transaction.Commit();
                        SupportFragmentManager.PopBackStackImmediate(null, (int)Android.App.PopBackStackFlags.Inclusive);
                    }
                    else if (fragment is LocationsFragment)
                    {
                        Fragment current = SupportFragmentManager.FindFragmentById(Resource.Id.fragment_container);

                        if (current is WeatherNowFragment)
                        {
                            // Hide home frag
                            if (current.Tag == "home")
                            {
                                transaction.Hide(current);
                            }
                            else
                            {
                                // Destroy lingering WNow frag
                                SupportFragmentManager.BeginTransaction()
                                .Remove(current)
                                .Commit();
                                current.OnDestroy();
                                current = null;
                                SupportFragmentManager.PopBackStack();
                            }
                        }

                        if (SupportFragmentManager.FindFragmentByTag("locations") != null)
                        {
                            // Pop all frags if LocFrag in backstack
                            fragment = null;
                            transaction.Commit();
                        }
                        else
                        {
                            // Add LocFrag if not in backstack
                            // Commit the transaction
                            transaction
                            .Add(Resource.Id.fragment_container, fragment, "locations")
                            .AddToBackStack(null)
                            .Commit();
                        }
                    }
                }
            }

            drawer.CloseDrawer(GravityCompat.Start);
            return(true);
        }