@CheckReturnValue
  @NonNull public static Observable<Boolean> observeNightModeState(
      @NonNull Context context, @StringRes int enablePrefResId, @BoolRes int defaultValueResId) {
    final Observable<Boolean> nightMode =
        ((AnyApplication) context.getApplicationContext()).getNightModeObservable();
    final RxSharedPrefs prefs = AnyApplication.prefs(context);
    return Observable.combineLatest(
            prefs
                .getString(
                    R.string.settings_key_night_mode, R.string.settings_default_night_mode_value)
                .asObservable(),
            enablePrefResId == 0
                ? Observable.just(true)
                : prefs.getBoolean(enablePrefResId, defaultValueResId).asObservable(),
            nightMode,
            (nightModePref, enabledPref, nightModeState) -> {
              if (!enabledPref) return false;

              switch (nightModePref) {
                case "never":
                  return false;
                case "always":
                  return true;
                default:
                  return nightModeState;
              }
            })
        .distinctUntilChanged();
  }
@Test
  public void testAlwaysNightMode() {
    SharedPrefsHelper.setPrefsValue(R.string.settings_key_night_mode, "always");

    AtomicBoolean atomicBoolean = new AtomicBoolean();
    AnyApplication application = getApplicationContext();
    final Disposable subscribe =
        NightMode.observeNightModeState(application, 0, R.bool.settings_default_true)
            .subscribe(
                atomicBoolean::set,
                throwable -> {
                  throw new RuntimeException(throwable);
                });
    Assert.assertTrue(atomicBoolean.get());

    application.onConfigurationChanged(configurationForNightMode(Configuration.UI_MODE_NIGHT_YES));
    Assert.assertTrue(atomicBoolean.get());

    application.onConfigurationChanged(configurationForNightMode(Configuration.UI_MODE_NIGHT_NO));
    Assert.assertTrue(atomicBoolean.get());

    application.onConfigurationChanged(
        configurationForNightMode(Configuration.UI_MODE_NIGHT_UNDEFINED));
    Assert.assertTrue(atomicBoolean.get());

    application.onConfigurationChanged(configurationForNightMode(Configuration.UI_MODE_NIGHT_YES));
    Assert.assertTrue(atomicBoolean.get());

    subscribe.dispose();
  }