From fb385748b1f6d12c9b4a90711eef47bb9052ee64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vincent=20Barth=C3=A9l=C3=A9my?= Date: Sun, 31 May 2015 18:16:13 +0200 Subject: [PATCH] Fix the edition of the color items. Changes of the name of a ColorItem was not reflected in the PaletteDetailActivity. Once a ColorItem has been added to a Palette, it exists on its own, meaning that any changes on the original ColorPalette (e.g. name) won't affect the ColorItem in the Palette and vice versa. --- .../activities/ColorDetailActivity.java | 73 ++++++++++++++----- .../activities/PaletteDetailActivity.java | 65 +++++++++++++++-- .../views/ColorItemListPage.java | 5 +- 3 files changed, 114 insertions(+), 29 deletions(-) diff --git a/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/ColorDetailActivity.java b/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/ColorDetailActivity.java index 07fb3e0..62d2294 100644 --- a/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/ColorDetailActivity.java +++ b/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/ColorDetailActivity.java @@ -12,6 +12,7 @@ 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; @@ -29,10 +30,13 @@ 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; @@ -51,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. - *

- * 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. @@ -83,12 +86,17 @@ public class ColorDetailActivity extends AppCompatActivity implements View.OnCli private static final String FILE_PROVIDER_AUTHORITY = "fr.tvbarthel.apps.cameracolorpicker.fileprovider"; /** - * A request code to use in {@link EditTextDialogFragment#newInstance(int, int, int, int, int, String)}. + * 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, - boolean canBeDeleted) { + 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); @@ -96,7 +104,7 @@ public static void startWithColorItem(Context context, ColorItem colorItem, View 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); @@ -147,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. - *

- * 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) { @@ -162,14 +168,19 @@ 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())) { @@ -258,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; @@ -327,14 +346,34 @@ 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); - ColorItems.saveColorItem(this, mColorItem); + + // 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 colorItems = mPalette.getColors(); + for (ColorItem candidate : colorItems) { + if (candidate.getId() == mColorItem.getId()) { + candidate.setName(text); + break; + } + } + Palettes.saveColorPalette(this, mPalette); + } } } diff --git a/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/PaletteDetailActivity.java b/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/PaletteDetailActivity.java index 70b7bd0..7e3b143 100644 --- a/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/PaletteDetailActivity.java +++ b/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/activities/PaletteDetailActivity.java @@ -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; @@ -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) { @@ -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()); @@ -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); } }); @@ -230,6 +240,39 @@ public void onAnimationRepeat(Animator animation) { } }); } + + mOnPaletteChangeListener = new Palettes.OnPaletteChangeListener() { + @Override + public void onColorPaletteChanged(List 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 @@ -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. @@ -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); } } diff --git a/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/views/ColorItemListPage.java b/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/views/ColorItemListPage.java index 69405cc..1cb65ec 100644 --- a/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/views/ColorItemListPage.java +++ b/CameraColorPicker/app/src/main/java/fr/tvbarthel/apps/cameracolorpicker/views/ColorItemListPage.java @@ -15,8 +15,6 @@ import android.widget.ListView; import android.widget.Toast; -import com.melnykov.fab.FloatingActionButton; - import java.util.List; import fr.tvbarthel.apps.cameracolorpicker.R; @@ -114,7 +112,7 @@ private void init(Context context) { public void onItemClick(AdapterView parent, View view, int position, long id) { final ColorItem colorItem = mColorItemAdapter.getItem(position); ColorDetailActivity.startWithColorItem(view.getContext(), colorItem, - view.findViewById(R.id.row_color_item_preview), true); + view.findViewById(R.id.row_color_item_preview)); } }); @@ -175,7 +173,6 @@ public void onActivityDestroyed(Activity activity) { } - /** * Hide the current {@link android.widget.Toast}. */