Skip to content

Commit

Permalink
Added capability to "focus-in" on a particular color as a category
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacy2012 committed Apr 9, 2021
1 parent b276df7 commit 718ad6c
Show file tree
Hide file tree
Showing 16 changed files with 256 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
public class FormActivity extends AppCompatActivity {

int requestCode;
CollapsingToolbarLayout collapsingToolbarLayout;
RecyclerView rvColors;
ColorAdapter adapter;
int selectedColor = ColorItem.NO_COLOR;
Expand All @@ -45,7 +46,7 @@ protected void onCreate( Bundle savedInstanceState ) {


//Adding textChangedListener for nameEdit
CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.toolbar_layout);
collapsingToolbarLayout = findViewById(R.id.toolbar_layout);
ImageButton okButton = findViewById(R.id.okButton);
EditText nameEdit = findViewById(R.id.editName);
EditText stockEdit = findViewById(R.id.editStock);
Expand Down Expand Up @@ -170,7 +171,12 @@ public void onBackPressed() {
cancel();
}

public void setChosenColor( Integer color ) {
/**
* Sets chosen color.
*
* @param color the color
*/
public void setSelectedColor( Integer color ) {
this.selectedColor = color;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,30 @@ public void onResume() {
}
}

/**
* Focus on color.
*
* @param color the color
*/
public void focusOnColor( int color ) {
AppBarLayout appBarLayout = findViewById(R.id.app_bar);
appBarLayout.setExpanded(true, true);
ImageButton editButton = findViewById(R.id.editButton);
if (editMode == false) {
editButton.setEnabled(false);
adapter.setFocusColor(color);
}
}

/**
* Reset color focus.
*/
public void resetColorFocus() {
ImageButton editButton = findViewById(R.id.editButton);
adapter.reset();
editButton.setEnabled(true);
}

/**
* Update home widget.
*/
Expand Down Expand Up @@ -578,7 +602,8 @@ public void toFormUpdate(Item item, int position) {
/**
* To refill.
*
* @param itemId the item id
* @param item the item
* @param position the position
*/
public void toRefill( Item item, int position ) {
Intent intent = new Intent(this, RefillActivity.class);
Expand Down Expand Up @@ -651,7 +676,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
Refill expiringRefill = dao.getSoonestExpiringRefillOfItemId(replaceItem.getId(),
Converters.todayString());
replaceItem.setExpiringRefill(expiringRefill);
adapter.getItems().set(pos, replaceItem);
adapter.setItem(replaceItem, pos);
handler.post(() -> {
//UI Thread work here
// Add a new item
Expand All @@ -664,4 +689,15 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}


@Override
public void onBackPressed() {
ImageButton editButton = findViewById(R.id.editButton);
if (editButton.isEnabled() == false) {
resetColorFocus();
} else {
super.onBackPressed();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;

import androidx.core.content.ContextCompat;

Expand All @@ -20,4 +21,17 @@ public static int getDefaultTextColor( Context context ) {
TypedArray array = context.getTheme().obtainStyledAttributes(attribute);
return array.getColor(0, ContextCompat.getColor(context, R.color.transparent));
}

/**
* Gets attr color.
*
* @param context the context
* @return the attr color
*/
public static int getAttrColor( Context context, int colorId ) {
int[] attribute = new int[]{ colorId };
TypedArray array = context.obtainStyledAttributes(attribute);
return array.getColor(0, Color.TRANSPARENT);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import android.app.Activity;
import android.content.Context;
import android.widget.TextView;

import androidx.coordinatorlayout.widget.CoordinatorLayout;

Expand All @@ -29,22 +28,5 @@ public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
};
}

public static AppBarLayout.OnOffsetChangedListener createWithSubtitle( Context context, TextView subtitle ) {
return new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

CoordinatorLayout coordinatorLayout = ((Activity) context).findViewById(R.id.coordinatorLayout);
if (Math.abs(verticalOffset)-appBarLayout.getTotalScrollRange() == 0) {
// Collapsed
coordinatorLayout.setClipChildren(true);
} else {
//Expanded
coordinatorLayout.setClipChildren(false);
}
}
};
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
public class ColorAdapter extends
RecyclerView.Adapter<ColorAdapter.ViewHolder> {

private final float UNSELECTED_ALPHA = 0.3f;
private final float SELECTED_ALPHA = 1f;
private final int ANIMATION_LENGTH = 150;

private final List<ColorItem> colors;
private final Set<ViewHolder> mBoundViewHolders = new HashSet<>();
private int selectedColor = ColorItem.NO_COLOR;
Expand Down Expand Up @@ -52,10 +56,10 @@ public ViewHolder( View itemView, Context context ) {
if (colorItem.isSelected()) {
deselectAllExcept(colorItem);
colorButton.setAlpha(1f);
((FormActivity) context).setChosenColor(colorItem.getColor());
((FormActivity) context).setSelectedColor(colorItem.getColor());
} else {
deselectAll();
((FormActivity) context).setChosenColor(ColorItem.NO_COLOR);
((FormActivity) context).setSelectedColor(ColorItem.NO_COLOR);
}
});

Expand All @@ -73,7 +77,7 @@ public ViewHolder( View itemView, Context context ) {
public void animateColorButton(float fromAlpha, float toAlpha, Button colorButton) {
ValueAnimator va = ValueAnimator.ofFloat(fromAlpha, toAlpha);
va.addUpdateListener(valueAnimator -> colorButton.setAlpha((Float) valueAnimator.getAnimatedValue()));
va.setDuration(200);
va.setDuration(ANIMATION_LENGTH);
va.start();
}

Expand All @@ -83,10 +87,9 @@ public void animateColorButton(float fromAlpha, float toAlpha, Button colorButto
public void deselectAllExcept(ColorItem exceptColorItem) {
for (ViewHolder viewHolder : mBoundViewHolders) {
if (viewHolder.colorItem.equals(exceptColorItem) == false) {
//viewHolder.colorButton.setAlpha(0.5f);
viewHolder.colorItem.setSelected(false);
float currentAlpha = viewHolder.colorButton.getAlpha();
animateColorButton(currentAlpha, 0.5f, viewHolder.colorButton);
animateColorButton(currentAlpha, UNSELECTED_ALPHA, viewHolder.colorButton);
}
}
}
Expand All @@ -98,7 +101,7 @@ public void deselectAll() {
for (ViewHolder viewHolder : mBoundViewHolders) {
viewHolder.colorItem.setSelected(false);
float currentAlpha = viewHolder.colorButton.getAlpha();
animateColorButton(currentAlpha, 1f, viewHolder.colorButton);
animateColorButton(currentAlpha, SELECTED_ALPHA, viewHolder.colorButton);
}
}

Expand All @@ -116,7 +119,7 @@ public void setSelectedColor(int selectedColor) {
*/
public ColorAdapter() {
colors = new ArrayList<>();
colors.add(new ColorItem("#e53935")); //red
colors.add(new ColorItem("#b71c1c")); //red
colors.add(new ColorItem("#d81b60")); //pink
colors.add(new ColorItem("#1e88e5")); //blue
colors.add(new ColorItem("#00acc1")); //turquoise
Expand Down Expand Up @@ -155,9 +158,9 @@ public void onBindViewHolder( ColorAdapter.ViewHolder holder, int position ) {
colorButton.setBackgroundColor(holder.colorItem.getColor());
if (holder.colorItem.getColor() == selectedColor) {
holder.colorItem.setSelected(true);
holder.colorButton.setAlpha(1f);
holder.colorButton.setAlpha(SELECTED_ALPHA);
} else if (selectedColor != ColorItem.NO_COLOR) {
holder.colorButton.setAlpha(0.5f);
holder.colorButton.setAlpha(UNSELECTED_ALPHA);
}

mBoundViewHolders.add(holder);
Expand Down
Loading

0 comments on commit 718ad6c

Please sign in to comment.