Created
November 8, 2018 21:12
-
-
Save marcusedu/820711166dfab38e283bace15c32b6e4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.firebase.database.* | |
import com.linkaberto.aproveitafrete.aproveitafrete.data.repositorio.RepoSingleton | |
import com.linkaberto.aproveitafrete.aproveitafrete.model.Anuncio | |
import info.marcussoftware.mschat.interfaces.Message | |
import io.reactivex.Observable | |
import io.reactivex.subjects.BehaviorSubject | |
import io.reactivex.subjects.PublishSubject | |
/** | |
* Created by Marcus Eduardo - [email protected] on 12/02/2018. | |
*/ | |
class Conversa(val anuncio: Anuncio) { | |
private val CONVERSA = "conversa" | |
private val behaviorSubject = BehaviorSubject.create<Message>()!! | |
private val digitandoPublishSubject = PublishSubject.create<Boolean>()!! | |
private val meuUsuario = RepoSingleton.getUsuarioRepo().usuarioCorrente | |
private val chatRef: DatabaseReference | |
private val numMsg = 50 | |
private var queryInicial: Query? = null | |
private var queryCarregarMais: Query? = null | |
private var queryDigitando: Query? = null | |
var executando = false | |
private var mChildEventListener: ChildEventListener? = null | |
private var mDigitandoValueListener: ValueEventListener? = null | |
init { | |
val menorId = Math.min(meuUsuario!!.idmotorista, anuncio.user.idmotorista) | |
val maiorId = Math.max(meuUsuario.idmotorista, anuncio.user.idmotorista) | |
chatRef = FirebaseDatabase.getInstance().getReference("chat/${anuncio.idMeusFretes}/$menorId$maiorId") | |
queryDigitando = chatRef.child("status/${anuncio.user.idmotorista}/digitando") | |
} | |
fun receberMensagens(): Observable<Message> { | |
iniciaSeNaoIniciado() | |
return behaviorSubject | |
} | |
private fun iniciaSeNaoIniciado() { | |
if (executando.not()) { | |
queryInicial = chatRef.child(CONVERSA).limitToLast(numMsg) | |
queryInicial!!.addChildEventListener(getChildListener()) | |
queryDigitando!!.addValueEventListener(getDigitandoValueListener()) | |
executando = true | |
} | |
} | |
private fun getChildListener(): ChildEventListener { | |
if (mChildEventListener == null) { | |
mChildEventListener = object : ChildEventListener { | |
override fun onCancelled(p0: DatabaseError) {} | |
override fun onChildMoved(p0: DataSnapshot, p1: String?) {} | |
override fun onChildChanged(p0: DataSnapshot, p1: String?) { | |
emiteMessage(p0) | |
} | |
override fun onChildAdded(p0: DataSnapshot, p1: String?) { | |
emiteMessage(p0) | |
} | |
override fun onChildRemoved(p0: DataSnapshot) {} | |
private fun emiteMessage(snapshot: DataSnapshot?) { | |
val msg = snapshot?.getValue(com.linkaberto.aproveitafrete.aproveitafrete.model.Message::class.java) | |
if (msg?.userId == anuncio.user.idmotorista.toString()) | |
msg.userName = anuncio.user.nome | |
if (msg != null) behaviorSubject.onNext(msg) | |
} | |
} | |
} | |
return mChildEventListener!! | |
} | |
private fun getDigitandoValueListener(): ValueEventListener { | |
if (mDigitandoValueListener == null) { | |
mDigitandoValueListener = object : ValueEventListener { | |
override fun onCancelled(p0: DatabaseError) = Unit | |
override fun onDataChange(p0: DataSnapshot) { | |
if (p0.value != null) | |
digitandoPublishSubject.onNext(p0.value as Boolean) | |
} | |
} | |
} | |
return mDigitandoValueListener!! | |
} | |
fun estouDigitando(typing: Boolean) = | |
chatRef.child("status/${meuUsuario!!.idmotorista}/digitando").setValue(typing) | |
fun enviarMensagem(msg: com.linkaberto.aproveitafrete.aproveitafrete.model.Message) { | |
val msgRef = chatRef.child(CONVERSA).push() | |
msg.messageId = msgRef.key | |
msgRef.setValue(msg) | |
// .addOnSuccessListener { msg.status = Message.Status.SYNC } | |
// behaviorSubject.onNext(msg) | |
} | |
fun stop() { | |
behaviorSubject.onComplete() | |
queryInicial?.removeEventListener(getChildListener()) | |
queryCarregarMais?.removeEventListener(getChildListener()) | |
queryDigitando?.removeEventListener(getDigitandoValueListener()) | |
} | |
fun carregarMaisMensagens(latestItem: Message?) { | |
queryCarregarMais?.removeEventListener(getChildListener()) | |
queryCarregarMais = queryInicial?.startAt(latestItem?.messageId) | |
queryCarregarMais?.addChildEventListener(getChildListener()) | |
} | |
fun receberSeDigitando(): Observable<Boolean> { | |
iniciaSeNaoIniciado() | |
return digitandoPublishSubject | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment