functionality finished

This commit is contained in:
Bnyro 2022-06-08 18:29:00 +02:00
parent b59ba7b9c1
commit 512fdd8e90
3 changed files with 25 additions and 19 deletions

View File

@ -8,6 +8,7 @@ import android.widget.ImageView
import android.widget.TextView import android.widget.TextView
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.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.MainActivity import com.github.libretube.MainActivity
import com.github.libretube.R import com.github.libretube.R
@ -16,11 +17,11 @@ import com.github.libretube.obj.Comment
import com.github.libretube.obj.CommentsPage import com.github.libretube.obj.CommentsPage
import com.github.libretube.util.RetrofitInstance import com.github.libretube.util.RetrofitInstance
import com.squareup.picasso.Picasso import com.squareup.picasso.Picasso
import java.io.IOException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import retrofit2.HttpException import retrofit2.HttpException
import java.io.IOException
class CommentsAdapter( class CommentsAdapter(
private val videoId: String, private val videoId: String,
@ -31,6 +32,7 @@ class CommentsAdapter(
private var isLoading = false private var isLoading = false
private var nextPage = "" private var nextPage = ""
private lateinit var repliesRecView: RecyclerView private lateinit var repliesRecView: RecyclerView
private lateinit var repliesPage: CommentsPage
fun updateItems(newItems: List<Comment>) { fun updateItems(newItems: List<Comment>) {
var commentsSize = comments.size var commentsSize = comments.size
@ -77,15 +79,17 @@ class CommentsAdapter(
} }
} }
repliesRecView = holder.v.findViewById(R.id.replies_recView) repliesRecView = holder.v.findViewById(R.id.replies_recView)
// repliesRecView.layoutManager = LinearLayoutManager(holder.v.context) repliesRecView.layoutManager = LinearLayoutManager(holder.v.context)
val repliesAdapter = RepliesAdapter(CommentsPage().comments)
repliesRecView.adapter = repliesAdapter
holder.v.setOnClickListener { holder.v.setOnClickListener {
if (repliesRecView.visibility == View.GONE) { Log.e("clicekd", "clicked")
repliesRecView.visibility = View.VISIBLE if (repliesAdapter.itemCount == 0) {
nextPage = comments[position].repliesPage!! nextPage = comments[position].repliesPage!!
repliesRecView.adapter = CommentsAdapter(videoId, comments) fetchReplies(repliesAdapter)
// fetchReplies() // repliesAdapter.updateItems(repliesPage.comments)
} else { } else {
repliesRecView.visibility = View.GONE repliesAdapter.clear()
} }
} }
} }
@ -94,23 +98,20 @@ class CommentsAdapter(
return comments.size return comments.size
} }
private fun fetchReplies() { private fun fetchReplies(repliesAdapter: RepliesAdapter) {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.Main).launch {
if (!isLoading && nextPage != null) { if (!isLoading && nextPage != null) {
var response = CommentsPage()
isLoading = true isLoading = true
try { try {
response = 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 (response.nextpage != null) response.nextpage!! // nextPage = if (repliesPage.nextpage!! != null) repliesPage.nextpage!! else ""
else "" repliesAdapter.updateItems(repliesPage.comments)
// commentsAdapter?.updateItems(response.comments)
repliesRecView.adapter = RepliesAdapter(response.comments)
isLoading = false isLoading = false
} }
} }

View File

@ -22,6 +22,12 @@ class RepliesAdapter(
private var isLoading = false private var isLoading = false
private var nextPage = "" private var nextPage = ""
fun clear() {
val size: Int = replies.size
replies.clear()
notifyItemRangeRemoved(0, size)
}
fun updateItems(newItems: List<Comment>) { fun updateItems(newItems: List<Comment>) {
var repliesSize = replies.size var repliesSize = replies.size
replies.addAll(newItems) replies.addAll(newItems)

View File

@ -105,10 +105,9 @@
android:id="@+id/replies_recView" android:id="@+id/replies_recView"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="20dp" android:layout_marginLeft="16dp"
android:layout_marginLeft="20dp" android:layout_marginBottom="10dp"
android:nestedScrollingEnabled="false" android:nestedScrollingEnabled="false" />
android:visibility="gone" />
</LinearLayout> </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>