mirror of
https://gitlab.futo.org/videostreaming/grayjay.git
synced 2025-08-21 09:49:29 +00:00
catch exception remove UI lag
Changelog: changed
This commit is contained in:
parent
9a435f8859
commit
ae36a24ad1
1 changed files with 45 additions and 15 deletions
|
@ -13,6 +13,7 @@ import androidx.media3.common.util.UnstableApi
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
import androidx.viewpager2.widget.ViewPager2
|
import androidx.viewpager2.widget.ViewPager2
|
||||||
import com.futo.platformplayer.R
|
import com.futo.platformplayer.R
|
||||||
|
import com.futo.platformplayer.UIDialogs
|
||||||
import com.futo.platformplayer.api.media.models.video.IPlatformVideo
|
import com.futo.platformplayer.api.media.models.video.IPlatformVideo
|
||||||
import com.futo.platformplayer.api.media.structures.IPager
|
import com.futo.platformplayer.api.media.structures.IPager
|
||||||
import com.futo.platformplayer.constructs.Event0
|
import com.futo.platformplayer.constructs.Event0
|
||||||
|
@ -90,14 +91,7 @@ class ShortsFragment : MainFragment() {
|
||||||
|
|
||||||
loadPagerJob!!.invokeOnCompletion {
|
loadPagerJob!!.invokeOnCompletion {
|
||||||
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
|
viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
|
||||||
@OptIn(UnstableApi::class)
|
fun play(adapter: CustomViewAdapter, position: Int) {
|
||||||
override fun onPageSelected(position: Int) {
|
|
||||||
val adapter = (viewPager.adapter as CustomViewAdapter)
|
|
||||||
adapter.previousShownView?.stop()
|
|
||||||
adapter.previousShownView = null
|
|
||||||
|
|
||||||
// the post prevents lag when swiping
|
|
||||||
viewPager.post {
|
|
||||||
val recycler = (viewPager.getChildAt(0) as RecyclerView)
|
val recycler = (viewPager.getChildAt(0) as RecyclerView)
|
||||||
val viewHolder =
|
val viewHolder =
|
||||||
recycler.findViewHolderForAdapterPosition(position) as CustomViewHolder?
|
recycler.findViewHolderForAdapterPosition(position) as CustomViewHolder?
|
||||||
|
@ -110,6 +104,32 @@ class ShortsFragment : MainFragment() {
|
||||||
adapter.previousShownView = focusedView
|
adapter.previousShownView = focusedView
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onPageSelected(position: Int) {
|
||||||
|
val adapter = (viewPager.adapter as CustomViewAdapter)
|
||||||
|
if (adapter.previousShownView == null) {
|
||||||
|
// play if this page selection didn't trigger by a swipe from another page
|
||||||
|
play(adapter, position)
|
||||||
|
} else {
|
||||||
|
adapter.previousShownView?.stop()
|
||||||
|
adapter.previousShownView = null
|
||||||
|
adapter.newPosition = position
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// wait for the state to idle to prevent UI lag
|
||||||
|
override fun onPageScrollStateChanged(state: Int) {
|
||||||
|
super.onPageScrollStateChanged(state)
|
||||||
|
if (state == ViewPager2.SCROLL_STATE_IDLE) {
|
||||||
|
val adapter = (viewPager.adapter as CustomViewAdapter)
|
||||||
|
val position = adapter.newPosition
|
||||||
|
if (position == null) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
adapter.newPosition = null
|
||||||
|
|
||||||
|
play(adapter, position)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
|
@ -159,6 +179,15 @@ class ShortsFragment : MainFragment() {
|
||||||
}
|
}
|
||||||
} catch (_: CancellationException) {
|
} catch (_: CancellationException) {
|
||||||
return@launch
|
return@launch
|
||||||
|
} catch (err: Throwable) {
|
||||||
|
val message = "Unable to load shorts $err"
|
||||||
|
Logger.i(TAG, message)
|
||||||
|
UIDialogs.showDialog(
|
||||||
|
requireContext(), R.drawable.ic_sources, message, null, null, 0, UIDialogs.Action(
|
||||||
|
"Close", { }, UIDialogs.ActionStyle.PRIMARY
|
||||||
|
)
|
||||||
|
)
|
||||||
|
return@launch
|
||||||
}
|
}
|
||||||
|
|
||||||
// if it's been canceled then don't set the video pager
|
// if it's been canceled then don't set the video pager
|
||||||
|
@ -210,6 +239,7 @@ class ShortsFragment : MainFragment() {
|
||||||
) : RecyclerView.Adapter<CustomViewHolder>() {
|
) : RecyclerView.Adapter<CustomViewHolder>() {
|
||||||
val onResetTriggered = Event0()
|
val onResetTriggered = Event0()
|
||||||
var previousShownView: ShortView? = null
|
var previousShownView: ShortView? = null
|
||||||
|
var newPosition: Int? = null
|
||||||
var needToPlay: Int? = null
|
var needToPlay: Int? = null
|
||||||
|
|
||||||
@OptIn(UnstableApi::class)
|
@OptIn(UnstableApi::class)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue