began refactoring to avoid code duplicates
This commit is contained in:
parent
4d841133da
commit
07be587796
4 changed files with 66 additions and 27 deletions
|
|
@ -0,0 +1,44 @@
|
||||||
|
package de.cdaut.climategoapp.activities;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.drawerlayout.widget.DrawerLayout;
|
||||||
|
|
||||||
|
import de.cdaut.climategoapp.R;
|
||||||
|
|
||||||
|
public final class ActivityUtils {
|
||||||
|
private ActivityUtils() {
|
||||||
|
throw new IllegalAccessError("Utility Class");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean handleNavBarItemClicks(@NonNull MenuItem item,
|
||||||
|
AppCompatActivity rootClass,
|
||||||
|
DrawerLayout drawer) {
|
||||||
|
|
||||||
|
Class<? extends AppCompatActivity> targetActivityClass;
|
||||||
|
|
||||||
|
switch (item.getItemId()) {
|
||||||
|
case R.id.nav_main:
|
||||||
|
targetActivityClass = HomeActivity.class;
|
||||||
|
break;
|
||||||
|
case R.id.nav_debug:
|
||||||
|
targetActivityClass = DebugActivity.class;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new IllegalAccessError("Illegal activity met in nav drawer");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (targetActivityClass.equals(rootClass.getClass())) {
|
||||||
|
drawer.closeDrawers();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Intent targetIntent = new Intent(rootClass, targetActivityClass);
|
||||||
|
rootClass.startActivity(targetIntent);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
package de.cdaut.climategoapp.activities;
|
package de.cdaut.climategoapp.activities;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.ActionBarDrawerToggle;
|
import androidx.appcompat.app.ActionBarDrawerToggle;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
|
|
@ -18,7 +20,9 @@ import de.cdaut.climategoapp.databinding.ActivityDebugBinding;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
|
||||||
public class DebugActivity extends AppCompatActivity {
|
import com.google.android.material.navigation.NavigationView;
|
||||||
|
|
||||||
|
public class DebugActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
|
||||||
|
|
||||||
private AppBarConfiguration appBarConfiguration;
|
private AppBarConfiguration appBarConfiguration;
|
||||||
private ActivityDebugBinding binding;
|
private ActivityDebugBinding binding;
|
||||||
|
|
@ -37,7 +41,7 @@ public class DebugActivity extends AppCompatActivity {
|
||||||
|
|
||||||
// drawer layout instance to toggle the menu icon to open
|
// drawer layout instance to toggle the menu icon to open
|
||||||
// drawer and back button to close drawer
|
// drawer and back button to close drawer
|
||||||
drawerLayout = findViewById(R.id.my_drawer_layout);
|
drawerLayout = findViewById(R.id.debug_drawer_layout);
|
||||||
actionBarDrawerToggle = new ActionBarDrawerToggle(this,
|
actionBarDrawerToggle = new ActionBarDrawerToggle(this,
|
||||||
drawerLayout,
|
drawerLayout,
|
||||||
R.string.nav_open,
|
R.string.nav_open,
|
||||||
|
|
@ -50,6 +54,8 @@ public class DebugActivity extends AppCompatActivity {
|
||||||
|
|
||||||
// to make the Navigation drawer icon always appear on the action bar
|
// to make the Navigation drawer icon always appear on the action bar
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
||||||
|
|
||||||
|
setNavigationViewListener();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -84,4 +90,14 @@ public class DebugActivity extends AppCompatActivity {
|
||||||
return NavigationUI.navigateUp(navController, appBarConfiguration)
|
return NavigationUI.navigateUp(navController, appBarConfiguration)
|
||||||
|| super.onSupportNavigateUp();
|
|| super.onSupportNavigateUp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
||||||
|
return ActivityUtils.handleNavBarItemClicks(item, this, drawerLayout);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setNavigationViewListener() {
|
||||||
|
NavigationView navigationView = findViewById(R.id.nav_drawer);
|
||||||
|
navigationView.setNavigationItemSelectedListener(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -52,7 +52,7 @@ public class HomeActivity extends AppCompatActivity implements NavigationView.On
|
||||||
actionBarDrawerToggle.syncState();
|
actionBarDrawerToggle.syncState();
|
||||||
|
|
||||||
// to make the Navigation drawer icon always appear on the action bar
|
// to make the Navigation drawer icon always appear on the action bar
|
||||||
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
getSupportActionBar(). setDisplayHomeAsUpEnabled(true);
|
||||||
|
|
||||||
setNavigationViewListener();
|
setNavigationViewListener();
|
||||||
}
|
}
|
||||||
|
|
@ -93,29 +93,7 @@ public class HomeActivity extends AppCompatActivity implements NavigationView.On
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
||||||
|
return ActivityUtils.handleNavBarItemClicks(item, this, drawerLayout);
|
||||||
Class<? extends AppCompatActivity> targetActivityClass;
|
|
||||||
|
|
||||||
switch (item.getItemId()) {
|
|
||||||
case R.id.nav_main:
|
|
||||||
targetActivityClass = HomeActivity.class;
|
|
||||||
break;
|
|
||||||
case R.id.nav_debug:
|
|
||||||
targetActivityClass = DebugActivity.class;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new IllegalAccessError("Illegal activity met in nav drawer");
|
|
||||||
}
|
|
||||||
|
|
||||||
if(targetActivityClass.equals(this.getClass())) {
|
|
||||||
drawerLayout.closeDrawers();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Intent targetIntent = new Intent(this, targetActivityClass);
|
|
||||||
startActivity(targetIntent);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setNavigationViewListener() {
|
private void setNavigationViewListener() {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:id="@+id/my_drawer_layout"
|
android:id="@+id/debug_drawer_layout"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".activities.HomeActivity"
|
tools:context=".activities.HomeActivity"
|
||||||
|
|
@ -28,6 +28,7 @@
|
||||||
and shows the navigation drawer-->
|
and shows the navigation drawer-->
|
||||||
<!--include the menu created in the menu folder-->
|
<!--include the menu created in the menu folder-->
|
||||||
<com.google.android.material.navigation.NavigationView
|
<com.google.android.material.navigation.NavigationView
|
||||||
|
android:id="@+id/nav_drawer"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_gravity="start"
|
android:layout_gravity="start"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue