Feature/threads

This commit is contained in:
Ivan Agosto 2024-04-16 03:55:53 +00:00
parent 0705d6dd80
commit 2484406cc7
14 changed files with 376 additions and 56 deletions

View File

@ -69,7 +69,7 @@ class LoginActivity : AppCompatActivity() {
"-2" -> {
// TODO: Start 2FA modal
runOnUiThread {
val builder = AlertDialog.Builder(this)
val builder = AlertDialog.Builder(this, R.style.Widget_Material3_MaterialCalendar_Fullscreen)
val dialog = layoutInflater.inflate(R.layout.two_factor_dialog, null)
val inputTwoFactor = dialog.findViewById<EditText>(R.id.twoFactorText)

View File

@ -368,6 +368,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte
mini_player_title.text = PlaybackSingleton.video!!.name
mini_player_author.text = PlaybackSingleton.video!!.username
Picasso.get().load("https://${ManagerSingleton.url}${PlaybackSingleton.video!!.thumbUrl}").into(mini_player_image)
mini_play_pause.setImageResource(R.drawable.ic_pause_24)
mini.visibility = View.VISIBLE
} else {
mini.visibility = View.GONE

View File

@ -28,6 +28,10 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.activity_reproductor.*
import kotlinx.android.synthetic.main.comment_component.commentaryBtn
import kotlinx.android.synthetic.main.comment_component.commentaryLayout
import kotlinx.android.synthetic.main.comment_component.commentaryText
import kotlinx.android.synthetic.main.comment_component.userImgCom
import org.libre.agosto.p2play.adapters.CommentariesAdapter
import org.libre.agosto.p2play.ajax.Actions
import org.libre.agosto.p2play.ajax.Comments
@ -275,7 +279,7 @@ class ReproductorActivity : AppCompatActivity() {
private fun setDataComments(data: ArrayList<CommentaryModel>) {
// Set data for RecyclerView
viewAdapter = CommentariesAdapter(data)
viewAdapter = CommentariesAdapter(data).setFragmentManager(supportFragmentManager)
recyclerView = findViewById<RecyclerView>(R.id.listCommentaries).apply {
// use this setting to improve performance if you know that changes
@ -330,10 +334,12 @@ class ReproductorActivity : AppCompatActivity() {
if (ManagerSingleton.user.avatar != "") {
Picasso.get().load("https://" + ManagerSingleton.url + ManagerSingleton.user.avatar).into(userImgCom)
}
} else {
commentaryLayout.visibility = View.GONE
}
}
fun getDescription() {
private fun getDescription() {
AsyncTask.execute {
val fullDescription = this.videos.fullDescription(this.video.id)
runOnUiThread {

View File

@ -1,22 +1,35 @@
package org.libre.agosto.p2play.adapters
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentTransaction
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
import org.libre.agosto.p2play.ManagerSingleton
import org.libre.agosto.p2play.R
import org.libre.agosto.p2play.ajax.Comments
import org.libre.agosto.p2play.dialogs.ThreadDialog
import org.libre.agosto.p2play.models.CommentaryModel
import java.io.Serializable
@Suppress("DEPRECATION")
class CommentariesAdapter(private val myDataset: ArrayList<CommentaryModel>) :
RecyclerView.Adapter<CommentariesAdapter.ViewHolder>() {
private lateinit var fragmentManager: FragmentManager
fun setFragmentManager (manager: FragmentManager): CommentariesAdapter {
this.fragmentManager = manager
return this
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder.
@ -25,6 +38,7 @@ class CommentariesAdapter(private val myDataset: ArrayList<CommentaryModel>) :
val userImg: ImageView
val username: TextView
val commentary: TextView
val replyBtn: TextView
val context: Context
init {
@ -32,6 +46,7 @@ class CommentariesAdapter(private val myDataset: ArrayList<CommentaryModel>) :
username = view.findViewById(R.id.userTxt)
commentary = view.findViewById(R.id.userCommentary)
userImg = view.findViewById(R.id.userCommentImg)
replyBtn = view.findViewById(R.id.replyBtn)
context = view.context
}
}
@ -40,7 +55,7 @@ class CommentariesAdapter(private val myDataset: ArrayList<CommentaryModel>) :
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int,
): CommentariesAdapter.ViewHolder {
): ViewHolder {
// create a new view
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.view_commentary, parent, false) as View
@ -66,6 +81,11 @@ class CommentariesAdapter(private val myDataset: ArrayList<CommentaryModel>) :
}
holder.commentary.text = Html.fromHtml(myDataset[position].commentary)
holder.replyBtn.setOnClickListener { this.initRepliesDialog(myDataset[position]) }
if (myDataset[position].replies > 0) {
holder.replyBtn.text = holder.itemView.context.getString(R.string.see_replies, myDataset[position].replies)
}
// TODO: Support for view and account (is different than a video channel)
// holder.userImg.setOnClickListener {
@ -77,4 +97,23 @@ class CommentariesAdapter(private val myDataset: ArrayList<CommentaryModel>) :
// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount() = myDataset.size
private fun initRepliesDialog (commentData: CommentaryModel) {
val dialog = ThreadDialog()
val bundle = Bundle()
bundle.putSerializable("comment", commentData as Serializable)
dialog.arguments = bundle
dialog.fragmentManager2 = this.fragmentManager
val transaction = fragmentManager.beginTransaction()
// For a polished look, specify a transition animation.
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
// To make it fullscreen, use the 'content' root view as the container
// for the fragment, which is always the root view for the activity.
transaction
.add(android.R.id.content, dialog)
.addToBackStack("comments")
.commit()
}
}

View File

@ -6,7 +6,6 @@ import org.libre.agosto.p2play.models.CommentaryModel
import java.io.InputStreamReader
class Comments : Client() {
private fun parseCommentaries(data: JsonReader): ArrayList<CommentaryModel> {
val commentaries = arrayListOf<CommentaryModel>()
data.beginObject()
@ -70,4 +69,68 @@ class Comments : Client() {
con.disconnect()
return response
}
fun getCommentariesThread(videoId: Int, threadId: Int): ArrayList<CommentaryModel> {
var commentaries = arrayListOf<CommentaryModel>()
val con = this.newCon("videos/$videoId/comment-threads/${threadId}", "GET")
try {
if (con.responseCode == 200) {
val response = InputStreamReader(con.inputStream)
val data = JsonReader(response)
data.beginObject()
while (data.hasNext()) {
when (data.nextName()) {
"children" -> {
data.beginArray()
while (data.hasNext()) {
data.beginObject()
while (data.hasNext()) {
when(data.nextName()) {
"comment" -> {
val comment = CommentaryModel()
comment.parseCommentary(data)
commentaries.add(comment)
}
else -> data.skipValue()
}
}
data.endObject()
}
data.endArray()
}
else -> data.skipValue()
}
}
data.endObject()
data.close()
}
} catch (err: Exception) {
err.printStackTrace()
}
con.disconnect()
return commentaries
}
fun replyThread(token: String, videoId: Int, threadId: Int , text: String): Boolean {
val con = this.newCon("videos/$videoId/comments/${threadId}", "POST", token)
val params = "text=$text"
con.outputStream.write(params.toByteArray())
val response: Boolean = try {
if (con.responseCode == 200) {
con.disconnect()
true
} else {
Log.d("Status", con.responseMessage)
false
}
} catch (err: Exception) {
err.printStackTrace()
false
}
con.disconnect()
return response
}
}

View File

@ -0,0 +1,136 @@
package org.libre.agosto.p2play.dialogs
import android.app.Dialog
import android.content.AsyncQueryHandler
import android.content.Context
import android.os.AsyncTask
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.comment_component.commentaryLayout
import kotlinx.android.synthetic.main.comment_component.commentaryText
import kotlinx.android.synthetic.main.comment_component.userImgCom
import kotlinx.android.synthetic.main.comment_component.view.commentaryBtn
import kotlinx.android.synthetic.main.comment_component.view.commentaryLayout
import kotlinx.android.synthetic.main.comment_component.view.commentaryText
import kotlinx.android.synthetic.main.comment_component.view.userImgCom
import kotlinx.android.synthetic.main.dialog_thread.view.materialToolbar
import kotlinx.android.synthetic.main.two_factor_dialog.twoFactorText
import kotlinx.android.synthetic.main.view_commentary.view.replyBtn
import kotlinx.android.synthetic.main.view_commentary.view.userCommentImg
import kotlinx.android.synthetic.main.view_commentary.view.userCommentary
import kotlinx.android.synthetic.main.view_commentary.view.userTxt
import org.libre.agosto.p2play.ManagerSingleton
import org.libre.agosto.p2play.R
import org.libre.agosto.p2play.adapters.CommentariesAdapter
import org.libre.agosto.p2play.ajax.Comments
import org.libre.agosto.p2play.models.CommentaryModel
class ThreadDialog : DialogFragment() {
private lateinit var comment: CommentaryModel
lateinit var fragmentManager2: FragmentManager
private val client: Comments = Comments()
// The system calls this to get the DialogFragment's layout, regardless of
// whether it's being displayed as a dialog or an embedded fragment.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
// Inflate the layout to use as a dialog or embedded fragment.
val view = inflater.inflate(R.layout.dialog_thread, container, false)
comment = arguments?.getSerializable("comment") as CommentaryModel
view.userTxt.text = comment.username
view.userCommentary.text = comment.commentary
Picasso.get().load("https://${ManagerSingleton.url}${comment.userImageUrl}").into(view.userCommentImg)
view.replyBtn.visibility = View.GONE
if (ManagerSingleton.user.status == 1) {
view.commentaryText.setText("${comment.username}@${comment.userHost} ")
if (ManagerSingleton.user.avatar != "") {
Picasso.get().load("https://${ManagerSingleton.url}${ManagerSingleton.user.avatar}").into(view.userImgCom)
}
view.commentaryText.requestFocus()
view.commentaryBtn.setOnClickListener { this.replyThread() }
} else {
view.commentaryLayout.visibility = View.GONE
}
view.materialToolbar.setTitle("Thread")
view.materialToolbar.setNavigationIcon(R.drawable.baseline_arrow_back_24)
view.materialToolbar.setNavigationOnClickListener {
dismiss()
this.fragmentManager2.popBackStack()
}
this.getComments()
return view
}
// The system calls this only when creating the layout in a dialog.
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
return dialog
}
private fun getComments() {
AsyncTask.execute {
val data =
this.client.getCommentariesThread(this.comment.videoId, this.comment.id)
activity?.runOnUiThread {
this.setDataComments(data)
}
}
}
private fun setDataComments(data: ArrayList<CommentaryModel>) {
// Set data for RecyclerView
val viewAdapter = CommentariesAdapter(data).setFragmentManager(this.fragmentManager2)
view?.findViewById<RecyclerView>(R.id.listCommentaries)?.let {
// use this setting to improve performance if you know that changes
// in content do not change the layout size of the RecyclerView
it.setHasFixedSize(true)
// use a linear layout manager
it.layoutManager = LinearLayoutManager(activity)
// specify an viewAdapter (see also next example)
it.adapter = viewAdapter
}
}
private fun replyThread() {
val commentary = view?.commentaryText?.text.toString()
if (commentary == "") {
ManagerSingleton.toast(getString(R.string.emptyCommentaryMsg), requireActivity())
return
}
AsyncTask.execute {
val res = this.client.replyThread(ManagerSingleton.token.token, this.comment.videoId, this.comment.id, commentary)
activity?.runOnUiThread {
if (res) {
ManagerSingleton.toast(getString(R.string.makedCommentaryMsg), requireActivity())
commentaryText.text?.clear()
this.getComments()
} else {
ManagerSingleton.toast(getString(R.string.errorCommentaryMsg), requireActivity())
}
}
}
}
}

View File

@ -2,6 +2,7 @@ package org.libre.agosto.p2play.models
import android.util.JsonReader
import android.util.JsonToken
import java.io.Serializable
class CommentaryModel(
var id: Int = 0,
@ -13,7 +14,8 @@ class CommentaryModel(
var userHost: String = "",
var replies: Int = 0,
var nameChannel: String = "",
) {
var videoId: Int = 0
): Serializable {
fun parseCommentary(data: JsonReader) {
data.beginObject()
while (data.hasNext()) {
@ -23,6 +25,7 @@ class CommentaryModel(
"threadId" -> this.threadId = data.nextInt()
"text" -> this.commentary = data.nextString()
"totalReplies" -> this.replies = data.nextInt()
"videoId" -> this.videoId = data.nextInt()
"account" -> {
data.beginObject()
while (data.hasNext()) {

View File

@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
<path android:fillColor="@android:color/white" android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
</vector>

View File

@ -220,15 +220,16 @@
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_weight="1"
android:gravity="center|center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/userImg"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:layout_weight="1"
android:adjustViewBounds="false"
android:adjustViewBounds="true"
android:cropToPadding="false"
android:padding="5dp"
android:scaleType="fitCenter"
@ -236,7 +237,7 @@
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:orientation="vertical">
@ -244,6 +245,7 @@
android:id="@+id/userTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxWidth="300dp"
android:textAppearance="@android:style/TextAppearance.Material.Large"
android:textSize="18sp"
android:textStyle="bold" />
@ -323,50 +325,7 @@
android:layout_width="match_parent"
android:layout_height="20dp" />
<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/commentaryLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/userImgCom"
android:layout_width="10dp"
android:layout_height="60dp"
android:layout_weight="1"
app:srcCompat="@drawable/default_avatar" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="3"
android:hint="@string/commentHolder">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/commentaryText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:inputType="textMultiLine" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/commentaryBtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/commentaryText" />
</androidx.appcompat.widget.LinearLayoutCompat>
<include layout="@layout/comment_component" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listCommentaries"

View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/commentaryLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/userImgCom"
android:layout_width="5dp"
android:layout_height="57dp"
android:layout_weight="1"
app:srcCompat="@drawable/default_avatar" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_weight="3"
android:hint="@string/commentHolder">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/commentaryText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:inputType="textMultiLine" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/commentaryBtn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="@string/commentaryText" />
</androidx.appcompat.widget.LinearLayoutCompat>

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="?attr/colorSurface"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="130dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.Toolbar
android:id="@+id/materialToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize" />
<include
layout="@layout/view_commentary"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/listCommentaries"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="10dp"/>
</LinearLayout>
<include
layout="@layout/comment_component"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -7,6 +7,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
@ -46,6 +47,14 @@
android:linksClickable="true"
android:maxLength="1000"
android:maxLines="10" />
<TextView
android:id="@+id/replyBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/reply"
android:textColor="?attr/colorSecondary"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -73,7 +73,7 @@
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:adjustViewBounds="false"
android:adjustViewBounds="true"
android:clickable="false"
android:cropToPadding="false"
android:padding="5dp"

View File

@ -95,6 +95,9 @@
<string name="unSubscribeBtn">Unsubscribe</string>
<string name="commentaryText">Comment</string>
<string name="showMore">Show more</string>
<!-- Comments -->
<string name="reply">Reply</string>
<string name="see_replies">See replies (%1$d)</string>
<!-- Messages -->
<string name="subscribeMsg">You have subscribed to this channel</string>
<string name="rateMsg">You have rated the video</string>