/*
 * Copyright 2016 Peter Kenji Yamanaka
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pyamsoft.powermanager.app.base;

import android.os.Bundle;
import android.support.annotation.CheckResult;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.annotation.XmlRes;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.view.View;
import timber.log.Timber;

public abstract class BaseManagePreferenceFragment extends PreferenceFragmentCompat {

  private String manageKey;
  private String presetTimeKey;
  private String timeKey;

  @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(getPreferencesResId());
    manageKey = getString(getManageKeyResId());
    presetTimeKey = getString(getPresetTimeKeyResId());
    timeKey = getString(getTimeKeyResId());
  }

  @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    final Preference managePreference = findPreference(manageKey);
    managePreference.setOnPreferenceChangeListener((preference, newValue) -> {
      if (newValue instanceof Boolean) {
        final boolean b = (boolean) newValue;
        Timber.d("onPreferenceChange for key: %s", preference.getKey());
        return onManagePreferenceChanged(b);
      }
      return false;
    });
  }

  /**
   * Override if you implement any custom conditions for changing preferences
   */
  @CheckResult protected boolean onManagePreferenceChanged(boolean b) {
    return true;
  }

  @StringRes protected abstract int getManageKeyResId();

  @StringRes protected abstract int getPresetTimeKeyResId();

  @StringRes protected abstract int getTimeKeyResId();

  @XmlRes protected abstract int getPreferencesResId();
}

@@ -1689,16 +1689,54 @@
 geKey);%0A
+    if (managePreference != null) %7B%0A  
     mana
@@ -1806,16 +1806,18 @@
 e) -%3E %7B%0A
+  
       if
@@ -1853,24 +1853,26 @@
 ) %7B%0A        
+  
 final boolea
@@ -1905,16 +1905,18 @@
         
+ 
+ 
 Timber.d
@@ -1976,24 +1976,26 @@
 ));%0A        
+  
 return onMan
@@ -2025,18 +2025,22 @@
 ;%0A      
-%7D%0A
+  %7D%0A  
       re
@@ -2059,175 +2059,1034 @@
     
+  
 %7D);%0A  
-%7D%0A%0A  /**%0A   * Override if you implement any custom conditions for changing preferences%0A   */%0A  @CheckResult protected boolean onManagePreferenceChanged(boolean b
+  %7D%0A%0A    final Preference customTimePreference = findPreference(timeKey);%0A    final Preference presetTimePreference = findPreference(presetTimeKey);%0A    if (presetTimePreference != null) %7B%0A      presetTimePreference.setOnPreferenceChangeListener((preference, newValue) -%3E %7B%0A        if (newValue instanceof String) %7B%0A          final String string = (String) newValue;%0A          final long time = Long.parseLong(string);%0A          Timber.d(%22onPreferenceChange for key: %25s%22, preference.getKey());%0A          return onPresetTimePreferenceChanged(time, customTimePreference);%0A        %7D%0A        return false;%0A      %7D);%0A    %7D%0A  %7D%0A%0A  /**%0A   * Override if you implement any custom conditions for changing preferences%0A   */%0A  @CheckResult protected boolean onManagePreferenceChanged(boolean b) %7B%0A    return true;%0A  %7D%0A%0A  /**%0A   * Override if you implement any custom conditions for changing preferences%0A   */%0A  @CheckResult protected boolean onPresetTimePreferenceChanged(long time,%0A      @Nullable Preference customTimePreference
 ) %7B%0A

Add some more preference change hooks