Skip to content

Commit 5d7815e

Browse files
committed
Fixed/Avoid NPEs
1 parent c914748 commit 5d7815e

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

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
}
@@ -263,6 +264,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
263264
}
264265

265266
override fun onDestroyView() {
267+
// Ensure that all animations are canceled
268+
feedBinding.newItemsLoadedButton?.clearAnimation()
269+
266270
feedBinding.itemsList.adapter = null
267271
_feedBinding = null
268272
super.onDestroyView()
@@ -624,9 +628,9 @@ class FeedFragment : BaseStateFragment<FeedState>() {
624628
}
625629

626630
private fun showNewItemsLoaded() {
627-
feedBinding.newItemsLoadedButton.clearAnimation()
628-
feedBinding.newItemsLoadedButton
629-
.slideUp(
631+
tryGetNewItemsLoadedButton()?.clearAnimation()
632+
tryGetNewItemsLoadedButton()
633+
?.slideUp(
630634
250L,
631635
delay = 100,
632636
execOnEnd = {
@@ -641,23 +645,32 @@ class FeedFragment : BaseStateFragment<FeedState>() {
641645
}
642646

643647
private fun hideNewItemsLoaded(animate: Boolean, delay: Long = 0) {
644-
feedBinding.newItemsLoadedButton.clearAnimation()
648+
tryGetNewItemsLoadedButton()?.clearAnimation()
645649
if (animate) {
646-
feedBinding.newItemsLoadedButton.animate(
650+
tryGetNewItemsLoadedButton()?.animate(
647651
false,
648652
200,
649653
delay = delay,
650654
execOnEnd = {
651655
// Make the layout invisible so that the onScroll toTop method
652656
// only does necessary work
653-
feedBinding?.newItemsLoadedButton?.isVisible = false
657+
tryGetNewItemsLoadedButton()?.isVisible = false
654658
}
655659
)
656660
} else {
657-
feedBinding.newItemsLoadedButton.isVisible = false
661+
tryGetNewItemsLoadedButton()?.isVisible = false
658662
}
659663
}
660664

665+
/**
666+
* The view/button can be disposed/set to null under certain circumstances.
667+
* E.g. when the animation is still in progress but the view got destroyed.
668+
* This method is a helper for such states and can be used in affected code blocks.
669+
*/
670+
private fun tryGetNewItemsLoadedButton(): Button? {
671+
return _feedBinding?.newItemsLoadedButton
672+
}
673+
661674
// /////////////////////////////////////////////////////////////////////////
662675
// Load Service Handling
663676
// /////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)