Skip to content

Commit

Permalink
Extract night mode code so it works on CheeseDetailActivity too
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-caryl committed Mar 24, 2017
1 parent 081893e commit 22269b3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@

import com.bumptech.glide.Glide;

import java.util.Random;

public class CheeseDetailActivity extends AppCompatActivity {

public static final String EXTRA_NAME = "cheese_name";
private NightModeUtils nightModeUtils;

@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -50,6 +49,8 @@ public void onCreate(Bundle savedInstanceState) {
collapsingToolbar.setTitle(cheeseName);

loadBackdrop();

nightModeUtils = NightModeUtils.from(this);
}

private void loadBackdrop() {
Expand All @@ -62,4 +63,16 @@ public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sample_actions, menu);
return true;
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
nightModeUtils.checkNightMode(menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
return nightModeUtils.onNightModeItemSelected(item) || super.onOptionsItemSelected(item);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.support.android.designlibdemo;

import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
Expand All @@ -30,14 +29,10 @@
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -48,6 +43,7 @@
public class MainActivity extends AppCompatActivity {

private DrawerLayout mDrawerLayout;
private NightModeUtils nightModeUtils;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -84,6 +80,8 @@ public void onClick(View view) {

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);

nightModeUtils = NightModeUtils.from(this);
}

@Override
Expand All @@ -94,50 +92,22 @@ public boolean onCreateOptionsMenu(Menu menu) {

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
switch (AppCompatDelegate.getDefaultNightMode()) {
case AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM:
menu.findItem(R.id.menu_night_mode_system).setChecked(true);
break;
case AppCompatDelegate.MODE_NIGHT_AUTO:
menu.findItem(R.id.menu_night_mode_auto).setChecked(true);
break;
case AppCompatDelegate.MODE_NIGHT_YES:
menu.findItem(R.id.menu_night_mode_night).setChecked(true);
break;
case AppCompatDelegate.MODE_NIGHT_NO:
menu.findItem(R.id.menu_night_mode_day).setChecked(true);
break;
}
nightModeUtils.checkNightMode(menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
case R.id.menu_night_mode_system:
setNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
break;
case R.id.menu_night_mode_day:
setNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case R.id.menu_night_mode_night:
setNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case R.id.menu_night_mode_auto:
setNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
break;
}
return super.onOptionsItemSelected(item);
}

private void setNightMode(@AppCompatDelegate.NightMode int nightMode) {
AppCompatDelegate.setDefaultNightMode(nightMode);

if (Build.VERSION.SDK_INT >= 11) {
recreate();
if (nightModeUtils.onNightModeItemSelected(item)) {
return true;
} else {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.support.android.designlibdemo;

import android.app.Activity;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatDelegate;
import android.view.Menu;
import android.view.MenuItem;

class NightModeUtils {
private Activity activity;

private NightModeUtils(@NonNull Activity activity) {
this.activity = activity;
}

static NightModeUtils from(@NonNull Activity activity) {
return new NightModeUtils(activity);
}

boolean checkNightMode(@NonNull Menu menu) {
switch (AppCompatDelegate.getDefaultNightMode()) {
case AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM:
menu.findItem(R.id.menu_night_mode_system).setChecked(true);
break;
case AppCompatDelegate.MODE_NIGHT_AUTO:
menu.findItem(R.id.menu_night_mode_auto).setChecked(true);
break;
case AppCompatDelegate.MODE_NIGHT_YES:
menu.findItem(R.id.menu_night_mode_night).setChecked(true);
break;
case AppCompatDelegate.MODE_NIGHT_NO:
menu.findItem(R.id.menu_night_mode_day).setChecked(true);
break;
default:
return false;
}
return true;
}

public boolean onNightModeItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_night_mode_system:
setNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
break;
case R.id.menu_night_mode_day:
setNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
case R.id.menu_night_mode_night:
setNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case R.id.menu_night_mode_auto:
setNightMode(AppCompatDelegate.MODE_NIGHT_AUTO);
break;
default:
return false;
}
return true;
}

private void setNightMode(@AppCompatDelegate.NightMode int nightMode) {
AppCompatDelegate.setDefaultNightMode(nightMode);

if (Build.VERSION.SDK_INT >= 11) {
activity.recreate();
}
}

}

0 comments on commit 22269b3

Please sign in to comment.