Skip to content

Commit 53294ed

Browse files
committed
Merge pull request #9 from tvbarthel/vb/color-item-name
Add an optionnal name to the color items.
2 parents 97b1bfb + fb38574 commit 53294ed

File tree

11 files changed

+277
-46
lines changed

11 files changed

+277
-46
lines changed

CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/ColorDetailActivity.java

Lines changed: 98 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,37 @@
1212
import android.graphics.Rect;
1313
import android.net.Uri;
1414
import android.os.Bundle;
15+
import android.os.PersistableBundle;
1516
import android.support.annotation.NonNull;
1617
import android.support.v4.content.FileProvider;
1718
import android.support.v7.app.AppCompatActivity;
19+
import android.text.TextUtils;
1820
import android.view.Menu;
1921
import android.view.MenuItem;
2022
import android.view.View;
2123
import android.view.ViewTreeObserver;
2224
import android.widget.TextView;
2325
import android.widget.Toast;
2426

27+
import org.w3c.dom.Text;
28+
2529
import java.io.ByteArrayOutputStream;
2630
import java.io.File;
2731
import java.io.FileOutputStream;
2832
import java.io.IOException;
33+
import java.util.List;
2934

3035
import fr.tvbarthel.apps.cameracolorpicker.R;
3136
import fr.tvbarthel.apps.cameracolorpicker.data.ColorItem;
3237
import fr.tvbarthel.apps.cameracolorpicker.data.ColorItems;
38+
import fr.tvbarthel.apps.cameracolorpicker.data.Palette;
39+
import fr.tvbarthel.apps.cameracolorpicker.data.Palettes;
3340
import fr.tvbarthel.apps.cameracolorpicker.fragments.DeleteColorDialogFragment;
41+
import fr.tvbarthel.apps.cameracolorpicker.fragments.EditTextDialogFragment;
3442
import fr.tvbarthel.apps.cameracolorpicker.utils.ClipDatas;
3543

