예제 #1
0
파일: MainActivity.cs 프로젝트: Gokhu18/CSE
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // login check
            var prefs = new AppPreferences(this.ApplicationContext);

            if (prefs.GetUserName().Length == 0)
            {
                var intent = new Intent(this, typeof(LoginActivity));
                StartActivity(intent);
                Finish();
            }

            SetContentView(Resource.Layout.activity_main);

            var toolbar = FindViewById <Toolbar>(Resource.Id.toolbar);


            _drawerLayout   = FindViewById <DrawerLayout>(Resource.Id.drawer_layout);
            _navigationView = FindViewById <NavigationView>(Resource.Id.nav_view);


            var nav_header = _navigationView.GetHeaderView(0).FindViewById <TextView>(Resource.Id.navheader_username);

            nav_header.Text = prefs.GetUserName();

            SetSupportActionBar(toolbar);
            var drawerToggle = new Android.Support.V7.App.ActionBarDrawerToggle(this, _drawerLayout, Resource.String.drawer_open,
                                                                                Resource.String.drawer_close);

            _drawerLayout.SetDrawerListener(drawerToggle);
            drawerToggle.SyncState();

            SupportActionBar.SetDisplayHomeAsUpEnabled(true);
            SupportActionBar.SetDisplayShowTitleEnabled(false);
            SupportActionBar.SetHomeButtonEnabled(true);


            SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu_black_24dp);

            _stackFragment = new Stack <SupportFragment>();

            // add fragments to layout and hide all except for main
            var receiptLotteryMainFragment = new ReceiptLotteryMainFragment();
            var mainFragment = new MainFragment();
            var trans        = SupportFragmentManager.BeginTransaction();

            trans.Add(Resource.Id.fragmentContainer, mainFragment, newFragmentId.ToString());
            trans.Commit();
            newFragmentId++;

            _currFragment = mainFragment;

            _navigationView.NavigationItemSelected += (sender, e) => {
                e.MenuItem.SetChecked(true);
                //react to click here and swap fragments or navigate
                long id = e.MenuItem.ItemId;
                switch (id)
                {
                case Resource.Id.nav_lottery:
                    ShowFragment(receiptLotteryMainFragment);
                    break;

                case Resource.Id.nav_home:
                    ShowFragment(mainFragment);
                    break;

                case Resource.Id.nav_signout:
                    var ap = new AppPreferences(this.ApplicationContext);
                    ap.SaveUserName("");
                    var intent = new Intent(this, typeof(LoginActivity));
                    StartActivity(intent);
                    Finish();
                    break;

                case Resource.Id.nav_yourReceipt:
                    if (itemList != null)
                    {
                        ShowFragment(confirmedListFragment);
                    }
                    else
                    {
                        ShowFragment(mainFragment);
                        Toast.MakeText(ApplicationContext, "Please scan receipt first!", ToastLength.Long).Show();
                    }
                    break;

                    /*
                     * case Resource.Id.nav_about:
                     * var aboutFragment = new AboutFragment();
                     * ShowFragment(aboutFragment);
                     * break;
                     * case Resource.Id.nav_statistics:
                     * var statisticsFragment = new StatisticsFragment();
                     * ShowFragment(statisticsFragment);
                     * break;
                     * case Resource.Id.nav_settings:
                     * var settingsFragment = new SettingsFragment();
                     * ShowFragment(settingsFragment);
                     * break;
                     */
                }

                _drawerLayout.CloseDrawers();
            };
        }