diff options
Diffstat (limited to 'Client')
4 files changed, 174 insertions, 26 deletions
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<CommentsAdapter.ViewHolder>? = 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<CommentSend>?=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<CommentSend>) +        adapterComments=CommentsAdapter(comments as MutableList<CommentSend>,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<CommentSend>):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<CommentSend>) +class CommentsAdapter (val items : MutableList<CommentSend>,val activity: Activity)      : RecyclerView.Adapter<CommentsAdapter.ViewHolder>(){      //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<CommentSend>)              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<CommentSend>,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<CommentSend?> { +                override fun onResponse(call: Call<CommentSend?>, response: Response<CommentSend?>) { +                    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<CommentSend?>, 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 Binary files differnew file mode 100644 index 0000000..0c1071b --- /dev/null +++ b/Client/BrzoDoLokacije/app/src/main/res/drawable/reply.png 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 @@  <?xml version="1.0" encoding="utf-8"?> -<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" +<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" +    xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="wrap_content"      android:padding="@dimen/component_padding"      xmlns:app="http://schemas.android.com/apk/res-auto"> -    <TextView -        android:id="@+id/tvCommentAuthor" + +    <ImageView +        android:layout_width="50dp" +        android:layout_height="50dp" +        android:src="@drawable/ic_nav_profile" +        app:layout_constraintEnd_toStartOf="@+id/tvCommentText" +        app:layout_constraintHorizontal_bias="0.0" +        app:layout_constraintStart_toStartOf="parent" +        tools:layout_editor_absoluteY="27dp" /> + +    <androidx.appcompat.widget.LinearLayoutCompat          android:layout_width="wrap_content"          android:layout_height="wrap_content" -        android:text="autor" -        android:textStyle="bold" -        android:padding="@dimen/text_padding"/> -    <TextView -        android:id="@+id/tvCommentText" -        android:layout_width="match_parent" -        android:layout_height="wrap_content" -        android:text="autor" -        android:padding="@dimen/text_padding" -        app:layout_constraintTop_toBottomOf="@id/tvCommentAuthor"/> +        android:orientation="vertical" +        android:layout_weight="1"> + +        <TextView +            android:id="@+id/tvCommentAuthor" +            android:layout_width="wrap_content" +            android:layout_height="wrap_content" +            android:text="autor" +            android:textStyle="bold" +            android:padding="@dimen/text_padding"/> + +        <TextView +            android:id="@+id/tvCommentText" +            android:layout_width="match_parent" +            android:layout_height="wrap_content" +            android:padding="@dimen/text_padding" +            app:layout_constraintTop_toBottomOf="@id/tvCommentAuthor" +            tools:layout_editor_absoluteX="54dp" /> +        <androidx.appcompat.widget.LinearLayoutCompat +            android:layout_width="match_parent" +            android:layout_height="wrap_content" +            android:orientation="horizontal" +            android:layoutDirection="rtl"> +            <ImageButton +                android:layout_width="50dp" +                android:layout_height="50dp" +                android:src="@drawable/reply" +                android:layout_weight="0" +                android:id="@+id/btnReply" +                android:layout_gravity="end" +                android:backgroundTint="@color/white" +                android:scaleType="centerCrop"/> + +            <EditText +                android:layout_width="match_parent" +                android:layout_height="wrap_content" +                android:layout_weight="1" +                android:id="@+id/etReply" +                android:layoutDirection="ltr" +                android:hint="odgovor na komentar"/> +        </androidx.appcompat.widget.LinearLayoutCompat> +        <androidx.recyclerview.widget.RecyclerView +            android:layout_width="match_parent" +            android:layout_height="wrap_content" +            android:id="@+id/rvReplies"/> +    </androidx.appcompat.widget.LinearLayoutCompat> -</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file +</androidx.appcompat.widget.LinearLayoutCompat>
\ No newline at end of file  | 
