Merge pull request #438 from Bnyro/master

fix comment replies
This commit is contained in:
Bnyro 2022-06-09 09:17:12 +02:00 committed by GitHub
commit d7b3a4bdbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 61 deletions

View File

@ -6,6 +6,7 @@ import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageView import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
import android.widget.Toast
import androidx.constraintlayout.motion.widget.MotionLayout import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -30,9 +31,8 @@ class CommentsAdapter(
private val TAG = "CommentsAdapter" private val TAG = "CommentsAdapter"
private var isLoading = false private var isLoading = false
private var nextPage = "" private var nextpage = ""
private lateinit var repliesRecView: RecyclerView private var repliesPage = CommentsPage()
private lateinit var repliesPage: CommentsPage
fun updateItems(newItems: List<Comment>) { fun updateItems(newItems: List<Comment>) {
var commentsSize = comments.size var commentsSize = comments.size
@ -78,14 +78,18 @@ class CommentsAdapter(
} catch (e: Exception) { } catch (e: Exception) {
} }
} }
repliesRecView = holder.v.findViewById(R.id.replies_recView) val repliesRecView = holder.v.findViewById<RecyclerView>(R.id.replies_recView)
repliesRecView.layoutManager = LinearLayoutManager(holder.v.context) repliesRecView.layoutManager = LinearLayoutManager(holder.v.context)
val repliesAdapter = RepliesAdapter(CommentsPage().comments) val repliesAdapter = RepliesAdapter(CommentsPage().comments)
repliesRecView.adapter = repliesAdapter repliesRecView.adapter = repliesAdapter
holder.v.setOnClickListener { holder.v.setOnClickListener {
if (repliesAdapter.itemCount == 0) { if (repliesAdapter.itemCount == 0) {
nextPage = comments[position].repliesPage!! if (comments[position].repliesPage != null) {
fetchReplies(repliesAdapter) nextpage = comments[position].repliesPage!!
fetchReplies(nextpage, repliesAdapter)
} else {
Toast.makeText(holder.v.context, R.string.no_replies, Toast.LENGTH_SHORT).show()
}
// repliesAdapter.updateItems(repliesPage.comments) // repliesAdapter.updateItems(repliesPage.comments)
} else { } else {
repliesAdapter.clear() repliesAdapter.clear()
@ -97,19 +101,19 @@ class CommentsAdapter(
return comments.size return comments.size
} }
private fun fetchReplies(repliesAdapter: RepliesAdapter) { private fun fetchReplies(nextpage: String, repliesAdapter: RepliesAdapter) {
CoroutineScope(Dispatchers.Main).launch { CoroutineScope(Dispatchers.Main).launch {
if (!isLoading && nextPage != null) { if (!isLoading && nextpage != null) {
isLoading = true isLoading = true
try { try {
repliesPage = RetrofitInstance.api.getCommentsNextPage(videoId!!, nextPage!!) repliesPage = RetrofitInstance.api.getCommentsNextPage(videoId!!, nextpage!!)
} catch (e: IOException) { } catch (e: IOException) {
println(e) println(e)
Log.e(TAG, "IOException, you might not have internet connection") Log.e(TAG, "IOException, you might not have internet connection")
} catch (e: HttpException) { } catch (e: HttpException) {
Log.e(TAG, "HttpException, unexpected response," + e.response()) Log.e(TAG, "HttpException, unexpected response," + e.response())
} }
// nextPage = if (repliesPage.nextpage!! != null) repliesPage.nextpage!! else "" // nextpage = if (repliesPage.nextpage!! != null) repliesPage.nextpage!! else ""
repliesAdapter.updateItems(repliesPage.comments) repliesAdapter.updateItems(repliesPage.comments)
isLoading = false isLoading = false
} }

View File

@ -1,41 +0,0 @@
package com.github.libretube.dialogs
import android.app.Dialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import com.github.libretube.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class UpdateAvailableDialog(
private val versionTag: String,
private val updateLink: String,
private val updateAvailable: Boolean
) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
if (updateAvailable) {
MaterialAlertDialogBuilder(requireContext())
.setTitle(context?.getString(R.string.update_available, versionTag))
.setMessage(context?.getString(R.string.update_available_text))
.setNegativeButton(context?.getString(R.string.cancel)) { _, _ ->
dismiss()
}
.setPositiveButton(context?.getString(R.string.okay)) { _, _ ->
val uri = Uri.parse(updateLink)
val intent = Intent(Intent.ACTION_VIEW).setData(uri)
startActivity(intent)
}
.show()
} else {
MaterialAlertDialogBuilder(requireContext())
.setTitle(context?.getString(R.string.app_uptodate))
.setMessage(context?.getString(R.string.no_update_available))
.setPositiveButton(context?.getString(R.string.okay)) { _, _ -> }
.show()
}
} ?: throw IllegalStateException("Activity cannot be null")
}
}

View File

@ -0,0 +1,46 @@
package com.github.libretube.dialogs
import android.app.Dialog
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.fragment.app.DialogFragment
import com.github.libretube.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class UpdateAvailableDialog(
private val versionTag: String,
private val updateLink: String
) : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
MaterialAlertDialogBuilder(requireContext())
.setTitle(context?.getString(R.string.update_available, versionTag))
.setMessage(context?.getString(R.string.update_available_text))
.setNegativeButton(context?.getString(R.string.cancel)) { _, _ ->
dismiss()
}
.setPositiveButton(context?.getString(R.string.okay)) { _, _ ->
val uri = Uri.parse(updateLink)
val intent = Intent(Intent.ACTION_VIEW).setData(uri)
startActivity(intent)
}
.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}
class NoUpdateAvailableDialog() : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return activity?.let {
MaterialAlertDialogBuilder(requireContext())
.setTitle(context?.getString(R.string.app_uptodate))
.setMessage(context?.getString(R.string.no_update_available))
.setPositiveButton(context?.getString(R.string.okay)) { _, _ -> }
.create()
} ?: throw IllegalStateException("Activity cannot be null")
}
}

View File

@ -3,6 +3,7 @@ package com.github.libretube.util
import android.util.Log import android.util.Log
import androidx.fragment.app.FragmentManager import androidx.fragment.app.FragmentManager
import com.github.libretube.BuildConfig import com.github.libretube.BuildConfig
import com.github.libretube.dialogs.NoUpdateAvailableDialog
import com.github.libretube.dialogs.UpdateAvailableDialog import com.github.libretube.dialogs.UpdateAvailableDialog
import java.io.BufferedReader import java.io.BufferedReader
import java.io.InputStreamReader import java.io.InputStreamReader
@ -28,18 +29,13 @@ fun checkUpdate(childFragmentManager: FragmentManager) {
if (updateInfo?.tagName != "" && BuildConfig.VERSION_NAME != updateInfo?.tagName) { if (updateInfo?.tagName != "" && BuildConfig.VERSION_NAME != updateInfo?.tagName) {
val updateAvailableDialog = UpdateAvailableDialog( val updateAvailableDialog = UpdateAvailableDialog(
updateInfo?.tagName!!, updateInfo?.tagName!!,
updateInfo?.updateUrl!!, updateInfo?.updateUrl!!
true
)
updateAvailableDialog.show(childFragmentManager, "UpdateDialog")
} else {
// otherwise show the no update available dialog
val updateAvailableDialog = UpdateAvailableDialog(
updateInfo?.tagName!!,
updateInfo?.updateUrl!!,
false
) )
updateAvailableDialog.show(childFragmentManager, "UpdateAvailableDialog") updateAvailableDialog.show(childFragmentManager, "UpdateAvailableDialog")
} else {
// otherwise show the no update available dialog
val noUpdateAvailableDialog = NoUpdateAvailableDialog()
noUpdateAvailableDialog.show(childFragmentManager, "NoUpdateAvailableDialog")
} }
} }

View File

@ -141,4 +141,5 @@
<string name="appearance_summary">Make the app look how you want to.</string> <string name="appearance_summary">Make the app look how you want to.</string>
<string name="advanced_summary">Player, Downloads, History</string> <string name="advanced_summary">Player, Downloads, History</string>
<string name="live">Live</string> <string name="live">Live</string>
<string name="no_replies">This comment has no replies.</string>
</resources> </resources>