Skip to content

Commit

Permalink
Merge pull request #9 from tvbarthel/vb/color-item-name
Browse files Browse the repository at this point in the history
Add an optionnal name to the color items.
  • Loading branch information
tbarthel-fr committed May 31, 2015
2 parents 97b1bfb + fb38574 commit 53294ed
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,37 @@
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import fr.tvbarthel.apps.cameracolorpicker.R;
import fr.tvbarthel.apps.cameracolorpicker.data.ColorItem;
import fr.tvbarthel.apps.cameracolorpicker.data.ColorItems;
import fr.tvbarthel.apps.cameracolorpicker.data.Palette;
import fr.tvbarthel.apps.cameracolorpicker.data.Palettes;
import fr.tvbarthel.apps.cameracolorpicker.fragments.DeleteColorDialogFragment;
import fr.tvbarthel.apps.cameracolorpicker.fragments.EditTextDialogFragment;
import fr.tvbarthel.apps.cameracolorpicker.utils.ClipDatas;

public class ColorDetailActivity extends AppCompatActivity implements View.OnClickListener, DeleteColorDialogFragment.Callback {
public class ColorDetailActivity extends AppCompatActivity implements View.OnClickListener,
DeleteColorDialogFragment.Callback, EditTextDialogFragment.Callback {

/**
* A key for passing a color item as extra.
Expand All @@ -46,11 +55,10 @@ public class ColorDetailActivity extends AppCompatActivity implements View.OnCli
private static final String EXTRA_START_BOUNDS = "ColorDetailActivity.Extras.EXTRA_START_BOUNDS";

/**
* A key for knowing if the {@link ColorItem} can be deleted.
* <p/>
* If the color item can not be deleted, the delete action will be removed from the menu.
* A key for passing an optional palette that is associated with the color item displayed.
*
*/
private static final String EXTRA_CAN_BE_DELETED = "ColorDetailActivity.Extras.EXTRA_CAN_BE_DELETED";
private static final String EXTRA_PALETTE = "ColorDetailActivity.Extras.EXTRA_PALETTE";

/**
* The quality of the image compressed before sharing.
Expand All @@ -77,16 +85,26 @@ public class ColorDetailActivity extends AppCompatActivity implements View.OnCli
*/
private static final String FILE_PROVIDER_AUTHORITY = "fr.tvbarthel.apps.cameracolorpicker.fileprovider";

public static void startWithColorItem(Context context, ColorItem colorItem, View colorPreviewClicked,
boolean canBeDeleted) {
/**
* A request code to use in {@link EditTextDialogFragment#newInstance(int, int, int, int, String, String, boolean)}.
*/
private static final int REQUEST_CODE_EDIT_COLOR_ITEM_NAME = 15;

public static void startWithColorItem(Context context, ColorItem colorItem,
View colorPreviewClicked){
startWithColorItem(context, colorItem, colorPreviewClicked, null);
}

public static void startWithColorItem(Context context, ColorItem colorItem,
View colorPreviewClicked, Palette palette) {
final boolean isActivity = context instanceof Activity;
final Rect startBounds = new Rect();
colorPreviewClicked.getGlobalVisibleRect(startBounds);

final Intent intent = new Intent(context, ColorDetailActivity.class);
intent.putExtra(EXTRA_COLOR_ITEM, colorItem);
intent.putExtra(EXTRA_START_BOUNDS, startBounds);
intent.putExtra(EXTRA_CAN_BE_DELETED, canBeDeleted);
intent.putExtra(EXTRA_PALETTE, palette);

if (!isActivity) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Expand Down Expand Up @@ -137,11 +155,9 @@ public static void startWithColorItem(Context context, ColorItem colorItem, View
private ColorItem mColorItem;

/**
* A boolean for knowing if the {@link ColorItem} can be deleted or not.
* <p/>
* If false, the delete action will be removed from the menu.
* An optional {@link Palette} that is associated with the {@link ColorItem}.
*/
private boolean mCanBeDeleted;
private Palette mPalette;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -152,14 +168,26 @@ protected void onCreate(Bundle savedInstanceState) {
// ensure correct extras.
final Intent intent = getIntent();
if (!intent.hasExtra(EXTRA_COLOR_ITEM) || !intent.hasExtra(EXTRA_START_BOUNDS)
|| !intent.hasExtra(EXTRA_CAN_BE_DELETED)) {
|| !intent.hasExtra(EXTRA_PALETTE)) {
throw new IllegalStateException("Missing extras. Please use startWithColorItem.");
}

// Retrieve the extras.
mColorItem = intent.getParcelableExtra(EXTRA_COLOR_ITEM);
final Rect startBounds = intent.getParcelableExtra(EXTRA_START_BOUNDS);
mCanBeDeleted = intent.getBooleanExtra(EXTRA_CAN_BE_DELETED, true);
if (savedInstanceState == null) {
mColorItem = intent.getParcelableExtra(EXTRA_COLOR_ITEM);
mPalette = intent.getParcelableExtra(EXTRA_PALETTE);
} else {
mColorItem = savedInstanceState.getParcelable(EXTRA_COLOR_ITEM);
mPalette = savedInstanceState.getParcelable(EXTRA_PALETTE);
}

// Set the title of the activity with the name of the color, if not null.
if (!TextUtils.isEmpty(mColorItem.getName())) {
setTitle(mColorItem.getName());
} else {
setTitle(mColorItem.getHexString());
}

// Create a rect that will be used to retrieve the stop bounds.
final Rect stopBounds = new Rect();
Expand Down Expand Up @@ -241,11 +269,19 @@ protected void onPause() {
hideToast();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(EXTRA_COLOR_ITEM, mColorItem);
outState.putParcelable(EXTRA_PALETTE, mPalette);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_color_detail, menu);
if (!mCanBeDeleted) {
if (mPalette != null) {
// A color associated with a palette can't be deleted.
menu.removeItem(R.id.menu_color_detail_action_delete);
}
return true;
Expand All @@ -266,6 +302,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
finish();
} else if (id == R.id.menu_color_detail_action_share) {
return handleActionShare();
} else if (id == R.id.menu_color_detail_action_edit) {
EditTextDialogFragment.newInstance(REQUEST_CODE_EDIT_COLOR_ITEM_NAME,
R.string.activity_color_detail_edit_text_dialog_fragment_title,
R.string.activity_color_detail_edit_text_dialog_fragment_positive_button,
android.R.string.cancel,
mColorItem.getHexString(),
mColorItem.getName(), true).show(getSupportFragmentManager(), null);
}

return super.onOptionsItemSelected(item);
Expand Down Expand Up @@ -300,6 +343,45 @@ public void onColorDeletionConfirmed(@NonNull ColorItem colorItemToDelete) {
}
}

@Override
public void onEditTextDialogFragmentPositiveButtonClick(int requestCode, String text) {
if (requestCode == REQUEST_CODE_EDIT_COLOR_ITEM_NAME) {
// Update the title of the activity.
if (TextUtils.isEmpty(text)) {
setTitle(mColorItem.getHexString());
} else {
setTitle(text);
}

// Set the new name.
mColorItem.setName(text);

// Persist the change.
if (mPalette == null) {
// The color item is a standalone color.
// It's not associated with a palette.
// Just save the color item.
ColorItems.saveColorItem(this, mColorItem);
} else {
// The color item is associated with a palette.
// Edit and save the palette.
final List<ColorItem> colorItems = mPalette.getColors();
for (ColorItem candidate : colorItems) {
if (candidate.getId() == mColorItem.getId()) {
candidate.setName(text);
break;
}
}
Palettes.saveColorPalette(this, mPalette);
}
}
}

@Override
public void onEditTextDialogFragmentNegativeButtonClick(int requestCode) {
// nothing to do here.
}


protected void clipColor(int labelResourceId, CharSequence colorString) {
ClipDatas.clipPainText(this, getString(labelResourceId), colorString);
Expand Down Expand Up @@ -379,5 +461,4 @@ private boolean handleActionShare() {

return handled;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public void onClick(View v) {
R.string.activity_palette_creation_edit_text_dialog_fragment_title,
R.string.activity_palette_creation_edit_text_dialog_fragment_positive_button,
android.R.string.cancel,
R.string.activity_palette_creation_edit_text_dialog_fragment_hint,
getString(R.string.activity_palette_creation_edit_text_dialog_fragment_hint),
null);
editTextDialogFragment.show(getSupportFragmentManager(), null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.content.FileProvider;
Expand Down Expand Up @@ -129,6 +130,11 @@ public static void startWithColorPalette(Context context, Palette palette, View
*/
private Toast mToast;

/**
* A {@link fr.tvbarthel.apps.cameracolorpicker.data.Palettes.OnPaletteChangeListener} for updating the palette when the user change the name for instance.
*/
private Palettes.OnPaletteChangeListener mOnPaletteChangeListener;


@Override
public void onCreate(Bundle savedInstanceState) {
Expand All @@ -144,7 +150,11 @@ public void onCreate(Bundle savedInstanceState) {
}

// Retrieve the extras.
mPalette = intent.getParcelableExtra(EXTRA_COLOR_PALETTE);
if (savedInstanceState == null) {
mPalette = intent.getParcelableExtra(EXTRA_COLOR_PALETTE);
} else {
mPalette = savedInstanceState.getParcelable(EXTRA_COLOR_PALETTE);
}
final Rect startBounds = intent.getParcelableExtra(EXTRA_START_BOUNDS);
setTitle(mPalette.getName());

Expand All @@ -164,7 +174,7 @@ public void onCreate(Bundle savedInstanceState) {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final ColorItem colorItem = adapter.getItem(position);
ColorDetailActivity.startWithColorItem(view.getContext(), colorItem,
view.findViewById(R.id.row_color_item_preview), false);
view.findViewById(R.id.row_color_item_preview), mPalette);
}
});

Expand Down Expand Up @@ -230,6 +240,39 @@ public void onAnimationRepeat(Animator animation) {
}
});
}

mOnPaletteChangeListener = new Palettes.OnPaletteChangeListener() {
@Override
public void onColorPaletteChanged(List<Palette> palettes) {
Palette newPalette = null;
for (Palette candidate : palettes) {
if (candidate.getId() == mPalette.getId()) {
newPalette = candidate;
break;
}
}

if (newPalette == null) {
// The palette opened is not in the saved palettes.
// It has been deleted, just finish the activity.
finish();
} else {
// Reload the palette.
mPalette = newPalette;
setTitle(mPalette.getName());
adapter.clear();
adapter.addAll(mPalette.getColors());
}
}
};

Palettes.registerListener(this, mOnPaletteChangeListener);
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(EXTRA_COLOR_PALETTE, mPalette);
}

@Override
Expand All @@ -238,6 +281,12 @@ protected void onPause() {
super.onPause();
}

@Override
protected void onDestroy() {
Palettes.unregisterListener(this, mOnPaletteChangeListener);
super.onDestroy();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
Expand Down Expand Up @@ -267,18 +316,18 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
public void onPaletteDeletionConfirmed(@NonNull Palette paletteToDelete) {
if (Palettes.deleteColorPalette(this, paletteToDelete)) {
finish();
}
// Delete the palette
// Note: we don't finish the activity, it will be finished in mOnPaletteChangeListener.
Palettes.deleteColorPalette(this, paletteToDelete);
}

@Override
public void onEditTextDialogFragmentPositiveButtonClick(int requestCode, String text) {
if (!mPalette.getName().equals(text)) {
// Set the new name and save the palette.
// Note: we don't update the UI there, it will be updated in mOnPaletteChangeListener.
mPalette.setName(text);
if (Palettes.saveColorPalette(this, mPalette)) {
setTitle(text);
}
Palettes.saveColorPalette(this, mPalette);
}
}

Expand All @@ -295,7 +344,7 @@ private boolean handleActionEditName() {
R.string.activity_palette_detail_edit_palette_name_dialog_title,
R.string.activity_palette_detail_edit_palette_name_dialog_positive_action,
android.R.string.cancel,
R.string.activity_palette_detail_edit_palette_name_dialog_hint,
getString(R.string.activity_palette_detail_edit_palette_name_dialog_hint),
mPalette.getName()).show(getSupportFragmentManager(), null);
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.graphics.PorterDuff;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -59,7 +60,11 @@ protected View createView(ViewGroup parent) {
protected void bindViewHolder(ViewHolder viewHolder, int position) {
final ColorItem colorItem = getItem(position);
viewHolder.mColorPreview.getBackground().setColorFilter(colorItem.getColor(), PorterDuff.Mode.MULTIPLY);
viewHolder.mColorText.setText(colorItem.getHexString());
if (!TextUtils.isEmpty(colorItem.getName())) {
viewHolder.mColorText.setText(colorItem.getName());
} else {
viewHolder.mColorText.setText(colorItem.getHexString());
}
}

/**
Expand Down
Loading

0 comments on commit 53294ed

Please sign in to comment.