Skip to content

Commit

Permalink
Solve bug with pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Jamet committed Mar 7, 2017
1 parent 5b491b6 commit e722626
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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());
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e722626

Please sign in to comment.