Skip to content

Commit

Permalink
Fixed default visibility of "new feed items" button
Browse files Browse the repository at this point in the history
Fixed/Avoid NPEs
  • Loading branch information
litetex committed Nov 15, 2021
1 parent 7456a25 commit e054554
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
29 changes: 21 additions & 8 deletions app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.annotation.Nullable
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.content.res.AppCompatResources
Expand Down Expand Up @@ -151,7 +152,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
!recyclerView.canScrollVertically(-1)
) {

if (feedBinding.newItemsLoadedButton.isVisible) {
if (tryGetNewItemsLoadedButton()?.isVisible == true) {
hideNewItemsLoaded(true)
}
}
Expand Down Expand Up @@ -263,6 +264,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
}

override fun onDestroyView() {
// Ensure that all animations are canceled
feedBinding.newItemsLoadedButton?.clearAnimation()

feedBinding.itemsList.adapter = null
_feedBinding = null
super.onDestroyView()
Expand Down Expand Up @@ -624,9 +628,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
}

private fun showNewItemsLoaded() {
feedBinding.newItemsLoadedButton.clearAnimation()
feedBinding.newItemsLoadedButton
.slideUp(
tryGetNewItemsLoadedButton()?.clearAnimation()
tryGetNewItemsLoadedButton()
?.slideUp(
250L,
delay = 100,
execOnEnd = {
Expand All @@ -641,23 +645,32 @@ class FeedFragment : BaseStateFragment<FeedState>() {
}

private fun hideNewItemsLoaded(animate: Boolean, delay: Long = 0) {
feedBinding.newItemsLoadedButton.clearAnimation()
tryGetNewItemsLoadedButton()?.clearAnimation()
if (animate) {
feedBinding.newItemsLoadedButton.animate(
tryGetNewItemsLoadedButton()?.animate(
false,
200,
delay = delay,
execOnEnd = {
// Make the layout invisible so that the onScroll toTop method
// only does necessary work
feedBinding?.newItemsLoadedButton?.isVisible = false
tryGetNewItemsLoadedButton()?.isVisible = false
}
)
} else {
feedBinding.newItemsLoadedButton.isVisible = false
tryGetNewItemsLoadedButton()?.isVisible = false
}
}

/**
* The view/button can be disposed/set to null under certain circumstances.
* E.g. when the animation is still in progress but the view got destroyed.
* This method is a helper for such states and can be used in affected code blocks.
*/
private fun tryGetNewItemsLoadedButton(): Button? {
return _feedBinding?.newItemsLoadedButton
}

// /////////////////////////////////////////////////////////////////////////
// Load Service Handling
// /////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/res/layout/fragment_feed.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@
android:layout_marginBottom="5sp"
android:text="@string/feed_new_items"
android:textSize="12sp"
android:theme="@style/ServiceColoredButton" />
android:theme="@style/ServiceColoredButton"
android:visibility="gone"
tools:visibility="visible" />

<LinearLayout
android:layout_width="wrap_content"
Expand Down

0 comments on commit e054554

Please sign in to comment.