mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
Simplify API for sending messages
This commit is contained in:
@@ -29,22 +29,20 @@ class SendMessage @Inject constructor(
|
||||
private val messageRepo: MessageRepository
|
||||
) : Interactor<SendMessage.Params>() {
|
||||
|
||||
data class Params(val subId: Int, val threadId: Long, val addresses: List<String>, val body: String, val attachments: List<Attachment> = listOf())
|
||||
data class Params(
|
||||
val subId: Int,
|
||||
val threadId: Long,
|
||||
val addresses: List<String>,
|
||||
val body: String,
|
||||
val attachments: List<Attachment> = listOf(),
|
||||
val delay: Int = 0)
|
||||
|
||||
override fun buildObservable(params: Params): Flowable<Unit> {
|
||||
return Flowable.just(Unit)
|
||||
.filter { params.addresses.isNotEmpty() }
|
||||
.doOnNext {
|
||||
if (params.addresses.size == 1 && params.attachments.isEmpty()) {
|
||||
messageRepo.sendSmsAndPersist(params.subId, params.threadId, params.addresses.first(), params.body)
|
||||
} else {
|
||||
messageRepo.sendMms(params.subId, params.threadId, params.addresses, params.body, params.attachments)
|
||||
}
|
||||
}
|
||||
// If this was the first message sent in the conversation, the conversation might not exist yet
|
||||
.doOnNext { conversationRepo.getOrCreateConversation(params.threadId) }
|
||||
.doOnNext { conversationRepo.updateConversations(params.threadId) }
|
||||
.doOnNext { conversationRepo.markUnarchived(params.threadId) }
|
||||
}
|
||||
override fun buildObservable(params: Params): Flowable<Unit> = Flowable.just(Unit)
|
||||
.filter { params.addresses.isNotEmpty() }
|
||||
.doOnNext { messageRepo.sendMessage(params.subId, params.threadId, params.addresses, params.body, params.attachments, params.delay) }
|
||||
// If this was the first message sent in the conversation, the conversation might not exist yet
|
||||
.doOnNext { conversationRepo.getOrCreateConversation(params.threadId) }
|
||||
.doOnNext { conversationRepo.updateConversations(params.threadId) }
|
||||
.doOnNext { conversationRepo.markUnarchived(params.threadId) }
|
||||
|
||||
}
|
||||
@@ -5,39 +5,35 @@ import android.net.Uri
|
||||
import com.moez.QKSMS.compat.TelephonyCompat
|
||||
import com.moez.QKSMS.extensions.mapNotNull
|
||||
import com.moez.QKSMS.model.Attachment
|
||||
import com.moez.QKSMS.repository.ConversationRepository
|
||||
import com.moez.QKSMS.repository.MessageRepository
|
||||
import com.moez.QKSMS.repository.ScheduledMessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import io.reactivex.rxkotlin.toFlowable
|
||||
import io.realm.RealmList
|
||||
import javax.inject.Inject
|
||||
|
||||
class SendScheduledMessage @Inject constructor(
|
||||
private val context: Context,
|
||||
private val conversationRepo: ConversationRepository,
|
||||
private val messageRepo: MessageRepository,
|
||||
private val scheduledMessageRepo: ScheduledMessageRepository
|
||||
private val scheduledMessageRepo: ScheduledMessageRepository,
|
||||
private val sendMessage: SendMessage
|
||||
) : Interactor<Long>() {
|
||||
|
||||
override fun buildObservable(params: Long): Flowable<*> {
|
||||
return Flowable.just(params)
|
||||
.mapNotNull(scheduledMessageRepo::getScheduledMessage)
|
||||
.map { message ->
|
||||
TelephonyCompat.getOrCreateThreadId(context, message.recipients).also { threadId ->
|
||||
if ((message.recipients.size == 1 || !message.sendAsGroup) && message.attachments.isEmpty()) {
|
||||
message.recipients.forEach { address ->
|
||||
messageRepo.sendSmsAndPersist(message.subId, threadId, address, message.body)
|
||||
}
|
||||
} else {
|
||||
val attachments = message.attachments.mapNotNull(Uri::parse).map { Attachment(it) }
|
||||
messageRepo.sendMms(message.subId, threadId, message.recipients, message.body, attachments)
|
||||
}
|
||||
}
|
||||
.flatMap { message ->
|
||||
if (message.sendAsGroup) {
|
||||
listOf(message)
|
||||
} else {
|
||||
message.recipients.map { recipient -> message.copy(recipients = RealmList(recipient)) }
|
||||
}.toFlowable()
|
||||
}
|
||||
.doOnNext { scheduledMessageRepo.deleteScheduledMessage(params) }
|
||||
// If this was the first message sent in the conversation, the conversation might not exist yet
|
||||
.doOnNext { conversationRepo.getOrCreateConversation(it) }
|
||||
.doOnNext { conversationRepo.updateConversations(it) }
|
||||
.doOnNext { conversationRepo.markUnarchived(it) }
|
||||
.map { message ->
|
||||
val threadId = TelephonyCompat.getOrCreateThreadId(context, message.recipients)
|
||||
val attachments = message.attachments.mapNotNull(Uri::parse).map { Attachment(it) }
|
||||
SendMessage.Params(message.subId, threadId, message.recipients, message.body, attachments)
|
||||
}
|
||||
.flatMap(sendMessage::buildObservable)
|
||||
.doFinally { scheduledMessageRepo.deleteScheduledMessage(params) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,4 +12,17 @@ open class ScheduledMessage(
|
||||
var sendAsGroup: Boolean = true,
|
||||
var body: String = "",
|
||||
var attachments: RealmList<String> = RealmList()
|
||||
) : RealmObject()
|
||||
) : RealmObject() {
|
||||
|
||||
fun copy(id: Long = this.id,
|
||||
date: Long = this.date,
|
||||
subId: Int = this.subId,
|
||||
recipients: RealmList<String> = this.recipients,
|
||||
sendAsGroup: Boolean = this.sendAsGroup,
|
||||
body: String = this.body,
|
||||
attachments: RealmList<String> = this.attachments): ScheduledMessage {
|
||||
|
||||
return ScheduledMessage(id, date, subId, recipients, sendAsGroup, body, attachments)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -57,18 +57,13 @@ interface MessageRepository {
|
||||
|
||||
fun markUnread(vararg threadIds: Long)
|
||||
|
||||
/**
|
||||
* Persists the SMS and attempts to send it
|
||||
*/
|
||||
fun sendSmsAndPersist(subId: Int, threadId: Long, address: String, body: String)
|
||||
fun sendMessage(subId: Int, threadId: Long, addresses: List<String>, body: String, attachments: List<Attachment>, delay: Int = 0)
|
||||
|
||||
/**
|
||||
* Attempts to send the SMS message. This can be called if the message has already been persisted
|
||||
*/
|
||||
fun sendSms(message: Message)
|
||||
|
||||
fun sendMms(subId: Int, threadId: Long, addresses: List<String>, body: String, attachments: List<Attachment>)
|
||||
|
||||
/**
|
||||
* Attempts to cancel sending the message with the given id
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user