36-
public class ColorDetailActivity extends AppCompatActivity implements View.OnClickListener, DeleteColorDialogFragment.Callback {
44+
public class ColorDetailActivity extends AppCompatActivity implements View.OnClickListener,
45+
DeleteColorDialogFragment.Callback, EditTextDialogFragment.Callback {
3746

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

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

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

80-
public static void startWithColorItem(Context context, ColorItem colorItem, View colorPreviewClicked,
81-
boolean canBeDeleted) {
88+
/**
89+
* A request code to use in {@link EditTextDialogFragment#newInstance(int, int, int, int, String, String, boolean)}.
90+
*/
91+
private static final int REQUEST_CODE_EDIT_COLOR_ITEM_NAME = 15;
92+
93+
public static void startWithColorItem(Context context, ColorItem colorItem,
94+
View colorPreviewClicked){
95+
startWithColorItem(context, colorItem, colorPreviewClicked, null);
96+
}
97+
98+
public static void startWithColorItem(Context context, ColorItem colorItem,
99+
View colorPreviewClicked, Palette palette) {
82100
final boolean isActivity = context instanceof Activity;
83101
final Rect startBounds = new Rect();
84102
colorPreviewClicked.getGlobalVisibleRect(startBounds);
85103

86104
final Intent intent = new Intent(context, ColorDetailActivity.class);
87105
intent.putExtra(EXTRA_COLOR_ITEM, colorItem);
88106
intent.putExtra(EXTRA_START_BOUNDS, startBounds);
89-
intent.putExtra(EXTRA_CAN_BE_DELETED, canBeDeleted);
107+
intent.putExtra(EXTRA_PALETTE, palette);
90108

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

139157
/**
140-
* A boolean for knowing if the {@link ColorItem} can be deleted or not.
141-
* <p/>
142-
* If false, the delete action will be removed from the menu.
158+
* An optional {@link Palette} that is associated with the {@link ColorItem}.
143159
*/
144-
private boolean mCanBeDeleted;
160+
private Palette mPalette;
145161

146162
@Override
147163
protected void onCreate(Bundle savedInstanceState) {
@@ -152,14 +168,26 @@ protected void onCreate(Bundle savedInstanceState) {
152168
// ensure correct extras.
153169
final Intent intent = getIntent();
154170
if (!intent.hasExtra(EXTRA_COLOR_ITEM) || !intent.hasExtra(EXTRA_START_BOUNDS)
155-
|| !intent.hasExtra(EXTRA_CAN_BE_DELETED)) {
171+
|| !intent.hasExtra(EXTRA_PALETTE)) {
156172
throw new IllegalStateException("Missing extras. Please use startWithColorItem.");
157173
}
158174

159175
// Retrieve the extras.
160-
mColorItem = intent.getParcelableExtra(EXTRA_COLOR_ITEM);
161176
final Rect startBounds = intent.getParcelableExtra(EXTRA_START_BOUNDS);
162-
mCanBeDeleted = intent.getBooleanExtra(EXTRA_CAN_BE_DELETED, true);
177+
if (savedInstanceState == null) {
178+
mColorItem = intent.getParcelableExtra(EXTRA_COLOR_ITEM);
179+
mPalette = intent.getParcelableExtra(EXTRA_PALETTE);
180+
} else {
181+
mColorItem = savedInstanceState.getParcelable(EXTRA_COLOR_ITEM);
182+
mPalette = savedInstanceState.getParcelable(EXTRA_PALETTE);
183+
}
184+
185+
// Set the title of the activity with the name of the color, if not null.
186+
if (!TextUtils.isEmpty(mColorItem.getName())) {
187+
setTitle(mColorItem.getName());
188+
} else {
189+
setTitle(mColorItem.getHexString());
190+
}
163191

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

272+
@Override
273+
public void onSaveInstanceState(Bundle outState) {
274+
super.onSaveInstanceState(outState);
275+
outState.putParcelable(EXTRA_COLOR_ITEM, mColorItem);
276+
outState.putParcelable(EXTRA_PALETTE, mPalette);
277+
}
278+
244279
@Override
245280
public boolean onCreateOptionsMenu(Menu menu) {
246281
// Inflate the menu; this adds items to the action bar if it is present.
247282
getMenuInflater().inflate(R.menu.menu_color_detail, menu);
248-
if (!mCanBeDeleted) {
283+
if (mPalette != null) {
284+
// A color associated with a palette can't be deleted.
249285
menu.removeItem(R.id.menu_color_detail_action_delete);
250286
}
251287
return true;
@@ -266,6 +302,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
266302
finish();
267303
} else if (id == R.id.menu_color_detail_action_share) {
268304
return handleActionShare();
305+
} else if (id == R.id.menu_color_detail_action_edit) {
306+
EditTextDialogFragment.newInstance(REQUEST_CODE_EDIT_COLOR_ITEM_NAME,
307+
R.string.activity_color_detail_edit_text_dialog_fragment_title,
308+
R.string.activity_color_detail_edit_text_dialog_fragment_positive_button,
309+
android.R.string.cancel,
310+
mColorItem.getHexString(),
311+
mColorItem.getName(), true).show(getSupportFragmentManager(), null);
269312
}
270313

271314
return super.onOptionsItemSelected(item);
@@ -300,6 +343,45 @@ public void onColorDeletionConfirmed(@NonNull ColorItem colorItemToDelete) {
300343
}
301344
}
302345

346+
@Override
347+
public void onEditTextDialogFragmentPositiveButtonClick(int requestCode, String text) {
348+
if (requestCode == REQUEST_CODE_EDIT_COLOR_ITEM_NAME) {
349+
// Update the title of the activity.
350+
if (TextUtils.isEmpty(text)) {
351+
setTitle(mColorItem.getHexString());
352+
} else {
353+
setTitle(text);
354+
}
355+
356+
// Set the new name.
357+
mColorItem.setName(text);
358+
359+
// Persist the change.
360+
if (mPalette == null) {
361+
// The color item is a standalone color.
362+
// It's not associated with a palette.
363+
// Just save the color item.
364+
ColorItems.saveColorItem(this, mColorItem);
365+
} else {
366+
// The color item is associated with a palette.
367+
// Edit and save the palette.
368+
final List<ColorItem> colorItems = mPalette.getColors();
369+
for (ColorItem candidate : colorItems) {
370+
if (candidate.getId() == mColorItem.getId()) {
371+
candidate.setName(text);
372+
break;
373+
}
374+
}
375+
Palettes.saveColorPalette(this, mPalette);
376+
}
377+
}
378+
}
379+
380+
@Override
381+
public void onEditTextDialogFragmentNegativeButtonClick(int requestCode) {
382+
// nothing to do here.
383+
}
384+
303385

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

380462
return handled;
381463
}
382-
383464
}

CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/PaletteCreationActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public void onClick(View v) {
102102
R.string.activity_palette_creation_edit_text_dialog_fragment_title,
103103
R.string.activity_palette_creation_edit_text_dialog_fragment_positive_button,
104104
android.R.string.cancel,
105-
R.string.activity_palette_creation_edit_text_dialog_fragment_hint,
105+
getString(R.string.activity_palette_creation_edit_text_dialog_fragment_hint),
106106
null);
107107
editTextDialogFragment.show(getSupportFragmentManager(), null);
108108
}

CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/PaletteDetailActivity.java

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import android.graphics.RectF;
1414
import android.net.Uri;
1515
import android.os.Bundle;
16+
import android.os.PersistableBundle;
1617
import android.support.annotation.NonNull;
1718
import android.support.annotation.StringRes;
1819
import android.support.v4.content.FileProvider;
@@ -129,6 +130,11 @@ public static void startWithColorPalette(Context context, Palette palette, View
129130
*/
130131
private Toast mToast;
131132

133+
/**
134+
* A {@link fr.tvbarthel.apps.cameracolorpicker.data.Palettes.OnPaletteChangeListener} for updating the palette when the user change the name for instance.
135+
*/
136+
private Palettes.OnPaletteChangeListener mOnPaletteChangeListener;
137+
132138

133139
@Override
134140
public void onCreate(Bundle savedInstanceState) {
@@ -144,7 +150,11 @@ public void onCreate(Bundle savedInstanceState) {
144150
}
145151

146152
// Retrieve the extras.
147-
mPalette = intent.getParcelableExtra(EXTRA_COLOR_PALETTE);
153+
if (savedInstanceState == null) {
154+
mPalette = intent.getParcelableExtra(EXTRA_COLOR_PALETTE);
155+
} else {
156+
mPalette = savedInstanceState.getParcelable(EXTRA_COLOR_PALETTE);
157+
}
148158
final Rect startBounds = intent.getParcelableExtra(EXTRA_START_BOUNDS);
149159
setTitle(mPalette.getName());
150160

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

@@ -230,6 +240,39 @@ public void onAnimationRepeat(Animator animation) {
230240
}
231241
});
232242
}
243+
244+
mOnPaletteChangeListener = new Palettes.OnPaletteChangeListener() {
245+
@Override
246+
public void onColorPaletteChanged(List<Palette> palettes) {
247+
Palette newPalette = null;
248+
for (Palette candidate : palettes) {
249+
if (candidate.getId() == mPalette.getId()) {
250+
newPalette = candidate;
251+
break;
252+
}
253+
}
254+
255+
if (newPalette == null) {
256+
// The palette opened is not in the saved palettes.
257+
// It has been deleted, just finish the activity.
258+
finish();
259+
} else {
260+
// Reload the palette.
261+
mPalette = newPalette;
262+
setTitle(mPalette.getName());
263+
adapter.clear();
264+
adapter.addAll(mPalette.getColors());
265+
}
266+
}
267+
};
268+
269+
Palettes.registerListener(this, mOnPaletteChangeListener);
270+
}
271+
272+
@Override
273+
public void onSaveInstanceState(Bundle outState) {
274+
super.onSaveInstanceState(outState);
275+
outState.putParcelable(EXTRA_COLOR_PALETTE, mPalette);
233276
}
234277

235278
@Override
@@ -238,6 +281,12 @@ protected void onPause() {
238281
super.onPause();
239282
}
240283

284+
@Override
285+
protected void onDestroy() {
286+
Palettes.unregisterListener(this, mOnPaletteChangeListener);
287+
super.onDestroy();
288+
}
289+
241290
@Override
242291
public boolean onCreateOptionsMenu(Menu menu) {
243292
// Inflate the menu; this adds items to the action bar if it is present.
@@ -267,18 +316,18 @@ public boolean onOptionsItemSelected(MenuItem item) {
267316

268317
@Override
269318
public void onPaletteDeletionConfirmed(@NonNull Palette paletteToDelete) {
270-
if (Palettes.deleteColorPalette(this, paletteToDelete)) {
271-
finish();
272-
}
319+
// Delete the palette
320+
// Note: we don't finish the activity, it will be finished in mOnPaletteChangeListener.
321+
Palettes.deleteColorPalette(this, paletteToDelete);
273322
}
274323

275324
@Override
276325
public void onEditTextDialogFragmentPositiveButtonClick(int requestCode, String text) {
277326
if (!mPalette.getName().equals(text)) {
327+
// Set the new name and save the palette.
328+
// Note: we don't update the UI there, it will be updated in mOnPaletteChangeListener.
278329
mPalette.setName(text);
279-
if (Palettes.saveColorPalette(this, mPalette)) {
280-
setTitle(text);
281-
}
330+
Palettes.saveColorPalette(this, mPalette);
282331
}
283332
}
284333

@@ -295,7 +344,7 @@ private boolean handleActionEditName() {
295344
R.string.activity_palette_detail_edit_palette_name_dialog_title,
296345
R.string.activity_palette_detail_edit_palette_name_dialog_positive_action,
297346
android.R.string.cancel,
298-
R.string.activity_palette_detail_edit_palette_name_dialog_hint,
347+
getString(R.string.activity_palette_detail_edit_palette_name_dialog_hint),
299348
mPalette.getName()).show(getSupportFragmentManager(), null);
300349
return true;
301350
}

CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/adapters/ColorItemAdapter.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.graphics.PorterDuff;
5+
import android.text.TextUtils;
56
import android.view.LayoutInflater;
67
import android.view.View;
78
import android.view.ViewGroup;
@@ -59,7 +60,11 @@ protected View createView(ViewGroup parent) {
5960
protected void bindViewHolder(ViewHolder viewHolder, int position) {
6061
final ColorItem colorItem = getItem(position);
6162
viewHolder.mColorPreview.getBackground().setColorFilter(colorItem.getColor(), PorterDuff.Mode.MULTIPLY);
62-
viewHolder.mColorText.setText(colorItem.getHexString());
63+
if (!TextUtils.isEmpty(colorItem.getName())) {
64+
viewHolder.mColorText.setText(colorItem.getName());
65+
} else {
66+
viewHolder.mColorText.setText(colorItem.getHexString());
67+
}
6368
}
6469

6570
/**

0 commit comments

Comments
 (0)