Search in sources :

Example 1 with FragmentSearch

use of com.bigyoshi.qrhunt.bottom_navigation.search.FragmentSearch in project QRHunt by CMPUT301W22T00.

the class MainActivity method onCreate.

/**
 * Sets up screen (toolbar, bottom menu), initializes player if need be
 * manages which fragment the app is in and adjusts accordingly, & passes things from
 * fragment to fragment
 *
 * @param savedInstanceState SavedInstanceState
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    db = FirebaseFirestore.getInstance();
    Toolbar toolbar = findViewById(R.id.top_navigation_view);
    setSupportActionBar(toolbar);
    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayShowTitleEnabled(false);
    actionbar.setDisplayShowCustomEnabled(true);
    player = new Player(this);
    // This will check if the player already has an account
    if (!player.getPlayerId().matches("")) {
        player.initialize();
    }
    scoreView = toolbar.findViewById(R.id.top_scanner_score);
    updateFirebaseListeners();
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    NavController navController = Navigation.findNavController(this, R.id.main_bottom_navigation_host_fragment);
    NavigationUI.setupWithNavController(binding.bottomNavigationView, navController);
    navProfile = findViewById(R.id.top_navigation_profile);
    navProfile.setOnClickListener(view -> {
        binding.bottomNavigationView.setVisibility(View.INVISIBLE);
        FragmentProfile profile = new FragmentProfile();
        Bundle bundle = new Bundle();
        bundle.putSerializable("player", player);
        profile.setArguments(bundle);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.container, profile, "profile");
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    });
    navSearch = findViewById(R.id.top_navigation_search);
    navSearch.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            binding.bottomNavigationView.setVisibility(View.INVISIBLE);
            actionbar.hide();
            FragmentSearch search = new FragmentSearch(player, navController.getCurrentDestination().getId());
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.container, search, "search");
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
    // determines current fragment so the right button is visible
    navController.addOnDestinationChangedListener((navController1, navDestination, bundle) -> {
        if (navDestination.getId() == R.id.navigation_map) {
            actionbar.show();
            navSearch.setVisibility(View.GONE);
            // TEMPORARY
            navProfile.setVisibility(View.GONE);
        }
        if (navDestination.getId() == R.id.navigation_scanner) {
            actionbar.show();
            Bundle result = new Bundle();
            result.putSerializable("player", player);
            getSupportFragmentManager().setFragmentResult("getPlayer", result);
            navSearch.setVisibility(View.VISIBLE);
            navProfile.setVisibility(View.VISIBLE);
        }
        if (navDestination.getId() == R.id.navigation_leaderBoard) {
            actionbar.hide();
            binding.bottomNavigationView.setVisibility(View.INVISIBLE);
        }
    });
    Intent intent = this.getIntent();
    Bundle s = intent.getExtras();
    int prevFrag = -1;
    if (s != null) {
        prevFrag = (int) s.getSerializable("previous");
    }
    if (prevFrag == R.id.navigation_map) {
        actionbar.show();
        navSearch.setVisibility(View.GONE);
    // Need to figure out how to go to the Map -> currently goes to Scanner (start dest)
    // For now I made this invisible
    } else if (prevFrag == R.id.navigation_scanner) {
        actionbar.show();
        navSearch.setVisibility(View.VISIBLE);
    }
}
Also used : Player(com.bigyoshi.qrhunt.player.Player) Bundle(android.os.Bundle) NavController(androidx.navigation.NavController) Intent(android.content.Intent) FragmentProfile(com.bigyoshi.qrhunt.player.FragmentProfile) View(android.view.View) TextView(android.widget.TextView) FragmentManager(androidx.fragment.app.FragmentManager) FragmentSearch(com.bigyoshi.qrhunt.bottom_navigation.search.FragmentSearch) FragmentTransaction(androidx.fragment.app.FragmentTransaction) ActionBar(androidx.appcompat.app.ActionBar) Toolbar(androidx.appcompat.widget.Toolbar)

Aggregations

Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 View (android.view.View)1 TextView (android.widget.TextView)1 ActionBar (androidx.appcompat.app.ActionBar)1 Toolbar (androidx.appcompat.widget.Toolbar)1 FragmentManager (androidx.fragment.app.FragmentManager)1 FragmentTransaction (androidx.fragment.app.FragmentTransaction)1 NavController (androidx.navigation.NavController)1 FragmentSearch (com.bigyoshi.qrhunt.bottom_navigation.search.FragmentSearch)1 FragmentProfile (com.bigyoshi.qrhunt.player.FragmentProfile)1 Player (com.bigyoshi.qrhunt.player.Player)1