Skip to content

Commit

Permalink
Merge pull request #7 from StephaneBg/master
Browse files Browse the repository at this point in the history
New way to handle listeners
  • Loading branch information
J-Jamet authored Mar 7, 2017
2 parents 9f57a74 + 630f547 commit 5b491b6
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 108 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

<img src="https://raw.githubusercontent.com/Kunzisoft/AndroidClearChroma/master/art/logo.png"> A customisable material color picker view for Android.

<img src="https://raw.githubusercontent.com/Kunzisoft/AndroidClearChroma/master/art/movie1.gif" width="250">
<img src="https://raw.githubusercontent.com/Kunzisoft/AndroidClearChroma/master/art/movie1.gif" width="505">
<img src="https://raw.githubusercontent.com/Kunzisoft/AndroidClearChroma/master/art/screen1.png" width="250">
<img src="https://raw.githubusercontent.com/Kunzisoft/AndroidClearChroma/master/art/screen4.png" width="505">

- supports RGB, ARGB, HSV, HSL, CMYK, CMYK255 color modes (with alpha preview)
- can indicate current color in either DECIMAL or HEXADECIMAL mode
Expand Down Expand Up @@ -45,19 +44,30 @@ And add the dependency

### ChromaDialog

To display a color picker `DialogFragment`:
To display a color picker `DialogFragment` from your Activity:
``` java
new ChromaDialog.Builder()
.initialColor(Color.GREEN)
.colorMode(ColorMode.ARGB) // RGB, ARGB, HVS, CMYK, CMYK255, HSL
.indicatorMode(IndicatorMode.HEX) //HEX or DECIMAL; Note that (HSV || HSL || CMYK) && IndicatorMode.HEX is a bad idea
.setOnColorSelectedListener(OnColorSelectedListener selectListener)
.setOnColorChangedListener(OnColorChangedListener changeListener)
.create()
.show(getSupportFragmentManager(), "ChromaDialog");
```

To display a color picker `DialogFragment` from your Fragment:
``` java
new ChromaDialog.Builder()
.initialColor(Color.GREEN)
.colorMode(ColorMode.ARGB) // RGB, ARGB, HVS, CMYK, CMYK255, HSL
.indicatorMode(IndicatorMode.HEX) //HEX or DECIMAL; Note that (HSV || HSL || CMYK) && IndicatorMode.HEX is a bad idea
.create()
.show(getChildFragmentManager(), "ChromaDialog");
```

### Listeners

Your parent Activity or Fragment must implement the listener interfaces.

#### OnColorSelectedListener
*OnColorSelectedListener* contains two methods :
`void onPositiveButtonClick(@ColorInt int color)`called when positiveButton is clicked and
Expand All @@ -72,6 +82,8 @@ new ChromaDialog.Builder()
for complete sample of ChromaDialog

## Style

