Skip to content

Commit 4eb02f5

Browse files
committed
Fixed default visibility of "new feed items" button
Fixed/Avoid NPEs
1 parent 700c1b4 commit 4eb02f5

2 files changed

Lines changed: 24 additions & 9 deletions

File tree

app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import android.view.MenuInflater
3232
import android.view.MenuItem
3333
import android.view.View
3434
import android.view.ViewGroup
35+
import android.widget.Button
3536
import androidx.annotation.Nullable
3637
import androidx.appcompat.app.AlertDialog
3738
import androidx.appcompat.content.res.AppCompatResources
@@ -151,7 +152,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
151152
!recyclerView.canScrollVertically(-1)
152153
) {
153154

154-
if (feedBinding.newItemsLoadedButton.isVisible) {
155+
if (tryGetNewItemsLoadedButton()?.isVisible == true) {
155156
hideNewItemsLoaded(true)
156157
}
157158
}
@@ -264,6 +265,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
264265
}
265266

266267
override fun onDestroyView() {
268+
// Ensure that all animations are canceled
269+
feedBinding.newItemsLoadedButton?.clearAnimation()
270+
267271
feedBinding.itemsList.adapter = null
268272
_feedBinding = null
269273
super.onDestroyView()
@@ -619,9 +623,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
619623
}
620624

621625
private fun showNewItemsLoaded() {
622-
feedBinding.newItemsLoadedButton.clearAnimation()
623-
feedBinding.newItemsLoadedButton
624-
.slideUp(
626+
tryGetNewItemsLoadedButton()?.clearAnimation()
627+
tryGetNewItemsLoadedButton()
628+
?.slideUp(
625629
250L,
626630
delay = 100,
627631
execOnEnd = {
@@ -636,23 +640,32 @@ class FeedFragment : BaseStateFragment<FeedState>() {
636640
}
637641

638642
private fun hideNewItemsLoaded(animate: Boolean, delay: Long = 0) {
639-
feedBinding.newItemsLoadedButton.clearAnimation()
643+
tryGetNewItemsLoadedButton()?.clearAnimation()
640644
if (animate) {
641-
feedBinding.newItemsLoadedButton.animate(
645+
tryGetNewItemsLoadedButton()?.animate(
642646
false,
643647
200,
644648
delay = delay,
645649
execOnEnd = {
646650
// Make the layout invisible so that the onScroll toTop method
647651
// only does necessary work
648-
feedBinding?.newItemsLoadedButton?.isVisible = false
652+
tryGetNewItemsLoadedButton()?.isVisible = false
649653
}
650654
)
651655
} else {
652-
feedBinding.newItemsLoadedButton.isVisible = false
656+
tryGetNewItemsLoadedButton()?.isVisible = false
653657
}
654658
}
655659

660+
/**
661+
* The view/button can be disposed/set to null under certain circumstances.
662+
* E.g. when the animation is still in progress but the view got destroyed.
663+
* This method is a helper for such states and can be used in affected code blocks.
664+
*/
665+
private fun tryGetNewItemsLoadedButton(): Button? {
666+
return _feedBinding?.newItemsLoadedButton
667+
}
668+
656669
// /////////////////////////////////////////////////////////////////////////
657670
// Load Service Handling
658671
// /////////////////////////////////////////////////////////////////////////

app/src/main/res/layout/fragment_feed.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@
9696
android:layout_marginBottom="5sp"
9797
android:text="@string/feed_new_items"
9898
android:textSize="12sp"
99-
android:theme="@style/ServiceColoredButton" />
99+
android:theme="@style/ServiceColoredButton"
100+
android:visibility="gone"
101+
tools:visibility="visible" />
100102

101103
<LinearLayout
102104
android:layout_width="wrap_content"

0 commit comments

Comments
 (0)