Format domain module

This commit is contained in:
moezbhatti
2019-04-10 22:12:57 -04:00
parent b64672c472
commit cc7f573e54
11 changed files with 73 additions and 34 deletions
@@ -27,16 +27,21 @@ class AddScheduledMessage @Inject constructor(
private val updateScheduledMessageAlarms: UpdateScheduledMessageAlarms
) : Interactor<AddScheduledMessage.Params>() {
data class Params(val date: Long,
val subId: Int,
val recipients: List<String>,
val sendAsGroup: Boolean,
val body: String,
val attachments: List<String>)
data class Params(
val date: Long,
val subId: Int,
val recipients: List<String>,
val sendAsGroup: Boolean,
val body: String,
val attachments: List<String>
)
override fun buildObservable(params: Params): Flowable<*> {
return Flowable.just(params)
.map { scheduledMessageRepo.saveScheduledMessage(it.date, it.subId, it.recipients, it.sendAsGroup, it.body, it.attachments) }
.map {
scheduledMessageRepo.saveScheduledMessage(it.date, it.subId, it.recipients, it.sendAsGroup, it.body,
it.attachments)
}
.flatMap { updateScheduledMessageAlarms.buildObservable(Unit) }
}
@@ -36,7 +36,9 @@ class DeleteMessages @Inject constructor(
override fun buildObservable(params: Params): Flowable<*> {
return Flowable.just(params.messageIds.toLongArray())
.doOnNext { messageIds -> messageRepo.deleteMessages(*messageIds) } // Delete the messages
.doOnNext { params.threadId?.let { conversationRepo.updateConversations(it) } } // Update the conversation
.doOnNext {
params.threadId?.let { conversationRepo.updateConversations(it) } // Update the conversation
}
.doOnNext { params.threadId?.let { notificationManager.update(it) } }
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
@@ -58,10 +58,17 @@ class ReceiveMms @Inject constructor(
if (blocked) messageRepo.deleteMessages(message.id)
}
}
.doOnNext { message -> conversationRepo.updateConversations(message.threadId) } // Update the conversation
.mapNotNull { message -> conversationRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.doOnNext { message ->
conversationRepo.updateConversations(message.threadId) // Update the conversation
}
.mapNotNull { message ->
conversationRepo.getOrCreateConversation(message.threadId) // Map message to conversation
}
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) conversationRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.doOnNext { conversation ->
// Unarchive conversation if necessary
if (conversation.archived) conversationRepo.markUnarchived(conversation.id)
}
.map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread
.doOnNext(notificationManager::update) // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
@@ -53,14 +53,21 @@ class ReceiveSms @Inject constructor(
val time = messages[0].timestampMillis
val body: String = messages
.mapNotNull { message -> message.displayMessageBody }
.reduce { body, new -> body + new } ?: ""
.reduce { body, new -> body + new }
messageRepo.insertReceivedSms(it.subId, address, body, time) // Add the message to the db
}
.doOnNext { message -> conversationRepo.updateConversations(message.threadId) } // Update the conversation
.mapNotNull { message -> conversationRepo.getOrCreateConversation(message.threadId) } // Map message to conversation
.doOnNext { message ->
conversationRepo.updateConversations(message.threadId) // Update the conversation
}
.mapNotNull { message ->
conversationRepo.getOrCreateConversation(message.threadId) // Map message to conversation
}
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation -> if (conversation.archived) conversationRepo.markUnarchived(conversation.id) } // Unarchive conversation if necessary
.doOnNext { conversation ->
// Unarchive conversation if necessary
if (conversation.archived) conversationRepo.markUnarchived(conversation.id)
}
.map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread
.doOnNext { threadId -> notificationManager.update(threadId) } // Update the notification
.doOnNext { shortcutManager.updateShortcuts() } // Update shortcuts
@@ -21,14 +21,12 @@ package com.moez.QKSMS.interactor
import com.moez.QKSMS.model.Attachment
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import com.moez.QKSMS.repository.SyncRepository
import io.reactivex.Flowable
import javax.inject.Inject
class SendMessage @Inject constructor(
private val conversationRepo: ConversationRepository,
private val messageRepo: MessageRepository,
private val syncRepo: SyncRepository
private val messageRepo: MessageRepository
) : Interactor<SendMessage.Params>() {
data class Params(
@@ -37,11 +35,15 @@ class SendMessage @Inject constructor(
val addresses: List<String>,
val body: String,
val attachments: List<Attachment> = listOf(),
val delay: Int = 0)
val delay: Int = 0
)
override fun buildObservable(params: Params): Flowable<*> = Flowable.just(Unit)
.filter { params.addresses.isNotEmpty() }
.doOnNext { messageRepo.sendMessage(params.subId, params.threadId, params.addresses, params.body, params.attachments, params.delay) }
.doOnNext {
messageRepo.sendMessage(params.subId, params.threadId, params.addresses, params.body,
params.attachments, params.delay)
}
.map {
// On some manufacturers, we can't obtain a threadId for a new conversation. In
// this case, find the threadId manually now that it contains a message
@@ -34,7 +34,9 @@ class UpdateScheduledMessageAlarms @Inject constructor(
.map { scheduledMessageRepo.getScheduledMessages() } // Get all the scheduled messages
.map { it.map { message -> Pair(message.id, message.date) } } // Map the data we need out of Realm
.flatMap { messages -> Flowable.fromIterable(messages) } // Turn the list into a stream
.doOnNext { (id, date) -> alarmManager.setAlarm(date, alarmManager.getScheduledMessageIntent(id)) } // Create alarm
.doOnNext { (id, date) ->
alarmManager.setAlarm(date, alarmManager.getScheduledMessageIntent(id)) // Create alarm
}
.filter { (_, date) -> date < System.currentTimeMillis() } // Filter messages that should have already been sent
.flatMap { (id, _) -> sendScheduledMessage.buildObservable(id) } // Send them
}
@@ -51,4 +51,4 @@ sealed class Attachment {
}
class Attachments(attachments: List<Attachment>): List<Attachment> by attachments
class Attachments(attachments: List<Attachment>) : List<Attachment> by attachments
@@ -36,8 +36,7 @@ open class Conversation(
var me: Boolean = false,
var draft: String = "",
// For group chats, the user is allowed to set a custom title for the conversation
var name: String = ""
var name: String = "" // For group chats, the user is allowed to set a custom title for the conversation
) : RealmObject() {
fun getTitle(): String {
@@ -32,13 +32,15 @@ open class ScheduledMessage(
var attachments: RealmList<String> = RealmList()
) : 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 {
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,7 +57,14 @@ interface MessageRepository {
fun markUnread(vararg threadIds: Long)
fun sendMessage(subId: Int, threadId: Long, addresses: List<String>, body: String, attachments: List<Attachment>, delay: Int = 0)
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
@@ -26,8 +26,14 @@ interface ScheduledMessageRepository {
/**
* Saves a scheduled message
*/
fun saveScheduledMessage(date: Long, subId: Int, recipients: List<String>, sendAsGroup: Boolean, body: String,
attachments: List<String>)
fun saveScheduledMessage(
date: Long,
subId: Int,
recipients: List<String>,
sendAsGroup: Boolean,
body: String,
attachments: List<String>
)
/**
* Returns all of the scheduled messages, sorted chronologically