Create interaction for receiving message

This commit is contained in:
Moez Bhatti
2017-10-01 18:41:01 -04:00
parent 2cab83433e
commit 4ad44c47ca
3 changed files with 39 additions and 21 deletions
@@ -68,22 +68,6 @@ class MessageRepository @Inject constructor(
.findAllSorted("date")
}
fun insertReceivedMessage(address: String, body: String, time: Long) {
val values = ContentValues()
values.put(Sms.ADDRESS, address)
values.put(Sms.BODY, body)
values.put(Sms.DATE_SENT, time)
val contentResolver = context.contentResolver
Flowable.just(values)
.subscribeOn(Schedulers.io())
.map { contentResolver.insert(Sms.Inbox.CONTENT_URI, values) }
.subscribe { uri ->
copyMessageToRealm(uri)
notificationManager.update(this)
}
}
fun markAllSeen() {
nativeMessageTransaction.markSeen()
realmMessageTransaction.markSeen()
@@ -139,7 +123,7 @@ class MessageRepository @Inject constructor(
}
}
private fun updateMessage(values: ContentValues, uri: Uri) {
fun updateMessage(values: ContentValues, uri: Uri) {
val contentResolver = context.contentResolver
Flowable.just(values)
.subscribeOn(Schedulers.io())
@@ -147,7 +131,7 @@ class MessageRepository @Inject constructor(
.subscribe { copyMessageToRealm(uri) }
}
private fun copyMessageToRealm(uri: Uri) {
fun copyMessageToRealm(uri: Uri) {
val cursor = context.contentResolver.query(uri, null, null, null, "date DESC")
if (cursor.moveToFirst()) {
val columns = MessageColumns(cursor)
@@ -0,0 +1,34 @@
package com.moez.QKSMS.domain.interactor
import android.content.ContentValues
import android.content.Context
import android.provider.Telephony
import com.moez.QKSMS.common.util.NotificationManager
import com.moez.QKSMS.data.repository.MessageRepository
import io.reactivex.Flowable
import javax.inject.Inject
class ReceiveMessage @Inject constructor(
val context: Context,
val messageRepo: MessageRepository,
val notificationManager: NotificationManager) : Interactor<Unit, ReceiveMessage.Params>() {
data class Params(val address: String, val body: String, val sentTime: Long)
override fun buildUseCaseObservable(params: Params): Flowable<Unit> {
val values = ContentValues()
values.put(Telephony.Sms.ADDRESS, params.address)
values.put(Telephony.Sms.BODY, params.body)
values.put(Telephony.Sms.DATE_SENT, params.sentTime)
val contentResolver = context.contentResolver
return Flowable.just(values)
.map { contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) }
.doOnNext { uri ->
messageRepo.copyMessageToRealm(uri)
notificationManager.update(messageRepo)
}
.map { Unit }
}
}
@@ -5,13 +5,13 @@ import android.content.Context
import android.content.Intent
import android.provider.Telephony.Sms
import com.moez.QKSMS.common.di.AppComponentManager
import com.moez.QKSMS.data.repository.MessageRepository
import com.moez.QKSMS.domain.interactor.ReceiveMessage
import timber.log.Timber
import javax.inject.Inject
class SmsReceiver : BroadcastReceiver() {
@Inject lateinit var messageRepo: MessageRepository
@Inject lateinit var receiveMessage: ReceiveMessage
override fun onReceive(context: Context, intent: Intent) {
Timber.v("Received SMS: $intent")
@@ -25,7 +25,7 @@ class SmsReceiver : BroadcastReceiver() {
.map { message -> message.displayMessageBody }
.reduce { body, new -> body + new }
messageRepo.insertReceivedMessage(address, body, time)
receiveMessage.execute({}, ReceiveMessage.Params(address, body, time))
}
}