mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 11:38:51 +01:00
Create interactors for MarkSent and MarkFailed
This commit is contained in:
@@ -7,7 +7,6 @@ import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony.Sms
|
||||
import android.telephony.SmsManager
|
||||
import com.moez.QKSMS.common.util.NotificationManager
|
||||
import com.moez.QKSMS.common.util.extensions.insertOrUpdate
|
||||
import com.moez.QKSMS.data.datasource.MessageTransaction
|
||||
import com.moez.QKSMS.data.model.Conversation
|
||||
@@ -28,7 +27,6 @@ import javax.inject.Singleton
|
||||
@Singleton
|
||||
class MessageRepository @Inject constructor(
|
||||
private val context: Context,
|
||||
private val notificationManager: NotificationManager,
|
||||
@Named("Realm") private val realmMessageTransaction: MessageTransaction,
|
||||
@Named("Native") private val nativeMessageTransaction: MessageTransaction) {
|
||||
|
||||
@@ -83,19 +81,6 @@ class MessageRepository @Inject constructor(
|
||||
realmMessageTransaction.markRead(threadId)
|
||||
}
|
||||
|
||||
fun markSent(uri: Uri) {
|
||||
val values = ContentValues()
|
||||
values.put(Sms.TYPE, Sms.MESSAGE_TYPE_SENT)
|
||||
updateMessage(values, uri)
|
||||
}
|
||||
|
||||
fun markFailed(uri: Uri, resultCode: Int) {
|
||||
val values = ContentValues()
|
||||
values.put(Sms.TYPE, Sms.MESSAGE_TYPE_FAILED)
|
||||
values.put(Sms.ERROR_CODE, resultCode)
|
||||
updateMessage(values, uri)
|
||||
}
|
||||
|
||||
fun sendMessage(threadId: Long, address: String, body: String) {
|
||||
val values = ContentValues()
|
||||
values.put(Sms.ADDRESS, address)
|
||||
@@ -110,7 +95,7 @@ class MessageRepository @Inject constructor(
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map { contentResolver.insert(Sms.CONTENT_URI, values) }
|
||||
.subscribe { uri ->
|
||||
copyMessageToRealm(uri)
|
||||
addMessageFromUri(uri)
|
||||
|
||||
val sentIntent = Intent(context, MessageSentReceiver::class.java).putExtra("uri", uri.toString())
|
||||
val sentPI = PendingIntent.getBroadcast(context, uri.lastPathSegment.toInt(), sentIntent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
@@ -123,15 +108,15 @@ class MessageRepository @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun updateMessage(values: ContentValues, uri: Uri) {
|
||||
fun updateMessageFromUri(values: ContentValues, uri: Uri) {
|
||||
val contentResolver = context.contentResolver
|
||||
Flowable.just(values)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.map { contentResolver.update(uri, values, null, null) }
|
||||
.subscribe { copyMessageToRealm(uri) }
|
||||
.subscribe { addMessageFromUri(uri) }
|
||||
}
|
||||
|
||||
fun copyMessageToRealm(uri: Uri) {
|
||||
fun addMessageFromUri(uri: Uri) {
|
||||
val cursor = context.contentResolver.query(uri, null, null, null, "date DESC")
|
||||
if (cursor.moveToFirst()) {
|
||||
val columns = MessageColumns(cursor)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.moez.QKSMS.domain.interactor
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import javax.inject.Inject
|
||||
|
||||
class MarkFailed @Inject constructor(val messageRepo: MessageRepository) : Interactor<Unit, MarkFailed.Params>() {
|
||||
|
||||
data class Params(val uri: Uri, val resultCode: Int)
|
||||
|
||||
override fun buildUseCaseObservable(params: Params): Flowable<Unit> {
|
||||
return Flowable.just(Unit)
|
||||
.doOnNext {
|
||||
val values = ContentValues()
|
||||
values.put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_FAILED)
|
||||
values.put(Telephony.Sms.ERROR_CODE, params.resultCode)
|
||||
messageRepo.updateMessageFromUri(values, params.uri)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.moez.QKSMS.domain.interactor
|
||||
|
||||
import android.content.ContentValues
|
||||
import android.net.Uri
|
||||
import android.provider.Telephony
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import io.reactivex.Flowable
|
||||
import javax.inject.Inject
|
||||
|
||||
class MarkSent @Inject constructor(val messageRepo: MessageRepository) : Interactor<Unit, Uri>() {
|
||||
|
||||
override fun buildUseCaseObservable(params: Uri): Flowable<Unit> {
|
||||
return Flowable.just(params)
|
||||
.doOnNext {
|
||||
val values = ContentValues()
|
||||
values.put(Telephony.Sms.TYPE, Telephony.Sms.MESSAGE_TYPE_SENT)
|
||||
messageRepo.updateMessageFromUri(values, params)
|
||||
}
|
||||
.map { Unit }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,7 +25,7 @@ class ReceiveMessage @Inject constructor(
|
||||
return Flowable.just(values)
|
||||
.map { contentResolver.insert(Telephony.Sms.Inbox.CONTENT_URI, values) }
|
||||
.doOnNext { uri ->
|
||||
messageRepo.copyMessageToRealm(uri)
|
||||
messageRepo.addMessageFromUri(uri)
|
||||
notificationManager.update(messageRepo)
|
||||
}
|
||||
.map { Unit }
|
||||
|
||||
@@ -7,12 +7,14 @@ import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.telephony.SmsManager
|
||||
import com.moez.QKSMS.common.di.AppComponentManager
|
||||
import com.moez.QKSMS.data.repository.MessageRepository
|
||||
import com.moez.QKSMS.domain.interactor.MarkFailed
|
||||
import com.moez.QKSMS.domain.interactor.MarkSent
|
||||
import javax.inject.Inject
|
||||
|
||||
class MessageSentReceiver : BroadcastReceiver() {
|
||||
|
||||
@Inject lateinit var messageRepo: MessageRepository
|
||||
@Inject lateinit var markSent: MarkSent
|
||||
@Inject lateinit var markFailed: MarkFailed
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
AppComponentManager.appComponent.inject(this)
|
||||
@@ -20,12 +22,12 @@ class MessageSentReceiver : BroadcastReceiver() {
|
||||
val uri = Uri.parse(intent.getStringExtra("uri"))
|
||||
|
||||
when (resultCode) {
|
||||
Activity.RESULT_OK -> messageRepo.markSent(uri)
|
||||
Activity.RESULT_OK -> markSent.execute({}, uri)
|
||||
|
||||
SmsManager.RESULT_ERROR_GENERIC_FAILURE,
|
||||
SmsManager.RESULT_ERROR_NO_SERVICE,
|
||||
SmsManager.RESULT_ERROR_NULL_PDU,
|
||||
SmsManager.RESULT_ERROR_RADIO_OFF -> messageRepo.markFailed(uri, resultCode)
|
||||
SmsManager.RESULT_ERROR_RADIO_OFF -> markFailed.execute({}, MarkFailed.Params(uri, resultCode))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user