<img src="https://raw.githubusercontent.com/Kunzisoft/AndroidClearChroma/master/art/screen4.png" width="505">
For custom dialog, simply redefined following nodes :
```
<style name="Chroma.AlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
Expand Down
Binary file modified art/movie1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Fri Mar 03 15:12:45 CET 2017
#Mon Mar 06 08:45:57 CET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kunzisoft.androidclearchroma;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
Expand All @@ -8,6 +9,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.TypedValue;
Expand All @@ -20,7 +22,6 @@

import com.kunzisoft.androidclearchroma.colormode.ColorMode;
import com.kunzisoft.androidclearchroma.fragment.ChromaColorFragment;
import com.kunzisoft.androidclearchroma.listener.OnColorChangedListener;
import com.kunzisoft.androidclearchroma.listener.OnColorSelectedListener;

import static com.kunzisoft.androidclearchroma.fragment.ChromaColorFragment.ARG_COLOR_MODE;
Expand All @@ -42,9 +43,6 @@ public class ChromaDialog extends DialogFragment {

private final static String TAG_FRAGMENT_COLORS = "TAG_FRAGMENT_COLORS";

private OnColorChangedListener onColorChangedListener;
private OnColorSelectedListener onColorSelectedListener;

private ChromaColorFragment chromaColorFragment;

public static ChromaDialog newInstance(String key, @ColorInt int initialColor, ColorMode colorMode, IndicatorMode indicatorMode) {
Expand Down Expand Up @@ -85,7 +83,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
chromaColorFragment = (ChromaColorFragment) fragmentManager.findFragmentByTag(TAG_FRAGMENT_COLORS);
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

if(chromaColorFragment == null) {
if (chromaColorFragment == null) {
chromaColorFragment = ChromaColorFragment.newInstance(getArguments());
fragmentTransaction.add(R.id.color_dialog_container, chromaColorFragment, TAG_FRAGMENT_COLORS).commit();
}
Expand All @@ -98,17 +96,27 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
positiveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onColorSelectedListener != null)
onColorSelectedListener.onPositiveButtonClick(chromaColorFragment.getCurrentColor());
final Activity activity = getActivity();
final Fragment fragment = getTargetFragment();
if (activity instanceof OnColorSelectedListener) {
((OnColorSelectedListener) activity).onPositiveButtonClick(chromaColorFragment.getCurrentColor());
} else if (fragment instanceof OnColorSelectedListener) {
((OnColorSelectedListener) fragment).onPositiveButtonClick(chromaColorFragment.getCurrentColor());
}
dismiss();
}
});

negativeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(onColorSelectedListener != null)
onColorSelectedListener.onNegativeButtonClick(chromaColorFragment.getCurrentColor());
final Activity activity = getActivity();
final Fragment fragment = getTargetFragment();
if (activity instanceof OnColorSelectedListener) {
((OnColorSelectedListener) activity).onNegativeButtonClick(chromaColorFragment.getCurrentColor());
} else if (fragment instanceof OnColorSelectedListener) {
((OnColorSelectedListener) fragment).onNegativeButtonClick(chromaColorFragment.getCurrentColor());
}
dismiss();
}
});
Expand All @@ -118,6 +126,7 @@ public void onClick(View v) {

/**
* Set new dimensions to dialog
*
* @param ad
*/
private void measureLayout(Dialog ad) {
Expand All @@ -130,19 +139,19 @@ private void measureLayout(Dialog ad) {
float widthMultiplier = typedValue.getFloat();
int width = (int) ((ad.getContext().getResources().getDisplayMetrics().widthPixels) * widthMultiplier);

if(ad.getWindow() != null)
if (ad.getWindow() != null)
ad.getWindow().setLayout(width, height);
}

/**
* Builder class for ChromaDialog objects. Provides a convenient way to set the various fields of a Chroma Dialog and generate fragment associated.
*/
public static class Builder {
private @ColorInt int initialColor = DEFAULT_COLOR;
private
@ColorInt
int initialColor = DEFAULT_COLOR;
private ColorMode colorMode = DEFAULT_MODE;
private IndicatorMode indicatorMode = IndicatorMode.DECIMAL;
private OnColorChangedListener onColorChangedListener;
private OnColorSelectedListener onColorSelectedListener;

public Builder initialColor(@ColorInt int initialColor) {
this.initialColor = initialColor;
Expand All @@ -166,21 +175,8 @@ public Builder indicatorMode(IndicatorMode indicatorMode) {
return this;
}

public Builder setOnColorChangedListener(OnColorChangedListener onColorChangedListener) {
this.onColorChangedListener = onColorChangedListener;
return this;
}

public Builder setOnColorSelectedListener(OnColorSelectedListener onColorSelectedListener) {
this.onColorSelectedListener = onColorSelectedListener;
return this;
}

public ChromaDialog create() {
ChromaDialog chromaDialog = newInstance(initialColor, colorMode, indicatorMode);
chromaDialog.setOnColorChangedListener(onColorChangedListener);
chromaDialog.setOnColorSelectedListener(onColorSelectedListener);
return chromaDialog;
return newInstance(initialColor, colorMode, indicatorMode);
}
}

Expand All @@ -205,25 +201,10 @@ public void onShow(DialogInterface dialog) {

/**
* Get key associated to preference, or null if key not present
*
* @return a String value, or null
*/
public String getKeyPreference() {
return getArguments().getString(ARG_KEY);
}

public OnColorSelectedListener getOnColorSelectedListener() {
return onColorSelectedListener;
}

public OnColorChangedListener getOnColorChangedListener() {
return onColorChangedListener;
}

public void setOnColorChangedListener(OnColorChangedListener onColorChangedListener) {
this.onColorChangedListener = onColorChangedListener;
}

public void setOnColorSelectedListener(OnColorSelectedListener onColorSelectedListener) {
this.onColorSelectedListener = onColorSelectedListener;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ public abstract class ChromaPreferenceFragmentCompat extends PreferenceFragmentC

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
View view = super.onCreateView(inflater, container, savedInstanceState);

ChromaDialog chromaDialog = (ChromaDialog) getChildFragmentManager().findFragmentByTag(TAG_FRAGMENT_DIALOG);
if(chromaDialog != null) {
if (chromaDialog != null) {
String keyPreference = chromaDialog.getKeyPreference();
if(keyPreference != null)
if (keyPreference != null)
currentPreference = getPreferenceManager().findPreference(keyPreference);
chromaDialog.setOnColorSelectedListener(this);
}

return view;
Expand All @@ -53,7 +52,6 @@ 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 All @@ -75,5 +73,6 @@ public void onPositiveButtonClick(@ColorInt int color) {
}

@Override
public void onNegativeButtonClick(@ColorInt int color) {}
public void onNegativeButtonClick(@ColorInt int color) {
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package com.kunzisoft.androidclearchroma.fragment;

import android.content.Context;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.v4.app.Fragment;
import android.support.v7.widget.AppCompatImageView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.kunzisoft.androidclearchroma.IndicatorMode;
import com.kunzisoft.androidclearchroma.listener.OnColorChangedListener;
import com.kunzisoft.androidclearchroma.R;
import com.kunzisoft.androidclearchroma.colormode.Channel;
import com.kunzisoft.androidclearchroma.colormode.ColorMode;
import com.kunzisoft.androidclearchroma.listener.OnColorChangedListener;
import com.kunzisoft.androidclearchroma.listener.OnColorSelectedListener;
import com.kunzisoft.androidclearchroma.view.ChannelView;

import java.util.ArrayList;
Expand All @@ -29,7 +29,7 @@
* Created by joker on 16/02/17.
*/

public class ChromaColorFragment extends Fragment{
public class ChromaColorFragment extends Fragment {

private static final String TAG = "ChromaColorFragment";

Expand All @@ -39,12 +39,12 @@ public class ChromaColorFragment extends Fragment{
public final static String ARG_COLOR_MODE = "arg_color_mode";
public final static String ARG_INDICATOR_MODE = "arg_indicator_mode";

private @ColorInt int currentColor;
private
@ColorInt
int currentColor;
private ColorMode colorMode;
private IndicatorMode indicatorMode;

private OnColorChangedListener onColorChangedListener;

public static ChromaColorFragment newInstance(@ColorInt int initialColor, ColorMode colorMode, IndicatorMode indicatorMode) {
ChromaColorFragment chromaColorFragment = new ChromaColorFragment();

Expand All @@ -64,17 +64,6 @@ public static ChromaColorFragment newInstance(Bundle args) {
return chromaColorFragment;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

try {
onColorChangedListener = (OnColorChangedListener) context;
} catch(ClassCastException e) {
Log.w(TAG, context.toString() + "doesn't implement" + OnColorChangedListener.class.getSimpleName());
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand All @@ -91,7 +80,7 @@ private void init(ViewGroup root, Bundle savedInstanceState) {
if (savedInstanceState != null) {
// Restore last state for checked position.
assignArguments(savedInstanceState);
} else if(getArguments() != null) {
} else if (getArguments() != null) {
assignArguments(getArguments());
}
if (currentColor == 0)
Expand All @@ -107,29 +96,34 @@ private void init(ViewGroup root, Bundle savedInstanceState) {

List<Channel> channels = colorMode.getColorMode().getChannels();
final List<ChannelView> channelViews = new ArrayList<>();
for(Channel channel : channels) {
for (Channel channel : channels) {
channelViews.add(new ChannelView(channel, currentColor, indicatorMode, getContext()));
}

ChannelView.OnProgressChangedListener seekBarChangeListener = new ChannelView.OnProgressChangedListener() {
@Override
public void onProgressChanged() {
List<Channel> channels = new ArrayList<>();
for(ChannelView chan : channelViews) {
for (ChannelView chan : channelViews) {
channels.add(chan.getChannel());
}
currentColor = colorMode.getColorMode().evaluateColor(channels);
// Listener for color selected in real time
if(onColorChangedListener !=null)
onColorChangedListener.onColorChanged(currentColor);
final Activity activity = getActivity();
final Fragment fragment = getTargetFragment();
if (activity instanceof OnColorSelectedListener) {
((OnColorChangedListener) activity).onColorChanged(currentColor);
} else if (fragment instanceof OnColorSelectedListener) {
((OnColorChangedListener) fragment).onColorChanged(currentColor);
}
// Change view for visibility of color
Drawable colorViewDrawable = new ColorDrawable(currentColor);
colorView.setImageDrawable(colorViewDrawable);
}
};

ViewGroup channelContainer = (ViewGroup) root.findViewById(R.id.channel_container);
for(ChannelView c : channelViews) {
for (ChannelView c : channelViews) {
channelContainer.addView(c);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) c.getLayoutParams();
params.topMargin =
Expand All @@ -150,11 +144,11 @@ public void onSaveInstanceState(Bundle outState) {
}

private void assignArguments(Bundle args) {
if(args.containsKey(ARG_INITIAL_COLOR))
if (args.containsKey(ARG_INITIAL_COLOR))
currentColor = args.getInt(ARG_INITIAL_COLOR);
if(args.containsKey(ARG_COLOR_MODE))
if (args.containsKey(ARG_COLOR_MODE))
colorMode = ColorMode.getColorModeFromId(args.getInt(ARG_COLOR_MODE));
if(args.containsKey(ARG_INDICATOR_MODE))
if (args.containsKey(ARG_INDICATOR_MODE))
indicatorMode = IndicatorMode.getIndicatorModeFromId(args.getInt(ARG_INDICATOR_MODE));
}

Expand All @@ -176,12 +170,4 @@ public ColorMode getColorMode() {
public IndicatorMode getIndicatorMode() {
return indicatorMode;
}

public OnColorChangedListener getOnColorChangedListener() {
return onColorChangedListener;
}

public void setOnColorChangedListener(OnColorChangedListener onColorChangedListener) {
this.onColorChangedListener = onColorChangedListener;
}
}
Loading

0 comments on commit 5b491b6

Please sign in to comment.