From 71bfced6c95b79399fc1faf0adfd0af9f87edcd1 Mon Sep 17 00:00:00 2001 From: Jelena Petrovic Date: Fri, 2 Dec 2022 01:28:21 +0100 Subject: Omoguceno ugnjezdeno komentarisanje #67 --- .../Activities/ActivitySinglePost.kt | 31 ++++--- .../brzodolokacije/Adapters/CommentsAdapter.kt | 95 ++++++++++++++++++++- .../app/src/main/res/drawable/reply.png | Bin 0 -> 4690 bytes .../app/src/main/res/layout/single_comment.xml | 74 +++++++++++++--- 4 files changed, 174 insertions(+), 26 deletions(-) create mode 100644 Client/BrzoDoLokacije/app/src/main/res/drawable/reply.png diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt index edbec21..f3d8a63 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Activities/ActivitySinglePost.kt @@ -3,12 +3,9 @@ package com.example.brzodolokacije.Activities import android.content.Intent import android.graphics.Color import android.graphics.drawable.ColorDrawable -import android.media.Image import android.os.Bundle import android.preference.PreferenceManager -import android.provider.ContactsContract.CommonDataKinds.Im import android.util.Log -import android.widget.ImageButton import android.widget.TextView import android.widget.Toast import androidx.appcompat.app.AppCompatActivity @@ -24,7 +21,6 @@ import com.example.brzodolokacije.Services.SharedPreferencesHelper import com.example.brzodolokacije.databinding.ActivitySinglePostBinding import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.gson.Gson -import okhttp3.ResponseBody import org.osmdroid.config.Configuration import org.osmdroid.tileprovider.tilesource.TileSourceFactory import org.osmdroid.util.GeoPoint @@ -42,7 +38,7 @@ class ActivitySinglePost : AppCompatActivity() { private var adapterComments: RecyclerView.Adapter? = null private var recyclerViewImages: RecyclerView?=null private var recyclerViewComments: RecyclerView?=null - private lateinit var post:PostPreview + public lateinit var post:PostPreview private var comments:MutableList?=mutableListOf() private var starNumber:Number=0 private lateinit var userData:UserReceive @@ -114,9 +110,9 @@ class ActivitySinglePost : AppCompatActivity() { fun buildRecyclerViewComments(){ recyclerViewComments=binding.rvComments - adapterComments=CommentsAdapter(comments as MutableList) + adapterComments=CommentsAdapter(comments as MutableList,this@ActivitySinglePost) layoutManagerComments= LinearLayoutManager(this@ActivitySinglePost,LinearLayoutManager.VERTICAL,false) - recyclerViewComments!!.setHasFixedSize(true) + recyclerViewComments!!.setHasFixedSize(false) recyclerViewComments!!.layoutManager=layoutManagerComments recyclerViewComments!!.adapter= adapterComments } @@ -245,9 +241,9 @@ class ActivitySinglePost : AppCompatActivity() { if(comments!=null && comments!!.isNotEmpty()){ buildRecyclerViewComments() if(comments!=null) - binding.tvCommentCount.text=comments?.size.toString() + binding.tvCommentCount.text=countComments(comments!!).toString() else - binding.tvCommentCount.text="0" + binding.tvCommentCount.text="12" } }else{ if(response.errorBody()!=null) @@ -265,8 +261,7 @@ class ActivitySinglePost : AppCompatActivity() { else{ (adapterComments as CommentsAdapter).items.add(0,newComment) recyclerViewComments?.adapter=adapterComments - Log.d("main",newComment.username) - binding.tvCommentCount.text=comments?.size.toString() + addedComment() } } @@ -368,4 +363,18 @@ class ActivitySinglePost : AppCompatActivity() { } }) } + fun countComments(comments:List):Int{ + var count:Int=0 + for(c in comments){ + if(c.replies!=null) + count=count+countComments(c.replies!!) + count=count+1 + } + return count + } + + public fun addedComment(){ + binding.tvCommentCount.text=(Integer.parseInt(binding.tvCommentCount.text.toString())+1).toString() + binding.tvCommentCount.invalidate() + } } diff --git a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Adapters/CommentsAdapter.kt b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Adapters/CommentsAdapter.kt index d43057f..06713ec 100644 --- a/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Adapters/CommentsAdapter.kt +++ b/Client/BrzoDoLokacije/app/src/main/java/com/example/brzodolokacije/Adapters/CommentsAdapter.kt @@ -1,12 +1,26 @@ package com.example.brzodolokacije.Adapters +import android.app.Activity +import android.content.Context +import android.util.Log import android.view.LayoutInflater +import android.view.View import android.view.ViewGroup +import android.view.inputmethod.InputMethodManager +import android.widget.EditText +import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.example.brzodolokacije.Activities.ActivitySinglePost +import com.example.brzodolokacije.Models.CommentReceive import com.example.brzodolokacije.Models.CommentSend +import com.example.brzodolokacije.R +import com.example.brzodolokacije.Services.RetrofitHelper +import com.example.brzodolokacije.Services.SharedPreferencesHelper import com.example.brzodolokacije.databinding.SingleCommentBinding +import retrofit2.Call +import retrofit2.Response -class CommentsAdapter (val items : MutableList) +class CommentsAdapter (val items : MutableList,val activity: Activity) : RecyclerView.Adapter(){ //constructer has one argument - list of objects that need to be displayed //it is bound to xml of single item @@ -32,7 +46,86 @@ class CommentsAdapter (val items : MutableList) binding.apply { tvCommentAuthor.text=item.username tvCommentText.text=item.comment + etReply.visibility= View.GONE + etReply.forceLayout() + etReply.showSoftInputOnFocus=true + etReply.setOnFocusChangeListener { _, focused -> + if(!focused){ + etReply.visibility= View.GONE + etReply.forceLayout() + btnReply.setImageResource(R.drawable.reply) + hideKeyboard(etReply) + } + else{ + showKeyboard(etReply) + btnReply.setImageResource(R.drawable.post_comment) + btnReply.setOnClickListener{ + if(etReply.text.isNotEmpty()){ + val postId=(activity as ActivitySinglePost).post._id + val comment= CommentReceive(etReply.text.toString(),item._id) + requestAddComment(binding,comment,postId) + } + else{ + Log.d("komentari","greska") + } + } + } + } + btnReply.setOnClickListener { + etReply.visibility=View.VISIBLE + etReply.forceLayout() + etReply.requestFocus() + } + + var rv: RecyclerView = rvReplies + rv.setHasFixedSize(false) + rv.layoutManager=LinearLayoutManager(activity,LinearLayoutManager.VERTICAL,false) + if(item.replies!=null) + rv.adapter=CommentsAdapter(item.replies as MutableList,activity) + else + rv.adapter=CommentsAdapter(mutableListOf(),activity) } } + fun showKeyboard(item:EditText){ + var imm:InputMethodManager=activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + imm.showSoftInput(item,InputMethodManager.SHOW_FORCED) + } + + fun hideKeyboard(item: EditText){ + var imm:InputMethodManager=activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + imm.hideSoftInputFromWindow(item.windowToken,InputMethodManager.HIDE_IMPLICIT_ONLY) + } + fun requestAddComment(binding:SingleCommentBinding,comment:CommentReceive,postId:String){ + val postApi= RetrofitHelper.getInstance() + val token= SharedPreferencesHelper.getValue("jwt", activity) + val request=postApi.addComment("Bearer "+token,postId,comment) + request.enqueue(object : retrofit2.Callback { + override fun onResponse(call: Call, response: Response) { + if(response.isSuccessful){ + var newComment=response.body()!! + requestGetComments(binding,newComment) + binding.etReply.text.clear() + hideKeyboard(binding.etReply) + }else{ + if(response.errorBody()!=null) + Log.d("main1",response.message().toString()) + } + + + } + + override fun onFailure(call: Call, t: Throwable) { + Log.d("main2",t.message.toString()) + } + }) + } + + private fun requestGetComments(binding:SingleCommentBinding,newComment: CommentSend) { + var rv: RecyclerView = binding.rvReplies + var adapter:CommentsAdapter=rv.adapter as CommentsAdapter + adapter.items.add(0,newComment) + rv.adapter=adapter + (activity as ActivitySinglePost).addedComment() + } } } \ No newline at end of file diff --git a/Client/BrzoDoLokacije/app/src/main/res/drawable/reply.png b/Client/BrzoDoLokacije/app/src/main/res/drawable/reply.png new file mode 100644 index 0000000..0c1071b Binary files /dev/null and b/Client/BrzoDoLokacije/app/src/main/res/drawable/reply.png differ diff --git a/Client/BrzoDoLokacije/app/src/main/res/layout/single_comment.xml b/Client/BrzoDoLokacije/app/src/main/res/layout/single_comment.xml index f219ea2..51d9079 100644 --- a/Client/BrzoDoLokacije/app/src/main/res/layout/single_comment.xml +++ b/Client/BrzoDoLokacije/app/src/main/res/layout/single_comment.xml @@ -1,22 +1,68 @@ - - + + - + android:orientation="vertical" + android:layout_weight="1"> + + + + + + + + + + + - \ No newline at end of file + \ No newline at end of file -- cgit v1.2.3