this is the chat functionality on my app but the messages aren't appearing on my app although it is getting updated in the firebase. when I hardcoded the values it is working. this is the conversation activity. I've used a chat fragment and adapter as well. But the messages are just not getting posted on the app. any alternative solutions or a solution for this would be nice.
private val firebaseDB = FirebaseFirestore.getInstance()
private val userId = FirebaseAuth.getInstance().currentUser?.uid
private val conversationAdapter = ConversationAdapter(arrayListOf(), userId)
private var chatId: String? = null
private var imageUrl: String? = null
private var otherUserId: String? = null
private var chatName: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_conversation)
chatId = intent.extras.getString(PARAM_CHAT_ID)
imageUrl = intent.extras.getString(PARAM_IMAGE_URL)
chatName = intent.extras.getString(chatName)
otherUserId = intent.extras.getString(PARAM_OTHER_USER_ID)
if (chatId.isNullOrEmpty() || userId.isNullOrEmpty()) {
Toast.makeText(this, "chat room error", Toast.LENGTH_LONG).show()
finish()
}
topNameTV.text = chatName
populateImage(this, imageUrl, topPhotoIV, R.drawable.default_user)
messagesRV.apply {
setHasFixedSize(false)
layoutManager = LinearLayoutManager(context)
adapter = conversationAdapter
}
firebaseDB.collection(DATA_CHATS)
.document(chatId!!)
.collection(DATA_CHAT_MESSAGES)
.orderBy(DATA_CHAT_MESSAGE_TIME)
.addSnapshotListener{ querySnapshot, firebaseFirestoreException ->
if(firebaseFirestoreException != null){
firebaseFirestoreException.printStackTrace()
return#addSnapshotListener
}else{
if (querySnapshot != null){
for(change in querySnapshot.documentChanges){
when(change.type){
DocumentChange.Type.ADDED -> {
val message = change.document.toObject(Convo::class.java)
if(message != null){
conversationAdapter.addMessage(message)
messagesRV.post {
messagesRV.smoothScrollToPosition(conversationAdapter.itemCount -1)
}
}
}
}
}
}
}
}
}
fun onSend(v: View) {
if (!messageET.text.isNullOrEmpty()){
val message = Convo(userId, messageET.text.toString(), System.currentTimeMillis())
firebaseDB.collection(DATA_CHATS).document(chatId!!)
.collection(DATA_CHAT_MESSAGES)
.document()
.set(message)
messageET.setText("",TextView.BufferType.EDITABLE)
}
}
companion object {
private val PARAM_CHAT_ID = "Chat id"
private val PARAM_IMAGE_URL = "Image url"
private val PARAM_OTHER_USER_ID = "Other user id"
private val PARAM_CHAT_NAME = "Chat name"
fun newIntent(context: Context?, chatId: String?, imageUrl: String?, otherUserId: String?
chatName: String?): Intent{
val intent = Intent(context,ConversationActivity::class.java)
intent.putExtra(PARAM_CHAT_ID,chatId)
intent.putExtra(PARAM_IMAGE_URL, imageUrl)
intent.putExtra(PARAM_OTHER_USER_ID, otherUserId)
intent.putExtra(PARAM_CHAT_NAME, chatName)
return intent
}
}
}
Related
hi i am new in android and kotlin and i have a chat app that will work with sms and want save state of all chat for next run
i did use recyclerView and GroupieViewHolder for show chats , i see some post here but all was with java but i am using kotlin for this app
so if you can please help me and If possible, state the easiest way in the simplest possible way
app screenshot:
my smsActivity.kt:
class smsActivity : AppCompatActivity() {
private val messageAdapter = GroupAdapter<GroupieViewHolder>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sms)
val recyclerView = findViewById<RecyclerView>(R.id.listmessage)
val button = findViewById<Button>(R.id.button)
val editmessage = findViewById<EditText>(R.id.editText)
val sharedPeref = getSharedPreferences("setNumber", MODE_PRIVATE)
val number = sharedPeref.getString("number", null)
recyclerView.adapter = messageAdapter
populateData()
receiveAutoResponse()
button.setOnClickListener {
val message = Message(text = editmessage.text.toString(), sendBy = "me")
val smssend = editmessage.text.toString()
val sendMessageItem = SendMessageItem(message)
messageAdapter.add(sendMessageItem)
editmessage.text.clear()
receiveAutoResponse()
recyclerView.scrollToPosition(messageAdapter.getItemCount() - 1);
try {
val smsManager: SmsManager = SmsManager.getDefault()
smsManager.sendTextMessage(number, null, smssend, null, null)
Toast.makeText(
applicationContext,
"بخاری 3 =دریافت گزارش ✅ دریافت گزارش چندین ثانیه زمان می برد ، لطفا برای ارسال دستور بعدی کمی صبر کنید",
Toast.LENGTH_LONG
).show()
} catch (e: Exception) {
Toast.makeText(
applicationContext,
"لطفا ابتدا شماره سیستم را وارد کنید !",
Toast.LENGTH_SHORT
).show()
val intent = Intent(this, setnumberActivity::class.java)
this.startActivity(intent)
}
}
}
fun ScrollView.scrollToBottom() {
val lastChild = getChildAt(childCount - 1)
val bottom = lastChild.bottom + paddingBottom
val delta = bottom - (scrollY + height)
smoothScrollBy(0, delta)
}
private fun populateData() {
val data = listOf<Message>()
data.forEach {
if (it.sendBy == "me") {
messageAdapter.add(SendMessageItem(it))
} else {
messageAdapter.add(ReceiveMessageItem(it))
}
}
}
private fun receiveAutoResponse() {
GlobalScope.launch(Dispatchers.Main) {
delay(1000)
val sharedPeref = getSharedPreferences("setNumber", MODE_PRIVATE)
val nums = "+98" + sharedPeref.getString("number", null)
val cursor: Cursor? = getContentResolver().query(
Uri.parse("content://sms"),
null,
"address='$nums'",
null,
null
)
cursor?.moveToFirst()
val messageSend = cursor?.getString(12)
val receive = Message(text = "$messageSend", sendBy = "me")
val receiveItem = ReceiveMessageItem(receive)
messageAdapter.add(receiveItem)
val recyclerView = findViewById<RecyclerView>(R.id.listmessage)
recyclerView.scrollToPosition(messageAdapter.getItemCount() - 1);
}
}
}
class SendMessageItem(private val message: Message) : BindableItem<ItemMessageSendBinding>() {
override fun getLayout(): Int {
return R.layout.item_message_send
}
override fun bind(viewBinding: ItemMessageSendBinding, position: Int) {
viewBinding.message = message
}
}
class ReceiveMessageItem(private val message: Message) : BindableItem<ItemMessageReceiveBinding>() {
override fun getLayout(): Int {
return R.layout.item_message_receive
}
override fun bind(viewBinding: ItemMessageReceiveBinding, position: Int) {
viewBinding.message = message
}
}
If you want ensure that Activity re-creation doesn't destroy your content, you can refer to this answer
If you want to connect the data to your local Storage, I suggest you using Room
Basically I have a activity which shows all the records input by the user. This uses a recyclerview. When the user clicks on an item row, it takes that info and displays it on the screen in another activity which is "Edit buy screen" . The user can edit the information and update it and then will be taken back to the "all records screen" upon success. using getIntent, I managed to show the info on the screen. However when I try to update the info it doesnt change. According to debug. The var studentModel(Dataclass obj) is null on the second activity. This tells me that i need a way to bring the studentModel obj data from allrecords activity to the editScreen activity. Any way i can do this using getIntent or another way?
All records class
private lateinit var edName: EditText
private lateinit var edEmail: Button
private lateinit var edBuyAmount: EditText
private lateinit var edUseAmount: EditText
private lateinit var edReason: EditText
private lateinit var btnAdd: Button
private lateinit var btnView: Button
private lateinit var btnUpdate: Button
private lateinit var sqLiteHelper: SQLiteHelper
private lateinit var recyclerView: RecyclerView
private var adapter: StudentAdapter?=null
private var std:StudentModel?=null <-------------This holds the data when the user clicks a row of data
#RequiresApi(Build.VERSION_CODES.N)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_all_recordpage)
val textView: TextView =findViewById(R.id.DateText)
val simpleDateFormat= SimpleDateFormat("yyyy/MM/dd\n HH:mm", Locale.getDefault()).format(
Date()
)
val currentDateAndTime: String = simpleDateFormat.format(Date())
textView.text = currentDateAndTime
val button = findViewById<Button>(R.id.goBackToHome)
button.setOnClickListener{
val intent = Intent(this,MainMenu::class.java)
startActivity(intent)
}
initView()
initRecyclerView()
sqLiteHelper= SQLiteHelper(this)
//Show recycler view
val stdList = sqLiteHelper.getAllStudent()
Log.e("おっけ","${stdList.size}")
adapter?.setOnClickItem {
//購入・使用編集画面に遷移
if(it.buyamount > 0){
val intent = Intent(this,buyDetailsScreen::class.java)
intent.putExtra("date",it.email)
intent.putExtra("name", it.name)
intent.putExtra("buyAmount", it.buyamount)
//----> (I assume I have to carry the var std to the next intent?)
startActivity(intent)
}else if (it.useamount > 0){
val intent = Intent(this,useDetailsScreen::class.java)
startActivity(intent)
edName.setText(it.name)
edEmail.text = it.email
edUseAmount.setText(""+it.useamount)
edReason.setText(it.reason)
std = it
}else{
Toast.makeText(this,"エラーが発生しました",Toast.LENGTH_LONG).show()
}
}
adapter?.addItems(stdList)
adapter?.setOnClickDeleteItem{
deleteStudent(it.id)
}
}
private fun deleteStudent(id:Int){
val builder = AlertDialog.Builder(this)
builder.setMessage("データを削除してよろしいですか")
builder.setCancelable(true)
builder.setNegativeButton("いいえ"){dialog, _ ->
dialog.dismiss()
}
builder.setPositiveButton("はい"){dialog, _ ->
sqLiteHelper.deleteStudentById(id)
getStudents()
dialog.dismiss()
}
val alert = builder.create()
alert.show()
}
private fun updateStudent(){
val name = edName.text.toString()
val email = edEmail.text.toString()
val buyAmount = edBuyAmount.text.toString().toInt()
val useAmount = edUseAmount.text.toString().toInt()
val reason = edReason.text.toString()
//Check record not changed
if(name == std?.name && email == std?.email && buyAmount == std?.buyamount && useAmount == std?.useamount && reason == std?.reason){
Toast.makeText(this,"データが変更されてない", Toast.LENGTH_SHORT).show()
return
}
if(std == null) return
val std = StudentModel(id=std!!.id,name = name,email = email, buyamount = buyAmount, useamount = useAmount, reason = reason)
val status = sqLiteHelper.updateStudent(std)
if(status > -1){
clearEditText()
getStudents()
}else{
Toast.makeText(this,"更新失敗した", Toast.LENGTH_SHORT).show()
}
}
private fun getStudents(){
val stdList = sqLiteHelper.getAllStudent()
Log.e("おっけ","${stdList.size}")
adapter?.addItems(stdList)
}
private fun addStudent(){
val name = edName.text.toString()
val email = edEmail.text.toString()
val buyAmount = edBuyAmount.text.toString().toInt()
val useAmount = edUseAmount.text.toString().toInt()
val reason = edReason.text.toString()
if(name.isEmpty()||email.isEmpty()|| buyAmount.toString().isEmpty() ||useAmount.toString().isEmpty() || reason.toString().isEmpty()){
Toast.makeText(this,"データを入力してください", Toast.LENGTH_SHORT).show()
}else{
val std = StudentModel(name = name, email=email, buyamount=buyAmount, useamount=useAmount, reason = reason)
val status = sqLiteHelper.insertStudent(std)
//Check Insert success or not success
if(status > -2){
Toast.makeText(this,"データを追加しました。", Toast.LENGTH_SHORT).show()
clearEditText()
}else{
Toast.makeText(this,"データが保存されてないようです。", Toast.LENGTH_SHORT).show()
}
}
}
private fun clearEditText(){
edName.setText("")
edEmail.text = ""
edBuyAmount.setText("")
edUseAmount.setText("")
edReason.setText("")
edName.requestFocus()
}
private fun initRecyclerView(){
recyclerView.layoutManager=LinearLayoutManager(this)
adapter = StudentAdapter()
recyclerView.adapter=adapter
}
private fun initView(){
recyclerView=findViewById(R.id.recyclerView)
}
}``
Student model data class
import java.util.*
data class StudentModel(
var id: Int=getAutoId(),
var name: String = "",
var email: String = "",
var buyamount: Int = 0,
var useamount: Int=0,
var reason: String = ""
)
{
companion object{
fun getAutoId():Int{
val random = Random()
return random.nextInt(100)
}
}
}
Buy details screen
class buyDetailsScreen : AppCompatActivity() {
private lateinit var edName: EditText
private lateinit var edEmail: Button
private lateinit var edBuyAmount: EditText
private lateinit var btnUpdate: Button
private lateinit var sqLiteHelper: SQLiteHelper
private lateinit var recyclerView: RecyclerView
private var adapter: StudentAdapter?=null
private var std:StudentModel?= null //Has nothing inside it . I need to get the info from first activity and put here
#SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_buy_details_screen)
val button = findViewById<Button>(R.id.goBackToMain)
button.setOnClickListener{
val intent = Intent(this,allRecordpage::class.java)
startActivity(intent)
}
val button2 = findViewById<Button>(R.id.goBacktoMain2)
button2.setOnClickListener{
val intent = Intent(this,allRecordpage::class.java)
startActivity(intent)
}
initView()
initRecyclerView()
sqLiteHelper= SQLiteHelper(this)
//Show recycler view
val stdList = sqLiteHelper.getAllStudent()
Log.e("おっけ","${stdList.size}")
adapter?.addItems(stdList)
edEmail = findViewById(R.id.edEmail)
edEmail.setOnClickListener{
clickDatePicker()
}
edEmail.text = intent.getStringExtra("date")
edName.setText(intent.getStringExtra("name"))
edBuyAmount.setText(""+(intent.getIntExtra("buyAmount",0)))
btnUpdate.setOnClickListener{
updateStudent()
val intent = Intent(this,allRecordpage::class.java)
startActivity(intent)
}
}
private fun clickDatePicker() {
val calender = Calendar.getInstance()
val year = calender.get(Calendar.YEAR)
val month = calender.get(Calendar.MONTH)
val day = calender.get(Calendar.DAY_OF_MONTH)
val datepicker = DatePickerDialog(this,
{ view,selectedYear,selectedMonth,selectedDay ->
val selectedDate = "$selectedYear/${selectedMonth+1}/$selectedDay"
edEmail?.text=selectedDate
},
year,
month,
day
)
datepicker.datePicker.maxDate = System.currentTimeMillis()
datepicker.show()
}
private fun updateStudent(){
val name = edName.text.toString()
val email = edEmail.text.toString()
val buyAmount = edBuyAmount.text.toString().toInt()
//Check record not changed
if(name == std?.name && email == std?.email && buyAmount == std?.buyamount){
Toast.makeText(this,"データが変更されてない", Toast.LENGTH_SHORT).show()
return
}
if(std == null) return
val std = StudentModel(id=std!!.id,name = name,email = email, buyamount = buyAmount )
val status = sqLiteHelper.updateStudent(std)
if(status > -1){
clearEditText()
getStudents()
}else{
Toast.makeText(this,"更新失敗した", Toast.LENGTH_SHORT).show()
}
}
private fun getStudents(){
val stdList = sqLiteHelper.getAllStudent()
Log.e("おっけ","${stdList.size}")
adapter?.addItems(stdList)
}
private fun clearEditText(){
edName.setText("")
edEmail.text = ""
edBuyAmount.setText(0)
edName.requestFocus()
}
private fun initRecyclerView(){
recyclerView.layoutManager= LinearLayoutManager(this)
adapter = StudentAdapter()
recyclerView.adapter=adapter
}
private fun initView(){
edName = findViewById(R.id.edName)
edEmail = findViewById(R.id.edEmail)
edBuyAmount = findViewById(R.id.edBuyAmount)
btnUpdate=findViewById(R.id.btnUpdate)
recyclerView=findViewById(R.id.recyclerView)
}
If you see the read messages function in my activity class below, i wanted to update the isSeen field in firestore, but for some reason it does not work at all. My guess it requires a specific document value but that would not be possible as this a messaging app so there will be a lot of documents created.
Activity Class
class MessageActivity : AppCompatActivity() {
private lateinit var binding: ActivityMessageBinding
private lateinit var chat: ArrayList<Message>
private lateinit var messageAdapter: MessageAdapter
private lateinit var roomID: String
private lateinit var userID: String
private lateinit var recID: String
private var c: Int = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMessageBinding.inflate(layoutInflater)
setContentView(binding.root)
userID = FirebaseAuth.getInstance().uid.toString()
recID = intent.getStringExtra("userID").toString()
val recName:String = intent.getStringExtra("userName").toString()
binding.userName.text = recName
chat = arrayListOf()
messageAdapter = MessageAdapter(chat)
binding.recyclerView.setHasFixedSize(true)
binding.recyclerView.layoutManager = LinearLayoutManager(this)
when {
userID < recID ->
{
roomID = userID + recID
}
userID.compareTo(recID) == 0 ->
{
Toast.makeText(this, "Error you are chatting with yourself!!!", Toast.LENGTH_SHORT).show()
}
else -> {
roomID = recID + userID
}
}
readMessages(userID,recID)
binding.btnSend.setOnClickListener {
val message: String = binding.textSend.text.toString()
if(message.isNotEmpty()){
sendMessage(userID,recID,message)
binding.textSend.text.clear()
}
else{
Toast.makeText(this,"You can't send empty message", Toast.LENGTH_SHORT).show()
}
}
binding.gps.setOnClickListener {
val uri = "http://maps.google.com/maps?daddr="
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(uri))
intent.setPackage("com.google.android.apps.maps")
startActivity(intent)
}
}
private fun sendMessage(sender: String, rec: String, message: String){
val db = Firebase.firestore
val time: FieldValue = FieldValue.serverTimestamp()
val msg = hashMapOf(
"userID" to sender,
"recID" to rec,
"message" to message,
"time" to time,
"roomID" to roomID,
"isSeen" to false
)
db.collection("chats").document(roomID).collection("messages").document().set(msg,SetOptions.merge())
}
private fun readMessages(userId: String, recId: String){
val rootRef = Firebase.firestore
rootRef.collection("chats").document(roomID).collection("messages").orderBy("time", Query.Direction.ASCENDING).addSnapshotListener(object : EventListener<QuerySnapshot?>
{
override fun onEvent(#Nullable documentSnapshots: QuerySnapshot?, #Nullable e: FirebaseFirestoreException?)
{
if (e != null)
{
Log.e(TAG, "onEvent: Listen failed.", e)
return
}
chat.clear()
if (documentSnapshots != null)
{
for (queryDocumentSnapshots in documentSnapshots)
{
val msg = queryDocumentSnapshots.toObject(Message::class.java)
if (msg.recID == recId && msg.userID == userId || msg.recID == userId && msg.userID == recId)
{
chat.add(msg)
}
if(msg.recID.equals(userID).and(msg.userID.equals(recID))){
rootRef.collection("chats").document(roomID).collection("messages").document().update("isSeen",true)
}
messageAdapter = MessageAdapter(chat)
binding.recyclerView.adapter = messageAdapter
}
}
}
})
}
}
Adapter Class
class MessageAdapter(private val MessageList:ArrayList<Message>):RecyclerView.Adapter<MessageAdapter.MessageViewHolder>() {
private val left = 0
private val right = 1
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MessageViewHolder {
return if(viewType==right){
val view1 = LayoutInflater.from(parent.context).inflate(R.layout.chat_sender_item,parent,false)
MessageViewHolder(view1)
}else{
val view2 = LayoutInflater.from(parent.context).inflate(R.layout.chat_receiver_item,parent,false)
MessageViewHolder(view2)
}
}
override fun onBindViewHolder(holder: MessageViewHolder, position: Int) {
val message:Message = MessageList[position]
holder.showMessage.text = message.message
if(position==MessageList.size-1){
if(message.isSeen)
{
holder.textSeen.text = "Seen"
}else{
holder.textSeen.text = "Delivered"
}
}else{
holder.textSeen.visibility = View.GONE
}
}
override fun getItemCount(): Int {
return MessageList.size
}
class MessageViewHolder(itemView:View) : RecyclerView.ViewHolder(itemView){
val showMessage: TextView = itemView.findViewById(R.id.showMessage)
val textSeen: TextView = itemView.findViewById(R.id.textSeen)
}
override fun getItemViewType(position: Int): Int {
val userID = FirebaseAuth.getInstance().currentUser!!.uid
return if(MessageList[position].userID==userID)
{
right
}else
{
left
}
}
}
Model Class
package com.aarondcosta99.foodreuseapp.model
data class Message(var userID:String? = "",var message:String? = "",var recID:String? = "",var isSeen:Boolean=false)
Firestore
This won't work:
rootRef.collection("chats").document(roomID).collection("messages").document().update("isSeen",true)
The document() call without any arguments creates a reference to a new non-existing document, which you then try to update. But update() only works when a document already exists, you can't use update() to create a document, so this code ends up doing nothing.
To update a document, you need to specify the complete path to that document. The fact that you need to update a lot of documents makes no difference to that fact, it just means you'll need to paths to a lot of documents.
As far as I can tell, you are trying to update the document that you read in documentSnapshots, which means you already have the DocumentReference handy and can update it with:
queryDocumentSnapshots.reference.update("isSeen",true)
I make a mobile application using the "io fotoapparat" library and I want to create a document with a random id and in the document in the id field, add the document id, the photo we will take was updated in the firebase store and in the previously made about the same id in the receipt collection when taking a photo
link from which I took "io fotoapparat"
EDIT
add to firestore document
binding.button.setOnClickListener {
// Inflate the layout for this fragment
val biedronka = "biedronka"
val price = "20"
val img = " "
val identificator = " "
val from = biedronka
val value = price
val image = img
val idd = identificator
val db = Firebase.firestore
val data = hashMapOf(
"from" to from,
"value" to value,
"image" to image,
"id" to idd
)
db.collection("receipts")
.add(data)
.addOnSuccessListener { documentReference ->
Log.d(SCAN_DEBUG, "DocumentSnapshot written with ID: ${documentReference.id}")
db.collection("receipts")
.document(documentReference.id)
.update("id", documentReference.id)
.addOnSuccessListener {
}
}
.addOnFailureListener { e ->
Log.w(SCAN_DEBUG, "Error adding document", e)
}
}
2 add to storage and to document (doesnt work)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == REQUEST_CAPTURE_IMAGE && resultCode == RESULT_OK) {
val uid = profileVm.user.value?.uid!!
val imageBitmap = data?.extras?.get("data") as Bitmap
val userImage = binding.userImg
val stream = ByteArrayOutputStream()
val result = imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
val byteArray = stream.toByteArray()
if (result) profileVm.uploadUserPhoto(byteArray, "$uid.jpg")
}
}
repository to 2
fun uploadReceiptPhoto(bytes: ByteArray) {
storage.getReference("receipts")
.child("${docId}.jpg")
.putBytes(bytes)
.addOnCompleteListener{
Log.d(REPO_DEBUG, "COMPLETE UPLOAD PHOTO")
}
.addOnSuccessListener {
getReceiptPhotoDownloadUrl(it.storage)
}
.addOnFailureListener {
Log.d(REPO_DEBUG, it.message.toString())
}
}
private fun getReceiptPhotoDownloadUrl(storage: StorageReference) {
storage.downloadUrl
.addOnSuccessListener {
updateReceiptPhoto(it.toString())
}
.addOnFailureListener {
Log.d(REPO_DEBUG, it.message.toString())
}
}
private fun updateReceiptPhoto(url: String) {
cloud.collection("receipts")
.document(docId)
.update("image", url)
.addOnSuccessListener {
Log.d(REPO_DEBUG, "UPDATE USER PHOTO")
}
.addOnFailureListener {
Log.d(REPO_DEBUG, it.message.toString())
}
}
THE REST OF THE CODE (camera code "io fotoapparat" and take pictures)
class ScanFragment : Fragment(), OnReceiptsItemAdd {
private var _binding: FragmentScanBinding? = null
private val binding get() = _binding!!
private val scanVm by viewModels<ScanViewModel>()
private val SCAN_DEBUG = "SCAN_DEBUG"
private var fotoapparat: Fotoapparat? = null
private var fotoapparatState: FotoapparatState? = null
private var cameraStatus: CameraState? = null
private var flashState: FlashState? = null
private val permissions = arrayOf(Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
_binding = FragmentScanBinding.inflate(inflater, container, false)
createFotoapparat()
cameraStatus = CameraState.BACK
flashState = FlashState.OFF
fotoapparatState = FotoapparatState.OFF
binding.fabSwitchCamera.setOnClickListener {
switchCamera()
}
binding.fabFlash.setOnClickListener {
changeFlashState()
}
return binding.root
}
private fun createFotoapparat(){
val cameraView = binding.cameraView
fotoapparat = Fotoapparat(
context = requireContext(),
view = cameraView,
scaleType = ScaleType.CenterCrop,
lensPosition = back(),
logger = loggers(
logcat()
),
cameraErrorCallback = { error ->
println("Recorder errors: $error")
}
)
}
private fun changeFlashState() {
fotoapparat?.updateConfiguration(
CameraConfiguration(
flashMode = if(flashState == FlashState.TORCH) off() else torch()
)
)
flashState = if(flashState == FlashState.TORCH) FlashState.OFF
else FlashState.TORCH
}
private fun switchCamera() {
fotoapparat?.switchTo(
lensPosition = if (cameraStatus == CameraState.BACK) front() else back(),
cameraConfiguration = CameraConfiguration()
)
cameraStatus = if(cameraStatus == CameraState.BACK) CameraState.FRONT
else CameraState.BACK
}
private fun takePhoto() {
if (hasNoPermissions()) {
requestPermission()
}else{
fotoapparat
?.takePicture()
?.toBitmap()
}
}
override fun onStart() {
super.onStart()
if (hasNoPermissions()) {
requestPermission()
}else{
fotoapparat?.start()
fotoapparatState = FotoapparatState.ON
}
}
private fun hasNoPermissions(): Boolean{
return ContextCompat.checkSelfPermission(requireContext(),
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(requireContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(requireContext(),
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
}
private fun requestPermission(){
ActivityCompat.requestPermissions(requireActivity(), permissions,0)
}
override fun onStop() {
super.onStop()
fotoapparat?.stop()
FotoapparatState.OFF
}
}
enum class CameraState{
FRONT, BACK
}
enum class FlashState{
TORCH, OFF
}
enum class FotoapparatState{
ON, OFF
}
I am trying to build a chatting application but whenever I click on the any of the users it crashes.
class MessegeActivity : AppCompatActivity() {
var userIdVisit: String = ""
var currentUserId : FirebaseUser?=null
var chatsAdpater:ChatAdpater?=null
var mChatList:List<Chat>?=null
lateinit var recycler_view_chat:RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_messege)
intent = intent
userIdVisit = intent.getStringExtra("visit_id")
currentUserId =FirebaseAuth.getInstance().currentUser!!
recycler_view_chat =findViewById(R.id.recycler_view_chat)
recycler_view_chat.setHasFixedSize(true)
var linearLayoutManager =LinearLayoutManager(applicationContext)
linearLayoutManager.stackFromEnd =true
recycler_view_chat.layoutManager =linearLayoutManager
//showing other username and profile dp
val reference = FirebaseDatabase.getInstance().reference
.child("Users").child(userIdVisit)
reference.addValueEventListener(object : ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
val user: Users? = p0.getValue(Users::class.java)
username_mchat.text = user!!.getFullName()
Picasso.get().load(user.getImg()).into(profile_image_mchat)
retreiveMsg(currentUserId!!.uid,userIdVisit,user.getImg())
}
override fun onCancelled(error: DatabaseError) {
}
})
send_messege_btn.setOnClickListener {
val messege = text_messege.text.toString()
if (messege == "") {
Toast.makeText(this#MessegeActivity, "Please write something!!", Toast.LENGTH_SHORT)
.show()
} else {
sendMessegeToUser(currentUserId!!.uid, userIdVisit, messege)
}
text_messege.setText("")
}
attach_image_file.setOnClickListener {
val intent = Intent()
intent.action = Intent.ACTION_GET_CONTENT
intent.type = "image/*"
startActivityForResult(Intent.createChooser(intent, "Pick Image"), 198)
}
}
private fun sendMessegeToUser(senderId: String, receiverId: String?, messege: String) {
val reference = FirebaseDatabase.getInstance().reference
val messegeKey:String = reference.push().key!!
val messegeHashMap = HashMap<String, Any?>()
messegeHashMap["sender"] = senderId
messegeHashMap["messege"] = messege
messegeHashMap["receiver"] = receiverId
messegeHashMap["isseen"] = "false"
messegeHashMap["url"] = ""
messegeHashMap["messegeId"] = messegeKey
reference.child("Chats").child(messegeKey).setValue(messegeHashMap)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val ChatsListRefernce =
FirebaseDatabase.getInstance().reference.child("ChatLists").child(currentUserId!!.uid).child(userIdVisit)
ChatsListRefernce.addListenerForSingleValueEvent(object :ValueEventListener{
override fun onDataChange(p0: DataSnapshot) {
if(!p0.exists())
{
ChatsListRefernce.child("id").setValue(userIdVisit)
}
val ChatsListReceiverRefernce =
FirebaseDatabase.getInstance().reference.child("ChatLists").child(userIdVisit).child(currentUserId!!.uid)
ChatsListReceiverRefernce.child("id").setValue(currentUserId!!.uid)
}
override fun onCancelled(error: DatabaseError) {
}
})
//notification
// val reference = FirebaseDatabase.getInstance().reference
// .child("Users").child(currentUserId!!.uid)
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 198 && resultCode ==RESULT_OK && data!=null && data.data != null) {
val progressBar = ProgressDialog(this)
progressBar.setMessage("Please wait")
progressBar.show()
val fileUri = data.data
val storageRef = FirebaseStorage.getInstance().reference.child("Chat Images")
val ref = FirebaseDatabase.getInstance().reference
val messegeId = ref.push().key
val filepath = storageRef.child("$messegeId.jpg")
var uploadTask: StorageTask<*>
uploadTask = filepath.putFile(fileUri!!)
uploadTask.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>> { task ->
if (!task.isSuccessful) {
task.exception?.let {
throw it
}
}
return#Continuation filepath.downloadUrl
}).addOnCompleteListener { task ->
val downloadUrl = task.result
val url = downloadUrl.toString()
val messegeHashMap = HashMap<String,Any?>()
messegeHashMap["sender"] = currentUserId!!.uid
messegeHashMap["messege"] = "sent you an image"
messegeHashMap["receiver"] = userIdVisit
messegeHashMap["isseen"] = "false"
messegeHashMap["url"] = url
messegeHashMap["messegeId"] = messegeId.toString()
ref.child("Chats").child(messegeId!!).setValue(messegeHashMap)
progressBar.dismiss()
}
}
}
private fun retreiveMsg(senderId: String, receiverId: String?, recieverimgUrl: String?) {
mChatList =ArrayList()
val reference =FirebaseDatabase.getInstance().reference.child("Chats")
reference.addValueEventListener(object:ValueEventListener{
override fun onDataChange(p0: DataSnapshot) {
(mChatList as ArrayList<Chat>).clear()
for (snapshot in p0.children)
{
val chat= snapshot.getValue(Chat::class.java)
if(chat!!.getReceiver().equals(senderId)&& chat.getSender().equals(receiverId) ||
chat.getReceiver().equals(receiverId)&& chat.getSender().equals(senderId))
{
(mChatList as ArrayList<Chat>).add(chat)
}
chatsAdpater = ChatAdpater(this#MessegeActivity,(mChatList as ArrayList<Chat>),recieverimgUrl!!)
recycler_view_chat.adapter =chatsAdpater
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
}
package com.insidecoderz.trails2.Model
class Chat {
private var sender:String =""
private var messege:String =""
private var receiver:String =""
private var isseen :Boolean= false
private var url:String =""
private var messegeId:String =""
constructor()
constructor(
sender: String,
messege: String,
receiver: String,
isseen:Boolean,
url: String,
messegeId: String
) {
this.sender = sender
this.messege = messege
this.receiver = receiver
this.isseen = isseen
this.url = url
this.messegeId = messegeId
}
fun getSender(): String?{
return sender
}
fun setSender(sender: String){
this.sender =sender
}
fun getMessege(): String?{
return messege
}
fun setMessege(messege: String){
this.messege =messege
}
fun getReceiver(): String?{
return receiver
}
fun setReceiver(receiver: String){
this.receiver =receiver
}
fun isIsSeen(): Boolean {
return isseen
}
fun setIsSeen(isseen: Boolean){
this.isseen =isseen
}
fun getUrl(): String?{
return url
}
fun setUrl(url: String){
this.url =url
}
fun getMessegeId(): String?{
return messegeId
}
fun setMessegeId(messegeId: String){
this.messegeId =messegeId
}
}
I am facing this issue again and again, whenever I run the application in the application in the emulator it crashes with showing this error.
2020-08-22 23:19:59.920 17005-17005/com.insidecoderz.trails2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.insidecoderz.trails2, PID: 17005
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.Boolean to type com.insidecoderz.trails2.Model.Chat
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:435)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:231)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:79)
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
at com.insidecoderz.trails2.MessegeActivity$retreiveMsg$1.onDataChange(MessegeActivity.kt:202)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:224)
at android.app.ActivityThread.main(ActivityThread.java:7134)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:536)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
If you access the id of the user with the following code val reference = FirebaseDatabase.getInstance().reference.child("Users").child(userIdVisit) and then you try to get the data as a model Chat you can not do that, instead try FirebaseDatabase.getInstance().reference.child("Users").This way you'll be able to get your data as model Chat but it won't be the id you want, also if at the node "Users" you have more users then you'll obtain all the users info using the model Chat
If you need the information for the userIdVisit then you can you do the following
snapshot.child("userInfo1").getValue();
snapshot.child("userInfo2").getValue();
This is the sample for java, you should adapt it for kotlin, I'm sorry for not providing the sample in kotlin but I'm not familiar with it