Skip to content

Commit

Permalink
Rework popup and dialog scroll indicators
Browse files Browse the repository at this point in the history
Improve color view and adapter.
  • Loading branch information
pranavpandey committed Apr 15, 2021
1 parent 204dbc7 commit 7d906d5
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ public DynamicColorsAdapter(@NonNull @ColorInt Integer[] colors,
@Override
public View getView(final int position,
@Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder viewHolder;
final ViewHolder viewHolder;
final int color = (int) getItem(position);

if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(
Expand All @@ -135,33 +136,30 @@ public View getView(final int position,
viewHolder = (ViewHolder) convertView.getTag();
}

int color = (int) getItem(position);
final DynamicColorView dynamicColorView = viewHolder.getDynamicColorView();
dynamicColorView.setColor(color);
dynamicColorView.setColorShape(mColorShape);
dynamicColorView.setAlpha(mAlpha);
viewHolder.getDynamicColorView().setColor(color);
viewHolder.getDynamicColorView().setColorShape(mColorShape);
viewHolder.getDynamicColorView().setAlpha(mAlpha);
if (mSelectedColor != Theme.Color.UNKNOWN) {
dynamicColorView.setSelected(mSelectedColor == color);
viewHolder.getDynamicColorView().setSelected(mSelectedColor == color);
}
if (mContrastWithColor != Theme.Color.UNKNOWN) {
Dynamic.setContrastWithColor(viewHolder.getDynamicColorView(), mContrastWithColor);
}

dynamicColorView.setTooltip();
Dynamic.setOnClickListener(dynamicColorView, new View.OnClickListener() {
Dynamic.setOnClickListener(viewHolder.getDynamicColorView(), new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mDynamicColorListener != null) {
mDynamicColorListener.onColorSelected(
null, position, dynamicColorView.getColor());
mSelectedColor = dynamicColorView.getColor();
mDynamicColorListener.onColorSelected(null, position,
viewHolder.getDynamicColorView().getColor());
mSelectedColor = viewHolder.getDynamicColorView().getColor();

notifyDataSetChanged();
}
}
});

if (mContrastWithColor != Theme.Color.UNKNOWN) {
Dynamic.setContrastWithColor(dynamicColorView, mContrastWithColor);
}

viewHolder.getDynamicColorView().setTooltip();
return convertView;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
Expand All @@ -37,9 +36,9 @@
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.ViewStub;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
Expand All @@ -54,17 +53,16 @@
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.core.view.ViewCompat;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.RecyclerView;

import com.pranavpandey.android.dynamic.support.R;
import com.pranavpandey.android.dynamic.support.theme.DynamicTheme;
import com.pranavpandey.android.dynamic.support.widget.DynamicListView;
import com.pranavpandey.android.dynamic.utils.DynamicColorUtils;
import com.pranavpandey.android.dynamic.utils.DynamicViewUtils;

import java.lang.ref.WeakReference;

Expand Down Expand Up @@ -612,140 +610,69 @@ private void setScrollIndicators(ViewGroup contentPanel, View content,
View indicatorUp = mWindow.findViewById(R.id.scrollIndicatorUp);
View indicatorDown = mWindow.findViewById(R.id.scrollIndicatorDown);

if (Build.VERSION.SDK_INT >= 23) {
// We're on Marshmallow so can rely on the View APIs
ViewCompat.setScrollIndicators(content, indicators, mask);
// We can also remove the compat indicator views
if (indicatorUp != null) {
contentPanel.removeView(indicatorUp);
}
if (indicatorDown != null) {
contentPanel.removeView(indicatorDown);
}
} else {
// First, remove the indicator views if we're not set to use them
if (indicatorUp != null && (indicators & ViewCompat.SCROLL_INDICATOR_TOP) == 0) {
contentPanel.removeView(indicatorUp);
indicatorUp = null;
}
if (indicatorDown != null && (indicators & ViewCompat.SCROLL_INDICATOR_BOTTOM) == 0) {
contentPanel.removeView(indicatorDown);
indicatorDown = null;
}

if (indicatorUp != null || indicatorDown != null) {
final View top = indicatorUp;
final View bottom = indicatorDown;

if (mMessage != null) {
// We're just showing the ScrollView, set up listener.
mScrollView.setOnScrollChangeListener(
new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX,
int scrollY, int oldScrollX, int oldScrollY) {
manageScrollIndicators(v, top, bottom);
}
});

// Set up the indicators following layout.
mScrollView.post(new Runnable() {
@Override
public void run() {
manageScrollIndicators(mScrollView, top, bottom);
}
});
} else if (mListView != null) {
// We're just showing the AbsListView, set up listener.
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
manageScrollIndicators(view, top, bottom);
}
// First, remove the indicator views if we're not set to use them
if (indicatorUp != null && (indicators & ViewCompat.SCROLL_INDICATOR_TOP) == 0) {
contentPanel.removeView(indicatorUp);
indicatorUp = null;
}
if (indicatorDown != null && (indicators & ViewCompat.SCROLL_INDICATOR_BOTTOM) == 0) {
contentPanel.removeView(indicatorDown);
indicatorDown = null;
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
manageScrollIndicators(view, top, bottom);
}
});

// Set up the indicators following layout.
mListView.post(new Runnable() {
@Override
public void run() {
manageScrollIndicators(mListView, top, bottom);
}
});
} else if (mViewRoot != null) {
if (mViewRoot instanceof NestedScrollView) {
((NestedScrollView) mViewRoot).setOnScrollChangeListener(
new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX,
int scrollY, int oldScrollX, int oldScrollY) {
manageScrollIndicators(v, top, bottom);
}
});

// Set up the indicators following layout.
mViewRoot.post(new Runnable() {
@Override
public void run() {
manageScrollIndicators(mViewRoot, top, bottom);
}
});
} else if (mViewRoot instanceof AbsListView) {
((AbsListView) mViewRoot).setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
manageScrollIndicators(view, top, bottom);
}
if (indicatorUp != null || indicatorDown != null) {
final View top = indicatorUp;
final View bottom = indicatorDown;

if (mMessage != null) {
mScrollView.getViewTreeObserver().addOnScrollChangedListener(
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
manageScrollIndicators(view, top, bottom);
public void onScrollChanged() {
DynamicViewUtils.manageScrollIndicators(mScrollView, top, bottom);
}
});

mViewRoot.post(new Runnable() {
mScrollView.post(new Runnable() {
@Override
public void run() {
DynamicViewUtils.manageScrollIndicators(mScrollView, top, bottom);
}
});
} else if (mListView != null) {
mListView.getViewTreeObserver().addOnScrollChangedListener(
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void run() {
manageScrollIndicators(mViewRoot, top, bottom);
public void onScrollChanged() {
DynamicViewUtils.manageScrollIndicators(mListView, top, bottom);
}
});
} else if (mViewRoot instanceof RecyclerView) {
((RecyclerView) mViewRoot).addOnScrollListener(
new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(
@NonNull RecyclerView recyclerView, int newState) {
manageScrollIndicators(recyclerView, top, bottom);
}

@Override
public void onScrolled(@NonNull RecyclerView recyclerView,
int dx, int dy) {
manageScrollIndicators(recyclerView, top, bottom);
}
});

mViewRoot.post(new Runnable() {
mListView.post(new Runnable() {
@Override
public void run() {
DynamicViewUtils.manageScrollIndicators(mListView, top, bottom);
}
});
} else if (mViewRoot != null) {
mViewRoot.getViewTreeObserver().addOnScrollChangedListener(
new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void run() {
manageScrollIndicators(mViewRoot, top, bottom);
public void onScrollChanged() {
DynamicViewUtils.manageScrollIndicators(mViewRoot, top, bottom);
}
});
mViewRoot.post(new Runnable() {
@Override
public void run() {
DynamicViewUtils.manageScrollIndicators(mViewRoot, top, bottom);
}
} else {
// We don't have any content to scroll, remove the indicators.
if (top != null) {
contentPanel.removeView(top);
}
if (bottom != null) {
contentPanel.removeView(bottom);
}
});
} else {
// We don't have any content to scroll, remove the indicators.
if (top != null) {
contentPanel.removeView(top);
}
if (bottom != null) {
contentPanel.removeView(bottom);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

import com.pranavpandey.android.dynamic.support.Defaults;
import com.pranavpandey.android.dynamic.support.R;
import com.pranavpandey.android.dynamic.support.theme.DynamicTheme;
import com.pranavpandey.android.dynamic.support.utils.DynamicPickerUtils;
import com.pranavpandey.android.dynamic.support.utils.DynamicResourceUtils;
import com.pranavpandey.android.dynamic.support.widget.DynamicFrameLayout;
Expand Down Expand Up @@ -220,12 +219,11 @@ private void onUpdate() {
mColorPaint.setColor(getContrastWithColor());

if (getMeasuredWidth() > 0) {
RadialGradient gradient =
new RadialGradient(getMeasuredWidth() / 2f, getMeasuredWidth() / 2f,
getMeasuredWidth(), new int[] {
DynamicTheme.getInstance().get().getBackgroundColor(),
tintColor, DynamicTheme.getInstance().get().getPrimaryColor() },
null, Shader.TileMode.CLAMP);
RadialGradient gradient = new RadialGradient(
getMeasuredWidth() / 2f, getMeasuredWidth() / 2f,
getMeasuredWidth(), new int[] {
getContrastWithColor(), tintColor, getContrastWithColor() },
null, Shader.TileMode.CLAMP);
mColorPaint.setShader(gradient);
}
} else {
Expand Down Expand Up @@ -367,7 +365,7 @@ public void setTooltip() {
tintColor = DynamicColorUtils.removeAlpha(tintColor);

if (mSelected) {
icon = DynamicResourceUtils.getDrawable(getContext(), color == Theme.AUTO
icon = DynamicResourceUtils.getDrawable(getContext(), getColor() == Theme.AUTO
? R.drawable.ads_ic_play : R.drawable.ads_ic_check);
}

Expand Down

0 comments on commit 7d906d5

Please sign in to comment.