|
@@ -0,0 +1,134 @@
|
|
|
|
+package com.sharix.sportsmanfriend.view.fragments
|
|
|
|
+
|
|
|
|
+import android.annotation.SuppressLint
|
|
|
|
+import android.app.ActionBar
|
|
|
|
+import android.content.SharedPreferences
|
|
|
|
+import android.os.Bundle
|
|
|
|
+import android.speech.tts.TextToSpeech
|
|
|
|
+import android.util.Log
|
|
|
|
+import android.view.Gravity
|
|
|
|
+import androidx.fragment.app.Fragment
|
|
|
|
+import android.view.LayoutInflater
|
|
|
|
+import android.view.View
|
|
|
|
+import android.view.ViewGroup
|
|
|
|
+import android.widget.LinearLayout
|
|
|
|
+import android.widget.ScrollView
|
|
|
|
+import android.widget.TextView
|
|
|
|
+import androidx.lifecycle.ViewModelProvider
|
|
|
|
+import androidx.lifecycle.lifecycleScope
|
|
|
|
+import com.sharix.sportsmanfriend.R
|
|
|
|
+import com.sharix.sportsmanfriend.databinding.FragmentChat2Binding
|
|
|
|
+import com.sharix.sportsmanfriend.view.MainActivity
|
|
|
|
+import com.sharix.sportsmanfriend.viewmodel.XMPPViewModel
|
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
|
+import kotlinx.coroutines.launch
|
|
|
|
+import java.util.*
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+class ChatFragment : Fragment(),TextToSpeech.OnInitListener {
|
|
|
|
+ private lateinit var xmppViewModel: XMPPViewModel
|
|
|
|
+ private var _binding: FragmentChat2Binding? = null
|
|
|
|
+ private val binding get() = _binding!!
|
|
|
|
+ private var pref: SharedPreferences? = null
|
|
|
|
+ private var tts: TextToSpeech? = null
|
|
|
|
+ private val TAG: String = ChatFragment::class.java.simpleName
|
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
|
+ tts = TextToSpeech(context, this)
|
|
|
|
+ }
|
|
|
|
+ override fun onCreateView(
|
|
|
|
+ inflater: LayoutInflater, container: ViewGroup?,
|
|
|
|
+ savedInstanceState: Bundle?
|
|
|
|
+ ): View? {
|
|
|
|
+ _binding = FragmentChat2Binding.inflate(inflater, container, false)
|
|
|
|
+
|
|
|
|
+ xmppViewModel = ViewModelProvider(this@ChatFragment)[XMPPViewModel::class.java]
|
|
|
|
+
|
|
|
|
+// pref = activity?.getSharedPreferences(UsefullFunctions.PREFXMPP, Service.MODE_PRIVATE)
|
|
|
|
+// val jid = pref?.getString("jid", "!")
|
|
|
|
+// val pass = pref?.getString("pass", "!")
|
|
|
|
+ val jid = "test@msg.sharix-app.org"
|
|
|
|
+ val pass = "test1234@"
|
|
|
|
+ var room = activity?.intent?.getStringExtra("room") ?: "testbot@chat.msg.sharix-app.org"
|
|
|
|
+ //var intent = Intent(activity, XMPPListener::class.java)
|
|
|
|
+ //activity?.startService(intent)
|
|
|
|
+ //(activity as MainActivity).supportActionBar?.title = activity?.intent?.getStringExtra("room_name") ?: "Чат"
|
|
|
|
+ //binding.cpvLoader.visibility = View.VISIBLE
|
|
|
|
+ if(xmppViewModel.message.value.isNullOrEmpty()){
|
|
|
|
+ val value = lifecycleScope.launch(Dispatchers.Main) {
|
|
|
|
+ xmppViewModel.login(jid!!, pass!!, room)
|
|
|
|
+ Log.e("MAIN", "Out from corutine")
|
|
|
|
+ }
|
|
|
|
+ value.invokeOnCompletion {
|
|
|
|
+ Log.e("MAIN", value.toString())
|
|
|
|
+ //binding.cpvLoader.visibility = View.GONE
|
|
|
|
+ generateMessage(jid!!)
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ //binding.cpvLoader.visibility = View.GONE
|
|
|
|
+ generateMessage(jid!!)
|
|
|
|
+ }
|
|
|
|
+ binding.sendBtn.setOnClickListener {
|
|
|
|
+ xmppViewModel.sendMessage(binding.messageEditText.text.toString())
|
|
|
|
+ binding.messageEditText.text.clear()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return binding.root
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //@SuppressLint("UseCompatLoadingForDrawables", "RtlHardcoded")
|
|
|
|
+ fun generateMessage(login: String) {
|
|
|
|
+ xmppViewModel.message.observe(viewLifecycleOwner){
|
|
|
|
+ //binding.messageContainer.removeAllViews()
|
|
|
|
+ for(item in it){
|
|
|
|
+// var rectangle = TextView(context)
|
|
|
|
+// rectangle.width = 300
|
|
|
|
+// rectangle.height = 300
|
|
|
|
+// rectangle.background = context?.getDrawable(R.drawable.white_border)
|
|
|
|
+// binding.messageContainer.addView(rectangle)
|
|
|
|
+ var message = TextView(context)
|
|
|
|
+ val params = LinearLayout.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.WRAP_CONTENT)
|
|
|
|
+ //params.setMargins(10, 20, 10, 10)
|
|
|
|
+ context?.let { it1 -> message.setTextColor(it1.getColor(R.color.black)) }
|
|
|
|
+ //print(item.from)
|
|
|
|
+ if(item.from.toString().split("/")[1] == login) {
|
|
|
|
+ params.setMargins(200, 20, 10, 10)
|
|
|
|
+ message.background = context?.getDrawable(R.drawable.white_border)
|
|
|
|
+ params.gravity = Gravity.RIGHT
|
|
|
|
+ message.layoutParams = params
|
|
|
|
+ }else{
|
|
|
|
+ params.setMargins(10, 20, 200, 10)
|
|
|
|
+ message.background = context?.getDrawable(R.drawable.white_border)
|
|
|
|
+ message.layoutParams = params
|
|
|
|
+ //message.setOnClickListener { tts!!.speak(item.body.toString(), TextToSpeech.QUEUE_FLUSH, null, "Not supported") }
|
|
|
|
+ }
|
|
|
|
+ message.text = item.body.toString()
|
|
|
|
+ binding.messageContainer.addView(message)
|
|
|
|
+ scrollToBottom()
|
|
|
|
+ Log.e("XMPP", item.body)
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ private fun scrollToBottom(){
|
|
|
|
+ binding.chatScroll.post {
|
|
|
|
+ binding.chatScroll.fullScroll(ScrollView.FOCUS_DOWN)
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ override fun onInit(p0: Int) {
|
|
|
|
+
|
|
|
|
+ if (p0 == TextToSpeech.SUCCESS) {
|
|
|
|
+ val result = tts!!.setLanguage(Locale("ru"))
|
|
|
|
+
|
|
|
|
+ if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
|
|
|
|
+ Log.e("TTS","The Language not supported!")
|
|
|
|
+ } else {
|
|
|
|
+ Log.e("TTS","Supported!")
|
|
|
|
+ }
|
|
|
|
+ }else{
|
|
|
|
+ Log.e("TTS", "some error $p0")
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|