From e7226265b3323d9b18b2d8373b95bf8875c67ead Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Tue, 7 Mar 2017 17:54:48 +0100 Subject: [PATCH] Solve bug with pull request --- .../androidclearchroma/ChromaDialog.java | 28 +++++++++++++++++-- .../ChromaPreferenceFragmentCompat.java | 2 ++ .../fragment/ChromaColorFragment.java | 4 +-- .../sample/FragmentColorActivity.java | 18 ++++++------ 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/library/src/main/java/com/kunzisoft/androidclearchroma/ChromaDialog.java b/library/src/main/java/com/kunzisoft/androidclearchroma/ChromaDialog.java index 42097d0..563e72f 100644 --- a/library/src/main/java/com/kunzisoft/androidclearchroma/ChromaDialog.java +++ b/library/src/main/java/com/kunzisoft/androidclearchroma/ChromaDialog.java @@ -43,6 +43,8 @@ public class ChromaDialog extends DialogFragment { private final static String TAG_FRAGMENT_COLORS = "TAG_FRAGMENT_COLORS"; + private OnColorSelectedListener onColorSelectedListener; + private ChromaColorFragment chromaColorFragment; public static ChromaDialog newInstance(String key, @ColorInt int initialColor, ColorMode colorMode, IndicatorMode indicatorMode) { @@ -87,7 +89,6 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, chromaColorFragment = ChromaColorFragment.newInstance(getArguments()); fragmentTransaction.add(R.id.color_dialog_container, chromaColorFragment, TAG_FRAGMENT_COLORS).commit(); } - chromaColorFragment.setOnColorChangedListener(onColorChangedListener); LinearLayout buttonBar = (LinearLayout) root.findViewById(R.id.button_bar); Button positiveButton = (Button) buttonBar.findViewById(R.id.positive_button); @@ -98,7 +99,9 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, public void onClick(View v) { final Activity activity = getActivity(); final Fragment fragment = getTargetFragment(); - if (activity instanceof OnColorSelectedListener) { + if(onColorSelectedListener != null) { + onColorSelectedListener.onPositiveButtonClick(chromaColorFragment.getCurrentColor()); + } else if (activity instanceof OnColorSelectedListener) { ((OnColorSelectedListener) activity).onPositiveButtonClick(chromaColorFragment.getCurrentColor()); } else if (fragment instanceof OnColorSelectedListener) { ((OnColorSelectedListener) fragment).onPositiveButtonClick(chromaColorFragment.getCurrentColor()); @@ -112,7 +115,9 @@ public void onClick(View v) { public void onClick(View v) { final Activity activity = getActivity(); final Fragment fragment = getTargetFragment(); - if (activity instanceof OnColorSelectedListener) { + if(onColorSelectedListener != null) { + onColorSelectedListener.onNegativeButtonClick(chromaColorFragment.getCurrentColor()); + } else if (activity instanceof OnColorSelectedListener) { ((OnColorSelectedListener) activity).onNegativeButtonClick(chromaColorFragment.getCurrentColor()); } else if (fragment instanceof OnColorSelectedListener) { ((OnColorSelectedListener) fragment).onNegativeButtonClick(chromaColorFragment.getCurrentColor()); @@ -207,4 +212,21 @@ public void onShow(DialogInterface dialog) { public String getKeyPreference() { return getArguments().getString(ARG_KEY); } + + /** + * Get color listener if it was defined by setter else return null + * @return + */ + public OnColorSelectedListener getOnColorSelectedListener() { + return onColorSelectedListener; + } + + /** + * Defined listener for click on positive and negative button + * You can implement OnColorSelectedListener in activity or fragment without use this setter for a better usability + * @param onColorSelectedListener + */ + public void setOnColorSelectedListener(OnColorSelectedListener onColorSelectedListener) { + this.onColorSelectedListener = onColorSelectedListener; + } } diff --git a/library/src/main/java/com/kunzisoft/androidclearchroma/ChromaPreferenceFragmentCompat.java b/library/src/main/java/com/kunzisoft/androidclearchroma/ChromaPreferenceFragmentCompat.java index 4c764dc..cabd4f9 100644 --- a/library/src/main/java/com/kunzisoft/androidclearchroma/ChromaPreferenceFragmentCompat.java +++ b/library/src/main/java/com/kunzisoft/androidclearchroma/ChromaPreferenceFragmentCompat.java @@ -34,6 +34,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa String keyPreference = chromaDialog.getKeyPreference(); if (keyPreference != null) currentPreference = getPreferenceManager().findPreference(keyPreference); + chromaDialog.setOnColorSelectedListener(this); } return view; @@ -52,6 +53,7 @@ public void onDisplayPreferenceDialog(Preference preference) { ((ChromaPreferenceCompat) preference).getColor(), ((ChromaPreferenceCompat) preference).getColorMode(), ((ChromaPreferenceCompat) preference).getIndicatorMode()); + dialogFragment.setOnColorSelectedListener(this); } // If it was one of our custom Preferences, show its dialog diff --git a/library/src/main/java/com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.java b/library/src/main/java/com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.java index b38c114..b37abc2 100644 --- a/library/src/main/java/com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.java +++ b/library/src/main/java/com/kunzisoft/androidclearchroma/fragment/ChromaColorFragment.java @@ -111,9 +111,9 @@ public void onProgressChanged() { // Listener for color selected in real time final Activity activity = getActivity(); final Fragment fragment = getTargetFragment(); - if (activity instanceof OnColorSelectedListener) { + if (activity instanceof OnColorChangedListener) { ((OnColorChangedListener) activity).onColorChanged(currentColor); - } else if (fragment instanceof OnColorSelectedListener) { + } else if (fragment instanceof OnColorChangedListener) { ((OnColorChangedListener) fragment).onColorChanged(currentColor); } // Change view for visibility of color diff --git a/sample/src/main/java/com/kunzisoft/androidclearchroma/sample/FragmentColorActivity.java b/sample/src/main/java/com/kunzisoft/androidclearchroma/sample/FragmentColorActivity.java index efb2ed5..62950ee 100644 --- a/sample/src/main/java/com/kunzisoft/androidclearchroma/sample/FragmentColorActivity.java +++ b/sample/src/main/java/com/kunzisoft/androidclearchroma/sample/FragmentColorActivity.java @@ -40,18 +40,16 @@ public void onCreate(Bundle savedInstanceState) { if (savedInstanceState != null) { initialColor = savedInstanceState.getInt(SAVED_COLOR, initialColor); } + onColorChanged(initialColor); - ChromaColorFragment chromaColorFragment = - (ChromaColorFragment) getSupportFragmentManager().findFragmentByTag(TAG_COLOR_FRAGMENT); - - if(chromaColorFragment == null) - chromaColorFragment = + if (null == savedInstanceState) { + ChromaColorFragment chromaColorFragment = ChromaColorFragment.newInstance(initialColor, ColorMode.ARGB, IndicatorMode.HEX); - onColorChanged(initialColor); - getSupportFragmentManager() - .beginTransaction() - .replace(R.id.container_color_fragment, chromaColorFragment, TAG_COLOR_FRAGMENT) - .commit(); + getSupportFragmentManager() + .beginTransaction() + .replace(R.id.container_color_fragment, chromaColorFragment, TAG_COLOR_FRAGMENT) + .commit(); + } } @Override