Merge pull request #1688 from Bnyro/master

Channel tab fixes
This commit is contained in:
Bnyro 2022-10-29 16:29:06 +02:00 committed by GitHub
commit 037806361e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 21 deletions

View File

@ -44,7 +44,7 @@ class ChannelFragment : BaseFragment() {
private var onScrollEnd: () -> Unit = {}
val scope = CoroutineScope(Dispatchers.IO)
private val scope = CoroutineScope(Dispatchers.IO)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -115,11 +115,7 @@ class ChannelFragment : BaseFragment() {
val shareData = ShareData(currentChannel = response.name)
onScrollEnd = {
if (nextPage != null && !isLoading) {
isLoading = true
binding.channelRefresh.isRefreshing = true
fetchChannelNextPage()
}
fetchChannelNextPage()
}
// fetch and update the subscription status
@ -197,6 +193,12 @@ class ChannelFragment : BaseFragment() {
binding.videos.setOnClickListener {
binding.channelRecView.adapter = channelAdapter
onScrollEnd = {
fetchChannelNextPage()
}
binding.tabChips.children.forEach { child ->
if (child != it) (child as Chip).isChecked = false
}
}
response.tabs?.firstOrNull { it.name == "Playlists" }?.let {
@ -239,20 +241,11 @@ class ChannelFragment : BaseFragment() {
binding.channelRecView.adapter = adapter
}
var tabNextPage = response.nextpage
onScrollEnd = {
if (response.nextpage != null) {
scope.launch {
val newContent = try {
RetrofitInstance.api.getChannelTab(tab.data, response.nextpage)
} catch (e: Exception) {
e.printStackTrace()
null
}
runOnUiThread {
newContent?.content?.let {
adapter.updateItems(it)
}
}
tabNextPage?.let {
fetchTabNextPage(it, tab, adapter) { nextPage ->
tabNextPage = nextPage
}
}
}
@ -262,12 +255,15 @@ class ChannelFragment : BaseFragment() {
private fun fetchChannelNextPage() {
fun run() {
if (nextPage == null || isLoading) return
isLoading = true
binding.channelRefresh.isRefreshing = true
lifecycleScope.launchWhenCreated {
val response = try {
RetrofitInstance.api.getChannelNextPage(channelId!!, nextPage!!)
} catch (e: IOException) {
binding.channelRefresh.isRefreshing = false
println(e)
Log.e(TAG(), "IOException, you might not have internet connection")
return@launchWhenCreated
} catch (e: HttpException) {
@ -283,4 +279,26 @@ class ChannelFragment : BaseFragment() {
}
run()
}
private fun fetchTabNextPage(
nextPage: String,
tab: ChannelTab,
adapter: SearchAdapter,
onNewNextPage: (String?) -> Unit
) {
scope.launch {
val newContent = try {
RetrofitInstance.api.getChannelTab(tab.data ?: "", nextPage)
} catch (e: Exception) {
e.printStackTrace()
null
}
onNewNextPage.invoke(newContent?.nextpage)
runOnUiThread {
newContent?.content?.let {
adapter.updateItems(it)
}
}
}
}
}

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
@ -18,7 +19,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="8dp" />
android:paddingVertical="8dp"
android:paddingStart="8dp"
android:paddingEnd="40dp"
tools:ignore="RtlSymmetry" />
<Button
android:id="@+id/addToPlaylist"