Skip to content

Commit

Permalink
Rework scroll utils and dividers
Browse files Browse the repository at this point in the history
Add support for API level 29 and above.
Improve item view and fix divider layout.
Improve theme preset layouts and adapter.
  • Loading branch information
pranavpandey committed Apr 15, 2021
1 parent bc90ec7 commit 204dbc7
Show file tree
Hide file tree
Showing 31 changed files with 674 additions and 578 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ public class Defaults {
*/
public static final float ADS_ALPHA_PRESSED = 0.2f;

/**
* Default alpha when the selector is pressed.
*/
public static final float ADS_ALPHA_PRESSED_SELECTOR = 0.4f;

/**
* Default alpha for the scrim insets.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import com.pranavpandey.android.dynamic.support.theme.inflater.DynamicLayoutInflater;
import com.pranavpandey.android.dynamic.support.tutorial.Tutorial;
import com.pranavpandey.android.dynamic.support.utils.DynamicResourceUtils;
import com.pranavpandey.android.dynamic.support.utils.DynamicScrollUtils;
import com.pranavpandey.android.dynamic.support.utils.DynamicTintUtils;
import com.pranavpandey.android.dynamic.support.view.DynamicInfoView;
import com.pranavpandey.android.dynamic.support.view.DynamicItemView;
Expand Down Expand Up @@ -547,7 +548,20 @@ public static <T> void tint(@Nullable T dynamic,
}

/**
* Tint background according to the supplied contrast color.
* Tint dynamic object according to the supplied colors.
*
* @param dynamic The dynamic object to be tinted.
* @param color The color to be set.
* @param <T> The type of the dynamic object.
*
* @see #tint(Object, int, int)
*/
public static <T> void tint(@Nullable T dynamic, @ColorInt int color) {
tint(dynamic, color, DynamicTheme.getInstance().getDefaultContrastWith());
}

/**
* Tint background according to the supplied contrast with color.
*
* @param dynamic The dynamic object to be tinted.
* @param contrastWithColor The contrast with color to be considered.
Expand All @@ -567,6 +581,47 @@ public static <T> void tintBackground(@Nullable T dynamic, @ColorInt int contras
}
}

/**
* Tint background according to the default contrast with color.
*
* @param dynamic The dynamic object to be tinted.
* @param <T> The type of the dynamic object.
*
* @see #tintBackground(Object, int)
*/
public static <T> void tintBackground(@Nullable T dynamic) {
tintBackground(dynamic, DynamicTheme.getInstance().getDefaultContrastWith());
}

/**
* Tint scrollable according to the supplied contrast with color.
*
* @param dynamic The dynamic object to be tinted.
* @param contrastWithColor The contrast with color to be considered.
* @param <T> The type of the dynamic object.
*
* @see DynamicScrollUtils#tint(Object, int)
*/
public static <T> void tintScrollable(@Nullable T dynamic, @ColorInt int contrastWithColor) {
if (contrastWithColor == Theme.Color.UNKNOWN) {
return;
}

DynamicScrollUtils.tint(dynamic, contrastWithColor);
}

/**
* Tint scrollable according to the default contrast with color.
*
* @param dynamic The dynamic object to be tinted.
* @param <T> The type of the dynamic object.
*
* @see #tintScrollable(Object, int)
*/
public static <T> void tintScrollable(@Nullable T dynamic) {
tintScrollable(dynamic, DynamicTheme.getInstance().getDefaultContrastWith());
}

/**
* Set on click listener for the dynamic object.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,11 +643,11 @@ private void setScrollIndicators(ViewGroup contentPanel, View content,
new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX,
int scrollY,
int oldScrollX, int oldScrollY) {
int scrollY, int oldScrollX, int oldScrollY) {
manageScrollIndicators(v, top, bottom);
}
});

// Set up the indicators following layout.
mScrollView.post(new Runnable() {
@Override
Expand All @@ -659,14 +659,17 @@ public void run() {
// We're just showing the AbsListView, set up listener.
mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
public void onScrollStateChanged(AbsListView view, int scrollState) {
manageScrollIndicators(view, top, bottom);
}

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

// Set up the indicators following layout.
mListView.post(new Runnable() {
@Override
Expand All @@ -680,11 +683,11 @@ public void run() {
new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX,
int scrollY,
int oldScrollX, int oldScrollY) {
int scrollY, int oldScrollX, int oldScrollY) {
manageScrollIndicators(v, top, bottom);
}
});

// Set up the indicators following layout.
mViewRoot.post(new Runnable() {
@Override
Expand All @@ -695,12 +698,14 @@ public void run() {
} else if (mViewRoot instanceof AbsListView) {
((AbsListView) mViewRoot).setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {}
public void onScrollStateChanged(AbsListView view, int scrollState) {
manageScrollIndicators(view, top, bottom);
}

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

Expand All @@ -715,12 +720,14 @@ public void run() {
new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(
@NonNull RecyclerView view, int scrollState) { }
@NonNull RecyclerView recyclerView, int newState) {
manageScrollIndicators(recyclerView, top, bottom);
}

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

Expand Down Expand Up @@ -873,12 +880,13 @@ private void setupContent(ViewGroup contentPanel) {

static void manageScrollIndicators(View v, View upIndicator, View downIndicator) {
if (upIndicator != null) {
upIndicator.setVisibility(
v.canScrollVertically(-1) ? View.VISIBLE : View.INVISIBLE);
upIndicator.setVisibility(v.canScrollVertically(-1)
? View.VISIBLE : View.INVISIBLE);
}

if (downIndicator != null) {
downIndicator.setVisibility(
v.canScrollVertically(1) ? View.VISIBLE : View.INVISIBLE);
downIndicator.setVisibility(v.canScrollVertically(1)
? View.VISIBLE : View.INVISIBLE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public void onDismiss() {

if (mDynamics == null) {
DynamicTaskUtils.executeTask(mWallpaperColorsTask);
} else {
} else if (getView() != null) {
setDynamics(getView().findViewById(R.id.ads_color_picker_dynamics),
getView().findViewById(R.id.ads_color_picker_divider));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,26 +308,35 @@ public void onScrollChange(NestedScrollView v,
((AbsListView) mViewRoot).setOnScrollListener(
new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(
AbsListView view, int scrollState) { }
public void onScrollStateChanged(AbsListView view,
int scrollState) {
DynamicViewUtils.manageScrollIndicators(
view, top, bottom);
}

@Override
public void onScroll(AbsListView v, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
DynamicViewUtils.manageScrollIndicators(v, top, bottom);
public void onScroll(AbsListView view,
int firstVisibleItem, int visibleItemCount,
int totalItemCount) {
DynamicViewUtils.manageScrollIndicators(
view, top, bottom);
}
});
} else if (mViewRoot instanceof RecyclerView) {
((RecyclerView) mViewRoot).addOnScrollListener(
new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(
@NonNull RecyclerView view, int scrollState) { }
@NonNull RecyclerView recyclerView, int newState) {
DynamicViewUtils.manageScrollIndicators(
recyclerView, top, bottom);
}

@Override
public void onScrolled(@NonNull RecyclerView v,
public void onScrolled(@NonNull RecyclerView recyclerView,
int dx, int dy) {
DynamicViewUtils.manageScrollIndicators(v, top, bottom);
DynamicViewUtils.manageScrollIndicators(
recyclerView, top, bottom);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.pranavpandey.android.dynamic.support.model.DynamicAppTheme;
import com.pranavpandey.android.dynamic.support.theme.view.DynamicPresetsView;
import com.pranavpandey.android.dynamic.support.theme.view.ThemePreview;
import com.pranavpandey.android.dynamic.support.utils.DynamicTintUtils;
import com.pranavpandey.android.dynamic.theme.ThemeContract;
import com.pranavpandey.android.dynamic.theme.utils.DynamicThemeUtils;

Expand Down Expand Up @@ -156,11 +155,10 @@ public void onBindViewHolder(@NonNull ViewHolder<T> holder, int position) {
.setImageResource(R.drawable.ads_ic_palette);
holder.getThemePreview().setDynamicTheme(theme);
Dynamic.setCorner(holder.getRoot(), theme.getCornerRadius());
DynamicTintUtils.setViewForegroundTint(holder.getThemePreview(),
theme.getBackgroundColor(), false);
Dynamic.setContrastWithColor(holder.getForeground(), theme.getBackgroundColor());

if (mDynamicPresetsListener != null) {
Dynamic.setOnClickListener(holder.getThemePreview(), new View.OnClickListener() {
Dynamic.setOnClickListener(holder.getForeground(), new View.OnClickListener() {
@Override
public void onClick(View v) {
mDynamicPresetsListener.onPresetClick(v,
Expand All @@ -178,7 +176,7 @@ public void onClick(View v) {
}
});
} else {
Dynamic.setClickable(holder.getThemePreview(), false);
Dynamic.setClickable(holder.getForeground(), false);
Dynamic.setClickable(holder.getThemePreview().getActionView(), false);
}
} catch (Exception ignored) {
Expand Down Expand Up @@ -247,6 +245,11 @@ public static class ViewHolder<T extends DynamicAppTheme> extends RecyclerView.V
*/
private final ThemePreview<T> themePreview;

/**
* The foreground to provide the touch feedback.
*/
private final ViewGroup foreground;

/**
* Constructor to initialize views from the supplied layout.
*
Expand All @@ -257,6 +260,7 @@ public ViewHolder(@NonNull View view) {

root = view.findViewById(R.id.ads_preset_root);
themePreview = view.findViewById(R.id.ads_preset_theme_preview);
foreground = view.findViewById(R.id.ads_preset_theme_preview_foreground);
}

/**
Expand All @@ -276,5 +280,14 @@ public ViewHolder(@NonNull View view) {
public @NonNull ThemePreview<T> getThemePreview() {
return themePreview;
}

/**
* Get the foreground to provide the touch feedback.
*
* @return The foreground to provide the touch feedback.
*/
public @Nullable ViewGroup getForeground() {
return foreground;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,19 @@
import android.annotation.TargetApi;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.RippleDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.appcompat.graphics.drawable.DrawableWrapper;
import androidx.appcompat.widget.MenuPopupWindow;
import androidx.cardview.widget.CardView;
import androidx.core.view.ViewCompat;

import com.pranavpandey.android.dynamic.support.Defaults;
import com.pranavpandey.android.dynamic.support.Dynamic;
import com.pranavpandey.android.dynamic.support.R;
import com.pranavpandey.android.dynamic.support.theme.DynamicTheme;
import com.pranavpandey.android.dynamic.support.utils.DynamicResourceUtils;
import com.pranavpandey.android.dynamic.support.utils.DynamicScrollUtils;
import com.pranavpandey.android.dynamic.support.utils.DynamicTintUtils;
import com.pranavpandey.android.dynamic.support.widget.DynamicCardView;
import com.pranavpandey.android.dynamic.support.widget.DynamicPopupBackground;
import com.pranavpandey.android.dynamic.utils.DynamicColorUtils;
Expand Down Expand Up @@ -78,14 +71,6 @@ public void run() {
DynamicCardView cardView = new DynamicPopupBackground(mMenu.getContext(), mAttrs);
@ColorInt int backgroundColor = DynamicColorUtils.removeAlpha(cardView.getColor());
@ColorInt int tintColor = DynamicTheme.getInstance().get().getTintSurfaceColor();
@ColorInt int edgeEffectColor = DynamicTheme.getInstance().resolveColorType(
Defaults.ADS_COLOR_TYPE_EDGE_EFFECT);

if (DynamicTheme.getInstance().get().isBackgroundAware()) {
tintColor = DynamicColorUtils.getContrastColor(tintColor, backgroundColor);
edgeEffectColor = DynamicColorUtils.getContrastColor(
edgeEffectColor, backgroundColor);
}

Dynamic.setColor(mMenu.findViewById(android.R.id.icon), tintColor);
Dynamic.setColor(mMenu.findViewById(androidx.appcompat.R.id.icon), tintColor);
Expand Down Expand Up @@ -117,28 +102,7 @@ public void run() {
ViewGroup menu = (ViewGroup) this.mMenu.getParent();
ViewGroup group = (ViewGroup) menu.getParent();

if (menu instanceof MenuPopupWindow.MenuDropDownListView) {
DynamicScrollUtils.setEdgeEffectColor(
(MenuPopupWindow.MenuDropDownListView) menu, edgeEffectColor);

if (((MenuPopupWindow.MenuDropDownListView) menu).getSelector()
instanceof DrawableWrapper) {
final DrawableWrapper drawable = ((DrawableWrapper)
((MenuPopupWindow.MenuDropDownListView) menu).getSelector());
if (drawable != null && DynamicSdkUtils.is21()
&& drawable.getWrappedDrawable() instanceof RippleDrawable) {
DynamicTintUtils.colorizeRippleDrawable(menu,
drawable.getWrappedDrawable(), backgroundColor,
tintColor, false, false);
} else {
DynamicDrawableUtils.colorizeDrawable(drawable, tintColor);
}
} else {
DynamicDrawableUtils.colorizeDrawable(
((MenuPopupWindow.MenuDropDownListView) menu)
.getSelector(), tintColor);
}
}
Dynamic.tintScrollable(menu, backgroundColor);

if (group == null) {
return;
Expand Down Expand Up @@ -172,13 +136,8 @@ public void run() {
group.removeAllViews();
group.addView(menu);
}
} else if (group != null) {
// ViewCompat.setBackground(menu, null);
ViewCompat.setBackground(group, cardView.getBackground());
} else {
ViewCompat.setBackground(menu, DynamicDrawableUtils.colorizeDrawable(
DynamicResourceUtils.getDrawable(mMenu.getContext(),
R.drawable.ads_background), backgroundColor));
ViewCompat.setBackground(group, cardView.getBackground());
}
}

Expand Down

0 comments on commit 204dbc7

Please sign in to comment.