Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vbh committed Nov 21, 2021
1 parent adc1703 commit dc0e155
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 59 deletions.
44 changes: 22 additions & 22 deletions app/src/main/java/de/danoeh/antennapod/adapter/CoverLoader.java
@@ -1,7 +1,5 @@
package de.danoeh.antennapod.adapter;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand All @@ -24,7 +22,9 @@
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.glide.PaletteBitmap;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.ui.common.ThemeUtils;

public class CoverLoader {
private int resource = 0;
Expand Down Expand Up @@ -92,20 +92,22 @@ public void load() {
.fitCenter()
.dontAnimate();

RequestBuilder<Drawable> builder = Glide.with(activity)
RequestBuilder<PaletteBitmap> builder = Glide.with(activity)
.as(PaletteBitmap.class)
.load(uri)
.apply(options);

if (fallbackUri != null && txtvPlaceholder != null && imgvCover != null) {
builder = builder.error(Glide.with(activity)
.as(PaletteBitmap.class)
.load(fallbackUri)
.apply(options));
}

builder.into(coverTarget);
}

static class CoverTarget extends CustomViewTarget<ImageView, Drawable> {
static class CoverTarget extends CustomViewTarget<ImageView, PaletteBitmap> {
private final WeakReference<TextView> placeholder;
private final WeakReference<ImageView> cover;
private boolean textAndImageCombined;
Expand All @@ -126,40 +128,38 @@ public void onLoadFailed(Drawable errorDrawable) {
}

@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
setPlaceholderVisibility(placeholder.get(), textAndImageCombined, bitmap);
public void onResourceReady(@NonNull PaletteBitmap resource,
@Nullable Transition<? super PaletteBitmap> transition) {
ImageView ivCover = cover.get();
ivCover.setImageDrawable(resource);
ivCover.setImageBitmap(resource.bitmap);
setPlaceholderVisibility(placeholder.get(), textAndImageCombined, resource.palette);
}

@Override
protected void onResourceCleared(@Nullable Drawable placeholder) {
ImageView ivCover = cover.get();
ivCover.setImageDrawable(placeholder);
setPlaceholderVisibility(this.placeholder.get(), textAndImageCombined, null);
}

static void setPlaceholderVisibility(TextView placeholder, boolean textAndImageCombined, Bitmap bitmap) {
static void setPlaceholderVisibility(TextView placeholder, boolean textAndImageCombined, Palette palette) {
boolean showTitle = UserPreferences.shouldShowSubscriptionTitle();
if (placeholder != null) {
if (textAndImageCombined || showTitle) {
int bgColor = placeholder.getContext().getResources().getColor(R.color.feed_text_bg);
if (bitmap == null || !showTitle) {
if (palette == null || !showTitle) {
placeholder.setBackgroundColor(bgColor);
placeholder.setTextColor(ThemeUtils.getColorFromAttr(placeholder.getContext(),
android.R.attr.textColorPrimary));
return;
}
Palette.from(bitmap).generate(p -> {
if (p == null) {
return;
}
int dominantColor = p.getDominantColor(bgColor);
int textColor = placeholder.getContext().getResources().getColor(R.color.white);
if (ColorUtils.calculateLuminance(dominantColor) > 0.6) {
textColor = placeholder.getContext().getResources().getColor(R.color.black);
}
placeholder.setTextColor(textColor);
placeholder.setBackgroundColor(dominantColor);
});
int dominantColor = palette.getDominantColor(bgColor);
int textColor = placeholder.getContext().getResources().getColor(R.color.white);
if (ColorUtils.calculateLuminance(dominantColor) > 0.5) {
textColor = placeholder.getContext().getResources().getColor(R.color.black);
}
placeholder.setTextColor(textColor);
placeholder.setBackgroundColor(dominantColor);
} else {
placeholder.setVisibility(View.INVISIBLE);
}
Expand Down
@@ -1,10 +1,8 @@
package de.danoeh.antennapod.adapter;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.text.TextUtils;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuInflater;
Expand Down Expand Up @@ -44,6 +42,8 @@
*/
public class SubscriptionsRecyclerAdapter extends SelectableAdapter<SubscriptionsRecyclerAdapter.SubscriptionViewHolder>
implements View.OnCreateContextMenuListener {
private static final int COVER_WITH_TITLE = 1;

private final WeakReference<MainActivity> mainActivityRef;
private List<NavDrawerData.DrawerItem> listItems;
private Feed selectedFeed = null;
Expand All @@ -68,6 +68,23 @@ public Feed getSelectedFeed() {
@Override
public SubscriptionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(mainActivityRef.get()).inflate(R.layout.subscription_item, parent, false);
TextView feedTitle = itemView.findViewById(R.id.txtvTitle);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) feedTitle.getLayoutParams();
int topAndBottomItemId = R.id.imgvCover;
int belowItemId = 0;

if (viewType == COVER_WITH_TITLE) {
topAndBottomItemId = 0;
belowItemId = R.id.imgvCover;
feedTitle.setBackgroundColor(feedTitle.getContext().getResources().getColor(R.color.feed_text_bg));
int padding = (int) convertDpToPixel(feedTitle.getContext(), 6);
feedTitle.setPadding(padding, padding, padding, padding);
}
params.addRule(RelativeLayout.BELOW, belowItemId);
params.addRule(RelativeLayout.ALIGN_TOP, topAndBottomItemId);
params.addRule(RelativeLayout.ALIGN_BOTTOM, topAndBottomItemId);
feedTitle.setLayoutParams(params);
feedTitle.setSingleLine(viewType == COVER_WITH_TITLE);
return new SubscriptionViewHolder(itemView);
}

Expand Down Expand Up @@ -175,13 +192,17 @@ public void setSelected(int pos, boolean selected) {
}
}

@Override
public int getItemViewType(int position) {
return UserPreferences.shouldShowSubscriptionTitle() ? COVER_WITH_TITLE : 0;
}

public class SubscriptionViewHolder extends RecyclerView.ViewHolder {
private final TextView feedTitle;
private final ImageView imageView;
private final TriangleLabelView count;
private final FrameLayout selectView;
private final CheckBox selectCheckbox;
private ViewGroup.LayoutParams lastParams;

public SubscriptionViewHolder(@NonNull View itemView) {
super(itemView);
Expand All @@ -208,18 +229,6 @@ public void bind(NavDrawerData.DrawerItem drawerItem) {
count.setVisibility(View.GONE);
}

TypedArray res = feedTitle.getContext().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.textColorPrimary });
int textColorPrimary = res.getColor(0, 0);

if (UserPreferences.shouldShowSubscriptionTitle()) {
updateLayoutParams(feedTitle, textColorPrimary);
} else if (lastParams != null) {
feedTitle.setLayoutParams(lastParams);
feedTitle.setTextColor(textColorPrimary);
feedTitle.setSingleLine(false);
}

if (drawerItem.type == NavDrawerData.DrawerItem.Type.FEED) {
Feed feed = ((NavDrawerData.FeedDrawerItem) drawerItem).feed;
boolean textAndImageCombind = feed.isLocalFeed()
Expand All @@ -237,24 +246,6 @@ public void bind(NavDrawerData.DrawerItem drawerItem) {
.load();
}
}

private void updateLayoutParams(TextView feedTitle, int textColor) {
lastParams = feedTitle.getLayoutParams();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_LEFT, R.id.imgvCover);
params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.imgvCover);
params.addRule(RelativeLayout.BELOW, R.id.imgvCover);
feedTitle.setLayoutParams(params);
feedTitle.setEllipsize(TextUtils.TruncateAt.END);
feedTitle.setSingleLine(true);
feedTitle.setTextColor(textColor);
feedTitle.setBackgroundColor(feedTitle.getContext().getResources()
.getColor(R.color.feed_text_bg));

int padding = (int) convertDpToPixel(feedTitle.getContext(), 6);
feedTitle.setPadding(padding, padding, padding, padding);
}
}

public static float convertDpToPixel(Context context, float dp) {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/xml/preferences_user_interface.xml
Expand Up @@ -48,8 +48,7 @@
android:title="@string/pref_show_subscription_title"
android:key="prefSubscriptionTitle"
android:summary="@string/pref_show_subscription_title_summary"
android:defaultValue="false"
android:enabled="true"/>
android:defaultValue="false" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/external_elements">
<SwitchPreferenceCompat
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle
Expand Up @@ -40,6 +40,7 @@ dependencies {
implementation "androidx.preference:preference:$preferenceVersion"
implementation "androidx.work:work-runtime:$workManagerVersion"
implementation "com.google.android.material:material:$googleMaterialVersion"
implementation 'androidx.palette:palette:1.0.0'

implementation "org.apache.commons:commons-lang3:$commonslangVersion"
implementation "commons-io:commons-io:$commonsioVersion"
Expand Down
Expand Up @@ -2,6 +2,7 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -45,5 +46,6 @@ public void registerComponents(@NonNull Context context, @NonNull Glide glide, @
registry.append(String.class, InputStream.class, new NoHttpStringLoader.StreamFactory());

registry.append(EmbeddedChapterImage.class, ByteBuffer.class, new ChapterImageModelLoader.Factory());
registry.register(Bitmap.class, PaletteBitmap.class, new PaletteBitmapTranscoder());
}
}
@@ -0,0 +1,16 @@
package de.danoeh.antennapod.core.glide;

import android.graphics.Bitmap;

import androidx.annotation.NonNull;
import androidx.palette.graphics.Palette;

public class PaletteBitmap {
public final Palette palette;
public final Bitmap bitmap;

public PaletteBitmap(@NonNull Bitmap bitmap, Palette palette) {
this.bitmap = bitmap;
this.palette = palette;
}
}
@@ -0,0 +1,33 @@
package de.danoeh.antennapod.core.glide;

import androidx.annotation.NonNull;

import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.util.Util;

public class PaletteBitmapResource implements Resource<PaletteBitmap> {
private final PaletteBitmap paletteBitmap;

public PaletteBitmapResource(@NonNull PaletteBitmap paletteBitmap) {
this.paletteBitmap = paletteBitmap;
}

@NonNull
@Override
public Class<PaletteBitmap> getResourceClass() {
return PaletteBitmap.class;
}

@NonNull
@Override public PaletteBitmap get() {
return paletteBitmap;
}

@Override public int getSize() {
return Util.getBitmapByteSize(paletteBitmap.bitmap);
}

@Override public void recycle() {
paletteBitmap.bitmap.recycle();
}
}
@@ -0,0 +1,28 @@
package de.danoeh.antennapod.core.glide;

import android.graphics.Bitmap;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.palette.graphics.Palette;

import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.resource.transcode.ResourceTranscoder;

import de.danoeh.antennapod.core.preferences.UserPreferences;

public class PaletteBitmapTranscoder implements ResourceTranscoder<Bitmap, PaletteBitmap> {

@Nullable
@Override
public Resource<PaletteBitmap> transcode(@NonNull Resource<Bitmap> toTranscode, @NonNull Options options) {
Bitmap bitmap = toTranscode.get();
Palette palette = null;
if (UserPreferences.shouldShowSubscriptionTitle()) {
palette = new Palette.Builder(bitmap).generate();
}
PaletteBitmap result = new PaletteBitmap(bitmap, palette);
return new PaletteBitmapResource(result);
}
}
Expand Up @@ -1096,7 +1096,7 @@ public static boolean shouldShowSubscriptionTitle() {
return prefs.getBoolean(PREF_SUBSCRIPTION_TITLE, false);
}

public static void setSubscriptionTitleSetting(Boolean showTitle) {
public static void setSubscriptionTitleSetting(boolean showTitle) {
prefs.edit().putBoolean(PREF_SUBSCRIPTION_TITLE, showTitle).apply();
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/res/values/strings.xml
Expand Up @@ -543,7 +543,7 @@
<string name="pref_feed_settings_dialog_msg">This setting is unique to each podcast. You can change it by opening the podcast page.</string>
<string name="pref_contribute">Contribute</string>
<string name="pref_show_subscription_title">Show Subscription Title</string>
<string name="pref_show_subscription_title_summary">Display subscription title when checked.</string>
<string name="pref_show_subscription_title_summary">Display the subscription title below the cover image.</string>

<!-- About screen -->
<string name="about_pref">About</string>
Expand Down
Expand Up @@ -5,6 +5,7 @@
import androidx.annotation.ColorInt;
import android.util.TypedValue;
import androidx.annotation.DrawableRes;
import androidx.core.content.ContextCompat;

public class ThemeUtils {
private ThemeUtils() {
Expand All @@ -14,6 +15,9 @@ private ThemeUtils() {
public static @ColorInt int getColorFromAttr(Context context, @AttrRes int attr) {
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(attr, typedValue, true);
if (typedValue.resourceId != 0) {
return ContextCompat.getColor(context, typedValue.resourceId);
}
return typedValue.data;
}

Expand Down

0 comments on commit dc0e155

Please sign in to comment.