Skip to content

Commit

Permalink
Improved highlighting in FeedFragment
Browse files Browse the repository at this point in the history
Now keeps the ``selectableItemBackground`` when highligthing an item.
  • Loading branch information
litetex committed Nov 15, 2021
1 parent e054554 commit 2057bad
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ package org.schabi.newpipe.local.feed

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable
import android.os.Bundle
import android.os.Parcelable
import android.view.LayoutInflater
Expand All @@ -33,6 +36,7 @@ import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.annotation.AttrRes
import androidx.annotation.Nullable
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.content.res.AppCompatResources
Expand Down Expand Up @@ -579,12 +583,24 @@ class FeedFragment : BaseStateFragment<FeedState>() {
val item = groupAdapter.getItem(i) as StreamItem

var typeface = Typeface.DEFAULT
var resid = R.attr.selectableItemBackground
var backgroundSupplier = { ctx: Context ->
resolveDrawable(ctx, R.attr.selectableItemBackground)
}
if (doCheck) {
// If the uploadDate is null or true we should highlight the item
if (item.streamWithState.stream.uploadDate?.isAfter(updateTime) != false) {
typeface = Typeface.DEFAULT_BOLD
resid = R.attr.dashed_border
highlightCount++

typeface = Typeface.DEFAULT_BOLD
backgroundSupplier = { ctx: Context ->
// Merge the drawables together. Otherwise we would lose the "select" effect
LayerDrawable(
arrayOf(
resolveDrawable(ctx, R.attr.dashed_border),
resolveDrawable(ctx, R.attr.selectableItemBackground)
)
)
}
} else {
// Increases execution time due to the order of the items (newest always on top)
// Once a item is is before the updateTime we can skip all following items
Expand All @@ -597,17 +613,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
// due to the fact that itemRoot is getting recycled
item.execBindEnd = Consumer { viewBinding ->
val context = viewBinding.itemRoot.context
viewBinding.itemRoot.background =
androidx.core.content.ContextCompat.getDrawable(
context,
android.util.TypedValue().apply {
context.theme.resolveAttribute(
resid,
this,
true
)
}.resourceId
)
viewBinding.itemRoot.background = backgroundSupplier.invoke(context)
viewBinding.itemVideoTitleView.typeface = typeface
}
}
Expand All @@ -627,6 +633,19 @@ class FeedFragment : BaseStateFragment<FeedState>() {
lastNewItemsCount = highlightCount
}

private fun resolveDrawable(context: Context, @AttrRes attrResId: Int): Drawable? {
return androidx.core.content.ContextCompat.getDrawable(
context,
android.util.TypedValue().apply {
context.theme.resolveAttribute(
attrResId,
this,
true
)
}.resourceId
)
}

private fun showNewItemsLoaded() {
tryGetNewItemsLoadedButton()?.clearAnimation()
tryGetNewItemsLoadedButton()
Expand Down

0 comments on commit 2057bad

Please sign in to comment.