Skip to content

Commit

Permalink
Implement background aware for spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavpandey committed Jun 18, 2021
1 parent 320a660 commit e731d01
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.pranavpandey.android.dynamic.support.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
Expand All @@ -29,6 +30,7 @@

import com.pranavpandey.android.dynamic.support.Dynamic;
import com.pranavpandey.android.dynamic.support.model.DynamicMenu;
import com.pranavpandey.android.dynamic.support.widget.base.DynamicWidget;

import java.util.List;

Expand All @@ -49,6 +51,11 @@ public class DynamicSpinnerImageAdapter extends ArrayAdapter<DynamicMenu> {
*/
private final @IdRes int mTextViewResourceId;

/**
* View handling this adapter.
*/
private final View mParent;

/**
* Constructor to initialize an object of this class.
*
Expand All @@ -61,10 +68,27 @@ public class DynamicSpinnerImageAdapter extends ArrayAdapter<DynamicMenu> {
public DynamicSpinnerImageAdapter(@NonNull Context context,
int resource, int imageViewResourceId, int textViewResourceId,
@NonNull List<DynamicMenu> data) {
this(context, resource, imageViewResourceId, textViewResourceId, data, null);
}

/**
* Constructor to initialize an object of this class.
*
* @param context The context to retrieve the resources.
* @param resource The layout resource to be used.
* @param imageViewResourceId The resource id for the image view.
* @param textViewResourceId The resource id for the text view.
* @param data The data to be handled by this adapter.
* @param parent The view handling this adapter.
*/
public DynamicSpinnerImageAdapter(@NonNull Context context,
int resource, int imageViewResourceId, int textViewResourceId,
@NonNull List<DynamicMenu> data, @Nullable View parent) {
super(context, resource, textViewResourceId, data);

this.mImageViewResourceId = imageViewResourceId;
this.mTextViewResourceId = textViewResourceId;
this.mParent = parent;
}

@Override
Expand All @@ -78,6 +102,33 @@ public DynamicSpinnerImageAdapter(@NonNull Context context,
if (item != null) {
Dynamic.set(imageView, item.getIcon());
Dynamic.set(textView, item.getTitle());

if (mParent instanceof DynamicWidget) {
Dynamic.setContrastWithColorTypeOrColor(imageView,
((DynamicWidget) mParent).getContrastWithColorType(),
((DynamicWidget) mParent).getContrastWithColor());
Dynamic.setContrastWithColorTypeOrColor(textView,
((DynamicWidget) mParent).getContrastWithColorType(),
((DynamicWidget) mParent).getContrastWithColor());
Dynamic.setBackgroundAwareSafe(imageView,
((DynamicWidget) mParent).getBackgroundAware());
Dynamic.setBackgroundAwareSafe(textView,
((DynamicWidget) mParent).getBackgroundAware());

if (imageView != null && imageView.getParent() instanceof DynamicWidget) {
Dynamic.setContrastWithColorTypeOrColor(imageView.getParent(),
((DynamicWidget) mParent).getContrastWithColorType(),
((DynamicWidget) mParent).getContrastWithColor());
Dynamic.setBackgroundAwareSafe(imageView.getParent(),
((DynamicWidget) mParent).getBackgroundAware());
} else if (textView != null && textView.getParent() instanceof DynamicWidget) {
Dynamic.setContrastWithColorTypeOrColor(textView.getParent(),
((DynamicWidget) mParent).getContrastWithColorType(),
((DynamicWidget) mParent).getContrastWithColor());
Dynamic.setBackgroundAwareSafe(textView.getParent(),
((DynamicWidget) mParent).getBackgroundAware());
}
}
}

return itemView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public class DynamicSpinner extends AppCompatSpinner
/**
* Popup background used by this view.
*/
private DynamicPopupBackground mPopupBackground;
private final DynamicPopupBackground mPopupBackground;

public DynamicSpinner(@NonNull Context context) {
this(context, null);
Expand All @@ -108,13 +108,15 @@ public DynamicSpinner(@NonNull Context context) {
public DynamicSpinner(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);

mPopupBackground = new DynamicPopupBackground(getContext(), attrs);
loadFromAttributes(attrs);
}

public DynamicSpinner(@NonNull Context context,
@Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);

mPopupBackground = new DynamicPopupBackground(getContext(), attrs);
loadFromAttributes(attrs);
}

Expand Down Expand Up @@ -142,7 +144,6 @@ public void loadFromAttributes(@Nullable AttributeSet attrs) {
mElevationOnSameBackground = a.getBoolean(
R.styleable.DynamicSpinner_adt_elevationOnSameBackground,
Defaults.ADS_ELEVATION_ON_SAME_BACKGROUND);
mPopupBackground = new DynamicPopupBackground(getContext(), attrs);
mPopupBackground.setCorner(0f);
} finally {
a.recycle();
Expand Down Expand Up @@ -274,10 +275,7 @@ public void setElevationOnSameBackground(boolean elevationOnSameBackground) {

@Override
public void setSurface() {
if (mContrastWithColor != Theme.Color.UNKNOWN) {
mPopupBackground.setColor(mContrastWithColor);
}

Dynamic.setColorTypeOrColor(mPopupBackground, mContrastWithColorType, mContrastWithColor);
setPopupBackgroundDrawable(mPopupBackground.getBackground());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
style="@style/Widget.DynamicApp.Content.Card.Clickable.Background"
android:id="@+id/ads_array_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/ads_card_view_content_margin"
android:orientation="horizontal"
android:background="?attr/selectableItemBackground">
android:orientation="horizontal">

<com.pranavpandey.android.dynamic.support.widget.DynamicImageView
android:id="@+id/ads_array_item_icon"
Expand Down

0 comments on commit e731d01

Please sign in to comment.