Add ability to send messages

This commit is contained in:
Moez Bhatti
2017-09-20 00:19:55 -04:00
parent f639a652bb
commit 9a1ae958e0
8 changed files with 84 additions and 9 deletions
@@ -1,9 +1,11 @@
package com.moez.QKSMS.data.repository
import android.content.ContentResolver
import android.content.ContentValues
import android.content.Context
import android.net.Uri
import android.provider.Telephony
import android.telephony.SmsManager
import com.moez.QKSMS.data.model.Message
import com.moez.QKSMS.data.sync.MessageColumns
import io.realm.Realm
@@ -19,16 +21,36 @@ class MessageRepository(val context: Context) {
}
fun insertMessage(address: String, body: String, time: Long) {
val contentResolver = context.contentResolver
val cv = ContentValues()
cv.put("address", address)
cv.put("body", body)
cv.put("date_sent", time)
val contentResolver = context.contentResolver
val uri = contentResolver.insert(Uri.parse("content://sms/inbox"), cv)
copyLatestMessageToRealm(uri, contentResolver)
}
// TODO this is really sloppy, it should be fixed
fun sendMessage(threadId: Long, address: String, body: String) {
val smsManager = SmsManager.getDefault()
smsManager.sendTextMessage(address, null, body, null, null)
val values = ContentValues()
values.put("address", address)
values.put("body", body)
values.put("date", System.currentTimeMillis())
values.put("read", 1)
values.put("type", 4)
values.put("thread_id", threadId)
val contentResolver = context.contentResolver
val uri = contentResolver.insert(Uri.parse("content://sms/"), values)
copyLatestMessageToRealm(uri, contentResolver)
}
// TODO this is really sloppy, it should be fixed
private fun copyLatestMessageToRealm(uri: Uri, contentResolver: ContentResolver) {
val projection = arrayOf(Telephony.Sms.Conversations.THREAD_ID)
val cursor = contentResolver.query(uri, projection, null, null, null)
@@ -25,6 +25,8 @@ class MessageListActivity : QkActivity(), Observer<MessageListViewState> {
layoutManager.stackFromEnd = true
messageList.layoutManager = layoutManager
send.setOnClickListener { viewModel.sendMessage(message.text.toString()) }
viewModel.state.observe(this)
}
@@ -33,11 +35,12 @@ class MessageListActivity : QkActivity(), Observer<MessageListViewState> {
is MessageListViewState.ConversationLoaded -> onConversationLoaded(state)
is MessageListViewState.ConversationError -> onConversationError(state)
is MessageListViewState.MessagesLoaded -> onMessagesLoaded(state)
is MessageListViewState.DraftLoaded -> onDraftLoaded(state)
}
}
private fun onConversationLoaded(conversationLoaded: MessageListViewState.ConversationLoaded) {
title = conversationLoaded.data.getTitle()
title = conversationLoaded.conversation.getTitle()
}
private fun onConversationError(conversationError: MessageListViewState.ConversationError) {
@@ -45,7 +48,11 @@ class MessageListActivity : QkActivity(), Observer<MessageListViewState> {
}
private fun onMessagesLoaded(messagesLoaded: MessageListViewState.MessagesLoaded) {
messageList.adapter = MessageAdapter(this, messagesLoaded.data)
messageList.adapter = MessageAdapter(this, messagesLoaded.messages)
}
private fun onDraftLoaded(draftLoaded: MessageListViewState.DraftLoaded) {
message.setText(draftLoaded.draft)
}
}
@@ -17,6 +17,8 @@ class MessageListViewModel : ViewModel() {
val state: MutableLiveData<MessageListViewState> = MutableLiveData()
private var threadId: Long = 0
private var conversation: RealmResults<Conversation>? = null
private var messages: RealmResults<Message>? = null
@@ -27,9 +29,11 @@ class MessageListViewModel : ViewModel() {
fun setThreadId(threadId: Long) {
onCleared()
this.threadId = threadId
messageRepo.getMessages(threadId).let {
messages = it
state.value = MessageListViewState.MessagesLoaded(messages!!)
state.value = MessageListViewState.MessagesLoaded(it)
}
conversationRepo.getConversation(threadId).let {
@@ -43,6 +47,13 @@ class MessageListViewModel : ViewModel() {
}
}
fun sendMessage(body: String) {
conversation?.get(0)?.let { conversation ->
messageRepo.sendMessage(threadId, conversation.contacts[0].address, body)
state.value = MessageListViewState.DraftLoaded("")
}
}
override fun onCleared() {
super.onCleared()
conversation?.removeAllChangeListeners()
@@ -5,7 +5,8 @@ import com.moez.QKSMS.data.model.Message
import io.realm.OrderedRealmCollection
sealed class MessageListViewState {
data class ConversationLoaded(val data: Conversation) : MessageListViewState()
data class ConversationError(val count: Int) : MessageListViewState()
data class MessagesLoaded(val data: OrderedRealmCollection<Message>) : MessageListViewState()
data class ConversationLoaded(val conversation: Conversation) : MessageListViewState()
data class ConversationError(val errorCode: Int) : MessageListViewState()
data class MessagesLoaded(val messages: OrderedRealmCollection<Message>) : MessageListViewState()
data class DraftLoaded(val draft: String) : MessageListViewState()
}