Smooth closing of the navigation drawer. Change observerModeSwitch checking state when a menu item is clicked
@@ -10,20 +10,6 @@ import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
-import androidx.annotation.IdRes;
-import androidx.annotation.NonNull;
-import com.google.android.material.navigation.NavigationView;
-
-import androidx.core.content.ContextCompat;
-import androidx.core.view.ViewCompat;
-import androidx.fragment.app.Fragment;
-import androidx.core.view.GravityCompat;
-import androidx.drawerlayout.widget.DrawerLayout;
-import androidx.appcompat.app.ActionBarDrawerToggle;
-import androidx.appcompat.app.AppCompatActivity;
-import androidx.appcompat.widget.SwitchCompat;
-import androidx.appcompat.widget.Toolbar;
-
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuItem;
@@ -34,14 +20,27 @@ import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.Toast;
+import com.google.android.material.navigation.NavigationView;
+
import org.jetbrains.annotations.NotNull;
-import androidx.recyclerview.widget.RecyclerView;
-import butterknife.BindView;
import java.io.File;
import javax.inject.Inject;
+import androidx.annotation.IdRes;
+import androidx.annotation.NonNull;
+import androidx.appcompat.app.ActionBarDrawerToggle;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.appcompat.widget.SwitchCompat;
+import androidx.appcompat.widget.Toolbar;
+import androidx.core.content.ContextCompat;
+import androidx.core.view.GravityCompat;
+import androidx.core.view.ViewCompat;
+import androidx.drawerlayout.widget.DrawerLayout;
+import androidx.fragment.app.Fragment;
+import androidx.recyclerview.widget.RecyclerView;
+import butterknife.BindView;
import butterknife.ButterKnife;
import de.stephanlindauer.criticalmaps.handler.ApplicationCloseHandler;
import de.stephanlindauer.criticalmaps.handler.PermissionCheckHandler;
@@ -88,6 +87,8 @@ public class Main extends AppCompatActivity implements NavigationView.OnNavigati
@BindView(R.id.content_frame)
FrameLayout contentFrame;
+ private SwitchCompat observerModeSwitch;
+
private final SharedPreferences.OnSharedPreferenceChangeListener sharedPreferenceChangeListener =
(sharedPreferences, key) -> {
if (SharedPrefsKeys.SHOW_ON_LOCKSCREEN.equals(key)) {
@@ -168,10 +169,15 @@ public class Main extends AppCompatActivity implements NavigationView.OnNavigati
drawerLayout.addDrawerListener(mDrawerToggle);
mDrawerToggle.syncState();
- drawerLayout.addDrawerListener(new DrawerClosingDrawerLayoutListener());
+ drawerLayout.addDrawerListener(new DrawerClosingDrawerLayoutListener() {
+ @Override
+ public void onDrawerClosed(@NonNull View drawerView) {
+ super.onDrawerClosed(drawerView);
+ navigateTo(drawerNavigation.getCheckedItem().getItemId());
+ }
+ });
- SwitchCompat observerModeSwitch =
- drawerNavigation.getMenu().findItem(R.id.navigation_observer_mode)
+ observerModeSwitch = drawerNavigation.getMenu().findItem(R.id.navigation_observer_mode)
.getActionView().findViewById(R.id.navigation_observer_mode_switch);
observerModeSwitch.setChecked(new BooleanPreference(
sharedPreferences, SharedPrefsKeys.OBSERVER_MODE_ACTIVE).get());
@@ -342,10 +348,12 @@ public class Main extends AppCompatActivity implements NavigationView.OnNavigati
drawerLayout.closeDrawer(GravityCompat.START);
//noinspection ConstantConditions
getSupportActionBar().setTitle(item.getTitle());
- navigateTo(item.getItemId());
return true;
}
- //TODO set switch checked when observer switch item selected?
+ if (item.getItemId() == R.id.navigation_observer_mode) {
+ observerModeSwitch.setChecked(!observerModeSwitch.isChecked());
+ return true;
+ }
return false;
}
